-
Notifications
You must be signed in to change notification settings - Fork 2
/
bc
executable file
·264 lines (211 loc) · 6.66 KB
/
bc
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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
#!/bin/bash
if [ ${DEBUG:-0} -eq 1 ]; then
set -x
fi
# The shopt command MUST BE THE FIRST EXECUTED LINE IN THIS SCRIPT
# Otherwise the entire script will fail to parse!!!
shopt -s extglob
export LANG=C
nodeploy=0
compilets=''
clusters=''
remote=origin
branch=master
gitpwddir=/root/.quattor-secrets
gitqpwdfile=$gitpwddir'/passwd.py'
qpwdfile=cfg/sites/dict/site/secret/passwd.pan
lockfile=/var/lock/ugentbc
if [ "$USER" == 'root' ]; then
sudo=""
else
sudo="sudo"
fi
if [ ! -f $lockfile ]; then
$sudo touch $lockfile
fi
$sudo chown root.users $lockfile
$sudo chmod 770 $lockfile
exec 9>$lockfile
default=`hostname | awk -F. '$1 == "snorlax"{print "altaria shuppet cubone"; exit};
$2=="gastly"{print "raichu gligar delcatty golett phanpy swalot swablu banette gigalith"; exit};
$2=="ugent"{print "uxie vulpix"; exit};
{print $2; exit}
'`
error() {
echo "ERROR: $@"
exit 1
}
usage() {
cat <<EOF
Usage: bc [--nodeploy] [--cleanup] [--timestamp] [ <cluster> ] [ <remote> ] [ <branch> ]
With no arguments, it will deploy the default clusters to production.
The default clusters are $default.
The first argument, if present must be the cluster to deploy.
The next two arguments are a remote and a branch. The order doesn't
matter. "master" is only allowed if pulling from origin.
When --nodeploy is used, resulting profiles are not copied and nodes are not notified.
When --timestamp is used, the compilation timestamp is added to the quattorid
(creating unique profile for each compilation).
It can happen that bc fails and leaves the directory in an unclean state.
Then you can use --cleanup with correct <remote> and <branch> to restore the master.
EOF
exit 4
}
if ! flock -n 9; then
echo "Another instance of bc is already running. Leaving"
exit 1
fi
if [ $# -gt 4 ]; then
usage
fi
while [ $# -gt 0 ]; do
if [[ ("$1" == "help") || ("$1" == "-h") || ("$1" == "--help") ]]; then
usage
elif [ "$1" = '--nodeploy' ]; then
nodeploy=1
elif [ "$1" = '--timestamp' ]; then
compilets="-$(date +%s)"
elif [ "$1" = '--cleanup' ]; then
cleanup=1
elif [ -z "$clusters" -a -z "$cleanup" ]; then
clusters=$1
elif git remote|grep -Fxq $1 ; then
remote=$1
else
branch=$1
fi
shift
done
if [ -z "$clusters" ]; then
clusters=$default
fi
# Checks out a branch at remote
checkout_branch()
{
local r=$1
local b=$2
local f=$b
git fetch $r
if [ $? -gt 0 ]; then error "!!"; fi
if [ $b != 'master' ]; then
git branch --track "$r-$b" "$r/$b" ||
exit "Unable to create branch $r-$b"
f="$r-$b"
fi
git checkout $f
if [ $? -gt 0 ]; then error "!!"; fi
git reset --hard
if [ $? -gt 0 ]; then error "!!"; fi
git merge --ff $r/$b
if [ $? -gt 0 ]; then error "!!"; fi
}
cleanup_git() {
local r=$1
local b=$2
git reset --hard
if [ $? -gt 0 ]; then error "!!"; fi
git checkout master
if [ $? -gt 0 ]; then error "!!"; fi
if [ $b != 'master' ]; then
git branch -D "$r-$b"
else
git pull
fi
if [ $? -gt 0 ]; then error "!!"; fi
}
# Sets the deployment identifier based on the date of the current
# commit (and if --timestamp if used, the timestamp of the build).
set_quattorid() {
ts=`git log -1 --date=raw | awk '/Date:/{print $2}'`
if [ $? -gt 0 ]; then error "!!"; fi
sha=`git log -1 --date=raw | awk '/commit /{print substr($2,0,8)}'`
if [ $? -gt 0 ]; then error "!!"; fi
branch=`git name-rev HEAD|awk '{print $2}'`
if [ "$?" != "0" ]; then
echo "Something went wrong with git log."
exit 1
fi
if [ $branch -eq "master" ]; then
type="production"
else
type="test"
fi
branchmd="'author', '$USER', 'name', '$branch', 'type', '$type', 'id', '$sha$compilets', 'timestamp', $ts"
echo "dict('system', dict('quattorid', '$branch-$ts$compilets'), 'metadata', dict('template', dict('branch', dict($branchmd))))"
}
# Pulls the passwords and other secrets we ship via Quattor but don't
# wish to store in the global Git repository
set_secrets() {
$sudo ./utils/python/utils/genpasswd.py -c $gitqpwdfile -f $qpwdfile
if [ "$?" != "0" ]; then
echo "Failed to replace passwords in $qpwdfile."
exit 3
fi
$sudo chown $USER.users $qpwdfile
if [ "$?" != "0" ]; then
echo "Failed to change ownership $USER.$USER on passwords $qpwdfile."
exit 3
fi
}
files_for_cluster() {
local cl=$1
# all regex have to start with .* !!
if [ $cl = 'uxie' ]; then
find_regexargs='.*ugent.be.json.gz ! -regex .*vulpix.*'
else
if [ $cl = 'vulpix' ]; then
find_regexargs='.*vulpix.*ugent.be.json.gz'
else
if [ $cl = 'muk_quattor' ]; then
find_regexargs='.*muk.*json.gz'
else
find_regexargs=".*$cl.*json.gz"
fi
fi
fi
find build/xml -regex $find_regexargs -type f
}
# Compile and, if needed, deploy the specified clusters
compile_and_deploy() {
if [ `basename $0` == "bc" ]; then
local id=`set_quattorid`
for cluster in $clusters; do
echo "Processing cluster $cluster"
if [ $nodeploy -eq 0 ]; then
external/ant/bin/ant build.deploy.cluster \
-Dpan.root.element="$id" \
-Ddeploy.debug.task=true -Dcluster=$cluster
for fil in `files_for_cluster $cluster`; do
$sudo cp $fil /var/www/https/profiles
done
else
external/ant/bin/ant -Dcluster.name=$cluster -Dpan.root.element="$id"
fi
echo "Finished processing cluster $cluster: exitcode $?"
done
else
echo "Processing all clusters"
external/ant/bin/ant build2
echo "Finished processing all clusters: exitcode $?"
fi
# generate the profiles-info
prof_file=/var/www/https/profiles/profiles-info.xml
local_prof_file=build/xml/profiles-info.xml
# uniq -s 31 checks uniueness on hostnames, ignores the fixed length 31 characters incl the mtime
sudo bash -c "echo '<profiles>' > $prof_file.new; grep -h 'profile ' $local_prof_file $prof_file 2>/dev/null |sort | uniq -s 31 >> $prof_file.new;echo '</profiles>' >> $prof_file.new;mv -f $prof_file.new $prof_file"
}
if [ "$branch" = 'master' -a "$remote" != "origin" ]; then
usage
fi
if [ -z "$cleanup" ]; then
checkout_branch $remote $branch
set_secrets
$sudo ./updatetemplates
rc=$?
# Compile and deploy
compile_and_deploy
else
rc=0
fi
cleanup_git $remote $branch
exit $rc