-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathsurf-deploy
executable file
·178 lines (159 loc) · 3.93 KB
/
surf-deploy
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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
#!/bin/bash
set -e
ENV="test"
LOGDIR="logs"
RED=$(tput setaf 1)
GREEN=$(tput setaf 2)
RESET=$(tput sgr0)
FAIL="${RED}FAIL${RESET}"
OK="${GREEN}OK${RESET}"
# https://github.com/jmespath/jmespath.py/pull/188
export PYTHONWARNINGS=ignore
mkdir -p $LOGDIR || true
WARNING=0
STRICT=1
ARG=${1#--}
if [ "$ARG" = "bhr" -o "$ARG" = "beheer" ]
then
ENV=bhr
CONFDIR=./environments/aws_bhr
EXTRA="${EXTRA} --ask-become --ask-vault-pass"
STRICT=0
shift
elif [ "$ARG" = "prd" -o "$ARG" = "prod" ]
then
ENV=prd
CONFDIR=./environments/aws_prd/
EXTRA="${EXTRA} --ask-become --ask-vault-pass"
echo -n 'Are you sure you want to deploy to production? Type "prod" to confirm: '
read -r confirm
if [ "$confirm" != "prod" ]
then
echo "Ok, exiting..."
exit 1
fi
shift
elif [ "$ARG" = "acc" ]
then
ENV=acc
CONFDIR=./environments/aws_acc
EXTRA="${EXTRA} --ask-become --ask-vault-pass"
shift
elif [ "$ARG" = "tst" -o "$ARG" = "test" ]
then
ENV=tst
CONFDIR=./environments/aws_tst
EXTRA="${EXTRA} --ask-become"
STRICT=0
shift
elif [ "$ARG" = "tst2" -o "$ARG" = "test2" ]
then
ENV=test2
CONFDIR=./environments/surf_test2
EXTRA="${EXTRA} "
STRICT=0
shift
else
echo "Please specify an environment to deploy (bhr, prod, acc, test)"
exit 1
fi
LOG=$LOGDIR/deploy-${ENV}.$(date '+%Y%m%d_%H%M').log
date >> $LOG
echo "$0 $*" >> $LOG
# TODO: all output in log files
# first check that deploy and config repos are clean and are using the same version
# deploy
echo -n "Checking if SRAM-deploy is clean... "
if ! git diff-index --quiet HEAD --
then
echo -e "$FAIL"
git status | sed 's/^/ > /'
echo
WARNING=1
else
echo -e "$OK"
fi
branch_deploy=$(git symbolic-ref HEAD 2> /dev/null || git describe --exact)
# config
cwd=$(pwd)
cd "$CONFDIR"
echo -en "Checking if Environment repo is clean... "
if ! git diff-index --quiet HEAD --
then
echo -e "$FAIL"
git status | sed "s/^/ > /"
echo
WARNING=1
else
echo -e "$OK"
fi
branch_config=$(git symbolic-ref HEAD 2> /dev/null || git describe --exact)
cd "$cwd"
echo -n "Checking whether deploy and config are in sync... "
if [ "$branch_deploy" != "$branch_config" ]
then
echo -e "$FAIL"
echo " > Deploy branch: ${RED}'$branch_deploy'${RESET}"
echo " > Config branch: ${RED}'$branch_config'${RESET}"
echo
WARNING=1
else
echo -e "$OK ($branch_deploy)"
fi
if [ "$WARNING" != "0" ]
then
if [ "$STRICT" != "0" ]
then
echo "Warnings encountered, refusing to deploy in strict mode"
exit 1
fi
echo
read -p "Warning encountered, are you sure you want to continue (yes/no)? " result
if [ "$result" != "yes" ]
then
echo "Exiting..."
exit 1
fi
fi
# try to find mitogen
MITOGEN_PATH=
MITOGEN_SUBPATH=ansible_mitogen/plugins/strategy
for p in ./mitogen /opt/mitogen /usr/local/mitogen /usr/mitogen ~/.mitogen ~/mitogen
do
if [ -d "$p/$MITOGEN_SUBPATH/" ]
then
MITOGEN_PATH=$p
break
fi
done
if [ -n "$MITOGEN_PATH" -a "$NO_MITOGEN" != "1" ]
then
echo "Found Mitogen at $MITOGEN_PATH"
export ANSIBLE_STRATEGY_PLUGINS=$MITOGEN_PATH/$MITOGEN_SUBPATH/
export ANSIBLE_STRATEGY=mitogen_linear
else
echo "Mitogen not found; falling back to default ansible ssh"
fi
CMD="ansible-playbook provision.yml \
-i "$CONFDIR/inventory" \
--limit=${ENV} \
--diff \
${EXTRA} \
""$@"
CMD=$( echo $CMD | sed 's/\s\+/ /g' )
echo "Deploying to SURfscz $ENV..."
echo "Log file: $LOG"
echo "Will execute: $CMD"
if command -v stdbuf &> /dev/null
then
export ANSIBLE_FORCE_COLOR=True
stdbuf --output=0 --error=0 $CMD 2>&1 | tee -a "$LOG"
else
echo "stdbuf command not found, not storing output in log file" >&2
if [ "$(uname)" = "Darwin" ]
then
echo "You can install stdbuf with 'brew install coreutils'" >&2
fi
$CMD
fi
exit 0