2 # Copyright (C) The Arvados Authors. All rights reserved.
4 # SPDX-License-Identifier: AGPL-3.0
6 # This code runs after package variable definitions.
10 for DISTRO_FAMILY in $(. /etc/os-release && echo "${ID:-} ${ID_LIKE:-}"); do
11 case "$DISTRO_FAMILY" in
13 RESETUP_CMD="dpkg-reconfigure $PACKAGE_NAME"
16 RESETUP_CMD="dnf reinstall $PACKAGE_NAME"
20 if [ -z "$RESETUP_CMD" ]; then
21 echo "$PACKAGE_NAME postinst skipped: don't recognize the distribution from /etc/os-release" >&2
24 # Default documentation URL. This can be set to a more specific URL.
25 NOT_READY_DOC_URL="https://doc.arvados.org/install/install-api-server.html"
26 # This will be set to a command path after we install the version we need.
31 echo "error: systemd_quote requires exactly one argument" >&2
34 # See systemd.syntax(7) - Use double quotes with backslash escapes
35 echo "$1" | sed -re 's/[\\"]/\\\0/g; s/^/"/; s/$/"/'
38 report_web_service_warning() {
39 local warning="$1"; shift
44 To override, set the WEB_SERVICE environment variable to the name of the service
45 hosting the Rails server.
47 After you do that, resume $PACKAGE_NAME setup by running:
53 # Usage: run_and_report ACTION_MSG CMD
54 # This is the usual wrapper that prints ACTION_MSG, runs CMD, then writes
55 # a message about whether CMD succeeded or failed. Returns the exit code
57 local action_message="$1"; shift
59 echo -n "$action_message..."
70 for confdir in "$@"; do
71 if [ ! -d "$confdir" ]; then
72 install -d -g "$WWW_OWNER" -m 0750 "$confdir"
78 # Usage: setup_conffile CONFFILE_PATH [SOURCE_PATH]
79 # Both paths are relative to RELEASE_CONFIG_PATH.
80 # This function will try to safely ensure that a symbolic link for
81 # the configuration file points from RELEASE_CONFIG_PATH to CONFIG_PATH.
82 # If SOURCE_PATH is given, this function will try to install that file as
83 # the configuration file in CONFIG_PATH, and return 1 if the file in
84 # CONFIG_PATH is unmodified from the source.
85 local conffile_relpath="$1"; shift
86 local conffile_source="$1"
87 local release_conffile="$RELEASE_CONFIG_PATH/$conffile_relpath"
88 local etc_conffile="$CONFIG_PATH/$(basename "$conffile_relpath")"
90 # Note that -h can return true and -e will return false simultaneously
91 # when the target is a dangling symlink. We're okay with that outcome,
93 if [ ! -h "$release_conffile" ]; then
94 if [ ! -e "$release_conffile" ]; then
95 ln -s "$etc_conffile" "$release_conffile"
96 # If there's a config file in /var/www identical to the one in /etc,
97 # overwrite it with a symlink after porting its permissions.
98 elif cmp --quiet "$release_conffile" "$etc_conffile"; then
99 local ownership="$(stat -c "%u:%g" "$release_conffile")"
100 local owning_group="${ownership#*:}"
101 if [ 0 != "$owning_group" ]; then
102 chgrp "$owning_group" "$CONFIG_PATH" /etc/arvados
104 chown "$ownership" "$etc_conffile"
105 chmod --reference="$release_conffile" "$etc_conffile"
106 ln --force -s "$etc_conffile" "$release_conffile"
110 if [ -n "$conffile_source" ]; then
111 if [ ! -e "$etc_conffile" ]; then
112 install -g "$WWW_OWNER" -m 0640 \
113 "$RELEASE_CONFIG_PATH/$conffile_source" "$etc_conffile"
115 # Even if $etc_conffile already existed, it might be unmodified from
116 # the source. This is especially likely when a user installs, updates
117 # database.yml, then reconfigures before they update application.yml.
118 # Use cmp to be sure whether $etc_conffile is modified.
119 elif cmp --quiet "$RELEASE_CONFIG_PATH/$conffile_source" "$etc_conffile"; then
126 # Prevent PostgreSQL from trying to page output
128 DB_MIGRATE_STATUS=`"$BUNDLE" exec bin/rake db:migrate:status 2>&1 || true`
129 if echo "$DB_MIGRATE_STATUS" | grep -qF 'Schema migrations table does not exist yet.'; then
130 # The database exists, but the migrations table doesn't.
131 run_and_report "Setting up database" "$BUNDLE" exec bin/rake db:schema:load db:seed
132 elif echo "$DB_MIGRATE_STATUS" | grep -q '^database: '; then
133 run_and_report "Running db:migrate" "$BUNDLE" exec bin/rake db:migrate
134 elif echo "$DB_MIGRATE_STATUS" | grep -q 'database .* does not exist'; then
135 run_and_report "Running db:setup" "$BUNDLE" exec bin/rake db:setup
137 # We don't have enough configuration to even check the database.
142 configure_version() {
143 if [ -n "$WEB_SERVICE" ]; then
145 elif command -v nginx >/dev/null 2>&1; then
148 report_web_service_warning "Web service (nginx) not found"
151 case "$DISTRO_FAMILY" in
152 debian) WWW_OWNER=www-data ;;
153 rhel) WWW_OWNER="$(id --group --name nginx || true)" ;;
156 # Before we do anything else, make sure some directories and files are in place
157 if [ ! -e $SHARED_PATH/log ]; then mkdir -p $SHARED_PATH/log; fi
158 if [ ! -e $RELEASE_PATH/tmp ]; then mkdir -p $RELEASE_PATH/tmp; fi
159 if [ ! -e $RELEASE_PATH/log ]; then ln -s $SHARED_PATH/log $RELEASE_PATH/log; fi
160 if [ ! -e $SHARED_PATH/log/production.log ]; then touch $SHARED_PATH/log/production.log; fi
163 export RAILS_ENV=production
165 run_and_report "Installing bundler" gem install --conservative --version '~> 2.4.0' bundler
166 local ruby_minor_ver="$(ruby -e 'puts RUBY_VERSION.split(".")[..1].join(".")')"
167 BUNDLE="$(gem contents --version '~> 2.4.0' bundler | grep -E '/(bin|exe)/bundle$' | tail -n1)"
168 if ! [ -x "$BUNDLE" ]; then
169 # Some distros (at least Ubuntu 24.04) append the Ruby version to the
170 # executable name, but that isn't reflected in the output of
171 # `gem contents`. Check for that version.
172 BUNDLE="$BUNDLE$ruby_minor_ver"
173 if ! [ -x "$BUNDLE" ]; then
174 echo "Error: failed to find \`bundle\` command after installing bundler gem" >&2
179 local bundle_path="$SHARED_PATH/vendor_bundle"
180 run_and_report "Running bundle config set --local path $SHARED_PATH/vendor_bundle" \
181 "$BUNDLE" config set --local path "$bundle_path"
183 # As of April 2024/Bundler 2.4, `bundle install` tends not to install gems
184 # which are already installed system-wide, which causes bundle activation to
185 # fail later. Work around this by installing all gems manually.
186 find vendor/cache -maxdepth 1 -name '*.gem' -print0 \
187 | run_and_report "Installing bundle gems" xargs -0r \
188 gem install --conservative --ignore-dependencies \
189 --local --no-document --quiet \
190 --install-dir="$bundle_path/ruby/$ruby_minor_ver.0"
191 run_and_report "Running bundle install" "$BUNDLE" install --prefer-local --quiet
192 run_and_report "Verifying bundle is complete" "$BUNDLE" exec true
194 local passenger="$("$BUNDLE" exec gem contents passenger | grep -E '/(bin|exe)/passenger$' | tail -n1)"
195 if ! [ -x "$passenger" ]; then
196 echo "Error: failed to find \`passenger\` command after installing bundle" >&2
199 "$BUNDLE" exec "$passenger-config" build-native-support
200 "$BUNDLE" exec "$passenger-config" install-standalone-runtime --auto --brief
202 echo -n "Creating symlinks to configuration in $CONFIG_PATH ..."
203 setup_confdirs /etc/arvados "$CONFIG_PATH"
204 setup_conffile environments/production.rb environments/production.rb.example \
206 # Rails 5.2 does not tolerate dangling symlinks in the initializers
207 # directory, and this one can still be there, left over from a previous
208 # version of the API server package.
209 rm -f $RELEASE_PATH/config/initializers/omniauth.rb
212 echo -n "Extending systemd unit configuration ..."
214 if [ -z "$WWW_OWNER" ]; then
217 systemd_group="$(systemd_quote "$WWW_OWNER")"
219 install -d /lib/systemd/system/arvados-railsapi.service.d
220 # The 20 prefix is chosen so most user overrides should come after, which
221 # is what most admins will expect, but there's still space to put drop-ins
223 cat >/lib/systemd/system/arvados-railsapi.service.d/20-postinst.conf <<EOF
225 ExecStartPre=+/bin/chgrp $systemd_group log tmp
226 ExecStartPre=+-/bin/chgrp $systemd_group \${PASSENGER_LOG_FILE}
228 ExecStart=$(systemd_quote "$BUNDLE") exec $(systemd_quote "$passenger") start --daemonize --pid-file %t/%N/passenger.pid
230 ExecStop=$(systemd_quote "$BUNDLE") exec $(systemd_quote "$passenger") stop --pid-file %t/%N/passenger.pid
232 ExecReload=$(systemd_quote "$BUNDLE") exec $(systemd_quote "$passenger-config") reopen-logs
233 ${WWW_OWNER:+SupplementaryGroups=$WWW_OWNER}
235 systemctl daemon-reload
238 if [ -n "$NOT_READY_REASON" ]; then
240 # warn about config errors (deprecated/removed keys from
241 # previous version, etc)
242 elif ! run_and_report "Checking configuration for completeness" "$BUNDLE" exec bin/rake config:check; then
243 NOT_READY_REASON="you must add required configuration settings to /etc/arvados/config.yml"
244 NOT_READY_DOC_URL="https://doc.arvados.org/install/install-api-server.html#update-config"
245 elif ! prepare_database; then
246 NOT_READY_REASON="database setup could not be completed"
249 if [ -z "$NOT_READY_REASON" ]; then
250 systemctl try-restart arvados-railsapi.service
255 if [ -n "$NOT_READY_REASON" ]; then
257 NOTE: The $PACKAGE_NAME package was not configured completely because
259 Please refer to the documentation for next steps:
262 After you do that, resume $PACKAGE_NAME setup by running: