aboutsummaryrefslogtreecommitdiffstats
path: root/script/rails-deploy-before-down
blob: c44bfe552df717a36d420dd09c1a7455412c7b30 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
#!/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

# 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_PATH="${OPTION_BUNDLE_PATH:-vendor/bundle}"

bundle_install_options="--path $BUNDLE_PATH"

if [ "$OPTION_STAGING_SITE" = "0" ]
then
    bundle_install_options="$bundle_install_options --without development debug test --deployment"
fi
if [ "$OPTION_STAGING_SITE" = "1" ]
then
    bundle_install_options="$bundle_install_options"
fi

# Use script/wad to install bundle when on Travis
if [ "$TRAVIS" = "true" ]
then
    script/wad
fi

if [ ! "$TRAVIS" = "true" ]
then
    echo "Running bundle install with options: $bundle_install_options"
    bundle install $bundle_install_options
fi

bundle exec rake submodules:check

bundle exec rake themes:install

if [ "$OPTION_STAGING_SITE" = "0" ]
then
    bundle exec rake assets:precompile
fi