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.
40 report_web_service_warning() {
41 local warning=$1; shift
46 To override, set the WEB_SERVICE environment variable to the name of the service
47 hosting the Rails server.
49 For Debian-based systems, then reconfigure this package with dpkg-reconfigure.
51 For RPM-based systems, then reinstall this package.
57 WEB_SERVICE=${WEB_SERVICE:-$(service --status-all 2>/dev/null \
58 | grep -Eo '\bnginx|httpd[^[:space:]]*' || true)}
59 if [ -z "$WEB_SERVICE" ]; then
60 report_web_service_warning "Web service (Nginx or Apache) not found"
61 elif [ "$WEB_SERVICE" != "$(echo "$WEB_SERVICE" | head -n 1)" ]; then
62 WEB_SERVICE=$(echo "$WEB_SERVICE" | head -n 1)
63 report_web_service_warning \
64 "Multiple web services found. Choosing the first one ($WEB_SERVICE)"
67 if [ -e /etc/redhat-release ]; then
68 # Recognize any service that starts with "nginx"; e.g., nginx16.
69 if [ "$WEB_SERVICE" != "${WEB_SERVICE#nginx}" ]; then
72 WWW_OWNER=apache:apache
75 # Assume we're on a Debian-based system for now.
76 # Both Apache and Nginx run as www-data by default.
77 WWW_OWNER=www-data:www-data
81 echo "Assumption: $WEB_SERVICE is configured to serve your SSO server URL from"
83 echo "Assumption: configuration files are in $CONFIG_PATH"
84 echo "Assumption: $WEB_SERVICE and passenger run as $WWW_OWNER"
87 echo -n "Symlinking files from $CONFIG_PATH ..."
89 if [ ! -f $CONFIG_PATH/database.yml ]; then
91 cp $RELEASE_PATH/config/database.yml.example $CONFIG_PATH/database.yml
95 if [ ! -h $RELEASE_PATH/config/database.yml ]; then
96 ln -s $CONFIG_PATH/database.yml $RELEASE_PATH/config/database.yml
99 if [ ! -f $CONFIG_PATH/production.rb ]; then
100 mkdir -p $CONFIG_PATH
101 cp $RELEASE_PATH/config/environments/production.rb.example $CONFIG_PATH/production.rb
104 if [ ! -h $RELEASE_PATH/config/environments/production.rb ]; then
105 ln -s $CONFIG_PATH/production.rb $RELEASE_PATH/config/environments/production.rb
108 if [ ! -f $CONFIG_PATH/application.yml ]; then
109 mkdir -p $CONFIG_PATH
110 cp $RELEASE_PATH/config/application.yml.example $CONFIG_PATH/application.yml
114 if [ ! -h $RELEASE_PATH/config/application.yml ]; then
115 ln -s $CONFIG_PATH/application.yml $RELEASE_PATH/config/application.yml
120 # Before we do anything else, make sure some directories and files are in place
121 if [ ! -e $SHARED_PATH/log ]; then mkdir -p $SHARED_PATH/log; fi
122 if [ ! -e $RELEASE_PATH/tmp ]; then mkdir -p $RELEASE_PATH/tmp; fi
123 if [ ! -e $RELEASE_PATH/log ]; then ln -s $SHARED_PATH/log $RELEASE_PATH/log; fi
124 if [ ! -e $SHARED_PATH/log/production.log ]; then touch $SHARED_PATH/log/production.log; fi
127 export RAILS_ENV=production
129 echo "Making sure bundle is installed ..."
131 which bundle > /dev/null
132 if [[ "$?" != "0" ]]; then
133 $COMMAND_PREFIX gem install bundle
138 echo -n "Running bundle install ..."
139 $COMMAND_PREFIX bundle install --path $SHARED_PATH/vendor_bundle --quiet || exit $?
142 echo -n "Ensuring directory and file permissions ..."
143 # Ensure correct ownership of a few files
144 chown "$WWW_OWNER" $RELEASE_PATH/config/environment.rb
145 chown "$WWW_OWNER" $RELEASE_PATH/config.ru
146 chown "$WWW_OWNER" $RELEASE_PATH/config/database.yml
147 chown "$WWW_OWNER" $RELEASE_PATH/Gemfile.lock
148 chown -R "$WWW_OWNER" $RELEASE_PATH/tmp
149 chown -R "$WWW_OWNER" $SHARED_PATH/log
150 chown "$WWW_OWNER" $RELEASE_PATH/db/schema.rb
151 chmod 644 $SHARED_PATH/log/*
155 DB_MIGRATE_STATUS=`$COMMAND_PREFIX bundle exec rake db:migrate:status 2>&1`
156 DB_MIGRATE_STATUS_CODE=$?
159 if echo $DB_MIGRATE_STATUS | grep 'Schema migrations table does not exist yet.' >/dev/null; then
160 # The database exists, but the migrations table doesn't.
161 echo -n "Setting up database ..."
162 $COMMAND_PREFIX bundle exec rake db:schema:load db:seed || exit $?
164 elif echo $DB_MIGRATE_STATUS | grep '^database: ' >/dev/null; then
165 echo -n "Running db:migrate ..."
166 $COMMAND_PREFIX bundle exec rake db:migrate || exit $?
168 elif echo $DB_MIGRATE_STATUS | grep 'database .* does not exist' >/dev/null; then
169 echo -n "Running db:setup ..."
171 $COMMAND_PREFIX bundle exec rake db:setup 2>/dev/null
172 if [ "$?" = "0" ]; then
176 echo "Warning: unable to set up database." >&2
181 echo "Warning: Database is not ready to set up. Skipping database setup." >&2
185 echo -n "Precompiling assets ..."
186 # precompile assets; thankfully this does not take long
187 if [ "$APPLICATION_READY" = "1" ]; then
189 $COMMAND_PREFIX bundle exec rake assets:precompile -q -s 2>/dev/null
190 if [ "$?" = "0" ]; then
200 chown -R "$WWW_OWNER" $RELEASE_PATH/tmp
202 if [ ! -z "$WEB_SERVICE" ]; then
203 echo -n "Restarting $WEB_SERVICE ..."
204 service "$WEB_SERVICE" restart >/dev/null || exit $?
209 if [ "$1" = configure ]; then
210 # This is a debian-based system
212 elif [ "$1" = "0" ] || [ "$1" = "1" ] || [ "$1" = "2" ]; then
213 # This is an rpm-based system
217 report_not_ready "$DATABASE_READY" "$CONFIG_PATH/database.yml"
218 report_not_ready "$APPLICATION_READY" "$CONFIG_PATH/application.yml"