8014: Improve conffile handling in 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 setup_conffile() {
58     # Usage: setup_conffile CONFFILE_PATH [SOURCE_PATH]
59     # Both paths are relative to RELEASE_CONFIG_PATH.
60     # This function will try to safely ensure that a symbolic link for
61     # the configuration file points from RELEASE_CONFIG_PATH to CONFIG_PATH.
62     # If SOURCE_PATH is given, this function will try to install that file as
63     # the configuration file in CONFIG_PATH, and return 1 if the file in
64     # CONFIG_PATH is unmodified from the source.
65     local conffile_relpath=$1; shift
66     local conffile_source=$1; shift
67     local release_conffile=$RELEASE_CONFIG_PATH/$conffile_relpath
68     local etc_conffile=$CONFIG_PATH/$(basename "$conffile_relpath")
69
70     # Note that -h can return true and -e will return false simultaneously
71     # when the target is a dangling symlink.  We're okay with that outcome,
72     # so check -h first.
73     if [ ! -h "$release_conffile" ]; then
74         if [ ! -e "$release_conffile" ]; then
75             ln -s "$etc_conffile" "$release_conffile"
76         # If there's a config file in /var/www identical to the one in /etc,
77         # overwrite it with a symlink.
78         elif cmp --quiet "$release_conffile" "$etc_conffile"; then
79             ln --force -s "$etc_conffile" "$release_conffile"
80         fi
81     fi
82
83     if [ -n "$conffile_source" ]; then
84         cp --no-clobber "$RELEASE_CONFIG_PATH/$conffile_source" "$etc_conffile"
85         # Even if $etc_conffile already existed, it might be unmodified from
86         # the source.  This is especially likely when a user installs, updates
87         # database.yml, then reconfigures before they update application.yml.
88         # Use cmp to be sure whether $etc_conffile is modified.
89         if cmp --quiet "$RELEASE_CONFIG_PATH/$conffile_source" "$etc_conffile"; then
90             return 1
91         fi
92     fi
93 }
94
95 configure_version() {
96   WEB_SERVICE=${WEB_SERVICE:-$(service --status-all 2>/dev/null \
97       | grep -Eo '\bnginx|httpd[^[:space:]]*' || true)}
98   if [ -z "$WEB_SERVICE" ]; then
99     report_web_service_warning "Web service (Nginx or Apache) not found"
100   elif [ "$WEB_SERVICE" != "$(echo "$WEB_SERVICE" | head -n 1)" ]; then
101     WEB_SERVICE=$(echo "$WEB_SERVICE" | head -n 1)
102     report_web_service_warning \
103         "Multiple web services found.  Choosing the first one ($WEB_SERVICE)"
104   fi
105
106   if [ -e /etc/redhat-release ]; then
107       # Recognize any service that starts with "nginx"; e.g., nginx16.
108       if [ "$WEB_SERVICE" != "${WEB_SERVICE#nginx}" ]; then
109         WWW_OWNER=nginx:nginx
110       else
111         WWW_OWNER=apache:apache
112       fi
113   else
114       # Assume we're on a Debian-based system for now.
115       # Both Apache and Nginx run as www-data by default.
116       WWW_OWNER=www-data:www-data
117   fi
118
119   echo
120   echo "Assumption: $WEB_SERVICE is configured to serve your SSO server URL from"
121   echo "            $RELEASE_PATH"
122   echo "Assumption: configuration files are in $CONFIG_PATH"
123   echo "Assumption: $WEB_SERVICE and passenger run as $WWW_OWNER"
124   echo
125
126   echo -n "Symlinking files from $CONFIG_PATH ..."
127   mkdir -p $CONFIG_PATH
128   setup_conffile database.yml database.yml.example || DATABASE_READY=0
129   setup_conffile environments/production.rb environments/production.rb.example \
130       || true
131   setup_conffile application.yml application.yml.example || APPLICATION_READY=0
132   echo "... done."
133
134   # Before we do anything else, make sure some directories and files are in place
135   if [ ! -e $SHARED_PATH/log ]; then mkdir -p $SHARED_PATH/log; fi
136   if [ ! -e $RELEASE_PATH/tmp ]; then mkdir -p $RELEASE_PATH/tmp; fi
137   if [ ! -e $RELEASE_PATH/log ]; then ln -s $SHARED_PATH/log $RELEASE_PATH/log; fi
138   if [ ! -e $SHARED_PATH/log/production.log ]; then touch $SHARED_PATH/log/production.log; fi
139
140   cd "$RELEASE_PATH"
141   export RAILS_ENV=production
142
143   echo "Making sure bundle is installed ..."
144   set +e
145   which bundle > /dev/null
146   if [[ "$?" != "0" ]]; then
147     $COMMAND_PREFIX gem install bundle
148   fi
149   set -e
150   echo "... done."
151
152   echo -n "Running bundle install ..."
153   $COMMAND_PREFIX bundle install --path $SHARED_PATH/vendor_bundle --quiet || exit $?
154   echo "... done."
155
156   echo -n "Ensuring directory and file permissions ..."
157   # Ensure correct ownership of a few files
158   chown "$WWW_OWNER" $RELEASE_PATH/config/environment.rb
159   chown "$WWW_OWNER" $RELEASE_PATH/config.ru
160   chown "$WWW_OWNER" $RELEASE_PATH/config/database.yml
161   chown "$WWW_OWNER" $RELEASE_PATH/Gemfile.lock
162   chown -R "$WWW_OWNER" $RELEASE_PATH/tmp
163   chown -R "$WWW_OWNER" $SHARED_PATH/log
164   chown "$WWW_OWNER" $RELEASE_PATH/db/schema.rb
165   chmod 644 $SHARED_PATH/log/*
166   echo "... done."
167
168   set +e
169   DB_MIGRATE_STATUS=`$COMMAND_PREFIX bundle exec rake db:migrate:status 2>&1`
170   DB_MIGRATE_STATUS_CODE=$?
171   set -e
172
173   if echo $DB_MIGRATE_STATUS | grep 'Schema migrations table does not exist yet.' >/dev/null; then
174     # The database exists, but the migrations table doesn't.
175     echo -n "Setting up database ..."
176     $COMMAND_PREFIX bundle exec rake db:schema:load db:seed || exit $?
177     echo "... done."
178   elif echo $DB_MIGRATE_STATUS | grep '^database: ' >/dev/null; then
179     echo -n "Running db:migrate ..."
180     $COMMAND_PREFIX bundle exec rake db:migrate || exit $?
181     echo "... done."
182   elif echo $DB_MIGRATE_STATUS | grep 'database .* does not exist' >/dev/null; then
183     echo -n "Running db:setup ..."
184     set +e
185     $COMMAND_PREFIX bundle exec rake db:setup 2>/dev/null
186     if [ "$?" = "0" ]; then
187       echo "... done."
188     else
189       echo "... failed."
190       echo "Warning: unable to set up database." >&2
191       DATABASE_READY=0
192     fi
193     set -e
194   else
195     echo "Warning: Database is not ready to set up. Skipping database setup." >&2
196     DATABASE_READY=0
197   fi
198
199   echo -n "Precompiling assets ..."
200   # precompile assets; thankfully this does not take long
201   if [ "$APPLICATION_READY" = "1" ]; then
202     set +e
203     $COMMAND_PREFIX bundle exec rake assets:precompile -q -s 2>/dev/null
204     if [ "$?" = "0" ]; then
205       echo "... done."
206     else
207       echo "... failed."
208       APPLICATION_READY=0
209     fi
210     set -e
211   else
212     echo "... skipped."
213   fi
214   chown -R "$WWW_OWNER" $RELEASE_PATH/tmp
215
216   if [ ! -z "$WEB_SERVICE" ]; then
217     echo -n "Restarting $WEB_SERVICE ..."
218     service "$WEB_SERVICE" restart >/dev/null || exit $?
219     echo "... done."
220   fi
221 }
222
223 if [ "$1" = configure ]; then
224   # This is a debian-based system
225   configure_version
226 elif [ "$1" = "0" ] || [ "$1" = "1" ] || [ "$1" = "2" ]; then
227   # This is an rpm-based system
228   configure_version
229 fi
230
231 report_not_ready "$DATABASE_READY" "$CONFIG_PATH/database.yml"
232 report_not_ready "$APPLICATION_READY" "$CONFIG_PATH/application.yml"