-
Notifications
You must be signed in to change notification settings - Fork 0
/
upgrade-app.sh
executable file
·127 lines (113 loc) · 3.82 KB
/
upgrade-app.sh
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
#!/bin/bash
# upgrade-app.sh
# This script is part of dora -- Docker container for Rails
# https://github.com/bovender/dora
# If a command-line argument is given, capture all output.
# This will also cause the script to invoke DoraWebUpgrader.Reporter.run
# before it finishes.
if [ ! -z "$1" ]; then
LOGFILE=$1
# https://unix.stackexchange.com/q/61931/110635
exec > >(tee "$LOGFILE") 2>&1
fi
source bootstrap-script.sh
echo "# dora app upgrade script"
# Change to the Rails directory and make sure we return to where we were
# when the script exits.
pushd $RAILS_DIR
trap popd EXIT
LOCK_PRIMARY=/home/$DORA_USER/upgrade-lock.primary
LOCK_SECONDARY=/home/$DORA_USER/upgrade-lock.secondary
WAIT_SECONDS=300
WAIT_INTERVAL=30
E_UPGRADE_LOCKED=1
MESSAGE=undefined
if [[ -z $RAILS_ENV ]]; then
echo "WARNING: \$RAILS_ENV is empty! This is probably not what you want."
echo "WARNING: \$PASSENGER_APP_ENV=$PASSENGER_APP_ENV"
fi
# Check if an upgrade is currently in progress.
# When pushing and pushing --tags to the main branch, the web hook will be
# executed twice in short succession. We use two lock files to deal with this
# situation. If there is one lock file present, the script waits for a while
# before upgrading in order to be able to pull the latest tags. If there are
# two lock files, we bail out immediately.
function check_lock {
if [[ ! -f $LOCK_PRIMARY ]]; then
MY_LOCK=$LOCK_PRIMARY
touch $LOCK_PRIMARY || echo "WARNING: Unable to create primary lock file!"
elif [[ -f $LOCK_SECONDARY ]]; then
echo "FATAL: Two lock files present -- not attempting to upgrade the app!"
echo "FATAL: Lock file 1: $LOCK_PRIMARY"
echo "FATAL: Lock file 2: $LOCK_SECONDARY"
echo "FATAL: If this is an error, remove the lock files manually."
exit 0
else
# Primary lock present, but secondary lock not: set secondary lock and wait
MY_LOCK=$LOCK_SECONDARY
touch $LOCK_SECONDARY || echo "WARNING: Unable to create secondary lock file!"
echo "INFO: Another upgrade is in progress, waiting at most $WAIT_SECONDS seconds..."
echo -n "INFO: "
WAITING=0
while [[ -f $LOCK_PRIMARY ]] && (( $WAITING < $WAIT_SECONDS )); do
(( WAITING = WAITING + WAIT_INTERVAL ))
echo -n "$WAITING... "
sleep $WAIT_INTERVAL
done
echo
rm $LOCK_SECONDARY 2>/dev/null || echo "WARNING: Unable to remove secondary lock file!"
# Quit if the primary lock is still present after waiting politely
if [[ -f $LOCK_PRIMARY ]]; then
echo "FATAL: Upgrade still locked after waiting $WAITING seconds, exiting!"
echo "FATAL: If you think this is an error, remove the lock file manually:"
echo "FATAL: rm $LOCK_PRIMARY"
exit $E_UPGRADE_LOCKED
fi
fi
}
function release_lock {
if [[ -f $MY_LOCK ]]; then
rm $MY_LOCK || echo "WARNING: Unable to remove my lock file!"
fi
}
function pull {
# GIT_PULL is a global configuration flag of dora
if [ "$GIT_PULL" != "false" ]; then
git pull
fi
}
function upgrade {
bundle install &&\
yarn install --check-files &&\
bundle exec rails db:migrate &&\
bundle exec rails assets:precompile &&\
git describe --always > tmp/version &&\
passenger-config restart-app $RAILS_DIR &&\
set +x &&\
MESSAGE=succeeded
echo -e "\n\n***** UPGRADE SUCCEEDED! :-) *****\n"
}
function rollback {
set +x
MESSAGE=failed
echo "***** UPGRADE FAILED! :-( *****"
echo "Rolling back to $PREVIOUS_VERSION"
set -x
git reset --hard $PREVIOUS_VERSION
}
function main {
check_lock
set -x
cd $RAILS_DIR
PREVIOUS_VERSION=$(git describe 2>/dev/null || git rev-parse HEAD)
sudo sv stop sidekiq
pull && (upgrade || rollback)
sudo sv start sidekiq
set +x
release_lock
if [ -z "$LOGFILE" ]; then
[ -z "$RAILS_ENV" ] && RUNNER_ENV="-e $RAILS_ENV"
bin/rails runner $RUNNER_ENV DoraWebUpgrader.Reporter.run "$MESSAGE" "$LOGFILE"
fi
}
main