More fixes for the SSO server packages.
[arvados-dev.git] / jenkins / arvados-sso-server-extras / arvados-sso-server.postinst
index e6db44cd2188946351c1c7f71380ac1b14a0d3d9..436f5bdaf617b6408c4d8ee234b5bce096ce59fd 100755 (executable)
@@ -6,6 +6,10 @@ INSTALL_PATH=/var/www/arvados-sso
 RELEASE_PATH=$INSTALL_PATH/current
 SHARED_PATH=$INSTALL_PATH/shared
 CONFIG_PATH=/etc/arvados/sso
+PACKAGE_NAME=arvados-sso-server
+
+DATABASE_READY=1
+APPLICATION_READY=1
 
 VERSION=`cat $RELEASE_PATH/git-commit.version`
 
@@ -31,56 +35,86 @@ configure_version() {
 
   [ "$VERSION" ] || { echo "Error: configure_version: need version parameter" >&2; exit 1; }
 
-  if [ -e /etc/redhat-release ]; then
-      WWW_OWNER=nginx:nginx
-  else
-      # Assume we're on a Debian-based system for now.
-      WWW_OWNER=www-data:www-data
-  fi
+  WEB_SERVICE=${WEB_SERVICE:-$(service --status-all 2>/dev/null \
+      | grep -Eo '\bnginx|httpd[^[:space:]]*' || true)}
+  if [ -z "$WEB_SERVICE" ]; then
+    cat >&2 <<EOF
 
-  NGINX_SERVICE=${NGINX_SERVICE:-$(service --status-all 2>/dev/null \
-      | grep -Eo '\bnginx[^[:space:]]*' || true)}
-  if [ -z "$NGINX_SERVICE" ]; then
-      cat >&2 <<EOF
-Error: nginx service not found. Aborting.
+Warning: web service (Nginx or Apache) not found.
 
-Set NGINX_SERVICE to the name of the service hosting the Rails server
-and reconfigure this package with dpkg-reconfigure.
+To override, set the WEB_SERVICE environment variable to the name of the service
+hosting the Rails server. Alternativey, install nginx.
+
+For Debian-based systems, then reconfigure this package with dpkg-reconfigure.
+
+For RPM-based systems, then reinstall this package.
 
 EOF
-      exit 0
-  elif [ "$NGINX_SERVICE" != "$(echo "$NGINX_SERVICE" | head -n 1)" ]; then
-      cat >&2 <<EOF
-Error: multiple nginx services found. Aborting.
+  elif [ "$WEB_SERVICE" != "$(echo "$WEB_SERVICE" | head -n 1)" ]; then
+    WEB_SERVICE=$(echo "$WEB_SERVICE" | head -n 1)
+    cat >&2 <<EOF
+Warning: multiple web services found. Choosing the first one ($WEB_SERVICE).
+
+To override, set the WEB_SERVICE environment variable to the name of the service
+hosting the Rails server.
 
-Set NGINX_SERVICE to the name of the service hosting the Rails server
-and reconfigure this package with dpkg-reconfigure.
+For Debian-based systems, then reconfigure this package with dpkg-reconfigure.
+
+For RPM-based systems, then reinstall this package.
 
 EOF
-      exit 0
+  fi
+
+  if [ -e /etc/redhat-release ]; then
+      if [ "$WEB_SERVICE" = "nginx" ]; then
+        WWW_OWNER=nginx:nginx
+      else
+        WWW_OWNER=apache:apache
+      fi
+  else
+      # Assume we're on a Debian-based system for now.
+      # Both Apache and Nginx run as www-data by default.
+      WWW_OWNER=www-data:www-data
   fi
 
   echo
-  echo "Assumption: $NGINX_SERVICE is configured to serve your SSO server URL from"
-  echo "            /var/www/arvados-sso/current"
-  echo "Assumption: configuration files are in /etc/arvados/sso/"
-  echo "Assumption: $NGINX_SERVICE and passenger run as $WWW_OWNER"
+  echo "Assumption: $WEB_SERVICE is configured to serve your SSO server URL from"
+  echo "            $RELEASE_PATH"
+  echo "Assumption: configuration files are in $CONFIG_PATH"
+  echo "Assumption: $WEB_SERVICE and passenger run as $WWW_OWNER"
   echo
 
   echo -n "Symlinking files from $CONFIG_PATH ..."
 
+  if [ ! -f $CONFIG_PATH/database.yml ]; then
+    mkdir -p $CONFIG_PATH
+    cp $RELEASE_PATH/config/database.yml.example $CONFIG_PATH/database.yml
+    DATABASE_READY=0
+  fi
+
   if [ ! -h $RELEASE_PATH/config/database.yml ]; then
     ln -s $CONFIG_PATH/database.yml $RELEASE_PATH/config/database.yml
   fi
+
+  if [ ! -f $CONFIG_PATH/production.rb ]; then
+    mkdir -p $CONFIG_PATH
+    cp $RELEASE_PATH/config/environments/production.rb.example $CONFIG_PATH/production.rb
+  fi
+
   if [ ! -h $RELEASE_PATH/config/environments/production.rb ]; then
-    if [ ! -f $CONFIG_PATH/production.rb ]; then
-      cp $RELEASE_PATH/config/environments/production.rb.example $CONFIG_PATH/production.rb
-    fi
     ln -s $CONFIG_PATH/production.rb $RELEASE_PATH/config/environments/production.rb
   fi
+
+  if [ ! -f $CONFIG_PATH/application.yml ]; then
+    mkdir -p $CONFIG_PATH
+    cp $RELEASE_PATH/config/application.yml.example $CONFIG_PATH/application.yml
+    APPLICATION_READY=0
+  fi
+
   if [ ! -h $RELEASE_PATH/config/application.yml ]; then
     ln -s $CONFIG_PATH/application.yml $RELEASE_PATH/config/application.yml
   fi
+
   echo "... done."
 
   # Before we do anything else, make sure some directories and files are in place
@@ -108,35 +142,97 @@ EOF
   chmod 644 $SHARED_PATH/log/*
   echo "... done."
 
-  # If we use `grep -q`, rake will write a backtrace on EPIPE.
-  if $COMMAND_PREFIX bundle exec rake db:migrate:status | grep '^database: ' >/dev/null; then
-      echo -n "Running db:migrate ..."
-      $COMMAND_PREFIX bundle exec rake db:migrate || exit $?
-  elif [ 0 -eq ${PIPESTATUS[0]} ]; then
-      # The database exists, but the migrations table doesn't.
-      echo -n "Setting up database ..."
-      $COMMAND_PREFIX bundle exec rake db:schema:load db:seed || exit $?
+  set +e
+  DB_MIGRATE_STATUS=`$COMMAND_PREFIX bundle exec rake db:migrate:status 2>&1`
+  DB_MIGRATE_STATUS_CODE=$?
+  set -e
+
+  if echo $DB_MIGRATE_STATUS | grep 'Schema migrations table does not exist yet.' >/dev/null; then
+    # The database exists, but the migrations table doesn't.
+    echo -n "Setting up database ..."
+    $COMMAND_PREFIX bundle exec rake db:schema:load db:seed || exit $?
+    echo "... done."
+  elif echo $DB_MIGRATE_STATUS | grep '^database: ' >/dev/null; then
+    echo -n "Running db:migrate ..."
+    $COMMAND_PREFIX bundle exec rake db:migrate || exit $?
+    echo "... done."
+  elif echo $DB_MIGRATE_STATUS | grep 'database .* does not exist' >/dev/null; then
+    echo -n "Running db:setup ..."
+    set +e
+    $COMMAND_PREFIX bundle exec rake db:setup 2>/dev/null
+    if [ "$?" = "0" ]; then
+      echo "... done."
+    else
+      echo "... failed."
+      echo "Warning: unable to set up database." >&2
+      DATABASE_READY=0
+    fi
+    set -e
   else
-      echo "Error: Database is not ready to set up. Aborting." >&2
-      exit 1
+    echo "Warning: Database is not ready to set up. Skipping database setup." >&2
+    DATABASE_READY=0
   fi
-  echo "... done."
 
   echo -n "Precompiling assets ..."
   # precompile assets; thankfully this does not take long
-  $COMMAND_PREFIX bundle exec rake assets:precompile -q -s || exit $?
+  if [ "$APPLICATION_READY" = "1" ]; then
+    set +e
+    $COMMAND_PREFIX bundle exec rake assets:precompile -q -s 2>/dev/null
+    if [ "$?" = "0" ]; then
+      echo "... done."
+    else
+      echo "... failed."
+      APPLICATION_READY=0
+    fi
+    set -e
+  else
+    echo "... skipped."
+  fi
   chown -R "$WWW_OWNER" $RELEASE_PATH/tmp
-  echo "... done."
 
-  echo -n "Restarting nginx ..."
-  service "$NGINX_SERVICE" restart || exit $?
-  echo "... done."
-  echo
+  if [ ! -z "$WEB_SERVICE" ]; then
+    echo -n "Restarting $WEB_SERVICE ..."
+    service "$WEB_SERVICE" restart >/dev/null || exit $?
+    echo "... done."
+  fi
 }
 
 if [ "$1" = configure ]; then
+  # This is a debian-based system
   setup_ruby_environment
   configure_version $VERSION "$2"
+elif [ "$1" = "0" ] || [ "$1" = "1" ] || [ "$1" = "2" ]; then
+  # This is an rpm-based system
+  setup_ruby_environment
+  configure_version $VERSION
 fi
 
+if [ "$DATABASE_READY" = "0" ]; then
+  cat <<EOF
+
+PLEASE NOTE:
+
+The $PACKAGE_NAME package was not configured completely because
+$CONFIG_PATH/database.yml needs some tweaking. Please refer to the
+documentation at http://doc.arvados.org/install/install-sso.html#configure for
+more details.
+
+When database.yml has been modified, reconfigure or reinstall this package.
+EOF
+fi
+
+if [ "$APPLICATION_READY" = "0" ]; then
+  cat <<EOF
+
+PLEASE NOTE:
+
+The $PACKAGE_NAME package was not configured completely because
+$CONFIG_PATH/application.yml needs some tweaking. Please refer to the
+documentation at http://doc.arvados.org/install/install-sso.html#configure for
+more details.
+
+When application.yml has been modified, reconfigure or reinstall this package.
+EOF
+fi
 
+echo