8014: Introduce run_and_report function to arvados-sso postinst.
[arvados.git] / jenkins / arvados-sso-server-extras / arvados-sso-server.postinst
1 #!/bin/bash
2
3 set -e
4
5 INSTALL_PATH=/var/www/arvados-sso
6 RELEASE_PATH=$INSTALL_PATH/current
7 RELEASE_CONFIG_PATH=$RELEASE_PATH/config
8 SHARED_PATH=$INSTALL_PATH/shared
9 CONFIG_PATH=/etc/arvados/sso
10 PACKAGE_NAME=arvados-sso-server
11 DOC_URL="http://doc.arvados.org/install/install-sso.html#configure"
12
13 DATABASE_READY=1
14 APPLICATION_READY=1
15
16 if [ -s "$HOME/.rvm/scripts/rvm" ] || [ -s "/usr/local/rvm/scripts/rvm" ]; then
17     COMMAND_PREFIX="/usr/local/rvm/bin/rvm-exec default"
18 else
19     COMMAND_PREFIX=
20 fi
21
22 report_not_ready() {
23     local ready_flag=$1; shift
24     local config_file=$1; shift
25     if [ "1" != "$ready_flag" ]; then cat >&2 <<EOF
26
27 PLEASE NOTE:
28
29 The $PACKAGE_NAME package was not configured completely because
30 $config_file needs some tweaking.
31 Please refer to the documentation at
32 <$DOC_URL> for more details.
33
34 When $(basename "$config_file") has been modified,
35 reconfigure or reinstall this package.
36
37 EOF
38     fi
39 }
40
41 report_web_service_warning() {
42     local warning=$1; shift
43     cat >&2 <<EOF
44
45 WARNING: $warning.
46
47 To override, set the WEB_SERVICE environment variable to the name of the service
48 hosting the Rails server.
49
50 For Debian-based systems, then reconfigure this package with dpkg-reconfigure.
51
52 For RPM-based systems, then reinstall this package.
53
54 EOF
55 }
56
57 run_and_report() {
58     # Usage: run_and_report ACTION_MSG CMD
59     # This is the usual wrapper that prints ACTION_MSG, runs CMD, then writes
60     # a message about whether CMD succeeded or failed.  Returns the exit code
61     # of CMD.
62     local action_message=$1; shift
63     local retcode=0
64     echo -n "$action_message..."
65     if "$@"; then
66         echo " done."
67     else
68         retcode=$?
69         echo " failed."
70     fi
71     return $retcode
72 }
73
74 setup_conffile() {
75     # Usage: setup_conffile CONFFILE_PATH [SOURCE_PATH]
76     # Both paths are relative to RELEASE_CONFIG_PATH.
77     # This function will try to safely ensure that a symbolic link for
78     # the configuration file points from RELEASE_CONFIG_PATH to CONFIG_PATH.
79     # If SOURCE_PATH is given, this function will try to install that file as
80     # the configuration file in CONFIG_PATH, and return 1 if the file in
81     # CONFIG_PATH is unmodified from the source.
82     local conffile_relpath=$1; shift
83     local conffile_source=$1; shift
84     local release_conffile=$RELEASE_CONFIG_PATH/$conffile_relpath
85     local etc_conffile=$CONFIG_PATH/$(basename "$conffile_relpath")
86
87     # Note that -h can return true and -e will return false simultaneously
88     # when the target is a dangling symlink.  We're okay with that outcome,
89     # so check -h first.
90     if [ ! -h "$release_conffile" ]; then
91         if [ ! -e "$release_conffile" ]; then
92             ln -s "$etc_conffile" "$release_conffile"
93         # If there's a config file in /var/www identical to the one in /etc,
94         # overwrite it with a symlink.
95         elif cmp --quiet "$release_conffile" "$etc_conffile"; then
96             ln --force -s "$etc_conffile" "$release_conffile"
97         fi
98     fi
99
100     if [ -n "$conffile_source" ]; then
101         cp --no-clobber "$RELEASE_CONFIG_PATH/$conffile_source" "$etc_conffile"
102         # Even if $etc_conffile already existed, it might be unmodified from
103         # the source.  This is especially likely when a user installs, updates
104         # database.yml, then reconfigures before they update application.yml.
105         # Use cmp to be sure whether $etc_conffile is modified.
106         if cmp --quiet "$RELEASE_CONFIG_PATH/$conffile_source" "$etc_conffile"; then
107             return 1
108         fi
109     fi
110 }
111
112 configure_version() {
113   WEB_SERVICE=${WEB_SERVICE:-$(service --status-all 2>/dev/null \
114       | grep -Eo '\bnginx|httpd[^[:space:]]*' || true)}
115   if [ -z "$WEB_SERVICE" ]; then
116     report_web_service_warning "Web service (Nginx or Apache) not found"
117   elif [ "$WEB_SERVICE" != "$(echo "$WEB_SERVICE" | head -n 1)" ]; then
118     WEB_SERVICE=$(echo "$WEB_SERVICE" | head -n 1)
119     report_web_service_warning \
120         "Multiple web services found.  Choosing the first one ($WEB_SERVICE)"
121   fi
122
123   if [ -e /etc/redhat-release ]; then
124       # Recognize any service that starts with "nginx"; e.g., nginx16.
125       if [ "$WEB_SERVICE" != "${WEB_SERVICE#nginx}" ]; then
126         WWW_OWNER=nginx:nginx
127       else
128         WWW_OWNER=apache:apache
129       fi
130   else
131       # Assume we're on a Debian-based system for now.
132       # Both Apache and Nginx run as www-data by default.
133       WWW_OWNER=www-data:www-data
134   fi
135
136   echo
137   echo "Assumption: $WEB_SERVICE is configured to serve your SSO server URL from"
138   echo "            $RELEASE_PATH"
139   echo "Assumption: configuration files are in $CONFIG_PATH"
140   echo "Assumption: $WEB_SERVICE and passenger run as $WWW_OWNER"
141   echo
142
143   echo -n "Symlinking files from $CONFIG_PATH ..."
144   mkdir -p $CONFIG_PATH
145   setup_conffile database.yml database.yml.example || DATABASE_READY=0
146   setup_conffile environments/production.rb environments/production.rb.example \
147       || true
148   setup_conffile application.yml application.yml.example || APPLICATION_READY=0
149   echo "... done."
150
151   # Before we do anything else, make sure some directories and files are in place
152   if [ ! -e $SHARED_PATH/log ]; then mkdir -p $SHARED_PATH/log; fi
153   if [ ! -e $RELEASE_PATH/tmp ]; then mkdir -p $RELEASE_PATH/tmp; fi
154   if [ ! -e $RELEASE_PATH/log ]; then ln -s $SHARED_PATH/log $RELEASE_PATH/log; fi
155   if [ ! -e $SHARED_PATH/log/production.log ]; then touch $SHARED_PATH/log/production.log; fi
156
157   cd "$RELEASE_PATH"
158   export RAILS_ENV=production
159
160   if ! $COMMAND_PREFIX bundle --version >/dev/null; then
161       run_and_report "Installing bundle" $COMMAND_PREFIX gem install bundle
162   fi
163
164   run_and_report "Running bundle install" \
165       $COMMAND_PREFIX bundle install --path $SHARED_PATH/vendor_bundle --quiet
166
167   echo -n "Ensuring directory and file permissions ..."
168   # Ensure correct ownership of a few files
169   chown "$WWW_OWNER" $RELEASE_PATH/config/environment.rb
170   chown "$WWW_OWNER" $RELEASE_PATH/config.ru
171   chown "$WWW_OWNER" $RELEASE_PATH/config/database.yml
172   chown "$WWW_OWNER" $RELEASE_PATH/Gemfile.lock
173   chown -R "$WWW_OWNER" $RELEASE_PATH/tmp
174   chown -R "$WWW_OWNER" $SHARED_PATH/log
175   chown "$WWW_OWNER" $RELEASE_PATH/db/schema.rb
176   chmod 644 $SHARED_PATH/log/*
177   echo "... done."
178
179   set +e
180   DB_MIGRATE_STATUS=`$COMMAND_PREFIX bundle exec rake db:migrate:status 2>&1`
181   DB_MIGRATE_STATUS_CODE=$?
182   set -e
183
184   if echo $DB_MIGRATE_STATUS | grep 'Schema migrations table does not exist yet.' >/dev/null; then
185       # The database exists, but the migrations table doesn't.
186       run_and_report "Setting up database" \
187                      $COMMAND_PREFIX bundle exec rake db:schema:load db:seed
188   elif echo $DB_MIGRATE_STATUS | grep '^database: ' >/dev/null; then
189       run_and_report "Running db:migrate" \
190                      $COMMAND_PREFIX bundle exec rake db:migrate
191   elif echo $DB_MIGRATE_STATUS | grep 'database .* does not exist' >/dev/null; then
192       if ! run_and_report "Running db:setup" \
193            $COMMAND_PREFIX bundle exec rake db:setup 2>/dev/null; then
194           echo "Warning: unable to set up database." >&2
195           DATABASE_READY=0
196       fi
197   else
198     echo "Warning: Database is not ready to set up. Skipping database setup." >&2
199     DATABASE_READY=0
200   fi
201
202   # precompile assets; thankfully this does not take long
203   if [ "$APPLICATION_READY" = "1" ]; then
204       run_and_report "Precompiling assets" \
205           $COMMAND_PREFIX bundle exec rake assets:precompile -q -s 2>/dev/null \
206           || APPLICATION_READY=0
207   else
208       echo "Precompiling assets... skipped."
209   fi
210   chown -R "$WWW_OWNER" $RELEASE_PATH/tmp
211
212   if [ ! -z "$WEB_SERVICE" ]; then
213       service "$WEB_SERVICE" restart
214   fi
215 }
216
217 if [ "$1" = configure ]; then
218   # This is a debian-based system
219   configure_version
220 elif [ "$1" = "0" ] || [ "$1" = "1" ] || [ "$1" = "2" ]; then
221   # This is an rpm-based system
222   configure_version
223 fi
224
225 report_not_ready "$DATABASE_READY" "$CONFIG_PATH/database.yml"
226 report_not_ready "$APPLICATION_READY" "$CONFIG_PATH/application.yml"