5 INSTALL_PATH=/var/www/arvados-sso
6 RELEASE_PATH=$INSTALL_PATH/current
7 RELEASE_CONFIG_PATH=$RELEASE_PATH/config
8 SHARED_PATH=$INSTALL_PATH/shared
9 CONFIG_PATH=/etc/arvados/sso
10 PACKAGE_NAME=arvados-sso-server
11 DOC_URL="http://doc.arvados.org/install/install-sso.html#configure"
16 if [ -s "$HOME/.rvm/scripts/rvm" ] || [ -s "/usr/local/rvm/scripts/rvm" ]; then
17 COMMAND_PREFIX="/usr/local/rvm/bin/rvm-exec default"
23 local ready_flag=$1; shift
24 local config_file=$1; shift
25 if [ "1" != "$ready_flag" ]; then cat >&2 <<EOF
29 The $PACKAGE_NAME package was not configured completely because
30 $config_file needs some tweaking.
31 Please refer to the documentation at
32 <$DOC_URL> for more details.
34 When $(basename "$config_file") has been modified,
35 reconfigure or reinstall this package.
41 report_web_service_warning() {
42 local warning=$1; shift
47 To override, set the WEB_SERVICE environment variable to the name of the service
48 hosting the Rails server.
50 For Debian-based systems, then reconfigure this package with dpkg-reconfigure.
52 For RPM-based systems, then reinstall this package.
58 # Usage: run_and_report ACTION_MSG CMD
59 # This is the usual wrapper that prints ACTION_MSG, runs CMD, then writes
60 # a message about whether CMD succeeded or failed. Returns the exit code
62 local action_message=$1; shift
64 echo -n "$action_message..."
75 # Usage: setup_conffile CONFFILE_PATH [SOURCE_PATH]
76 # Both paths are relative to RELEASE_CONFIG_PATH.
77 # This function will try to safely ensure that a symbolic link for
78 # the configuration file points from RELEASE_CONFIG_PATH to CONFIG_PATH.
79 # If SOURCE_PATH is given, this function will try to install that file as
80 # the configuration file in CONFIG_PATH, and return 1 if the file in
81 # CONFIG_PATH is unmodified from the source.
82 local conffile_relpath=$1; shift
83 local conffile_source=$1; shift
84 local release_conffile=$RELEASE_CONFIG_PATH/$conffile_relpath
85 local etc_conffile=$CONFIG_PATH/$(basename "$conffile_relpath")
87 # Note that -h can return true and -e will return false simultaneously
88 # when the target is a dangling symlink. We're okay with that outcome,
90 if [ ! -h "$release_conffile" ]; then
91 if [ ! -e "$release_conffile" ]; then
92 ln -s "$etc_conffile" "$release_conffile"
93 # If there's a config file in /var/www identical to the one in /etc,
94 # overwrite it with a symlink.
95 elif cmp --quiet "$release_conffile" "$etc_conffile"; then
96 ln --force -s "$etc_conffile" "$release_conffile"
100 if [ -n "$conffile_source" ]; then
101 cp --no-clobber "$RELEASE_CONFIG_PATH/$conffile_source" "$etc_conffile"
102 # Even if $etc_conffile already existed, it might be unmodified from
103 # the source. This is especially likely when a user installs, updates
104 # database.yml, then reconfigures before they update application.yml.
105 # Use cmp to be sure whether $etc_conffile is modified.
106 if cmp --quiet "$RELEASE_CONFIG_PATH/$conffile_source" "$etc_conffile"; then
112 configure_version() {
113 WEB_SERVICE=${WEB_SERVICE:-$(service --status-all 2>/dev/null \
114 | grep -Eo '\bnginx|httpd[^[:space:]]*' || true)}
115 if [ -z "$WEB_SERVICE" ]; then
116 report_web_service_warning "Web service (Nginx or Apache) not found"
117 elif [ "$WEB_SERVICE" != "$(echo "$WEB_SERVICE" | head -n 1)" ]; then
118 WEB_SERVICE=$(echo "$WEB_SERVICE" | head -n 1)
119 report_web_service_warning \
120 "Multiple web services found. Choosing the first one ($WEB_SERVICE)"
123 if [ -e /etc/redhat-release ]; then
124 # Recognize any service that starts with "nginx"; e.g., nginx16.
125 if [ "$WEB_SERVICE" != "${WEB_SERVICE#nginx}" ]; then
126 WWW_OWNER=nginx:nginx
128 WWW_OWNER=apache:apache
131 # Assume we're on a Debian-based system for now.
132 # Both Apache and Nginx run as www-data by default.
133 WWW_OWNER=www-data:www-data
137 echo "Assumption: $WEB_SERVICE is configured to serve your SSO server URL from"
138 echo " $RELEASE_PATH"
139 echo "Assumption: configuration files are in $CONFIG_PATH"
140 echo "Assumption: $WEB_SERVICE and passenger run as $WWW_OWNER"
143 echo -n "Symlinking files from $CONFIG_PATH ..."
144 mkdir -p $CONFIG_PATH
145 setup_conffile database.yml database.yml.example || DATABASE_READY=0
146 setup_conffile environments/production.rb environments/production.rb.example \
148 setup_conffile application.yml application.yml.example || APPLICATION_READY=0
151 # Before we do anything else, make sure some directories and files are in place
152 if [ ! -e $SHARED_PATH/log ]; then mkdir -p $SHARED_PATH/log; fi
153 if [ ! -e $RELEASE_PATH/tmp ]; then mkdir -p $RELEASE_PATH/tmp; fi
154 if [ ! -e $RELEASE_PATH/log ]; then ln -s $SHARED_PATH/log $RELEASE_PATH/log; fi
155 if [ ! -e $SHARED_PATH/log/production.log ]; then touch $SHARED_PATH/log/production.log; fi
158 export RAILS_ENV=production
160 if ! $COMMAND_PREFIX bundle --version >/dev/null; then
161 run_and_report "Installing bundle" $COMMAND_PREFIX gem install bundle
164 run_and_report "Running bundle install" \
165 $COMMAND_PREFIX bundle install --path $SHARED_PATH/vendor_bundle --quiet
167 echo -n "Ensuring directory and file permissions ..."
168 # Ensure correct ownership of a few files
169 chown "$WWW_OWNER" $RELEASE_PATH/config/environment.rb
170 chown "$WWW_OWNER" $RELEASE_PATH/config.ru
171 chown "$WWW_OWNER" $RELEASE_PATH/config/database.yml
172 chown "$WWW_OWNER" $RELEASE_PATH/Gemfile.lock
173 chown -R "$WWW_OWNER" $RELEASE_PATH/tmp
174 chown -R "$WWW_OWNER" $SHARED_PATH/log
175 chown "$WWW_OWNER" $RELEASE_PATH/db/schema.rb
176 chmod 644 $SHARED_PATH/log/*
179 DB_MIGRATE_STATUS=`$COMMAND_PREFIX bundle exec rake db:migrate:status 2>&1 || true`
180 if echo $DB_MIGRATE_STATUS | grep -qF 'Schema migrations table does not exist yet.'; then
181 # The database exists, but the migrations table doesn't.
182 run_and_report "Setting up database" \
183 $COMMAND_PREFIX bundle exec rake db:schema:load db:seed
184 elif echo $DB_MIGRATE_STATUS | grep -q '^database: '; then
185 run_and_report "Running db:migrate" \
186 $COMMAND_PREFIX bundle exec rake db:migrate
187 elif echo $DB_MIGRATE_STATUS | grep -q 'database .* does not exist'; then
188 if ! run_and_report "Running db:setup" \
189 $COMMAND_PREFIX bundle exec rake db:setup 2>/dev/null; then
190 echo "Warning: unable to set up database." >&2
194 echo "Warning: Database is not ready to set up. Skipping database setup." >&2
198 # precompile assets; thankfully this does not take long
199 if [ "$APPLICATION_READY" = "1" ]; then
200 run_and_report "Precompiling assets" \
201 $COMMAND_PREFIX bundle exec rake assets:precompile -q -s 2>/dev/null \
202 || APPLICATION_READY=0
204 echo "Precompiling assets... skipped."
206 chown -R "$WWW_OWNER" $RELEASE_PATH/tmp
208 if [ ! -z "$WEB_SERVICE" ]; then
209 service "$WEB_SERVICE" restart
213 if [ "$1" = configure ]; then
214 # This is a debian-based system
216 elif [ "$1" = "0" ] || [ "$1" = "1" ] || [ "$1" = "2" ]; then
217 # This is an rpm-based system
221 report_not_ready "$DATABASE_READY" "$CONFIG_PATH/database.yml"
222 report_not_ready "$APPLICATION_READY" "$CONFIG_PATH/application.yml"