Add support for CentOS6 to the improved SSO packages.
[arvados.git] / jenkins / arvados-sso-server-extras / arvados-sso-server.postinst
1 #!/bin/sh
2
3 set -e
4
5 INSTALL_PATH=/var/www/arvados-sso
6 RELEASE_PATH=$INSTALL_PATH/current
7 SHARED_PATH=$INSTALL_PATH/shared
8 CONFIG_PATH=/etc/arvados/sso
9
10 VERSION=`cat $RELEASE_PATH/git-commit.version`
11
12 setup_ruby_environment() {
13   if [ -s "$HOME/.rvm/scripts/rvm" ] ; then
14     using_rvm="true"
15   elif [ -s "/usr/local/rvm/scripts/rvm" ] ; then
16     using_rvm="true"
17   else
18     using_rvm="false"
19   fi
20
21   if [ "$using_rvm" = "true" ]; then
22     COMMAND_PREFIX="/usr/local/rvm/bin/rvm-exec default"
23   else
24     COMMAND_PREFIX=
25   fi
26 }
27
28 # arguments: <major version> <most recently configured package version>
29 configure_version() {
30   VERSION="$1"
31
32   [ "$VERSION" ] || { echo "Error: configure_version: need version parameter" >&2; exit 1; }
33
34   WEB_SERVICE=${WEB_SERVICE:-$(service --status-all 2>/dev/null \
35       | grep -Eo '\bnginx|httpd[^[:space:]]*' || true)}
36   if [ -z "$WEB_SERVICE" ]; then
37     cat >&2 <<EOF
38
39 Warning: web service (Nginx or Apache) not found.
40
41 To override, set the WEB_SERVICE environment variable to the name of the service
42 hosting the Rails server. Alternativey, install nginx.
43
44 For Debian-based systems, then reconfigure this package with dpkg-reconfigure.
45
46 For RPM-based systems, then reinstall this package.
47
48 EOF
49   elif [ "$WEB_SERVICE" != "$(echo "$WEB_SERVICE" | head -n 1)" ]; then
50     WEB_SERVICE=$(echo "$WEB_SERVICE" | head -n 1)
51     cat >&2 <<EOF
52 Warning: multiple web services found. Choosing the first one ($WEB_SERVICE).
53
54 To override, set the WEB_SERVICE environment variable to the name of the service
55 hosting the Rails server.
56
57 For Debian-based systems, then reconfigure this package with dpkg-reconfigure.
58
59 For RPM-based systems, then reinstall this package.
60
61 EOF
62   fi
63
64   if [ -e /etc/redhat-release ]; then
65       if [ "$WEB_SERVICE" = "nginx" ]; then
66         WWW_OWNER=nginx:nginx
67       else
68         WWW_OWNER=apache:apache
69       fi
70   else
71       # Assume we're on a Debian-based system for now.
72       # Both Apache and Nginx run as www-data by default.
73       WWW_OWNER=www-data:www-data
74   fi
75
76   echo
77   echo "Assumption: $WEB_SERVICE is configured to serve your SSO server URL from"
78   echo "            /var/www/arvados-sso/current"
79   echo "Assumption: configuration files are in /etc/arvados/sso/"
80   echo "Assumption: $WEB_SERVICE and passenger run as $WWW_OWNER"
81   echo
82
83   echo -n "Symlinking files from $CONFIG_PATH ..."
84
85   if [ ! -h $RELEASE_PATH/config/database.yml ]; then
86     ln -s $CONFIG_PATH/database.yml $RELEASE_PATH/config/database.yml
87   fi
88   if [ ! -h $RELEASE_PATH/config/environments/production.rb ]; then
89     if [ ! -f $CONFIG_PATH/production.rb ]; then
90       cp $RELEASE_PATH/config/environments/production.rb.example $CONFIG_PATH/production.rb
91     fi
92     ln -s $CONFIG_PATH/production.rb $RELEASE_PATH/config/environments/production.rb
93   fi
94   if [ ! -h $RELEASE_PATH/config/application.yml ]; then
95     ln -s $CONFIG_PATH/application.yml $RELEASE_PATH/config/application.yml
96   fi
97   echo "... done."
98
99   # Before we do anything else, make sure some directories and files are in place
100   if [ ! -e $SHARED_PATH/log ]; then mkdir -p $SHARED_PATH/log; fi
101   if [ ! -e $RELEASE_PATH/tmp ]; then mkdir -p $RELEASE_PATH/tmp; fi
102   if [ ! -e $RELEASE_PATH/log ]; then ln -s $SHARED_PATH/log $RELEASE_PATH/log; fi
103   if [ ! -e $SHARED_PATH/log/production.log ]; then touch $SHARED_PATH/log/production.log; fi
104
105   cd "$RELEASE_PATH"
106   export RAILS_ENV=production
107
108   echo -n "Running bundle install ..."
109   $COMMAND_PREFIX bundle install --path $SHARED_PATH/vendor_bundle --quiet || exit $?
110   echo "... done."
111
112   echo -n "Ensuring directory and file permissions ..."
113   # Ensure correct ownership of a few files
114   chown "$WWW_OWNER" $RELEASE_PATH/config/environment.rb
115   chown "$WWW_OWNER" $RELEASE_PATH/config.ru
116   chown "$WWW_OWNER" $RELEASE_PATH/config/database.yml
117   chown "$WWW_OWNER" $RELEASE_PATH/Gemfile.lock
118   chown -R "$WWW_OWNER" $RELEASE_PATH/tmp
119   chown -R "$WWW_OWNER" $SHARED_PATH/log
120   chown "$WWW_OWNER" $RELEASE_PATH/db/schema.rb
121   chmod 644 $SHARED_PATH/log/*
122   echo "... done."
123
124   set +e
125   DB_MIGRATE_STATUS=`$COMMAND_PREFIX bundle exec rake db:migrate:status 2>/dev/null`
126   DB_MIGRATE_STATUS_CODE=$?
127   set -e
128
129   if echo $DB_MIGRATE_STATUS | grep 'Schema migrations table does not exist yet.' >/dev/null; then
130     # The database exists, but the migrations table doesn't.
131     echo -n "Setting up database ..."
132     $COMMAND_PREFIX bundle exec rake db:schema:load db:seed || exit $?
133   elif echo $DB_MIGRATE_STATUS | grep '^database: ' >/dev/null; then
134     echo -n "Running db:migrate ..."
135     $COMMAND_PREFIX bundle exec rake db:migrate || exit $?
136   else
137     echo "Error: Database is not ready to set up. Aborting." >&2
138     exit 1
139   fi
140   echo "... done."
141
142   echo -n "Precompiling assets ..."
143   # precompile assets; thankfully this does not take long
144   $COMMAND_PREFIX bundle exec rake assets:precompile -q -s || exit $?
145   chown -R "$WWW_OWNER" $RELEASE_PATH/tmp
146   echo "... done."
147
148   if [ ! -z "$WEB_SERVICE" ]; then
149     echo -n "Restarting $WEB_SERVICE ..."
150     service "$WEB_SERVICE" restart || exit $?
151     echo "... done."
152   fi
153
154   echo
155 }
156
157 if [ "$1" = configure ]; then
158   # This is a debian-based system
159   setup_ruby_environment
160   configure_version $VERSION "$2"
161 elif [ "$1" = "0" ] || [ "$1" = "1" ] || [ "$1" = "2" ]; then
162   # This is an rpm-based system
163   setup_ruby_environment
164   configure_version $VERSION
165 fi