21906: Improve WWW_OWNER handling on Red Hat systems
[arvados.git] / build / rails-package-scripts / postinst.sh
index 17802a7b52b54f8e2614afb2059ebd2dbeef9855..8ac77ab001182242ce47f7c8fdf1bb9e0abedf74 100644 (file)
@@ -10,12 +10,6 @@ set -e
 DATABASE_READY=1
 APPLICATION_READY=1
 
-if [ -s "$HOME/.rvm/scripts/rvm" ] || [ -s "/usr/local/rvm/scripts/rvm" ]; then
-    COMMAND_PREFIX="/usr/local/rvm/bin/rvm-exec default"
-else
-    COMMAND_PREFIX=
-fi
-
 report_not_ready() {
     local ready_flag="$1"; shift
     local config_file="$1"; shift
@@ -125,17 +119,17 @@ setup_conffile() {
 }
 
 prepare_database() {
-  DB_MIGRATE_STATUS=`$COMMAND_PREFIX bin/rake db:migrate:status 2>&1 || true`
+  DB_MIGRATE_STATUS=`bin/rake db:migrate:status 2>&1 || true`
   if echo "$DB_MIGRATE_STATUS" | grep -qF 'Schema migrations table does not exist yet.'; then
       # The database exists, but the migrations table doesn't.
-      run_and_report "Setting up database" $COMMAND_PREFIX bin/rake \
+      run_and_report "Setting up database" bin/rake \
                      "$RAILSPKG_DATABASE_LOAD_TASK" db:seed
   elif echo "$DB_MIGRATE_STATUS" | grep -q '^database: '; then
       run_and_report "Running db:migrate" \
-                     $COMMAND_PREFIX bin/rake db:migrate
+                     bin/rake db:migrate
   elif echo "$DB_MIGRATE_STATUS" | grep -q 'database .* does not exist'; then
       if ! run_and_report "Running db:setup" \
-           $COMMAND_PREFIX bin/rake db:setup 2>/dev/null; then
+           bin/rake db:setup 2>/dev/null; then
           echo "Warning: unable to set up database." >&2
           DATABASE_READY=0
       fi
@@ -164,31 +158,17 @@ configure_version() {
   fi
 
   if [ -e /etc/redhat-release ]; then
-      # Recognize any service that starts with "nginx"; e.g., nginx16.
-      if [ "$WEB_SERVICE" != "${WEB_SERVICE#nginx}" ]; then
-        WWW_OWNER=nginx
-      else
-        WWW_OWNER=apache
-      fi
+      case "$WEB_SERVICE" in
+          "") ;;
+          nginx*) WWW_OWNER=nginx ;;
+          *) WWW_OWNER=apache ;;
+      esac
   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
   fi
 
-  echo
-  echo "Assumption: $WEB_SERVICE is configured to serve Rails from"
-  echo "            $RELEASE_PATH"
-  echo "Assumption: $WEB_SERVICE and passenger run as $WWW_OWNER"
-  echo
-
-  echo -n "Creating symlinks to configuration in $CONFIG_PATH ..."
-  setup_confdirs /etc/arvados "$CONFIG_PATH"
-  setup_conffile environments/production.rb environments/production.rb.example \
-      || true
-  setup_extra_conffiles
-  echo "... done."
-
   # Before we do anything else, make sure some directories and files are in place
   if [ ! -e $SHARED_PATH/log ]; then mkdir -p $SHARED_PATH/log; fi
   if [ ! -e $RELEASE_PATH/tmp ]; then mkdir -p $RELEASE_PATH/tmp; fi
@@ -198,47 +178,68 @@ configure_version() {
   cd "$RELEASE_PATH"
   export RAILS_ENV=production
 
-  if ! $COMMAND_PREFIX bundle --version >/dev/null 2>&1; then
-      run_and_report "Installing bundler" $COMMAND_PREFIX gem install bundler --version 2.2.19 --no-document
+  run_and_report "Installing bundler" gem install --conservative --version '~> 2.4.0' bundler
+  local ruby_minor_ver="$(ruby -e 'puts RUBY_VERSION.split(".")[..1].join(".")')"
+  local bundle="$(gem contents --version '~> 2.4.0' bundler | grep -E '/(bin|exe)/bundle$' | tail -n1)"
+  if ! [ -x "$bundle" ]; then
+      # Some distros (at least Ubuntu 24.04) append the Ruby version to the
+      # executable name, but that isn't reflected in the output of
+      # `gem contents`. Check for that version.
+      bundle="$bundle$ruby_minor_ver"
+      if ! [ -x "$bundle" ]; then
+          echo "Error: failed to find \`bundle\` command after installing bundler gem" >&2
+          return 1
+      fi
   fi
 
+  local bundle_path="$SHARED_PATH/vendor_bundle"
   run_and_report "Running bundle config set --local path $SHARED_PATH/vendor_bundle" \
-      $COMMAND_PREFIX bin/bundle config set --local path $SHARED_PATH/vendor_bundle
-
-  run_and_report "Running bundle install" \
-      $COMMAND_PREFIX bin/bundle install --local --quiet
-
-  # As of April 2024/Bundler 2.4, for some reason `bundle install` skips
-  # zlib if it's already installed as a system-wide gem, which it often will
-  # be because arvados gems pull it in. If this happened, install it in the
-  # bundle manually as a workaround.
-  if ! $COMMAND_PREFIX bin/bundle info zlib >/dev/null 2>&1; then
-      local RUBY_VERSION="$($COMMAND_PREFIX ruby -e 'puts RUBY_VERSION')"
-      run_and_report "Adding zlib to bundle" \
-                     $COMMAND_PREFIX gem install \
-                     --install-dir="$SHARED_PATH/vendor_bundle/ruby/$RUBY_VERSION" \
-                     vendor/cache/zlib-*.gem
-  fi
+                 "$bundle" config set --local path "$bundle_path"
+
+  # As of April 2024/Bundler 2.4, `bundle install` tends not to install gems
+  # which are already installed system-wide, which causes bundle activation to
+  # fail later. Work around this by installing all gems manually.
+  find vendor/cache -maxdepth 1 -name '*.gem' -print0 \
+      | run_and_report "Installing bundle gems" xargs -0r \
+                       gem install --conservative --ignore-dependencies --local --quiet \
+                       --install-dir="$bundle_path/ruby/$ruby_minor_ver.0"
+  run_and_report "Running bundle install" "$bundle" install --prefer-local --quiet
+  run_and_report "Verifying bundle is complete" "$bundle" exec true
 
-  echo -n "Ensuring directory and file permissions ..."
-  # Ensure correct ownership of a few files
-  chown "$WWW_OWNER:" $RELEASE_PATH/config/environment.rb
-  chown "$WWW_OWNER:" $RELEASE_PATH/config.ru
-  chown "$WWW_OWNER:" $RELEASE_PATH/Gemfile.lock
-  chown -R "$WWW_OWNER:" $RELEASE_PATH/tmp || true
-  chown -R "$WWW_OWNER:" $SHARED_PATH/log
-  # Make sure postgres doesn't try to use a pager.
-  export PAGER=
-  case "$RAILSPKG_DATABASE_LOAD_TASK" in
-      # db:structure:load was deprecated in Rails 6.1 and shouldn't be used.
-      db:schema:load | db:structure:load)
-          chown "$WWW_OWNER:" $RELEASE_PATH/db/schema.rb || true
-          chown "$WWW_OWNER:" $RELEASE_PATH/db/structure.sql || true
-          ;;
-  esac
-  chmod 644 $SHARED_PATH/log/*
-  chmod -R 2775 $RELEASE_PATH/tmp || true
-  echo "... done."
+  if [ -n "$WWW_OWNER" ]; then
+    cat <<EOF
+
+Assumption: $WEB_SERVICE is configured to serve Rails from
+            $RELEASE_PATH
+Assumption: $WEB_SERVICE and passenger run as $WWW_OWNER
+
+EOF
+
+    echo -n "Creating symlinks to configuration in $CONFIG_PATH ..."
+    setup_confdirs /etc/arvados "$CONFIG_PATH"
+    setup_conffile environments/production.rb environments/production.rb.example \
+        || true
+    setup_extra_conffiles
+    echo "... done."
+
+    echo -n "Ensuring directory and file permissions ..."
+    # Ensure correct ownership of a few files
+    chown "$WWW_OWNER:" $RELEASE_PATH/config/environment.rb
+    chown "$WWW_OWNER:" $RELEASE_PATH/config.ru
+    chown "$WWW_OWNER:" $RELEASE_PATH/Gemfile.lock
+    chown -R "$WWW_OWNER:" $SHARED_PATH/log
+    # Make sure postgres doesn't try to use a pager.
+    export PAGER=
+    case "$RAILSPKG_DATABASE_LOAD_TASK" in
+        # db:structure:load was deprecated in Rails 6.1 and shouldn't be used.
+        db:schema:load | db:structure:load)
+            chown "$WWW_OWNER:" $RELEASE_PATH/db/schema.rb || true
+            chown "$WWW_OWNER:" $RELEASE_PATH/db/structure.sql || true
+            ;;
+    esac
+    chmod 644 $SHARED_PATH/log/*
+    echo "... done."
+  fi
 
   if [ -n "$RAILSPKG_DATABASE_LOAD_TASK" ]; then
       prepare_database
@@ -248,14 +249,15 @@ configure_version() {
       # warn about config errors (deprecated/removed keys from
       # previous version, etc)
       run_and_report "Checking configuration for completeness" \
-                     $COMMAND_PREFIX bin/rake config:check || APPLICATION_READY=0
+                     bin/rake config:check || APPLICATION_READY=0
   else
       APPLICATION_READY=0
   fi
 
-  chown -R "$WWW_OWNER:" $RELEASE_PATH/tmp
-
-  setup_before_nginx_restart
+  if [ -n "$WWW_OWNER" ]; then
+    chown -R "$WWW_OWNER:" $RELEASE_PATH/tmp
+    chmod -R 2775 $RELEASE_PATH/tmp
+  fi
 
   if [ -n "$SERVICE_MANAGER" ]; then
       service_command "$SERVICE_MANAGER" restart "$WEB_SERVICE"