forked from dokku/dokku-mysql
-
Notifications
You must be signed in to change notification settings - Fork 0
/
common-functions
executable file
·896 lines (761 loc) · 28.7 KB
/
common-functions
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
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
#!/usr/bin/env bash
source "$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)/config"
set -eo pipefail
[[ $DOKKU_TRACE ]] && set -x
source "$PLUGIN_AVAILABLE_PATH/config/functions"
add_to_links_file() {
declare desc="add an app to the service link file"
declare SERVICE="$1" APP="$2"
local SERVICE_ROOT="$PLUGIN_DATA_ROOT/$SERVICE"
local LINKS_FILE="$SERVICE_ROOT/LINKS"
touch "$LINKS_FILE"
sed -i.bak "/^$APP\$/d" "$LINKS_FILE" && rm "$LINKS_FILE.bak"
echo "$APP" >>"$LINKS_FILE"
sort "$LINKS_FILE" -u -o "$LINKS_FILE"
}
docker_ports_options() {
declare desc="export a list of exposed ports"
declare PORTS=("$@")
for ((i = 0; i < ${#PLUGIN_DATASTORE_PORTS[@]}; i++)); do
echo -n "-p ${PORTS[i]}:${PLUGIN_DATASTORE_PORTS[i]} "
done
}
get_container_ip() {
declare desc="retrieve the ip address of a container"
declare CONTAINER_ID="$1"
docker inspect --format '{{ .NetworkSettings.IPAddress }}' "$CONTAINER_ID" 2>/dev/null
}
get_database_name() {
declare desc="retrieve a sanitized database name"
declare SERVICE="$1"
local SERVICE_ROOT="$PLUGIN_DATA_ROOT/$SERVICE"
if [[ ! -f "$SERVICE_ROOT/DATABASE_NAME" ]]; then
echo "$SERVICE" > "$SERVICE_ROOT/DATABASE_NAME"
fi
cat "$SERVICE_ROOT/DATABASE_NAME"
}
get_random_ports() {
declare desc="retrieve N random ports"
declare iterations="${1:-1}"
for ((i = 0; i < iterations; i++)); do
local port=$RANDOM
local quit=0
while [ "$quit" -ne 1 ]; do
netstat -an | grep $port >/dev/null
# shellcheck disable=SC2181
if [ $? -gt 0 ]; then
quit=1
else
port=$((port + 1))
fi
done
echo $port
done
}
get_service_name() {
declare desc="retrieve a docker service label"
declare SERVICE="$1"
echo "dokku.${PLUGIN_COMMAND_PREFIX}.$SERVICE"
}
get_url_from_config() {
declare desc="retrieve a given _URL from a list of configuration variables"
declare EXISTING_CONFIG="$1" CONFIG_VAR="$2"
echo "$EXISTING_CONFIG" | grep "$CONFIG_VAR" | sed "s/$CONFIG_VAR:\s*//" | xargs
}
in_links_file() {
declare desc="check if a service LINKS file contains an app"
declare SERVICE="$1" APP="$2"
local SERVICE_ROOT="$PLUGIN_DATA_ROOT/$SERVICE"
local LINKS_FILE="$SERVICE_ROOT/LINKS"
grep -qE "^$APP\$" "$LINKS_FILE"
}
is_container_status() {
declare desc="return 0 or 1 depending upon whether a given container has a certain status"
declare CID="$1" STATUS="$2"
local TEMPLATE="{{.State.$STATUS}}"
local CONTAINER_STATUS=$(docker inspect -f "$TEMPLATE" "$CID" 2>/dev/null || true)
if [[ "$CONTAINER_STATUS" == "true" ]]; then
return 0
fi
return 1
}
is_implemented_command() {
declare desc="return true if value ($1) is in list (all other arguments)"
declare CMD="$1"
CMD="$(echo "$CMD" | cut -d ':' -f2)"
if [[ ${#PLUGIN_UNIMPLEMENTED_SUBCOMMANDS[@]} -eq 0 ]]; then
return 0
fi
local e
for e in "${PLUGIN_UNIMPLEMENTED_SUBCOMMANDS[@]}"; do
[[ "$e" == "$CMD" ]] && return 1
done
return 0
}
is_valid_service_name() {
declare desc="validate a service name"
declare SERVICE="$1"
[[ -z "$SERVICE" ]] && return 1
if [[ "$SERVICE" =~ ^[A-Za-z0-9_-]+$ ]]; then
return 0
fi
return 1
}
remove_from_links_file() {
declare desc="remove an app from the service link file"
declare SERVICE="$1" APP="$2"
local SERVICE_ROOT="$PLUGIN_DATA_ROOT/$SERVICE"
local LINKS_FILE="$SERVICE_ROOT/LINKS"
if [[ ! -f "$LINKS_FILE" ]]; then
return
fi
sed -i.bak "/^$APP\$/d" "$LINKS_FILE" && rm "$LINKS_FILE.bak"
sort "$LINKS_FILE" -u -o "$LINKS_FILE"
}
retry-docker-command() {
local ID="$1" COMMAND="$2"
local i=0 success=false
until [ $i -ge 100 ]; do
set +e
suppress_output docker exec "$ID" sh -c "$COMMAND"
exit_code=$?
set -e
if [[ "$exit_code" == 0 ]]; then
success=true
break
fi
i=$((i + 1))
sleep 1
done
if [[ $i -gt 0 ]]; then
dokku_log_verbose "Container command retried ${i} time(s): ${COMMAND}"
fi
[[ "$success" == "true" ]] || dokku_log_fail "Failed to run command: ${COMMAND}"
}
service_alternative_alias() {
declare desc="retrieve an alternative alias for a service"
declare EXISTING_CONFIG="$1"
local COLORS=(AQUA BLACK BLUE FUCHSIA GRAY GREEN LIME MAROON NAVY OLIVE PURPLE RED SILVER TEAL WHITE YELLOW)
local ALIAS
for COLOR in "${COLORS[@]}"; do
ALIAS="${PLUGIN_ALT_ALIAS}_${COLOR}"
local IN_USE=$(echo "$EXISTING_CONFIG" | grep "${ALIAS}_URL")
if [[ -z "$IN_USE" ]]; then
break
fi
unset ALIAS
done
echo "$ALIAS"
}
service_app_links() {
declare desc="output all service links for a given app"
declare APP="$1"
local SERVICE LINKED_APP
pushd "$PLUGIN_DATA_ROOT" >/dev/null
for SERVICE in *; do
[[ -f "$SERVICE/LINKS" ]] || continue
for LINKED_APP in $(<"$SERVICE/LINKS"); do
if [[ "$LINKED_APP" == "$APP" ]]; then
echo "$SERVICE"
fi
done
done
}
service_backup() {
declare desc="create a backup of a service to an existing s3 bucket"
declare SERVICE="$1" BUCKET_NAME="$2" USE_IAM_OPTIONAL_FLAG="$3"
local SERVICE_BACKUP_ROOT="$PLUGIN_DATA_ROOT/$SERVICE/backup"
local BACKUP_ENCRYPTION_CONFIG_ROOT="$PLUGIN_DATA_ROOT/$SERVICE/backup-encryption"
local AWS_ACCESS_KEY_ID_FILE="$SERVICE_BACKUP_ROOT/AWS_ACCESS_KEY_ID"
local AWS_SECRET_ACCESS_KEY_FILE="$SERVICE_BACKUP_ROOT/AWS_SECRET_ACCESS_KEY"
local SERVICE_ROOT="$PLUGIN_DATA_ROOT/$SERVICE"
local ID="$(cat "$SERVICE_ROOT/ID")"
local BACKUP_PARAMETERS=""
if [[ -z "$USE_IAM_OPTIONAL_FLAG" ]]; then
[[ ! -f "$AWS_ACCESS_KEY_ID_FILE" ]] && dokku_log_fail "Missing AWS_ACCESS_KEY_ID file"
[[ ! -f "$AWS_SECRET_ACCESS_KEY_FILE" ]] && dokku_log_fail "Missing AWS_SECRET_ACCESS_KEY file"
BACKUP_PARAMETERS="$BACKUP_PARAMETERS -e AWS_ACCESS_KEY_ID=$(cat "$AWS_ACCESS_KEY_ID_FILE") -e AWS_SECRET_ACCESS_KEY=$(cat "$AWS_SECRET_ACCESS_KEY_FILE")"
elif [[ "$USE_IAM_OPTIONAL_FLAG" != "--use-iam" ]] && [[ "$USE_IAM_OPTIONAL_FLAG" != "-u" ]]; then
dokku_log_fail "Provide AWS credentials or use the --use-iam flag"
fi
BACKUP_TMPDIR=$(mktemp -d --tmpdir)
trap 'rm -rf "$BACKUP_TMPDIR" > /dev/null' RETURN INT TERM EXIT
docker inspect "$ID" &>/dev/null || dokku_log_fail "Service container does not exist"
is_container_status "$ID" "Running" || dokku_log_fail "Service container is not running"
(service_export "$SERVICE" >"${BACKUP_TMPDIR}/export")
# Build parameter list for s3backup tool
BACKUP_PARAMETERS="$BACKUP_PARAMETERS -e BUCKET_NAME=$BUCKET_NAME"
BACKUP_PARAMETERS="$BACKUP_PARAMETERS -e BACKUP_NAME=${PLUGIN_COMMAND_PREFIX}-${SERVICE}"
BACKUP_PARAMETERS="$BACKUP_PARAMETERS -v ${BACKUP_TMPDIR}:/backup"
if [[ -f "$SERVICE_BACKUP_ROOT/AWS_DEFAULT_REGION" ]]; then
BACKUP_PARAMETERS="$BACKUP_PARAMETERS -e AWS_DEFAULT_REGION=$(cat "$SERVICE_BACKUP_ROOT/AWS_DEFAULT_REGION")"
fi
if [[ -f "$SERVICE_BACKUP_ROOT/AWS_SIGNATURE_VERSION" ]]; then
BACKUP_PARAMETERS="$BACKUP_PARAMETERS -e AWS_SIGNATURE_VERSION=$(cat "$SERVICE_BACKUP_ROOT/AWS_SIGNATURE_VERSION")"
fi
if [[ -f "$SERVICE_BACKUP_ROOT/ENDPOINT_URL" ]]; then
BACKUP_PARAMETERS="$BACKUP_PARAMETERS -e ENDPOINT_URL=$(cat "$SERVICE_BACKUP_ROOT/ENDPOINT_URL")"
fi
if [[ -f "$BACKUP_ENCRYPTION_CONFIG_ROOT/ENCRYPTION_KEY" ]]; then
BACKUP_PARAMETERS="$BACKUP_PARAMETERS -e ENCRYPTION_KEY=$(cat "$BACKUP_ENCRYPTION_CONFIG_ROOT/ENCRYPTION_KEY")"
fi
# shellcheck disable=SC2086
docker run --rm $BACKUP_PARAMETERS "$PLUGIN_S3BACKUP_IMAGE"
}
service_commit_config() {
declare SERVICE="$1"
local SERVICE_ROOT="$PLUGIN_DATA_ROOT/$SERVICE"
local CONFIG_VARIABLE="${PLUGIN_VARIABLE}_CONFIG_OPTIONS"
local ENV_VARIABLE="${PLUGIN_VARIABLE}_CUSTOM_ENV"
custom_env="${!ENV_VARIABLE}"
[[ -n "$SERVICE_CUSTOM_ENV" ]] && custom_env="$SERVICE_CUSTOM_ENV"
if [[ -n $custom_env ]]; then
echo "$custom_env" | tr ';' "\n" >"$SERVICE_ROOT/ENV"
else
echo "" >"$SERVICE_ROOT/ENV"
fi
config_options="${!CONFIG_VARIABLE}"
[[ -n "$PLUGIN_CONFIG_OPTIONS" ]] && config_options="$PLUGIN_CONFIG_OPTIONS"
if [[ -n "$config_options" ]]; then
echo "$config_options" >"$SERVICE_ROOT/CONFIG_OPTIONS"
else
echo "" >"$SERVICE_ROOT/CONFIG_OPTIONS"
fi
if [[ -n "$SERVICE_MEMORY" ]]; then
echo "$SERVICE_MEMORY" >"$SERVICE_ROOT/SERVICE_MEMORY"
fi
if [[ -n "$SERVICE_SHM_SIZE" ]]; then
echo "$SERVICE_SHM_SIZE" >"$SERVICE_ROOT/SHM_SIZE"
fi
if [[ -n "$PLUGIN_IMAGE" ]]; then
echo "$PLUGIN_IMAGE" >"$SERVICE_ROOT/IMAGE"
fi
if [[ -n "$PLUGIN_IMAGE_VERSION" ]]; then
echo "$PLUGIN_IMAGE_VERSION" >"$SERVICE_ROOT/IMAGE_VERSION"
fi
}
service_backup_auth() {
declare desc="set up authentication"
declare SERVICE="$1" AWS_ACCESS_KEY_ID="$2" AWS_SECRET_ACCESS_KEY="$3" AWS_DEFAULT_REGION="$4" AWS_SIGNATURE_VERSION="$5" ENDPOINT_URL="$6"
local SERVICE_BACKUP_ROOT="$PLUGIN_DATA_ROOT/$SERVICE/backup"
mkdir "$SERVICE_BACKUP_ROOT"
echo "$AWS_ACCESS_KEY_ID" >"$SERVICE_BACKUP_ROOT/AWS_ACCESS_KEY_ID"
echo "$AWS_SECRET_ACCESS_KEY" >"$SERVICE_BACKUP_ROOT/AWS_SECRET_ACCESS_KEY"
if [[ -n "$AWS_DEFAULT_REGION" ]]; then
echo "$AWS_DEFAULT_REGION" >"$SERVICE_BACKUP_ROOT/AWS_DEFAULT_REGION"
fi
if [[ -n "$AWS_SIGNATURE_VERSION" ]]; then
echo "$AWS_SIGNATURE_VERSION" >"$SERVICE_BACKUP_ROOT/AWS_SIGNATURE_VERSION"
fi
if [[ -n "$ENDPOINT_URL" ]]; then
echo "$ENDPOINT_URL" >"$SERVICE_BACKUP_ROOT/ENDPOINT_URL"
fi
}
service_backup_deauth() {
declare desc="remove authentication"
declare SERVICE="$1"
local SERVICE_ROOT="${PLUGIN_DATA_ROOT}/${SERVICE}"
local SERVICE_BACKUP_ROOT="${SERVICE_ROOT}/backup/"
rm -rf "$SERVICE_BACKUP_ROOT"
}
service_backup_schedule() {
declare desc="schedules a backup of the service"
declare SERVICE="$1" SCHEDULE="$2" BUCKET_NAME="$3" USE_IAM_OPTIONAL_FLAG="$4"
local DOKKU_BIN="$(which dokku)"
local CRON_FILE="/etc/cron.d/dokku-${PLUGIN_COMMAND_PREFIX}-${SERVICE}"
local TMP_CRON_FILE="${PLUGIN_DATA_ROOT}/.TMP_CRON_FILE"
if [[ -n "$USE_IAM_OPTIONAL_FLAG" ]] && [[ "$USE_IAM_OPTIONAL_FLAG" != "--use-iam" ]] && [[ "$USE_IAM_OPTIONAL_FLAG" != "-u" ]]; then
dokku_log_fail "Invalid flag provided, only '--use-iam' allowed"
fi
echo "${SCHEDULE} dokku ${DOKKU_BIN} ${PLUGIN_COMMAND_PREFIX}:backup ${SERVICE} ${BUCKET_NAME} ${USE_IAM_OPTIONAL_FLAG}" >"$TMP_CRON_FILE"
sudo /bin/mv "$TMP_CRON_FILE" "$CRON_FILE"
sudo /bin/chown root:root "$CRON_FILE"
sudo /bin/chmod 644 "$CRON_FILE"
}
service_backup_schedule_cat() {
declare desc="cat the contents of the configured backup cronfile for the service"
declare SERVICE="$1"
local CRON_FILE="/etc/cron.d/dokku-${PLUGIN_COMMAND_PREFIX}-${SERVICE}"
if [[ ! -f "$CRON_FILE" ]]; then
dokku_log_fail "There is no scheduled backup for ${SERVICE}."
fi
cat "$CRON_FILE"
}
service_backup_set_encryption() {
declare desc="set up backup encryption"
declare SERVICE="$1" ENCRYPTION_KEY="$2"
local SERVICE_ROOT="${PLUGIN_DATA_ROOT}/${SERVICE}"
local SERVICE_BACKUP_ENCRYPTION_ROOT="${SERVICE_ROOT}/backup-encryption/"
mkdir "$SERVICE_BACKUP_ENCRYPTION_ROOT"
echo "$ENCRYPTION_KEY" >"${SERVICE_BACKUP_ENCRYPTION_ROOT}/ENCRYPTION_KEY"
}
service_backup_unschedule() {
declare desc="unschedule the backup of the service"
declare SERVICE="$1"
local CRON_FILE="/etc/cron.d/dokku-${PLUGIN_COMMAND_PREFIX}-${SERVICE}"
sudo /bin/rm -f "$CRON_FILE"
}
service_backup_unset_encryption() {
declare desc="remove backup encryption"
declare SERVICE="$1"
local SERVICE_ROOT="${PLUGIN_DATA_ROOT}/${SERVICE}"
local SERVICE_BACKUP_ENCRYPTION_ROOT="${SERVICE_ROOT}/backup-encryption/"
rm -rf "$SERVICE_BACKUP_ENCRYPTION_ROOT"
}
service_container_rm() {
declare desc="stop a service and remove the running container"
declare SERVICE="$1"
local SERVICE_NAME="$(get_service_name "$SERVICE")"
local ID
service_stop "$SERVICE"
ID=$(docker ps -aq --no-trunc --filter "name=^/$SERVICE_NAME$" --format '{{ .ID }}') || true
# this may be 'true' in tests...
if [[ -z "$ID" ]] || [[ "$ID" == "true" ]]; then
return 0
fi
dokku_log_verbose_quiet "Removing container"
docker update --restart=no "$SERVICE_NAME" >/dev/null 2>&1
if ! docker rm "$SERVICE_NAME" >/dev/null 2>&1; then
dokku_log_fail "Unable to remove container for service $SERVICE"
fi
}
service_dns_hostname() {
declare desc="retrieve the alias of a service"
declare SERVICE="$1"
local SERVICE_NAME="$(get_service_name "$SERVICE")"
echo "$SERVICE_NAME" | tr ._ -
}
service_enter() {
declare desc="enter running app container of specified proc type"
declare SERVICE="$1" && shift 1
local SERVICE_ROOT="$PLUGIN_DATA_ROOT/$SERVICE"
local ID="$(cat "$SERVICE_ROOT/ID")"
docker inspect "$ID" &>/dev/null || dokku_log_fail "Service container does not exist"
is_container_status "$ID" "Running" || dokku_log_fail "Service container is not running"
local EXEC_CMD=""
has_tty && local DOKKU_RUN_OPTS+=" -i -t"
# shellcheck disable=SC2086
docker exec $DOKKU_RUN_OPTS $ID $EXEC_CMD "${@:-/bin/bash}"
}
service_exposed_ports() {
declare desc="list exposed ports for a service"
declare SERVICE="$1"
local SERVICE_ROOT="$PLUGIN_DATA_ROOT/$SERVICE"
local PORT_FILE="$SERVICE_ROOT/PORT"
[[ ! -f $PORT_FILE ]] && echo '-' && return 0
local PORTS=($(cat "$PORT_FILE"))
for ((i = 0; i < ${#PLUGIN_DATASTORE_PORTS[@]}; i++)); do
echo -n "${PLUGIN_DATASTORE_PORTS[i]}->${PORTS[i]} "
done
}
service_image_exists() {
declare desc="check if the current image exists"
declare SERVICE="$1"
local plugin_image="$PLUGIN_IMAGE"
local plugin_image_version="$PLUGIN_IMAGE_VERSION"
[[ -f "$SERVICE_ROOT/IMAGE" ]] && plugin_image="$(cat "$SERVICE_ROOT/IMAGE")"
[[ -f "$SERVICE_ROOT/IMAGE_VERSION" ]] && plugin_image_version="$(cat "$SERVICE_ROOT/IMAGE_VERSION")"
local IMAGE="$plugin_image:$plugin_image_version"
if [[ "$(docker images -q "$IMAGE" 2>/dev/null)" == "" ]]; then
return 1
fi
return 0
}
service_info() {
declare desc="retrieve information about a given service"
declare SERVICE="$1" INFO_FLAG="$2"
local SERVICE_ROOT="$PLUGIN_DATA_ROOT/$SERVICE"
local SERVICE_URL=$(service_url "$SERVICE")
local PORT_FILE="$SERVICE_ROOT/PORT"
local SERVICE_CONTAINER_ID="$(cat "$SERVICE_ROOT/ID")"
local flag key valid_flags
local flag_map=(
"--config-dir: ${SERVICE_ROOT}/${PLUGIN_CONFIG_SUFFIX}"
"--config-options: $(cat "$SERVICE_ROOT/CONFIG_OPTIONS")"
"--data-dir: ${SERVICE_ROOT}/data"
"--dsn: ${SERVICE_URL}"
"--exposed-ports: $(service_exposed_ports "$SERVICE")"
"--id: ${SERVICE_CONTAINER_ID}"
"--internal-ip: $(get_container_ip "${SERVICE_CONTAINER_ID}")"
"--links: $(service_linked_apps "$SERVICE")"
"--service-root: ${SERVICE_ROOT}"
"--status: $(service_status "$SERVICE")"
"--version: $(service_version "$SERVICE")"
)
if [[ -z "$INFO_FLAG" ]]; then
dokku_log_info2 "$SERVICE $PLUGIN_COMMAND_PREFIX service information"
for flag in "${flag_map[@]}"; do
key="$(echo "${flag#--}" | cut -f1 -d' ' | tr - ' ')"
dokku_log_verbose "$(printf "%-20s %-25s" "${key^}" "${flag#*: }")"
done
else
local match=false
for flag in "${flag_map[@]}"; do
valid_flags="${valid_flags} $(echo "$flag" | cut -d':' -f1)"
if [[ "$flag" == "${INFO_FLAG}:"* ]]; then
echo "${flag#*: }" && match=true
fi
done
[[ "$match" == "true" ]] || dokku_log_fail "Invalid flag passed, valid flags:${valid_flags}"
fi
}
service_is_linked() {
declare desc="link a service to an application"
declare SERVICE="$1" APP="$2"
update_plugin_scheme_for_app "$APP"
local SERVICE_URL=$(service_url "$SERVICE")
local EXISTING_CONFIG=$(config_all "$APP")
local LINK=$(echo "$EXISTING_CONFIG" | grep "$SERVICE_URL" | cut -d: -f1) || true
if [[ -z $LINK ]]; then
dokku_log_warn "Service $SERVICE is not linked to $APP"
exit 1
fi
dokku_log_info1 "Service $SERVICE is linked to $APP"
}
service_link() {
declare desc="link a service to an application"
declare SERVICE="$1" APP="$2"
update_plugin_scheme_for_app "$APP"
local SERVICE_URL=$(service_url "$SERVICE")
local SERVICE_NAME="$(get_service_name "$SERVICE")"
local SERVICE_ROOT="$PLUGIN_DATA_ROOT/$SERVICE"
local EXISTING_CONFIG=$(config_all "$APP")
local LINK=$(echo "$EXISTING_CONFIG" | grep "$SERVICE_URL" | cut -d: -f1) || true
local SERVICE_DNS_HOSTNAME=$(service_dns_hostname "$SERVICE")
local LINKS_FILE="$SERVICE_ROOT/LINKS"
local ALIAS="$PLUGIN_DEFAULT_ALIAS"
local DEFAULT_ALIAS
if [[ -n "$SERVICE_ALIAS" ]]; then
ALIAS="$SERVICE_ALIAS"
ALIAS_IN_USE=$(echo "$EXISTING_CONFIG" | grep "${ALIAS}_URL") || true
[[ -n "$ALIAS_IN_USE" ]] && dokku_log_fail "Specified alias $ALIAS already in use"
else
DEFAULT_ALIAS=$(echo "$EXISTING_CONFIG" | grep "${PLUGIN_DEFAULT_ALIAS}_URL") || true
if [[ -n "$DEFAULT_ALIAS" ]]; then
ALIAS=$(service_alternative_alias "$EXISTING_CONFIG")
fi
[[ -z "$ALIAS" ]] && dokku_log_fail "Unable to use default or generated URL alias"
fi
[[ -n $LINK ]] && dokku_log_fail "Already linked as $LINK"
plugn trigger service-action pre-link "$SERVICE" "$APP"
add_to_links_file "$SERVICE" "$APP"
if declare -f -F add_passed_docker_option >/dev/null; then
# shellcheck disable=SC2034
local passed_phases=(build deploy run)
add_passed_docker_option passed_phases[@] "--link $SERVICE_NAME:$SERVICE_DNS_HOSTNAME"
else
dokku docker-options:add "$APP" build,deploy,run "--link $SERVICE_NAME:$SERVICE_DNS_HOSTNAME"
fi
[[ -n "$SERVICE_QUERYSTRING" ]] && SERVICE_URL="${SERVICE_URL}?${SERVICE_QUERYSTRING}"
plugn trigger service-action post-link "$SERVICE" "$APP"
config_set "$APP" "${ALIAS}_URL=$SERVICE_URL"
plugn trigger service-action post-link-complete "$SERVICE" "$APP"
}
service_linked_apps() {
declare desc="list all apps linked to a service for info output"
declare SERVICE="$1"
local SERVICE_ROOT="$PLUGIN_DATA_ROOT/$SERVICE"
local LINKS_FILE="$SERVICE_ROOT/LINKS"
touch "$LINKS_FILE"
[[ -z $(<"$LINKS_FILE") ]] && echo '-' && return 0
tr '\n' ' ' <"$LINKS_FILE"
}
service_links() {
declare desc="list all apps linked to a service"
declare SERVICE="$1"
local SERVICE_ROOT="$PLUGIN_DATA_ROOT/$SERVICE"
local LINKS_FILE="$SERVICE_ROOT/LINKS"
touch "$LINKS_FILE"
[[ -z $(<"$LINKS_FILE") ]] && return 0
cat "$LINKS_FILE"
}
service_list() {
declare desc="list all services and their status"
local SERVICES=$(ls "$PLUGIN_DATA_ROOT" 2>/dev/null)
if [[ -z $SERVICES ]]; then
dokku_log_warn "There are no $PLUGIN_SERVICE services"
return
fi
dokku_log_info2_quiet "$PLUGIN_SERVICE services"
for SERVICE in $SERVICES; do
echo "$SERVICE"
done
}
service_logs() {
declare desc="display logs for a service"
declare SERVICE="$1" TAIL_FLAG="$2"
local SERVICE_ROOT="$PLUGIN_DATA_ROOT/$SERVICE"
local ID=$(cat "$SERVICE_ROOT/ID")
local RE_INTEGER='^[0-9]+$'
DOKKU_LOGS_ARGS="--tail 100"
if [[ "$TAIL_FLAG" == "-t" ]] || [[ "$TAIL_FLAG" == "--tail" ]]; then
DOKKU_LOGS_ARGS="--follow"
fi
docker inspect "$ID" &>/dev/null || dokku_log_fail "Service container does not exist"
is_container_status "$ID" "Running" || dokku_log_warn "Service logs may not be output as service is not running"
# shellcheck disable=SC2086
docker logs $DOKKU_LOGS_ARGS "$ID" 2>&1
}
service_parse_args() {
declare desc="cli arg parser"
local next_index=1
local skip=false
local args=("$@")
for arg in "$@"; do
shift
case "$arg" in
"--alias") set -- "$@" "-a" ;;
"--config-options") set -- "$@" "-c" ;;
"--custom-env") set -- "$@" "-C" ;;
"--database") set -- "$@" "-d" ;;
"--image-version") set -- "$@" "-I" ;;
"--image") set -- "$@" "-i" ;;
"--memory") set -- "$@" "-m" ;;
"--password") set -- "$@" "-p" ;;
"--querystring") set -- "$@" "-q" ;;
"--restart-apps") set -- "$@" "-R" ;;
"--root-password") set -- "$@" "-r" ;;
"--shm-size") set -- "$@" "-s" ;;
"--user") set -- "$@" "-u" ;;
*) set -- "$@" "$arg" ;;
esac
done
OPTIND=1
while getopts "a:c:C:d:i:I:m:p:q:R:r:s:u:" opt; do
case "$opt" in
a)
SERVICE_ALIAS="${OPTARG^^}"
export SERVICE_ALIAS="${SERVICE_ALIAS%_URL}"
;;
c)
export PLUGIN_CONFIG_OPTIONS=$OPTARG
;;
C)
export SERVICE_CUSTOM_ENV=$OPTARG
;;
d)
export SERVICE_DATABASE=$OPTARG
;;
i)
export PLUGIN_IMAGE=$OPTARG
;;
I)
export PLUGIN_IMAGE_VERSION=$OPTARG
;;
m)
export SERVICE_MEMORY=$OPTARG
;;
p)
export SERVICE_PASSWORD=$OPTARG
;;
q)
export SERVICE_QUERYSTRING=${OPTARG#"?"}
;;
R)
export SERVICE_RESTART_APPS=$OPTARG
;;
r)
export SERVICE_ROOT_PASSWORD=$OPTARG
;;
s)
export SERVICE_SHM_SIZE=$OPTARG
;;
u)
export SERVICE_USER=$OPTARG
;;
esac
done
shift "$((OPTIND - 1))" # remove options from positional parameters
}
service_password() {
declare desc="fetch the password for a service"
declare SERVICE="$1"
local SERVICE_ROOT="$PLUGIN_DATA_ROOT/$SERVICE"
local PASSWORD_FILE="$SERVICE_ROOT/PASSWORD"
if [[ -f "$PASSWORD_FILE" ]]; then
cat "$PASSWORD_FILE"
fi
}
service_root_password() {
declare desc="fetch the root password for a service"
declare SERVICE="$1"
local SERVICE_ROOT="$PLUGIN_DATA_ROOT/$SERVICE"
local PASSWORD_FILE="$SERVICE_ROOT/ROOTPASSWORD"
if [[ -f "$PASSWORD_FILE" ]]; then
cat "$PASSWORD_FILE"
fi
}
service_port_expose() {
declare desc="wrapper for exposing service ports"
declare SERVICE="$1"
service_start "$SERVICE" "true"
service_port_unpause "$SERVICE" "true" "${@:2}"
}
service_port_pause() {
declare desc="pause service exposure"
declare SERVICE="$1" LOG_FAIL="$2"
local SERVICE_ROOT="$PLUGIN_DATA_ROOT/$SERVICE"
local EXPOSED_NAME="$(get_service_name "$SERVICE").ambassador"
local PORT_FILE="$SERVICE_ROOT/PORT"
if [[ "$LOG_FAIL" == "true" ]]; then
[[ ! -f "$PORT_FILE" ]] && dokku_log_fail "Service not exposed"
else
[[ ! -f "$PORT_FILE" ]] && return 0
fi
local GREP_NAME="^/${EXPOSED_NAME}$"
local CONTAINER_NAME="$(docker ps -f name="$GREP_NAME" --format "{{.Names}}")"
if [[ -z "$CONTAINER_NAME" ]]; then
if [[ "$LOG_FAIL" == "true" ]]; then
dokku_log_info1 "Service $SERVICE unexposed"
fi
return
fi
docker stop "$EXPOSED_NAME" >/dev/null 2>&1 || true
docker rm "$EXPOSED_NAME" >/dev/null 2>&1 || true
if [[ "$LOG_FAIL" == "true" ]]; then
dokku_log_info1 "Service $SERVICE unexposed"
fi
}
service_port_unexpose() {
declare desc="wrapper for pausing exposed service ports"
declare SERVICE="$1"
local SERVICE_ROOT="$PLUGIN_DATA_ROOT/$SERVICE"
local PORT_FILE="$SERVICE_ROOT/PORT"
service_port_pause "$SERVICE" "true"
rm -rf "$PORT_FILE"
}
service_port_unpause() {
declare desc="start service exposure"
declare SERVICE="$1" LOG_FAIL="$2"
local SERVICE_ROOT="$PLUGIN_DATA_ROOT/$SERVICE"
local SERVICE_NAME="$(get_service_name "$SERVICE")"
local EXPOSED_NAME="${SERVICE_NAME}.ambassador"
local PORT_FILE="$SERVICE_ROOT/PORT"
# shellcheck disable=SC2068
local PORTS=(${@:3})
# shellcheck disable=SC2068
PORTS=(${PORTS[@]:-$(get_random_ports ${#PLUGIN_DATASTORE_PORTS[@]})})
local ID=$(cat "$SERVICE_ROOT/ID")
[[ "${#PORTS[@]}" != "${#PLUGIN_DATASTORE_PORTS[@]}" ]] && dokku_log_fail "${#PLUGIN_DATASTORE_PORTS[@]} ports to be exposed need to be provided in the following order: ${PLUGIN_DATASTORE_PORTS[*]}"
if [[ "$LOG_FAIL" == "true" ]]; then
[[ -f "$PORT_FILE" ]] && PORTS=($(cat "$PORT_FILE")) && dokku_log_fail "Service $SERVICE already exposed on port(s) ${PORTS[*]}"
else
[[ ! -f "$PORT_FILE" ]] && return 0
PORTS=($(cat "$PORT_FILE"))
fi
echo "${PORTS[@]}" >"$PORT_FILE"
# shellcheck disable=SC2046
docker run -d --link "$SERVICE_NAME:$PLUGIN_COMMAND_PREFIX" --name "$EXPOSED_NAME" $(docker_ports_options "${PORTS[@]}") --restart always --label dokku=ambassador --label "dokku.ambassador=$PLUGIN_COMMAND_PREFIX" "$PLUGIN_AMBASSADOR_IMAGE" >/dev/null
if [[ "$LOG_FAIL" == "true" ]]; then
dokku_log_info1 "Service $SERVICE exposed on port(s) [container->host]: $(service_exposed_ports "$SERVICE")"
fi
}
service_promote() {
declare desc="promote a secondary service to the primary env var"
declare SERVICE="$1" APP="$2"
local PLUGIN_DEFAULT_CONFIG_VAR="${PLUGIN_DEFAULT_ALIAS}_URL"
local EXISTING_CONFIG=$(config_all "$APP")
update_plugin_scheme_for_app "$APP"
local SERVICE_URL=$(service_url "$SERVICE")
local CONFIG_VARS=($(echo "$EXISTING_CONFIG" | grep "$SERVICE_URL" | cut -d: -f1)) || true
local PREVIOUS_DEFAULT_URL=$(get_url_from_config "$EXISTING_CONFIG" "$PLUGIN_DEFAULT_CONFIG_VAR")
[[ -z ${CONFIG_VARS[*]} ]] && dokku_log_fail "Not linked to app $APP"
[[ ${CONFIG_VARS[*]} =~ $PLUGIN_DEFAULT_CONFIG_VAR ]] && dokku_log_fail "Service $1 already promoted as $PLUGIN_DEFAULT_CONFIG_VAR"
local NEW_CONFIG_VARS=""
if [[ -n $PREVIOUS_DEFAULT_URL ]]; then
local PREVIOUS_ALIAS=$(echo "$EXISTING_CONFIG" | grep "$PREVIOUS_DEFAULT_URL" | grep -v "$PLUGIN_DEFAULT_CONFIG_VAR") || true
if [[ -z $PREVIOUS_ALIAS ]]; then
local ALIAS=$(service_alternative_alias "$EXISTING_CONFIG")
NEW_CONFIG_VARS+="${ALIAS}_URL=$PREVIOUS_DEFAULT_URL "
fi
fi
local PROMOTE_URL=$(get_url_from_config "$EXISTING_CONFIG" "${CONFIG_VARS[0]}")
NEW_CONFIG_VARS+="$PLUGIN_DEFAULT_CONFIG_VAR=$PROMOTE_URL"
# shellcheck disable=SC2086
config_set "$APP" $NEW_CONFIG_VARS
}
service_set_alias() {
declare desc="sets the alias in use for a service"
declare SERVICE="$1" ALIAS="$2"
local SERVICE_ROOT="$PLUGIN_DATA_ROOT/$SERVICE"
local ALIAS_FILE="$SERVICE_ROOT/ALIAS"
touch "$ALIAS_FILE"
echo "$ALIAS" >"$ALIAS_FILE"
}
service_status() {
declare desc="display the status of a service"
declare SERVICE="$1"
local SERVICE_ROOT="$PLUGIN_DATA_ROOT/$SERVICE"
local ID="$(cat "$SERVICE_ROOT/ID")"
local CONTAINER_STATUS
CONTAINER_STATUS=$(docker inspect -f "{{.State.Status}}" "$ID" 2>/dev/null || true)
[[ -n "$CONTAINER_STATUS" ]] && echo "$CONTAINER_STATUS" && return 0
echo "missing" && return 0
}
service_stop() {
declare desc="stop a running service"
declare SERVICE="$1"
local SERVICE_ROOT="$PLUGIN_DATA_ROOT/$SERVICE"
local SERVICE_NAME="$(get_service_name "$SERVICE")"
local ID=$(docker ps -aq --no-trunc --filter "name=^/$SERVICE_NAME$" --format '{{ .ID }}') || true
[[ -z $ID ]] && dokku_log_warn "Service is already stopped" && return 0
if [[ -n $ID ]]; then
dokku_log_info2_quiet "Stopping container"
docker stop "$SERVICE_NAME" >/dev/null
service_port_pause "$SERVICE"
dokku_log_verbose_quiet "Container stopped"
else
dokku_log_verbose_quiet "No container exists for $SERVICE"
fi
}
service_unlink() {
declare desc="unlink an application from a service"
declare SERVICE="$1" APP="$2"
update_plugin_scheme_for_app "$APP"
local SERVICE_URL=$(service_url "$SERVICE")
local SERVICE_NAME="$(get_service_name "$SERVICE")"
local EXISTING_CONFIG=$(config_all "$APP")
local SERVICE_DNS_HOSTNAME=$(service_dns_hostname "$SERVICE")
local LINK=($(echo "$EXISTING_CONFIG" | grep "$SERVICE_URL" | cut -d: -f1)) || true
plugn trigger service-action pre-unlink "$SERVICE" "$APP"
remove_from_links_file "$SERVICE" "$APP"
if declare -f -F add_passed_docker_option >/dev/null; then
# shellcheck disable=SC2034
local passed_phases=(build deploy run)
remove_passed_docker_option passed_phases[@] "--link $SERVICE_NAME:$SERVICE_DNS_HOSTNAME"
else
dokku docker-options:remove "$APP" build,deploy,run "--link $SERVICE_NAME:$SERVICE_DNS_HOSTNAME"
fi
[[ -z ${LINK[*]} ]] && dokku_log_fail "Not linked to app $APP"
plugn trigger service-action post-unlink "$SERVICE" "$APP"
config_unset "$APP" "${LINK[@]}"
plugn trigger service-action post-unlink-complete "$SERVICE" "$APP"
}
service_version() {
declare desc="display the running version for an image"
declare SERVICE="$1"
local SERVICE_NAME="$(get_service_name "$SERVICE")"
docker inspect -f '{{.Config.Image}}' "$SERVICE_NAME" 2>/dev/null || true
}
update_plugin_scheme_for_app() {
declare desc="retrieve the updated plugin scheme"
declare APP="$1"
local DATABASE_SCHEME
DATABASE_SCHEME=$(config_get "$APP" "${PLUGIN_VARIABLE}_DATABASE_SCHEME" || true)
PLUGIN_SCHEME=${DATABASE_SCHEME:-$PLUGIN_SCHEME}
}
verify_service_name() {
declare desc="verify that a service exists"
declare SERVICE="$1"
[[ -z "$SERVICE" ]] && dokku_log_fail "(verify_service_name) SERVICE must not be null"
[[ ! -d "$PLUGIN_DATA_ROOT/$SERVICE" ]] && dokku_log_fail "$PLUGIN_SERVICE service $SERVICE does not exist"
return 0
}
write_database_name() {
declare desc="write a sanitized database name"
declare SERVICE="$1"
local SERVICE_ROOT="$PLUGIN_DATA_ROOT/$SERVICE"
# some datastores do not like special characters in database names
# so we need to normalize them out
echo "$SERVICE" | tr .- _ > "$SERVICE_ROOT/DATABASE_NAME"
}