mysociety/alaveteli

View on GitHub
script/rails-deploy-before-down

Summary

Maintainability
Test Coverage
#!/bin/bash
#
# rails-post-deploy
# For Ruby on Rails, run this in exec_before_down in vhosts.pl.
# This does all the tasks for a new deploying a new version that can
# be done in a new directory before taking the site down; for example,
# this is appropriate for long-running tasks like asset precompilation.
#
# Copyright (c) 2014 UK Citizens Online Democracy. All rights reserved.
# Email: hello@mysociety.org; WWW: http://www.mysociety.org/

set -e
#set -x # debug

TOP_DIR="$(dirname "$BASH_SOURCE")/.."
cd "$TOP_DIR"

# make sure that there is an app directory, so are in a rails app tree
if ! [ -d app ]
then
    echo "Error: the 'app' directory didn't exist"
    exit 1
fi

# Returns 0 if an element is present in a bash array, and 1 otherwise
# Taken from: http://stackoverflow.com/a/8574392/223092
contains () {
    local E
    for E in "${@:2}"
    do
        [ "$E" == "$1" ] && return 0
    done
    return 1
}

OPTION_SHARED_FILES_PATH=$(bin/config SHARED_FILES_PATH)
OPTION_SHARED_FILES=($(bin/config SHARED_FILES))
OPTION_SHARED_DIRECTORIES=($(bin/config SHARED_DIRECTORIES))

if [ x"$OPTION_SHARED_FILES_PATH" != x ]
then
    for F in "${OPTION_SHARED_FILES[@]}" "${OPTION_SHARED_DIRECTORIES[@]}"
    do
        # Remove any trailing slash from directories:
        NORMALIZED_F="${F%/}"
        RELATIVE_DIRECTORY="$(dirname $NORMALIZED_F)"
        DESTINATION="$OPTION_SHARED_FILES_PATH/$NORMALIZED_F"
        # Ensure that the directory in the shared path exists:
        mkdir -p "$OPTION_SHARED_FILES_PATH/$RELATIVE_DIRECTORY"
        # If it's a directory, and it doesn't exist, make sure that
        # it's created:
        if contains "$F" "${OPTION_SHARED_DIRECTORIES[@]}"
        then
            if [ ! -d "$DESTINATION" ]
            then
                mkdir -p "$DESTINATION"
            fi
        fi
        # Make sure we won't overwrite a file because it hasn't been
        # moved to the shared directory yet:
        if [ -e "$NORMALIZED_F" ] && [ ! -L "$NORMALIZED_F" ]
        then
            cat <<EOF
$F is a real file or directory, but is listed in SHARED_FILES or
SHARED_DIRECTORIES and SHARED_FILES_PATH is set.  $F should have
been moved to $OPTION_SHARED_FILES_PATH/$F - for mySociety
deployments you can use script/mysociety-switch-to-shared to automate
this.
EOF
            exit 1
        fi
        ln -snf "$DESTINATION" "$NORMALIZED_F"
    done
fi

OPTION_STAGING_SITE=$(bin/config STAGING_SITE)
STAGING_SITE="${OPTION_STAGING_SITE:-1}"

# Force appropriate environment in production
if [ ! -f config/rails_env.rb ]; then
    [ "$STAGING_SITE" = "0" ] && cat <<-END

    *****************************************************************
    WARNING: About to make config/rails_env.rb which, via special
    code in config/boot.rb, forces the Rails environment to be
    "production". If this is a development system, please edit your
    config/general.yml file and set the STAGING_SITE option to 1,
    and also delete the generated config/rails_env.rb file.
    Alternatively, you can override config/rails_env.rb at any time
    with an environment variable.
    *****************************************************************

END

else
    RAILS_ENV=$(ruby -r ./config/rails_env.rb -e "puts ENV['RAILS_ENV']")
    if ([ "$STAGING_SITE" = "0" ] && [ "$RAILS_ENV" != "production" ]) ||
      ([ "$STAGING_SITE" = "1" ] && [ "$RAILS_ENV" = "production" ]); then
        # STAGING_SITE and RAILS_ENV mismatch - remove line
        sed -i "/^ENV\['RAILS_ENV'\]/d" config/rails_env.rb
    fi
fi

if [ "$STAGING_SITE" = "0" ] && [ "$RAILS_ENV" != "production" ]; then
    # Set RAILS_ENV to production
    echo "ENV['RAILS_ENV'] ||= 'production'" >> config/rails_env.rb
fi

OPTION_BUNDLE_PATH=$(bin/config BUNDLE_PATH)
BUNDLE_PATH="${OPTION_BUNDLE_PATH:-vendor/bundle}"
bundle config set --local path $BUNDLE_PATH

if [ "$STAGING_SITE" = "0" ]
then
    bundle config set --local deployment 'true'
    bundle config set --local without 'development debug test'
else
    bundle config unset --local deployment
    bundle config unset --local without
fi

echo "Bundle install options:"
bundle config list

echo "Running bundle install:"
bundle install

bundle exec rake submodules:check

bundle exec rake themes:install

bundle exec rake geoip:download_data

if [ "$STAGING_SITE" = "0" ]
then
    bundle exec rake assets:precompile &&
      bundle exec rake assets:link_non_digest
fi