Fix quoting of local variable assignments throughout.
[arvados-dev.git] / jenkins / arvados-api-server-extras / arvados-api-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-api/current
29 SHARED_PATH=/var/www/arvados-api/shared
30 CONFIG_PATH=/etc/arvados/api/
31
32 echo "Assumption: $NGINX_SERVICE is configured to serve your API server URL from"
33 echo "            /var/www/arvados-api/current"
34 echo "Assumption: configuration files are in /etc/arvados/api/"
35 echo "Assumption: $NGINX_SERVICE and passenger run as $WWW_OWNER"
36 echo
37
38 echo "Copying files from $CONFIG_PATH"
39 cp -f $CONFIG_PATH/database.yml $RELEASE_PATH/config/database.yml
40 cp -f $RELEASE_PATH/config/environments/production.rb.example $RELEASE_PATH/config/environments/production.rb
41 cp -f $CONFIG_PATH/application.yml $RELEASE_PATH/config/application.yml
42 if [ -e $CONFIG_PATH/omniauth.rb ]; then
43     cp -f $CONFIG_PATH/omniauth.rb $RELEASE_PATH/config/initializers/omniauth.rb
44 fi
45 echo "Done."
46
47 # Before we do anything else, make sure some directories and files are in place
48 if [[ ! -e $SHARED_PATH/log ]]; then mkdir -p $SHARED_PATH/log; fi
49 if [[ ! -e $RELEASE_PATH/tmp ]]; then mkdir -p $RELEASE_PATH/tmp; fi
50 if [[ ! -e $RELEASE_PATH/log ]]; then ln -s $SHARED_PATH/log $RELEASE_PATH/log; fi
51 if [[ ! -e $SHARED_PATH/log/production.log ]]; then touch $SHARED_PATH/log/production.log; fi
52
53 cd "$RELEASE_PATH"
54 export RAILS_ENV=production
55
56 echo "Making sure bundle is installed"
57 set +e
58 which bundle > /dev/null
59 if [[ "$?" != "0" ]]; then
60   gem install bundle
61 fi
62 set -e
63 echo "Done."
64
65 echo "Running bundle install"
66 bundle install --path $SHARED_PATH/vendor_bundle
67 echo "Done."
68
69 echo "Precompiling assets"
70 # precompile assets; thankfully this does not take long
71 bundle exec rake assets:precompile
72 echo "Done."
73
74 echo "Ensuring directory and file permissions"
75 # Ensure correct ownership of a few files
76 chown "$WWW_OWNER" $RELEASE_PATH/config/environment.rb
77 chown "$WWW_OWNER" $RELEASE_PATH/config.ru
78 chown "$WWW_OWNER" $RELEASE_PATH/config/database.yml
79 chown "$WWW_OWNER" $RELEASE_PATH/Gemfile.lock
80 chown -R "$WWW_OWNER" $RELEASE_PATH/tmp
81 chown -R "$WWW_OWNER" $SHARED_PATH/log
82 chown "$WWW_OWNER" $RELEASE_PATH/db/structure.sql
83 chmod 644 $SHARED_PATH/log/*
84 # Rails creates the cache directory if it doesn't exist
85 if [[ -d $RELEASE_PATH/tmp/cache/ ]]; then
86   chmod -R 2775 $RELEASE_PATH/tmp/cache/
87 fi
88 echo "Done."
89
90 echo "Running sanity check"
91 bundle exec rake config:check
92 SANITY_CHECK_EXIT_CODE=$?
93 echo "Done."
94
95 if [[ "$SANITY_CHECK_EXIT_CODE" != "0" ]]; then
96   echo "Sanity check failed, aborting. Please roll back to the previous version of the package."
97   echo "The database has not been migrated yet, so reinstalling the previous version is safe."
98   exit $SANITY_CHECK_EXIT_CODE
99 fi
100
101 echo "Checking database status"
102 # If we use `grep -q`, rake will write a backtrace on EPIPE.
103 if bundle exec rake db:migrate:status | grep '^database: ' >/dev/null; then
104     echo "Starting db:migrate"
105     bundle exec rake db:migrate
106 elif [ 0 -eq ${PIPESTATUS[0]} ]; then
107     # The database exists, but the migrations table doesn't.
108     echo "Setting up database"
109     bundle exec rake db:structure:load db:seed
110 else
111     echo "Error: Database is not ready to set up. Aborting." >&2
112     exit 1
113 fi
114 echo "Done."
115
116 echo "Restarting nginx"
117 service "$NGINX_SERVICE" restart
118 echo "Done."