#!/bin/sh # Copyright (C) The Arvados Authors. All rights reserved. # # SPDX-License-Identifier: AGPL-3.0 # This code runs after package variable definitions and step2.sh. set -e DATABASE_READY=1 APPLICATION_READY=1 report_not_ready() { local ready_flag="$1"; shift local config_file="$1"; shift if [ "1" != "$ready_flag" ]; then cat >&2 < for more details. When $(basename "$config_file") has been modified, reconfigure or reinstall this package. EOF fi } report_web_service_warning() { local warning="$1"; shift cat >&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" bin/rake \ "$RAILSPKG_DATABASE_LOAD_TASK" db:seed elif echo "$DB_MIGRATE_STATUS" | grep -q '^database: '; then run_and_report "Running 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" \ bin/rake db:setup 2>/dev/null; then echo "Warning: unable to set up database." >&2 DATABASE_READY=0 fi else echo "Warning: Database is not ready to set up. Skipping database setup." >&2 DATABASE_READY=0 fi } configure_version() { if [ -n "$WEB_SERVICE" ]; then SERVICE_MANAGER=$(guess_service_manager) elif WEB_SERVICE=$(list_services_systemd | grep -E '^(nginx|httpd)'); then SERVICE_MANAGER=systemd elif WEB_SERVICE=$(list_services_service \ | grep -Eo '\b(nginx|httpd)[^[:space:]]*'); then SERVICE_MANAGER=service fi if [ -z "$WEB_SERVICE" ]; then report_web_service_warning "Web service (Nginx or Apache) not found" elif [ "$WEB_SERVICE" != "$(echo "$WEB_SERVICE" | head -n 1)" ]; then WEB_SERVICE=$(echo "$WEB_SERVICE" | head -n 1) report_web_service_warning \ "Multiple web services found. Choosing the first one ($WEB_SERVICE)" 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 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 if [ ! -e $RELEASE_PATH/log ]; then ln -s $SHARED_PATH/log $RELEASE_PATH/log; fi if [ ! -e $SHARED_PATH/log/production.log ]; then touch $SHARED_PATH/log/production.log; fi cd "$RELEASE_PATH" export RAILS_ENV=production # We install Bundler itself in the same place where Bundler will install # bundled gems, for a few reasons: # 1. Bundler will probably want to do this anyway to run itself with the # specific version named in Gemfile.lock. # 2. This is nicer to the sysadmin since we avoid messing with global state. # 3. We can know exactly where the `bundle` command got installed. local bundle_path="$SHARED_PATH/vendor_bundle" export GEM_HOME="$bundle_path/ruby/$(ruby -e 'puts RUBY_VERSION')" run_and_report "Installing bundler" gem install --conservative --version '~> 2.4.0' bundler local bundle="$GEM_HOME/bin/bundle" run_and_report "Running bundle config set --local path $SHARED_PATH/vendor_bundle" \ "$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 # The earlier `bundle config` should have it looking for installed gems in # the right place. Unset GEM_HOME now to be sure. unset GEM_HOME 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 "$RAILSPKG_DATABASE_LOAD_TASK" ]; then prepare_database fi if [ -e /etc/arvados/config.yml ]; then # warn about config errors (deprecated/removed keys from # previous version, etc) run_and_report "Checking configuration for completeness" \ 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 "$SERVICE_MANAGER" ]; then service_command "$SERVICE_MANAGER" restart "$WEB_SERVICE" fi } if [ "$1" = configure ]; then # This is a debian-based system configure_version elif [ "$1" = "0" ] || [ "$1" = "1" ] || [ "$1" = "2" ]; then # This is an rpm-based system configure_version fi report_not_ready "$APPLICATION_READY" "/etc/arvados/config.yml"