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