-
Notifications
You must be signed in to change notification settings - Fork 28
/
Copy pathsem-service
executable file
·570 lines (498 loc) · 18.1 KB
/
sem-service
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
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
#!/bin/bash
# SC2116: style suggestion that we don't care about.
# shellcheck disable=SC2116
# Misc
DATE_FORMAT='%H:%M %d/%m/%Y'
docker_status=0
state=('success' 'fail')
download_location='local'
registry_host='registry.semaphoreci.com'
[[ $(uname -m) =~ "aarch" ]] && registry_host="${registry_host}/arm"
declare -A sem_services
sem_services['memcached']="1.5 1.6"
sem_services['mysql']="5.5 5.6.51 5.6 5.6.50 5.7.13 5.7.25 5.7.27 5.7.31 5.7.32 5.7.33 5.7 5 8.0.16 8.0.22 8.0.23 8.0 8"
sem_services['redis']="2.8.23 2.8 2 3.2.4 3.2.12 3.2 3 4.0.12 4.0.14 4.0 4 5.0.6 5.0.9 5.0 5 6.0.5 6.0.8 6.0 6.2.1 6.2 6.2.7 6 7.0.5 7.0 7.2.4 7.2 7"
sem_services['mongo']="3.2.1 3.2 3.6.20 3.6 3 4.0.20 4.0 4.1.13 4.1 4.2.10 4.2.13 4.2 4.4.1 4.4.4 4.4 4 5.0.1 5.0.2 5.0.3 5.0.4 5.0.5 5.0.6 5.0.7 5.0.8 5.0.9 5.0 5 6.0.8 6.0 6"
sem_services['elasticsearch']="1.7.6 1.7 1 2.4.6 2.4 2 5.4.3 5.4 5.5.2 5.5 5.6.16 5.6 5 6.5.1 6.5.4 6.5 6.6 6.8.1 6.8.13 6.8 6 7.1.1 7.1 7.2.0 7.2 7.2.1 7.2 7.3.1 7.3.2 7.3 7.4.2 7.4 7.5.0 7.5.1 7.5.2 7.5 7.6.0 7.6.2 7.6 7.7.0 7.7.1 7.7 7.8.1 7.8 7.9.0 7.9.2 7.9.3 7.9 7.10.0 7.11.2 7.11 7.12.0 7.12 7.17.7 7 8.5.1 8.5.3 8.9.2 8.11.3"
sem_services['postgres']="9.4.26 9.4 9.5.15 9.5.23 9.5 9.6.6 9.6.11 9.6.18 9.6.19 9.6 9 10.0 10.5 10.6 10.7 10.11 10.12 10.13 10.14 10.16 10 11.0 11.2 11.5 11.6 11.7 11.8 11.9 11.11 11 12.1 12.2 12.3 12.4 12.6 12 13.0 13.2 13 14 15.1 15 16 17.2 17"
sem_services['rabbitmq']="3.6.16 3.6 3.8.2 3.8.9 3.8.14 3.8 3"
sem_services['cassandra']="3.11.3 3.11.8 3.11 3"
sem_services['rethinkdb']="2.3.6 2.3 2.4.1 2.4 2"
sem_services['postgis']="9.5-2.5 9.5-3.0 9.6-2.5 9.6-3.0 10-2.5 10-3.0 11-2.5 11-3.0 12-2.5 12-3.0 13-3.0 14-3.1 15-3.3 15-3.4 16-3.4"
sem_services['opensearch']="1.3.9 1 2.6.0 2.7.0 2"
service::pull_image_retry() {
counter=1
local image_name=$2
while ! docker pull "$image_name" && [ $counter -lt 3 ] ;do
counter=$((counter+1))
sleep 1
done
}
service::pull_image() {
local image_name=$1
if [[ -z $image_name ]]; then
service:err "Image name can't be empty"
fi
if ! echo "$image_name" | grep -E '^registry.semaphoreci.com/arm/[[:alnum:]]+(:[[:alnum:]]+.*)?$' &>/dev/null; then
if ! echo "$image_name" | grep -E '^registry.semaphoreci.com/[[:alnum:]]+(:[[:alnum:]]+.*)?$' &>/dev/null; then
if ! echo "$image_name" | grep -E '^[[:alnum:]]+(:[[:alnum:]]+.*)?$' &>/dev/null; then
service::err "Invalid image name provided '${image_name}'"
fi
fi
fi
service::duration service::run_cmd service::pull_image_retry pull "$image_name" download "$(echo "${image_name%%:*}")"
}
service::check_version(){
local version="$2"
local image_name="$1"
local have_it=1
local local_versions="${sem_services[$image_name]}"
# SC2068: we want the word splitting here, so no double quotes
# shellcheck disable=SC2068
for i in ${local_versions[@]}; do
if [ "$version" == "$i" ]; then
have_it=0
break
fi
done
########### Output message if we dont host the image
if [ $have_it -eq 1 ]; then
echo -e "\e[31m[error] sem-service doesn't support '$image_name' with version '$version'.\e[0m"
echo -e ""
echo -e "Available options for '$image_name' are:"
echo -e ""
local_versions="${sem_services[$image_name]}"
for v in "${local_versions[@]}"; do
echo -e " \e[34m- sem-version start ${image_name} ${v}\e[0m"
done
echo -e ""
echo -e "Check the full list of options:"
echo -e "--> \e[34mhttps://docs.semaphoreci.com/ci-cd-environment/semaphore-registry-images/\e[0m"
echo -e ""
echo -e "If the version you want is not supported, review alternative options:"
echo -e "--> \e[34mhttps://docs.semaphoreci.com/ci-cd-environment/semaphore-registry-images\e[0m"
echo -e ""
fi
}
service::start_mysql() {
service_version=$1
shift
local service_image="${registry_host}/mysql"
docker_params=( "$@" )
service::pull_image "$service_image:$service_version"
docker_output=$(docker run "${docker_params[@]}" "$service_image":"$service_version")
# run only if previous command exited successfully
docker_status=$(sem-dockerize 3306 mysql)
if [ "$docker_status" == "0" ]; then
echo "done."
echo "MySQL $service_version is running at 0.0.0.0:3306"
echo "To access it use username 'root' and blank password."
else
# if not started in 30 seconds print logs and exit
echo "$docker_output"
fi
}
service::start_postgres() {
service_version=$1
shift
local service_image="${registry_host}/postgres"
docker_params=( "$@" )
service::pull_image "$service_image:$service_version"
docker_output=$(docker run "${docker_params[@]}" "$service_image":"$service_version")
# run only if previous command exited successfully
docker_status=$(sem-dockerize 5432 postgres)
if [ "$docker_status" == "0" ]; then
echo "done."
echo "PostgreSQL $service_version is running at 0.0.0.0:5432"
echo "To access it use username 'postgres' and blank password."
else
# if not started in 30 seconds print logs and exit
echo "$docker_output"
fi
}
service::start_postgis() {
service_version=$1
shift
local service_image="${registry_host}/postgis"
docker_params=( "$@" )
service::pull_image "$service_image:$service_version"
docker_output=$(docker run "${docker_params[@]}" "$service_image":"$service_version")
# run only if previous command exited successfully
service::duration sem-dockerize 5432 boot postgis
docker_status=$(sem-dockerize 5432 postgis)
if [ "$docker_status" == "0" ]; then
echo "done."
echo "PostgreSQL with postgis $service_version is running at 0.0.0.0:5432"
echo "To access it use username 'postgres' and blank password."
else
# if not started in 30 seconds print logs and exit
echo "$docker_output"
fi
}
service::start_redis() {
service_version=$1
shift
local service_image="${registry_host}/redis"
docker_params=( "$@" )
service::pull_image "$service_image:$service_version"
docker_output=$(docker run "${docker_params[@]}" "$service_image":"$service_version" --appendonly yes)
# run only if previous command exited successfully
docker_status=$(sem-dockerize 6379 redis)
if [ "$docker_status" == "0" ]; then
echo "done."
echo "Redis $service_version is running at 0.0.0.0:6379"
else
# if not started in 30 seconds print logs and exit
echo "$docker_output"
fi
}
service::start_memcached() {
service_version="${1:-1.5}"
local service_image="${registry_host}/memcached"
service::pull_image "$service_image:$service_version"
docker_output=$(docker run --net=host --rm --name memcached -d "$service_image":"$service_version")
# run only if previous command exited successfully
docker_status=$(sem-dockerize 11211 memcached)
if [ "$docker_status" == "0" ]; then
echo "done."
echo "memcached $service_version is running at 0.0.0.0:11211"
else
# if not started in 30 seconds print logs and exit
echo "$docker_output"
fi
}
service::start_mongodb() {
service_version=$1
shift
local service_image="${registry_host}/mongo"
docker_params=( "$@" )
service::pull_image "$service_image:$service_version"
docker_output=$(docker run "${docker_params[@]}" "$service_image":"$service_version")
# run only if previous command exited successfully
docker_status=$(sem-dockerize 27017 mongodb)
if [ "$docker_status" == "0" ]; then
echo "done."
echo "mongodb $service_version is running at 0.0.0.0:27017"
else
# if not started in 30 seconds print logs and exit
echo "$docker_output"
fi
}
service::start_elasticsearch() {
service_version=$1
local service_image="${registry_host}/elasticsearch"
docker_cmd=$(echo "$2"|base64 -d )
service::pull_image "$service_image:$service_version"
docker_output=$(bash -c "$docker_cmd $service_image:$service_version")
# run only if previous command exited successfully
docker_status=$(sem-dockerize 9200 elasticsearch)
if [ "$docker_status" == "0" ]; then
echo "done."
echo "elasticsearch $service_version is accessible at 0.0.0.0:9200 and 0.0.0.0:9300"
else
# if not started in 30 seconds print logs and exit
echo "$docker_output"
fi
}
service::start_opensearch() {
service_version=$1
local service_image="${registry_host}/opensearch"
docker_cmd=$(echo "$2"|base64 -d )
service::pull_image "$service_image:$service_version"
docker_output=$(bash -c "$docker_cmd $service_image:$service_version")
# run only if previous command exited successfully
docker_status=$(sem-dockerize 9200 opensearch)
if [ "$docker_status" == "0" ]; then
echo "done."
echo "Opensearch $service_version is accessible at 0.0.0.0:9200 Username: admin, Password: admin"
else
# if not started in 30 seconds print logs and exit
echo "$docker_output"
fi
}
service::start_rabbitmq() {
service_version="${1:-3.8}"
local service_image="${registry_host}/rabbitmq"
service::pull_image "$service_image:$service_version"
docker_output=$(docker run --net=host --rm --name rabbitmq --hostname rabbitmq -d "$service_image":"$service_version")
# run only if previous command exited successfully
docker_status=$(sem-dockerize 5672 rabbitmq)
if [ "$docker_status" == "0" ]; then
echo "done."
echo "rabbitmq $service_version is accessible at 0.0.0.0:5672"
else
# if not started in 30 seconds print logs and exit
echo "$docker_output"
fi
}
service::start_cassandra() {
service_version="${1:-3.11}"
local service_image="${registry_host}/cassandra"
service::pull_image "$service_image:$service_version"
docker_output=$(docker run --net=host --rm --name cassandra -d "$service_image":"$service_version")
# run only if previous command exited successfully
docker_status=$(sem-dockerize 9042 cassandra)
if [ "$docker_status" == "0" ]; then
echo "done."
echo "cassandra $service_version is accessible at 0.0.0.0:9042"
else
# if not started in 30 seconds print logs and exit
echo "$docker_output"
fi
}
service::start_rethinkdb() {
service_version="${1:-2.3}"
local service_image="${registry_host}/rethinkdb"
service::pull_image "$service_image:$service_version"
docker_output=$(docker run --net=host --rm --name rethinkdb -d "$service_image":"$service_version")
# run only if previous command exited successfully
docker_status=$(sem-dockerize 28015 rethinkdb)
if [ "$docker_status" == "0" ]; then
echo "done."
echo "rethinkdb $service_version is accessible at 0.0.0.0:28015"
else
# if not started in 30 seconds print logs and exit
echo "$docker_output"
fi
}
service::start() {
service_name=$1
service_version=$2
if [ -z "$service_name" ]; then
service::print_usage
exit 1
fi
echo -n "Starting $service_name..."
case "$service_name" in
"mysql" )
shift
IFS=" " read -r -a version_and_args <<< "$(sem-service-check-params mysql "$@")"
service::check_version 'mysql' "${version_and_args[0]}"
service::duration service::start_mysql "${version_and_args[@]}" total "${service_name}"
exit 0
;;
"postgres" )
shift
IFS=" " read -r -a version_and_args <<< "$(sem-service-check-params postgres "$@")"
service::check_version 'postgres' "${version_and_args[0]}"
service::duration service::start_postgres "${version_and_args[@]}" total "${service_name}"
exit 0
;;
"postgis" )
shift
IFS=" " read -r -a version_and_args <<< "$(sem-service-check-params postgis "$@")"
service::check_version 'postgis' "${version_and_args[0]}"
service::duration service::start_postgis "${version_and_args[@]}" total "${service_name}"
exit 0
;;
"redis" )
shift
IFS=" " read -r -a version_and_args <<< "$(sem-service-check-params redis "$@")"
service::check_version 'redis' "${version_and_args[0]}"
service::duration service::start_redis "${version_and_args[@]}" total "${service_name}"
exit 0
;;
"memcached" )
shift
service_version="${service_version:-1.5}"
service::check_version 'memcached' "${service_version%% *}"
service::duration service::start_memcached "${service_version}" total "${service_name}"
exit 0
;;
"mongodb" )
shift
IFS=" " read -r -a version_and_args <<< "$(sem-service-check-params mongodb "$@")"
service::check_version 'mongo' "${version_and_args[0]}"
service::duration service::start_mongodb "${version_and_args[@]}" total "${service_name}"
exit 0
;;
"elasticsearch" )
shift
IFS=" " read -r -a version_and_args <<< "$(sem-service-check-params elasticsearch "$@")"
service::check_version 'elasticsearch' "${version_and_args[0]}"
service::duration service::start_elasticsearch "${version_and_args[@]}" total "${service_name}"
exit 0
;;
"opensearch" )
shift
IFS=" " read -r -a version_and_args <<< "$(sem-service-check-params opensearch "$@")"
service::check_version 'opensearch' "${version_and_args[0]}"
service::duration service::start_opensearch "${version_and_args[@]}" total "${service_name}"
exit 0
;;
"rabbitmq" )
shift
service_version="${service_version:-3.8}"
service::check_version 'rabbitmq' "${service_version%% *}"
service::duration service::start_rabbitmq "${service_version}" total "${service_name}"
exit 0
;;
"cassandra" )
shift
service_version="${service_version:-3.11}"
service::check_version 'cassandra' "${service_version%% *}"
service::duration service::start_cassandra "${service_version}" total "${service_name}"
exit 0
;;
"rethinkdb" )
shift
service_version="${service_version:-2.3}"
service::check_version 'rethinkdb' "${service_version%% *}"
service::duration service::start_rethinkdb "${service_version}" total "${service_name}"
exit 0
;;
* )
service::print_usage
exit 1
;;
esac
}
service::stop() {
local service_name
service_name=$1
service::status "${service_name}" &>/dev/null
service_status=$?
if [[ $service_status -eq 0 ]]; then
service::run_cmd docker stop "${service_name}"
sudo rm -rf "/var/tmp/${service_name}"
service::log "'${service_name}' stopped."
else
service::log "No running service with name '${service_name}' was found. Skipping."
fi
return 0
}
################################################################################
# Service status
# Globals:
# none
# Arguments:
# service_name
# Returns:
# 0,1...n
################################################################################
service::status() {
local service_name
service_name=$1
docker ps | grep "${service_name}" &>/dev/null
service_status=$?
if [[ $service_status -eq 0 ]]; then
service::log "${service_name} running."
else
service::log "'${service_name}' not running."
fi
return $service_status
}
################################################################################
# Helper for running commands with the ability to dry-run
# Globals:
# DRYRUN
# Arguments:
# command
# Returns:
# 0,1...n
################################################################################
service::run_cmd() {
local cmd=( "$@" )
local out=""
local status=0
if [[ -n $DRYRUN ]]; then
service::log "Dry-running '${cmd[*]}'"
else
out=$("${cmd[@]}" 2>&1)
status=$?
fi
if ! [[ $status -eq 0 ]]; then
service::err "Failed to run command '${cmd[*]}'\n\nReason:\n${out}"
fi
return $status
}
################################################################################
# Gets port information about the running service
# Globals:
# none
# Arguments:
# service_name
# Returns:
# port
################################################################################
service::port_info() {
local service_name
service_name=$1
port_check_log=$( service::run_cmd sudo netstat -tlpn | grep "${service_name}" 2>&1 )
if [[ $service_status -eq 0 ]]; then
listens_on=$( echo "${port_check_log}" | awk '{print $4}' )
echo "Listens on ${listens_on}"
else
echo "No port found for '${service_name}'."
fi
return 0
}
service::log() {
echo -e "[$(date +"${DATE_FORMAT}")]: $*" >&2
}
service::err() {
echo -e "\n! [$(date +"${DATE_FORMAT}")]: $*\n" >&2
exit 1
}
service::print_usage() {
echo -e "\nsem-service [start|stop|status] [service] [version]\n"
echo "Available services:"
echo "mysql (default: 5.6), available versions: ${sem_services['mysql']}"
echo "postgres (default: 9.6), available versions: ${sem_services['postgres']}"
echo "postgis (default: 9.6-2.5), available versions: ${sem_services['postgis']}"
echo "redis (default: 4.0), available versions: ${sem_services['redis']}"
echo "rabbitmq (default: 3.8), available versions: ${sem_services['rabbitmq']}"
echo "memcached (default: 1.5), available versions: ${sem_services['memcached']}"
echo "mongodb (default: 4.1), available versions: ${sem_services['mongo']}"
echo "elasticsearch (default: 6.5), available versions: ${sem_services['elasticsearch']}"
echo "opensearch (default: 2), available versions: ${sem_services['opensearch']}"
echo "cassandra (default: 3.11), available versions: ${sem_services['cassandra']}"
echo "rethinkdb (default: 2.3), available versions: ${sem_services['rethinkdb']}"
}
service::duration() {
local cmd
local service
local version
local type
local start
local end
local duration
cmd=( "${@:1:$#-2}" )
service=$(echo "${@: -1}")
version=$(echo "${@: 2:1}")
type=$(echo "${@: -2:1}")
start=$(date +%s%3N)
"${cmd[@]}"
end=$(date +%s%3N)
duration=$(( end - start ))
if [[ "$type" == "total" ]]; then
echo "semservice,service=$service,state=${state[$docker_status]},version=$version,location=$download_location duration=${duration}" >> /tmp/toolbox_semservice
fi
return 0
}
service::main () {
case "$1" in
"start" )
shift
service::start "$@"
;;
"stop" )
shift
service::stop "$@"
;;
"status" )
shift
service::status "$@"
;;
* )
service::print_usage
;;
esac
}
if [[ "$(uname)" == "Darwin" ]]; then
echo "'sem-service' is not supported in this environment."
echo "https://docs.semaphoreci.com/ci-cd-environment/sem-service-managing-databases-and-services-on-linux"
exit 1
else
service::main "$@"
fi