-
Notifications
You must be signed in to change notification settings - Fork 20
/
zebrunner.sh
executable file
·353 lines (292 loc) · 9.97 KB
/
zebrunner.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
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
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
#!/bin/bash
setup() {
source variables.env.original
# load current variables.env if exist to read actual vars even manually updated!
if [[ -f variables.env ]]; then
source variables.env
fi
if [[ $ZBR_INSTALLER -eq 1 ]]; then
# Zebrunner CE installer
url="$ZBR_PROTOCOL://$ZBR_HOSTNAME:$ZBR_PORT"
ZBR_MCLOUD_PORT=8082
else
# load default interactive installer settings
source backup/settings.env.original
# load ./backup/settings.env if exist to declare ZBR* vars from previous run!
if [[ -f backup/settings.env ]]; then
source backup/settings.env
fi
set_mcloud_settings
url="$ZBR_PROTOCOL://$ZBR_HOSTNAME:$ZBR_MCLOUD_PORT"
fi
cp .env.original .env
replace .env "STF_URL=http://localhost:8082" "STF_URL=${url}"
replace .env "STF_PORT=8082" "STF_PORT=$ZBR_MCLOUD_PORT"
if [[ $ZBR_INSTALLER -eq 1 ]]; then
replace .env "AUTH_SYSTEM=auth-mock" "AUTH_SYSTEM=auth-zebrunner"
replace .env "AUTH_URL=auth/mock/" "AUTH_URL=auth/zebrunner/"
fi
cp variables.env.original variables.env
replace variables.env "http://localhost:8082" "${url}"
replace variables.env "localhost" "${ZBR_HOSTNAME}"
replace variables.env "STF_TOKEN=" "STF_TOKEN=${STF_TOKEN}"
replace variables.env "SECRET=password" "SECRET=${SECRET}"
replace variables.env "AUTHKEY=" "AUTHKEY=${AUTHKEY}"
if [[ -z $ZBR_MCLOUD_ADMIN_NAME ]] && [[ ! -z $STF_ADMIN_NAME ]]; then
ZBR_MCLOUD_ADMIN_NAME=$STF_ADMIN_NAME
fi
if [[ -z $ZBR_MCLOUD_ADMIN_EMAIL ]] && [[ ! -z $STF_ADMIN_EMAIL ]]; then
ZBR_MCLOUD_ADMIN_EMAIL=$STF_ADMIN_EMAIL
fi
replace variables.env "STF_ROOT_GROUP_NAME=MCloud" "STF_ROOT_GROUP_NAME=${STF_ROOT_GROUP_NAME}"
replace variables.env "STF_ADMIN_NAME=admin" "STF_ADMIN_NAME=${ZBR_MCLOUD_ADMIN_NAME}"
replace variables.env "[email protected]" "STF_ADMIN_EMAIL=${ZBR_MCLOUD_ADMIN_EMAIL}"
if [[ $ZBR_INSTALLER -eq 1 ]]; then
#configure auth-zebrunner for mcloud
replace variables.env "ZEBRUNNER_LOGIN_URL=" "ZEBRUNNER_LOGIN_URL=${url}/api/iam/v1/auth/login"
replace variables.env "ZEBRUNNER_USERINFO_URL=" "ZEBRUNNER_USERINFO_URL=${url}/api/iam/v1/users"
fi
cp configuration/stf-proxy/nginx.conf.original configuration/stf-proxy/nginx.conf
replace configuration/stf-proxy/nginx.conf "server_name localhost" "server_name '$ZBR_HOSTNAME'"
# declare ssl protocol for NGiNX default config
if [[ "$ZBR_PROTOCOL" == "https" ]]; then
replace configuration/stf-proxy/nginx.conf "listen 80" "listen 80 ssl"
# uncomment default ssl settings
replace configuration/stf-proxy/nginx.conf "# ssl_" " ssl_"
#136 made ssl.crt and ssl.key not under source control
if [[ ! -f configuration/stf-proxy/ssl/ssl.crt ]]; then
echo "using self-signed certificate..."
cp configuration/stf-proxy/ssl/ssl.crt.original configuration/stf-proxy/ssl/ssl.crt
fi
if [[ ! -f configuration/stf-proxy/ssl/ssl.key ]]; then
echo "using self-signed key..."
cp configuration/stf-proxy/ssl/ssl.key.original configuration/stf-proxy/ssl/ssl.key
fi
fi
# export all ZBR* variables to save user input
export_settings
}
shutdown() {
if [[ -f .disabled ]]; then
rm -f .disabled
exit 0 #no need to proceed as nothing was configured
fi
if [[ ! -f .env ]]; then
echo_warning "Unable to erase as nothing is configured!"
exit 0 #no need to proceed as nothing was configured
fi
if [[ -z ${SHUTDOWN_CONFIRMED} ]] || [[ ${SHUTDOWN_CONFIRMED} -ne 1 ]]; then
# ask about confirmation if it is not confirmed in scope of CE
echo_warning "Shutdown will erase all settings and data for \"${BASEDIR}\"!"
confirm "" " Do you want to continue?" "n"
if [[ $? -eq 0 ]]; then
exit
fi
fi
docker compose down -v
rm -f backup/settings.env
rm -f .env
rm -f variables.env
rm -f configuration/stf-proxy/nginx.conf
rm -f configuration/stf-proxy/htpasswd/mcloud.htpasswd
if [[ -f configuration/stf-proxy/ssl/ssl.crt ]]; then
rm -f configuration/stf-proxy/ssl/ssl.crt
fi
if [[ -f configuration/stf-proxy/ssl/ssl.key ]]; then
rm -f configuration/stf-proxy/ssl/ssl.key
fi
}
start() {
if [[ -f .disabled ]]; then
exit 0
fi
# create infra network only if not exist
docker network inspect infra >/dev/null 2>&1 || docker network create infra
if [[ ! -f .env ]]; then
# need proceed with setup steps in advance!
setup
exit -1
fi
docker compose up -d
}
stop() {
if [[ -f .disabled ]]; then
exit 0
fi
docker compose stop
}
down() {
if [[ -f .disabled ]]; then
exit 0
fi
docker compose down
}
backup() {
if [[ -f .disabled ]]; then
exit 0
fi
cp variables.env variables.env.bak
cp .env .env.bak
cp backup/settings.env backup/settings.env.bak
cp configuration/stf-proxy/nginx.conf configuration/stf-proxy/nginx.conf.bak
if [[ -f configuration/stf-proxy/htpasswd/mcloud.htpasswd ]]; then
cp configuration/stf-proxy/htpasswd/mcloud.htpasswd configuration/stf-proxy/htpasswd/mcloud.htpasswd.bak
fi
if [[ -f configuration/stf-proxy/ssl/ssl.crt ]]; then
cp configuration/stf-proxy/ssl/ssl.crt configuration/stf-proxy/ssl/ssl.crt.bak
fi
if [[ -f configuration/stf-proxy/ssl/ssl.key ]]; then
cp configuration/stf-proxy/ssl/ssl.key configuration/stf-proxy/ssl/ssl.key.bak
fi
docker run --rm --volumes-from rethinkdb -v "$(pwd)"/backup:/var/backup "ubuntu" tar -czvf /var/backup/rethinkdb.tar.gz /data
}
restore() {
if [[ -f .disabled ]]; then
exit 0
fi
stop
cp variables.env.bak variables.env
cp .env.bak .env
cp backup/settings.env.bak backup/settings.env
cp configuration/stf-proxy/nginx.conf.bak configuration/stf-proxy/nginx.conf
if [[ -f configuration/stf-proxy/htpasswd/mcloud.htpasswd.bak ]]; then
cp configuration/stf-proxy/htpasswd/mcloud.htpasswd.bak configuration/stf-proxy/htpasswd/mcloud.htpasswd
fi
if [[ -f configuration/stf-proxy/ssl/ssl.crt.bak ]]; then
cp configuration/stf-proxy/ssl/ssl.crt.bak configuration/stf-proxy/ssl/ssl.crt
fi
if [[ -f configuration/stf-proxy/ssl/ssl.key.bak ]]; then
cp configuration/stf-proxy/ssl/ssl.key.bak configuration/stf-proxy/ssl/ssl.key
fi
docker run --rm --volumes-from rethinkdb -v "$(pwd)"/backup:/var/backup "ubuntu" bash -c "cd / && tar -xzvf /var/backup/rethinkdb.tar.gz"
down
}
version() {
if [[ -f .disabled ]]; then
exit 0
fi
source .env
echo "mcloud: ${TAG_STF}"
}
set_mcloud_settings() {
echo "Zebrunner MCloud Settings"
local is_confirmed=0
if [[ -z $ZBR_HOSTNAME ]]; then
ZBR_HOSTNAME=`curl -s ifconfig.me`
fi
while [[ $is_confirmed -eq 0 ]]; do
read -r -p "Protocol [$ZBR_PROTOCOL]: " local_protocol
if [[ ! -z $local_protocol ]]; then
ZBR_PROTOCOL=$local_protocol
fi
read -r -p "Fully qualified domain name (ip) [$ZBR_HOSTNAME]: " local_hostname
if [[ ! -z $local_hostname ]]; then
ZBR_HOSTNAME=$local_hostname
fi
read -r -p "Port [$ZBR_MCLOUD_PORT]: " local_port
if [[ ! -z $local_port ]]; then
ZBR_MCLOUD_PORT=$local_port
fi
confirm "Zebrunner MCloud STF URL: $ZBR_PROTOCOL://$ZBR_HOSTNAME:$ZBR_MCLOUD_PORT/stf" "Continue?" "y"
is_confirmed=$?
done
export ZBR_PROTOCOL=$ZBR_PROTOCOL
export ZBR_HOSTNAME=$ZBR_HOSTNAME
export ZBR_MCLOUD_PORT=$ZBR_MCLOUD_PORT
is_confirmed=0
while [[ $is_confirmed -eq 0 ]]; do
read -r -p "Admin username [$ZBR_MCLOUD_ADMIN_NAME]: " local_admin_name
if [[ ! -z $local_admin_name ]]; then
ZBR_MCLOUD_ADMIN_NAME=$local_admin_name
fi
read -r -p "Admin user email [$ZBR_MCLOUD_ADMIN_EMAIL]: " local_admin_mail
if [[ ! -z $local_admin_mail ]]; then
ZBR_MCLOUD_ADMIN_EMAIL=$local_admin_mail
fi
confirm "Zebrunner MCloud admin username: $ZBR_MCLOUD_ADMIN_NAME; email: $ZBR_MCLOUD_ADMIN_EMAIL" "Continue?" "y"
is_confirmed=$?
done
export ZBR_MCLOUD_ADMIN_NAME=$ZBR_MCLOUD_ADMIN_NAME
export ZBR_MCLOUD_ADMIN_EMAIL=$ZBR_MCLOUD_ADMIN_EMAIL
}
echo_warning() {
echo "
WARNING! $1"
}
echo_telegram() {
echo "
For more help join telegram channel: https://t.me/zebrunner
"
}
echo_help() {
echo "
Usage: ./zebrunner.sh [option]
Flags:
--help | -h Print help
Arguments:
start Start container
stop Stop and keep container
restart Restart container
down Stop and remove container
shutdown Stop and remove container, clear volumes
backup Backup container
restore Restore container
version Version of container"
echo_telegram
exit 0
}
replace() {
#TODO: https://github.com/zebrunner/zebrunner/issues/328 organize debug logging for setup/replace
file=$1
#echo "file: $file"
content=$(<"$file") # read the file's content into
#echo "content: $content"
old=$2
#echo "old: $old"
new=$3
#echo "new: $new"
content=${content//"$old"/$new}
#echo "content: $content"
printf '%s' "$content" >"$file" # write new content to disk
}
BASEDIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
cd "${BASEDIR}" || exit
# shellcheck disable=SC1091
source patch/utility.sh
case "$1" in
setup)
setup
;;
start)
start
;;
stop)
stop
;;
restart)
down
start
;;
down)
down
;;
shutdown)
shutdown
;;
backup)
backup
;;
restore)
restore
;;
version)
version
;;
--help | -h)
echo_help
;;
*)
echo_help
exit 1
;;
esac