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 PACKAGE_NAME=arvados-sso-server
10 DOC_URL="http://doc.arvados.org/install/install-sso.html#configure"
15 if [ -s "$HOME/.rvm/scripts/rvm" ] || [ -s "/usr/local/rvm/scripts/rvm" ]; then
16 COMMAND_PREFIX="/usr/local/rvm/bin/rvm-exec default"
22 local ready_flag=$1; shift
23 local config_file=$1; shift
24 if [ "1" != "$ready_flag" ]; then cat >&2 <<EOF
28 The $PACKAGE_NAME package was not configured completely because
29 $config_file needs some tweaking.
30 Please refer to the documentation at
31 <$DOC_URL> for more details.
33 When $(basename "$config_file") has been modified,
34 reconfigure or reinstall this package.
41 WEB_SERVICE=${WEB_SERVICE:-$(service --status-all 2>/dev/null \
42 | grep -Eo '\bnginx|httpd[^[:space:]]*' || true)}
43 if [ -z "$WEB_SERVICE" ]; then
46 Warning: web service (Nginx or Apache) not found.
48 To override, set the WEB_SERVICE environment variable to the name of the service
49 hosting the Rails server. Alternativey, install nginx.
51 For Debian-based systems, then reconfigure this package with dpkg-reconfigure.
53 For RPM-based systems, then reinstall this package.
56 elif [ "$WEB_SERVICE" != "$(echo "$WEB_SERVICE" | head -n 1)" ]; then
57 WEB_SERVICE=$(echo "$WEB_SERVICE" | head -n 1)
59 Warning: multiple web services found. Choosing the first one ($WEB_SERVICE).
61 To override, set the WEB_SERVICE environment variable to the name of the service
62 hosting the Rails server.
64 For Debian-based systems, then reconfigure this package with dpkg-reconfigure.
66 For RPM-based systems, then reinstall this package.
71 if [ -e /etc/redhat-release ]; then
72 if [ "$WEB_SERVICE" = "nginx" ]; then
75 WWW_OWNER=apache:apache
78 # Assume we're on a Debian-based system for now.
79 # Both Apache and Nginx run as www-data by default.
80 WWW_OWNER=www-data:www-data
84 echo "Assumption: $WEB_SERVICE is configured to serve your SSO server URL from"
86 echo "Assumption: configuration files are in $CONFIG_PATH"
87 echo "Assumption: $WEB_SERVICE and passenger run as $WWW_OWNER"
90 echo -n "Symlinking files from $CONFIG_PATH ..."
92 if [ ! -f $CONFIG_PATH/database.yml ]; then
94 cp $RELEASE_PATH/config/database.yml.example $CONFIG_PATH/database.yml
98 if [ ! -h $RELEASE_PATH/config/database.yml ]; then
99 ln -s $CONFIG_PATH/database.yml $RELEASE_PATH/config/database.yml
102 if [ ! -f $CONFIG_PATH/production.rb ]; then
103 mkdir -p $CONFIG_PATH
104 cp $RELEASE_PATH/config/environments/production.rb.example $CONFIG_PATH/production.rb
107 if [ ! -h $RELEASE_PATH/config/environments/production.rb ]; then
108 ln -s $CONFIG_PATH/production.rb $RELEASE_PATH/config/environments/production.rb
111 if [ ! -f $CONFIG_PATH/application.yml ]; then
112 mkdir -p $CONFIG_PATH
113 cp $RELEASE_PATH/config/application.yml.example $CONFIG_PATH/application.yml
117 if [ ! -h $RELEASE_PATH/config/application.yml ]; then
118 ln -s $CONFIG_PATH/application.yml $RELEASE_PATH/config/application.yml
123 # Before we do anything else, make sure some directories and files are in place
124 if [ ! -e $SHARED_PATH/log ]; then mkdir -p $SHARED_PATH/log; fi
125 if [ ! -e $RELEASE_PATH/tmp ]; then mkdir -p $RELEASE_PATH/tmp; fi
126 if [ ! -e $RELEASE_PATH/log ]; then ln -s $SHARED_PATH/log $RELEASE_PATH/log; fi
127 if [ ! -e $SHARED_PATH/log/production.log ]; then touch $SHARED_PATH/log/production.log; fi
130 export RAILS_ENV=production
132 echo "Making sure bundle is installed ..."
134 which bundle > /dev/null
135 if [[ "$?" != "0" ]]; then
136 $COMMAND_PREFIX gem install bundle
141 echo -n "Running bundle install ..."
142 $COMMAND_PREFIX bundle install --path $SHARED_PATH/vendor_bundle --quiet || exit $?
145 echo -n "Ensuring directory and file permissions ..."
146 # Ensure correct ownership of a few files
147 chown "$WWW_OWNER" $RELEASE_PATH/config/environment.rb
148 chown "$WWW_OWNER" $RELEASE_PATH/config.ru
149 chown "$WWW_OWNER" $RELEASE_PATH/config/database.yml
150 chown "$WWW_OWNER" $RELEASE_PATH/Gemfile.lock
151 chown -R "$WWW_OWNER" $RELEASE_PATH/tmp
152 chown -R "$WWW_OWNER" $SHARED_PATH/log
153 chown "$WWW_OWNER" $RELEASE_PATH/db/schema.rb
154 chmod 644 $SHARED_PATH/log/*
158 DB_MIGRATE_STATUS=`$COMMAND_PREFIX bundle exec rake db:migrate:status 2>&1`
159 DB_MIGRATE_STATUS_CODE=$?
162 if echo $DB_MIGRATE_STATUS | grep 'Schema migrations table does not exist yet.' >/dev/null; then
163 # The database exists, but the migrations table doesn't.
164 echo -n "Setting up database ..."
165 $COMMAND_PREFIX bundle exec rake db:schema:load db:seed || exit $?
167 elif echo $DB_MIGRATE_STATUS | grep '^database: ' >/dev/null; then
168 echo -n "Running db:migrate ..."
169 $COMMAND_PREFIX bundle exec rake db:migrate || exit $?
171 elif echo $DB_MIGRATE_STATUS | grep 'database .* does not exist' >/dev/null; then
172 echo -n "Running db:setup ..."
174 $COMMAND_PREFIX bundle exec rake db:setup 2>/dev/null
175 if [ "$?" = "0" ]; then
179 echo "Warning: unable to set up database." >&2
184 echo "Warning: Database is not ready to set up. Skipping database setup." >&2
188 echo -n "Precompiling assets ..."
189 # precompile assets; thankfully this does not take long
190 if [ "$APPLICATION_READY" = "1" ]; then
192 $COMMAND_PREFIX bundle exec rake assets:precompile -q -s 2>/dev/null
193 if [ "$?" = "0" ]; then
203 chown -R "$WWW_OWNER" $RELEASE_PATH/tmp
205 if [ ! -z "$WEB_SERVICE" ]; then
206 echo -n "Restarting $WEB_SERVICE ..."
207 service "$WEB_SERVICE" restart >/dev/null || exit $?
212 if [ "$1" = configure ]; then
213 # This is a debian-based system
215 elif [ "$1" = "0" ] || [ "$1" = "1" ] || [ "$1" = "2" ]; then
216 # This is an rpm-based system
220 report_not_ready "$DATABASE_READY" "$CONFIG_PATH/database.yml"
221 report_not_ready "$APPLICATION_READY" "$CONFIG_PATH/application.yml"