diff options
Diffstat (limited to 'script')
-rwxr-xr-x | script/mysociety-switch-to-shared | 71 | ||||
-rwxr-xr-x | script/rails-deploy-before-down | 148 | ||||
-rwxr-xr-x | script/rails-deploy-while-down | 16 | ||||
-rwxr-xr-x | script/rails-post-deploy | 96 |
4 files changed, 241 insertions, 90 deletions
diff --git a/script/mysociety-switch-to-shared b/script/mysociety-switch-to-shared new file mode 100755 index 000000000..f82e77706 --- /dev/null +++ b/script/mysociety-switch-to-shared @@ -0,0 +1,71 @@ +#!/bin/bash + +# This script is a one-off script to move the shared files on a +# mySociety instance of Alaveteli out of the repository and into the +# vhost's shared directory. This is specific to mySociety's servers - +# the list of files and directories which are moved would be unlikely +# to be correct in another environment. +# +# This should be run *before* the first time the site is deployed with +# the timestamped_deploy option. + +set -e + +# (Using `pwd -P` here instead of readlink -f so that it works on Mac +# OS as well, just in case that's required for testing on a laptop.) +RAILS_ROOT="$(cd "$(dirname "$BASH_SOURCE")/.." && pwd -P)" +VHOST_DIR="$(cd "$RAILS_ROOT/.." && pwd -P)" +SHARED_DIR="$VHOST_DIR/shared" + +cd "$RAILS_ROOT" + +mkdir -p "$SHARED_DIR" + +for F in \ + cache \ + public/foi-live-creation.png \ + public/foi-user-use.png \ + config/aliases \ + lib/acts_as_xapian/xapiandbs \ + vendor/bundle +do + SYMLINK_LOCATION="$F" + INTENDED_DESTINATION="$SHARED_DIR/$F" + echo "Switching to $SYMLINK_LOCATION -> $INTENDED_DESTINATION" + # If anything exists where the symlink should be: + if [ -e "$SYMLINK_LOCATION" ] + then + # First, if it's a symlink, check whether it's correct: + if [ -L "$SYMLINK_LOCATION" ] + then + SYMLINK_DESTINATION="$(readlink "$SYMLINK_LOCATION")" + if [ "$SYMLINK_DESTINATION" = "$INTENDED_DESTINATION" ] + then + echo " already correct!" + else + echo " ERROR: already symlinked to $INTENDED_DESTINATION" + fi + else + # So the file or directory is there, and it's not a + # symlink. Check first that that destination doesn't + # exist (in which case a move would either fail or + # overwrite what's there): + if [ -e "$INTENDED_DESTINATION" ] + then + echo " ERROR: would move, but something already existed at $INTENDED_DESTINATION" + else + # Otherwise (bar race condition) everything's fine, + # and we should be able to move the file or directory + # and create a symlink to its new location: + mkdir -p "$(dirname "$INTENDED_DESTINATION")" + mv "$SYMLINK_LOCATION" "$INTENDED_DESTINATION" + ln -snf "$INTENDED_DESTINATION" "$SYMLINK_LOCATION" + fi + fi + else + # This may not be anything to worry about, e.g. if the + # public/foi-user-use.png graph has never been generated: + echo " ERROR: nothing existed at $SYMLINK_LOCATION" + fi + +done diff --git a/script/rails-deploy-before-down b/script/rails-deploy-before-down new file mode 100755 index 000000000..ad1049e44 --- /dev/null +++ b/script/rails-deploy-before-down @@ -0,0 +1,148 @@ +#!/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 + +# read config file in for later (STAGING_SITE) +if [ -e "config/general" ] || [ -e "config/general.yml" ] +then + . commonlib/shlib/deployfns + read_conf config/general +else + OPTION_DOMAIN=127.0.0.1:3000 + OPTION_STAGING_SITE=1 +fi + +# create initial log files +if [ -e $TOP_DIR/../logs ] +then + # mySociety servers have logs dir in level above + if ! [ -h log ] && [ -d log ] + then + # If log is a directory rather than a symlink, move that + # directory out of the way: + mv log log.original + fi + ln -sfn $TOP_DIR/../logs log +else + # otherwise just make the directory + if [ -h log ] + then + # remove any old-style symlink first + rm -f log + fi + mkdir -p log +fi + +cd log +touch development.log fastcgi.crash.log production.log test.log +cd .. + +# 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 +} + +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 + +# Force appropriate environment in production +if [ "$OPTION_STAGING_SITE" = "0" ] +then + 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 + echo "ENV['RAILS_ENV'] ||= 'production'" > config/rails_env.rb +fi + +bundle_install_options="" +if [ "$OPTION_STAGING_SITE" = "0" ] +then + bundle_install_options="--without development:test --deployment" +fi +if [ "$OPTION_STAGING_SITE" = "1" ] +then + bundle_install_options="--path vendor/bundle" +fi +if [ "$TRAVIS" = "true" ] +then + bundle_install_options="--without development develop --deployment" +fi +bundle install $bundle_install_options + +bundle exec rake submodules:check + +bundle exec rake themes:install + +if [ "$OPTION_STAGING_SITE" = "0" ] +then + bundle exec rake assets:precompile +fi diff --git a/script/rails-deploy-while-down b/script/rails-deploy-while-down new file mode 100755 index 000000000..a5dc772d3 --- /dev/null +++ b/script/rails-deploy-while-down @@ -0,0 +1,16 @@ +#!/bin/bash +# +# rails-deploy-while-down + +# For Ruby on Rails, run this in exec_while_down in vhosts.pl. It does +# any deploy actions that should be done while the site is down; +# typically this is the database migrations. +# +# Copyright (c) 2014 UK Citizens Online Democracy. All rights reserved. +# Email: hello@mysociety.org; WWW: http://www.mysociety.org/ + +set -e +#set -x # debug + +# upgrade database +bundle exec rake db:migrate #--trace diff --git a/script/rails-post-deploy b/script/rails-post-deploy index a88e28b19..f1c63965e 100755 --- a/script/rails-post-deploy +++ b/script/rails-post-deploy @@ -1,9 +1,10 @@ #!/bin/bash # # rails-post-deploy -# For Ruby on Rails, run this in exec_extras in vhosts.pl. It makes symlinks -# from vendor to the server version of rails, and migrates the db to the most -# recent version. +# For Ruby on Rails, this used to be run in exec_extras in vhosts.pl; +# however, now we use exec_before_down and exec_while_down instead, so +# this is here for compatability with alternative deploy scripts that +# still call rails-post-deploy. # # Copyright (c) 2007 UK Citizens Online Democracy. All rights reserved. # Email: hello@mysociety.org; WWW: http://www.mysociety.org/ @@ -14,90 +15,5 @@ set -e 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 - -# read config file in for later (STAGING_SITE) -if [ -e "config/general" ] || [ -e "config/general.yml" ] -then - . commonlib/shlib/deployfns - read_conf config/general -else - OPTION_DOMAIN=127.0.0.1:3000 - OPTION_STAGING_SITE=1 -fi - -# create initial log files -if [ -e $TOP_DIR/../logs ] -then - # mySociety servers have logs dir in level above - if ! [ -h log ] && [ -d log ] - then - # If log is a directory rather than a symlink, move that - # directory out of the way: - mv log log.original - fi - ln -sfn $TOP_DIR/../logs log -else - # otherwise just make the directory - if [ -h log ] - then - # remove any old-style symlink first - rm -f log - fi - mkdir -p log -fi - -cd log -touch development.log fastcgi.crash.log production.log test.log -cd .. - -# Force appropriate environment in production -if [ "$OPTION_STAGING_SITE" = "0" ] -then - 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 - echo "ENV['RAILS_ENV'] ||= 'production'" > config/rails_env.rb -fi - -bundle_install_options="" -if [ "$OPTION_STAGING_SITE" = "0" ] -then - bundle_install_options="--without development:test --deployment" -fi -if [ "$OPTION_STAGING_SITE" = "1" ] -then - bundle_install_options="--path vendor/bundle" -fi -if [ "$TRAVIS" = "true" ] -then - bundle_install_options="--without development develop --deployment" -fi -bundle install $bundle_install_options - -bundle exec rake submodules:check - -# upgrade database -bundle exec rake db:migrate #--trace - -bundle exec rake themes:install - -if [ "$OPTION_STAGING_SITE" = "0" ] -then - bundle exec rake assets:precompile -fi +"$TOP_DIR/script/rails-deploy-before-down" +"$TOP_DIR/script/rails-deploy-while-down" |