7953: Fix Docker image to test CentOS 6.6 packages.
[arvados-dev.git] / jenkins / arvados-sso-server-extras / arvados-sso-server-upgrade.sh
1 #!/bin/bash
2
3 set -e
4
5 if [ -e /etc/redhat-release ]; then
6     WWW_OWNER=nginx:nginx
7 else
8     # Assume we're on a Debian-based system for now.
9     WWW_OWNER=www-data:www-data
10 fi
11
12 NGINX_SERVICE=${NGINX_SERVICE:-$(service --status-all 2>/dev/null \
13     | grep -Eo '\bnginx[^[:space:]]*' || true)}
14 if [ -z "$NGINX_SERVICE" ]; then
15     cat >&2 <<EOF
16 Error: nginx service not found. Aborting.
17 Set NGINX_SERVICE to the name of the service hosting the Rails server.
18 EOF
19     exit 1
20 elif [ "$NGINX_SERVICE" != "$(echo "$NGINX_SERVICE" | head -n 1)" ]; then
21     cat >&2 <<EOF
22 Error: multiple nginx services found. Aborting.
23 Set NGINX_SERVICE to the name of the service hosting the Rails server.
24 EOF
25     exit 1
26 fi
27
28 RELEASE_PATH=/var/www/arvados-sso/current
29 SHARED_PATH=/var/www/arvados-sso/shared
30 CONFIG_PATH=/etc/arvados/sso/
31
32 echo
33 echo "Assumption: $NGINX_SERVICE is configured to serve your SSO server URL from"
34 echo "            /var/www/arvados-sso/current"
35 echo "Assumption: configuration files are in /etc/arvados/sso/"
36 echo "Assumption: $NGINX_SERVICE and passenger run as $WWW_OWNER"
37 echo
38
39 echo "Copying files from $CONFIG_PATH ..."
40 cp -f $CONFIG_PATH/database.yml $RELEASE_PATH/config/database.yml
41 cp -f $RELEASE_PATH/config/environments/production.rb.example $RELEASE_PATH/config/environments/production.rb
42 cp -f $CONFIG_PATH/application.yml $RELEASE_PATH/config/application.yml
43 echo "... done."
44
45 # Before we do anything else, make sure some directories and files are in place
46 if [[ ! -e $SHARED_PATH/log ]]; then mkdir -p $SHARED_PATH/log; fi
47 if [[ ! -e $RELEASE_PATH/tmp ]]; then mkdir -p $RELEASE_PATH/tmp; fi
48 if [[ ! -e $RELEASE_PATH/log ]]; then ln -s $SHARED_PATH/log $RELEASE_PATH/log; fi
49 if [[ ! -e $SHARED_PATH/log/production.log ]]; then touch $SHARED_PATH/log/production.log; fi
50
51 cd "$RELEASE_PATH"
52 export RAILS_ENV=production
53
54 echo "Running bundle install ..."
55 bundle install --path $SHARED_PATH/vendor_bundle --quiet
56 echo "... done."
57
58 echo "Ensuring directory and file permissions ..."
59 # Ensure correct ownership of a few files
60 chown "$WWW_OWNER" $RELEASE_PATH/config/environment.rb
61 chown "$WWW_OWNER" $RELEASE_PATH/config.ru
62 chown "$WWW_OWNER" $RELEASE_PATH/config/database.yml
63 chown "$WWW_OWNER" $RELEASE_PATH/Gemfile.lock
64 chown -R "$WWW_OWNER" $RELEASE_PATH/tmp
65 chown -R "$WWW_OWNER" $SHARED_PATH/log
66 chown "$WWW_OWNER" $RELEASE_PATH/db/schema.rb
67 chmod 644 $SHARED_PATH/log/*
68 echo "... done."
69
70 # If we use `grep -q`, rake will write a backtrace on EPIPE.
71 if bundle exec rake db:migrate:status | grep '^database: ' >/dev/null; then
72     echo "Starting db:migrate ..."
73     bundle exec rake db:migrate
74 elif [ 0 -eq ${PIPESTATUS[0]} ]; then
75     # The database exists, but the migrations table doesn't.
76     echo "Setting up database ..."
77     bundle exec rake db:schema:load db:seed
78 else
79     echo "Error: Database is not ready to set up. Aborting." >&2
80     exit 1
81 fi
82 echo "... done."
83
84 echo "Precompiling assets ..."
85 # precompile assets; thankfully this does not take long
86 bundle exec rake assets:precompile -q -s
87 echo "... done."
88
89 echo "Restarting nginx ..."
90 service "$NGINX_SERVICE" restart
91 echo "... done."