21021: Merge branch 'main' into 21021-controller-logout
[arvados.git] / build / rails-package-scripts / postinst.sh
1 #!/bin/sh
2 # Copyright (C) The Arvados Authors. All rights reserved.
3 #
4 # SPDX-License-Identifier: AGPL-3.0
5
6 # This code runs after package variable definitions and step2.sh.
7
8 set -e
9
10 DATABASE_READY=1
11 APPLICATION_READY=1
12
13 if [ -s "$HOME/.rvm/scripts/rvm" ] || [ -s "/usr/local/rvm/scripts/rvm" ]; then
14     COMMAND_PREFIX="/usr/local/rvm/bin/rvm-exec default"
15 else
16     COMMAND_PREFIX=
17 fi
18
19 report_not_ready() {
20     local ready_flag="$1"; shift
21     local config_file="$1"; shift
22     if [ "1" != "$ready_flag" ]; then cat >&2 <<EOF
23
24 PLEASE NOTE:
25
26 The $PACKAGE_NAME package was not configured completely because
27 $config_file needs some tweaking.
28 Please refer to the documentation at
29 <$DOC_URL> for more details.
30
31 When $(basename "$config_file") has been modified,
32 reconfigure or reinstall this package.
33
34 EOF
35     fi
36 }
37
38 report_web_service_warning() {
39     local warning="$1"; shift
40     cat >&2 <<EOF
41
42 WARNING: $warning.
43
44 To override, set the WEB_SERVICE environment variable to the name of the service
45 hosting the Rails server.
46
47 For Debian-based systems, then reconfigure this package with dpkg-reconfigure.
48
49 For RPM-based systems, then reinstall this package.
50
51 EOF
52 }
53
54 run_and_report() {
55     # Usage: run_and_report ACTION_MSG CMD
56     # This is the usual wrapper that prints ACTION_MSG, runs CMD, then writes
57     # a message about whether CMD succeeded or failed.  Returns the exit code
58     # of CMD.
59     local action_message="$1"; shift
60     local retcode=0
61     echo -n "$action_message..."
62     if "$@"; then
63         echo " done."
64     else
65         retcode=$?
66         echo " failed."
67     fi
68     return $retcode
69 }
70
71 setup_confdirs() {
72     for confdir in "$@"; do
73         if [ ! -d "$confdir" ]; then
74             install -d -g "$WWW_OWNER" -m 0750 "$confdir"
75         fi
76     done
77 }
78
79 setup_conffile() {
80     # Usage: setup_conffile CONFFILE_PATH [SOURCE_PATH]
81     # Both paths are relative to RELEASE_CONFIG_PATH.
82     # This function will try to safely ensure that a symbolic link for
83     # the configuration file points from RELEASE_CONFIG_PATH to CONFIG_PATH.
84     # If SOURCE_PATH is given, this function will try to install that file as
85     # the configuration file in CONFIG_PATH, and return 1 if the file in
86     # CONFIG_PATH is unmodified from the source.
87     local conffile_relpath="$1"; shift
88     local conffile_source="$1"
89     local release_conffile="$RELEASE_CONFIG_PATH/$conffile_relpath"
90     local etc_conffile="$CONFIG_PATH/$(basename "$conffile_relpath")"
91
92     # Note that -h can return true and -e will return false simultaneously
93     # when the target is a dangling symlink.  We're okay with that outcome,
94     # so check -h first.
95     if [ ! -h "$release_conffile" ]; then
96         if [ ! -e "$release_conffile" ]; then
97             ln -s "$etc_conffile" "$release_conffile"
98         # If there's a config file in /var/www identical to the one in /etc,
99         # overwrite it with a symlink after porting its permissions.
100         elif cmp --quiet "$release_conffile" "$etc_conffile"; then
101             local ownership="$(stat -c "%u:%g" "$release_conffile")"
102             local owning_group="${ownership#*:}"
103             if [ 0 != "$owning_group" ]; then
104                 chgrp "$owning_group" "$CONFIG_PATH" /etc/arvados
105             fi
106             chown "$ownership" "$etc_conffile"
107             chmod --reference="$release_conffile" "$etc_conffile"
108             ln --force -s "$etc_conffile" "$release_conffile"
109         fi
110     fi
111
112     if [ -n "$conffile_source" ]; then
113         if [ ! -e "$etc_conffile" ]; then
114             install -g "$WWW_OWNER" -m 0640 \
115                     "$RELEASE_CONFIG_PATH/$conffile_source" "$etc_conffile"
116             return 1
117         # Even if $etc_conffile already existed, it might be unmodified from
118         # the source.  This is especially likely when a user installs, updates
119         # database.yml, then reconfigures before they update application.yml.
120         # Use cmp to be sure whether $etc_conffile is modified.
121         elif cmp --quiet "$RELEASE_CONFIG_PATH/$conffile_source" "$etc_conffile"; then
122             return 1
123         fi
124     fi
125 }
126
127 prepare_database() {
128   DB_MIGRATE_STATUS=`$COMMAND_PREFIX 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" $COMMAND_PREFIX bin/rake \
132                      "$RAILSPKG_DATABASE_LOAD_TASK" db:seed
133   elif echo "$DB_MIGRATE_STATUS" | grep -q '^database: '; then
134       run_and_report "Running db:migrate" \
135                      $COMMAND_PREFIX bin/rake db:migrate
136   elif echo "$DB_MIGRATE_STATUS" | grep -q 'database .* does not exist'; then
137       if ! run_and_report "Running db:setup" \
138            $COMMAND_PREFIX bin/rake db:setup 2>/dev/null; then
139           echo "Warning: unable to set up database." >&2
140           DATABASE_READY=0
141       fi
142   else
143     echo "Warning: Database is not ready to set up. Skipping database setup." >&2
144     DATABASE_READY=0
145   fi
146 }
147
148 configure_version() {
149   if [ -n "$WEB_SERVICE" ]; then
150       SERVICE_MANAGER=$(guess_service_manager)
151   elif WEB_SERVICE=$(list_services_systemd | grep -E '^(nginx|httpd)'); then
152       SERVICE_MANAGER=systemd
153   elif WEB_SERVICE=$(list_services_service \
154                          | grep -Eo '\b(nginx|httpd)[^[:space:]]*'); then
155       SERVICE_MANAGER=service
156   fi
157
158   if [ -z "$WEB_SERVICE" ]; then
159     report_web_service_warning "Web service (Nginx or Apache) not found"
160   elif [ "$WEB_SERVICE" != "$(echo "$WEB_SERVICE" | head -n 1)" ]; then
161     WEB_SERVICE=$(echo "$WEB_SERVICE" | head -n 1)
162     report_web_service_warning \
163         "Multiple web services found.  Choosing the first one ($WEB_SERVICE)"
164   fi
165
166   if [ -e /etc/redhat-release ]; then
167       # Recognize any service that starts with "nginx"; e.g., nginx16.
168       if [ "$WEB_SERVICE" != "${WEB_SERVICE#nginx}" ]; then
169         WWW_OWNER=nginx
170       else
171         WWW_OWNER=apache
172       fi
173   else
174       # Assume we're on a Debian-based system for now.
175       # Both Apache and Nginx run as www-data by default.
176       WWW_OWNER=www-data
177   fi
178
179   echo
180   echo "Assumption: $WEB_SERVICE is configured to serve Rails from"
181   echo "            $RELEASE_PATH"
182   echo "Assumption: $WEB_SERVICE and passenger run as $WWW_OWNER"
183   echo
184
185   echo -n "Creating symlinks to configuration in $CONFIG_PATH ..."
186   setup_confdirs /etc/arvados "$CONFIG_PATH"
187   setup_conffile environments/production.rb environments/production.rb.example \
188       || true
189   setup_extra_conffiles
190   echo "... done."
191
192   # Before we do anything else, make sure some directories and files are in place
193   if [ ! -e $SHARED_PATH/log ]; then mkdir -p $SHARED_PATH/log; fi
194   if [ ! -e $RELEASE_PATH/tmp ]; then mkdir -p $RELEASE_PATH/tmp; fi
195   if [ ! -e $RELEASE_PATH/log ]; then ln -s $SHARED_PATH/log $RELEASE_PATH/log; fi
196   if [ ! -e $SHARED_PATH/log/production.log ]; then touch $SHARED_PATH/log/production.log; fi
197
198   cd "$RELEASE_PATH"
199   export RAILS_ENV=production
200
201   if ! $COMMAND_PREFIX bundle --version >/dev/null 2>&1; then
202       run_and_report "Installing bundler" $COMMAND_PREFIX gem install bundler --version 2.2.19 --no-document
203   fi
204
205   run_and_report "Running bundle config set --local path $SHARED_PATH/vendor_bundle" \
206       $COMMAND_PREFIX bin/bundle config set --local path $SHARED_PATH/vendor_bundle
207
208   run_and_report "Running bundle install" \
209       $COMMAND_PREFIX bin/bundle install --local --quiet
210
211   echo -n "Ensuring directory and file permissions ..."
212   # Ensure correct ownership of a few files
213   chown "$WWW_OWNER:" $RELEASE_PATH/config/environment.rb
214   chown "$WWW_OWNER:" $RELEASE_PATH/config.ru
215   chown "$WWW_OWNER:" $RELEASE_PATH/Gemfile.lock
216   chown -R "$WWW_OWNER:" $RELEASE_PATH/tmp || true
217   chown -R "$WWW_OWNER:" $SHARED_PATH/log
218   # Make sure postgres doesn't try to use a pager.
219   export PAGER=
220   case "$RAILSPKG_DATABASE_LOAD_TASK" in
221       # db:structure:load was deprecated in Rails 6.1 and shouldn't be used.
222       db:schema:load | db:structure:load)
223           chown "$WWW_OWNER:" $RELEASE_PATH/db/schema.rb || true
224           chown "$WWW_OWNER:" $RELEASE_PATH/db/structure.sql || true
225           ;;
226   esac
227   chmod 644 $SHARED_PATH/log/*
228   chmod -R 2775 $RELEASE_PATH/tmp || true
229   echo "... done."
230
231   if [ -n "$RAILSPKG_DATABASE_LOAD_TASK" ]; then
232       prepare_database
233   fi
234
235   if [ -e /etc/arvados/config.yml ]; then
236       # warn about config errors (deprecated/removed keys from
237       # previous version, etc)
238       run_and_report "Checking configuration for completeness" \
239                      $COMMAND_PREFIX bin/rake config:check || APPLICATION_READY=0
240   else
241       APPLICATION_READY=0
242   fi
243
244   chown -R "$WWW_OWNER:" $RELEASE_PATH/tmp
245
246   setup_before_nginx_restart
247
248   if [ -n "$SERVICE_MANAGER" ]; then
249       service_command "$SERVICE_MANAGER" restart "$WEB_SERVICE"
250   fi
251 }
252
253 if [ "$1" = configure ]; then
254   # This is a debian-based system
255   configure_version
256 elif [ "$1" = "0" ] || [ "$1" = "1" ] || [ "$1" = "2" ]; then
257   # This is an rpm-based system
258   configure_version
259 fi
260
261 report_not_ready "$APPLICATION_READY" "/etc/arvados/config.yml"