-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathDockerExec
More file actions
executable file
·584 lines (479 loc) · 23.7 KB
/
DockerExec
File metadata and controls
executable file
·584 lines (479 loc) · 23.7 KB
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
#!/usr/bin/env bash
SCRIPT_PATH=$(realpath "$0")
export SCRIPT_PATH
BASE_DIR=$(dirname "$SCRIPT_PATH")
export BASE_DIR
CURRENT_DIR=$(pwd)
export CURRENT_DIR
export ENV_FILE="${DEFAULT_ENV_FILE:-.env}"
export LINUX_HOSTS=/etc/hosts
export STATUS_FILE_NAME="docker_current.txt"
export TEMP_HOSTS_FILE=".current-hosts"
export UPD_FILE_NAME=".last-update"
export IGNORED_HOSTS_CONTAINERS="${IGNORED_HOSTS_CONTAINERS:-}"
export SRC_DIR="$BASE_DIR/src"
export PROXY_ENV_FILE="$BASE_DIR/.env"
export CERTS_PATH="$BASE_DIR/certs"
export TEMP_HOSTS_PATH="$BASE_DIR/$TEMP_HOSTS_FILE"
export UPD_FILE_PATH="$BASE_DIR/$UPD_FILE_NAME"
export SPAWNS_AVAIL_PATH="$BASE_DIR/spawns-available"
export SPAWNS_ENABLED_PATH="$BASE_DIR/spawns-enabled"
# include variables from .env file in source directory
# shellcheck disable=SC1090
source "$PROXY_ENV_FILE"
# include scripts
. "$SRC_DIR/warnings.sh"
. "$SRC_DIR/helpers.sh"
. "$SRC_DIR/security.sh"
. "$SRC_DIR/generate.sh"
. "$SRC_DIR/spawns.sh"
. "$SRC_DIR/main.sh"
###
### How to install this script
###
if [[ ! -f "$PROXY_ENV_FILE" ]]; then
echo
echo -e "${COLOR_BLUE}Installation:"
echo
echo -e " ${COLOR_WHITE}Make sure to clone the repository via ${COLOR_ORANGE}git clone${COLOR_WHITE} to this machine."
echo -e " Then copy the .env.template for a .env and adapt it to your needs."
echo
echo -e " You should also create a softlink for DockerExec, e.g."
echo -e " ${COLOR_ORANGE}ln -s /path/to/this/docker-proxy/DockerExec /home/\$USER/.local/bin/DockerExec"
echo
echo -e " ${COLOR_WHITE}See https://sengorius.github.io/repositories/docker-proxy/install.html for further info"
echo
exit 0
fi
###
### start of program
###
# get the arguments and set default parameter, if none was given
ENVIRONMENT=$1; shift
case "$ENVIRONMENT" in
dev|prod|proxy|spawn|do)
ACTION=$1; shift
;;
*)
ACTION="$ENVIRONMENT"
ENVIRONMENT="do"
;;
esac
PARAMETERS=("$@")
if [[ -z "$ACTION" ]]; then
ACTION="ps"
fi
# return the help statement, if asked for
if [[ "help" == "$ACTION" || "--help" == "$ACTION" || "-h" == "$ACTION" ]]; then
BN=$(basename "$0")
VERSION=$(get_current_git_tag | head -n 1)
echo
echo -e "${COLOR_WHITE}DockerExec shell script in version ${COLOR_RED}$VERSION"
echo
echo -e "${COLOR_WHITE}Usage:"
echo -e " $BN ${COLOR_GREEN}[Environment] ${COLOR_BLUE}[Option] ${COLOR_RED}[\$Parameter(s)] ${COLOR_WHITE}"
echo
echo -e "${COLOR_WHITE}Find help and docs on:"
echo -e " ${COLOR_WHITE}https://sengorius.github.io/repositories/docker-proxy/index.html"
echo
echo -e "${COLOR_WHITE}Environments and their options:"
echo -e " ${COLOR_GREEN}dev${COLOR_WHITE}: Controls for docker-compose.yml and docker-compose.dev.yml"
echo -e " ${COLOR_BLUE}start ${COLOR_RED}[\$f]${COLOR_WHITE} - executes an extended docker-compose setup with the local file [and given .env file]"
echo -e " ${COLOR_BLUE}stop ${COLOR_RED}[\$f]${COLOR_WHITE} - shutdown for the local docker-compose setup [and given .env file]"
echo
echo -e " ${COLOR_GREEN}prod${COLOR_WHITE}: Controls for docker-compose.prod.yml"
echo -e " ${COLOR_BLUE}start ${COLOR_RED}[\$f]${COLOR_WHITE} - executes an extended docker-compose setup with the local file [and given .env file]"
echo -e " ${COLOR_BLUE}stop ${COLOR_RED}[\$f]${COLOR_WHITE} - shutdown for the local docker-compose setup [and given .env file]"
echo
echo -e " ${COLOR_GREEN}proxy${COLOR_WHITE}: Controls docker-compose.proxy.yml and the proxy stack"
echo -e " ${COLOR_BLUE}init${COLOR_WHITE} - starts the docker NGINX proxy stack and creates a network, if necessary"
echo -e " ${COLOR_BLUE}finish${COLOR_WHITE} - shuts the docker NGINX proxy stack down"
echo -e " ${COLOR_BLUE}start ${COLOR_RED}[\$f]${COLOR_WHITE} - executes an extended docker-compose setup with the local file [and given .env file]"
echo -e " ${COLOR_BLUE}stop ${COLOR_RED}[\$f]${COLOR_WHITE} - shutdown for the local docker-compose setup [and given .env file]"
echo -e " ${COLOR_BLUE}db-import${COLOR_WHITE} - import a .sql dump file into a database container"
echo -e " ${COLOR_BLUE}generate${COLOR_WHITE} ${COLOR_RED}[\$dp]${COLOR_WHITE} - creates a docker-compose.proxy.yaml + .env file with linked [\$dp].docker.test"
echo
echo -e " ${COLOR_GREEN}spawn${COLOR_WHITE}: Create or edit proxy containers"
echo -e " ${COLOR_BLUE}status${COLOR_WHITE} - returns details about available and enabled spawns"
echo -e " ${COLOR_BLUE}enable${COLOR_WHITE} - enables a configured container from 'spawns-available' namespace"
echo -e " ${COLOR_BLUE}disable${COLOR_WHITE} - disables a configured container from 'spawns-enabled' namespace"
echo -e " ${COLOR_BLUE}create${COLOR_WHITE} - creates a new spawn file from given input"
echo -e " ${COLOR_BLUE}mailcatcher${COLOR_WHITE} - create a mailcatcher spawn from Skript-Manufaktur"
echo -e " ${COLOR_BLUE}legacy${COLOR_WHITE} - create the legacy containers from first version of the Docker-Proxy-Stack"
echo
echo -e " ${COLOR_GREEN}do${COLOR_WHITE}: Other tools"
echo -e " ${COLOR_BLUE}ps${COLOR_WHITE} - lists all containers like 'docker ps -a'"
echo -e " ${COLOR_BLUE}images${COLOR_WHITE} - lists all local images like 'docker image ls -a'"
echo -e " ${COLOR_BLUE}remove${COLOR_WHITE} - does a 'docker rm' to all containers that have stopped running"
echo -e " ${COLOR_BLUE}cleanup${COLOR_WHITE} - deletes all docker images, that have a <none> as tag name or image id"
echo -e " ${COLOR_BLUE}finish${COLOR_WHITE} - stops and removes all current containers, like 'docker stop && docker rm'"
echo -e " ${COLOR_BLUE}watch-running ${COLOR_RED}\$cn${COLOR_WHITE} - tests, whether a container with name '\$cn' is running and returns 'TRUE' or 'FALSE' as a string"
echo -e " ${COLOR_BLUE}status ${COLOR_RED}[\$fn]${COLOR_WHITE} - creates a text file [\$fn] in current location that contains a list of images and runninge containers on this machine"
echo -e " ${COLOR_BLUE}init-certs${COLOR_WHITE} - create openssl certificates to be registered in your browsers"
echo -e " ${COLOR_BLUE}add-cert${COLOR_WHITE} - create addition openssl certificates for multilevel wildcards"
echo -e " ${COLOR_BLUE}self-update${COLOR_WHITE} - update the Docker Proxy Stack with the latest tag"
echo
exit 0
fi
# test existence of git, docker and docker-compose
command -v git > /dev/null 2>&1 || {
print_error "There was no git executable found on this machine. Please install git to make this project work." 1
exit 1
}
command -v docker > /dev/null 2>&1 || {
print_error "There was no docker executable found on this machine. Did you install docker correctly?" 1
exit 1
}
command -v docker-compose > /dev/null 2>&1 || [[ $(docker compose version) == "Docker Compose version v"* ]] || {
print_error "There was no 'docker-compose' or 'docker compose' executable found on this machine. Did you install docker correctly?"
print_error "If you use a differen alias, please create one called 'docker-compose' for your user or install the latest docker." 1
exit 1
}
# add environment variables for docker
export COMPOSE_DOCKER_CLI_BUILD=0
export DOCKER_BUILDKIT=0
###
### check for updates and execute environment + action
###
check_for_updates
# watch the environment
if [[ "dev" == "$ENVIRONMENT" ]]; then
# let's see, if a file override for .env was given
if [[ 0 != "${#PARAMETERS[@]}" && -n "${PARAMETERS[0]}" ]]; then
ENV_FILE="${PARAMETERS[0]}"
fi
# test project files
if ! COMPOSE=$(test_files "$ENV_FILE" "docker-compose"); then
echo "$COMPOSE"
exit 1
fi
# start the docker-compose
if [[ "start" == "$ACTION" ]]; then
compose_run "$COMPOSE" "$ENV_FILE"
# stop and remove the containers
elif [[ "stop" == "$ACTION" ]]; then
compose_halt "$COMPOSE" "$ENV_FILE"
else
print_error "Unknown option '$ACTION'..." 1
exit 1
fi
elif [[ "prod" == "$ENVIRONMENT" ]]; then
# let's see, if a file override for .env was given
if [[ 0 != "${#PARAMETERS[@]}" && -n "${PARAMETERS[0]}" ]]; then
ENV_FILE="${PARAMETERS[0]}"
fi
# test project files
if ! COMPOSE=$(test_files "$ENV_FILE" "docker-compose.prod"); then
echo "$COMPOSE"
exit 1
fi
# start the docker-compose
if [[ "start" == "$ACTION" ]]; then
compose_run "$COMPOSE" "$ENV_FILE"
# stop and remove the containers
elif [[ "stop" == "$ACTION" ]]; then
update_host_files "$ENV_FILE" "remove"
compose_halt "$COMPOSE" "$ENV_FILE"
else
print_error "Unknown option '$ACTION'..." 1
exit 1
fi
elif [[ "proxy" == "$ENVIRONMENT" ]]; then
COMPOSE="docker-compose.proxy"
# start docker-proxy in proxy path
if [[ "init" == "$ACTION" ]]; then
cd "$BASE_DIR" || (print_error "Could not change directory to $BASE_DIR" && exit 1)
# check the /certs directory
if [[ ! -d "$CERTS_PATH" || ! -f "$CERTS_PATH/docker.test.key" ]]; then
print_warning "The proxy is not fully configured, yet! Try 'DockerExec init-certs' to fix this."
fi
# first, test if the network exists and create, if not
if ! docker network ls | grep -q "$NETWORK_NAME"; then
docker network create --attachable "$NETWORK_NAME"
print_info "Network $NETWORK_NAME was created successfully!"
fi
# ensure the proxy main container is enabled
ensure_proxy_main
# ensure the docker.test domain is located in hosts file
publish_single_entry_hosts_file "docker.test"
# reset the temporary hosts
if [[ -f "$TEMP_HOSTS_PATH" ]]; then
rm "$TEMP_HOSTS_PATH"
fi
touch "$TEMP_HOSTS_PATH"
# shellcheck disable=SC2045
for file in $(ls "$SPAWNS_ENABLED_PATH"); do
start_spawn_container "$file"
done
# add all proxy containers (with their IP and hostname) to current hosts file
update_host_files_with_proxy
publish_host_files "APP"
# if attaching to logs is enabled, use the "{ command1 & command2 }" notation to aggregate docker logs
attach_to_logs
# stop and remove the proxy
elif [[ "finish" == "$ACTION" ]]; then
PROXY_CONTAINERS=$(docker ps -a --format "{{ .Names }}" -f name="proxy-" -f status="running")
if [[ -n "$PROXY_CONTAINERS" ]]; then
print_info "Shutting down proxy..."
for container in $PROXY_CONTAINERS; do
docker stop "$container" > /dev/null
echo " - stopped $container"
done
else
print_info "The Proxy is not started. No containers to stop."
fi
STOPPED_PROXY_CONTAINERS=$(docker ps -a --format "{{ .Names }}" -f name="proxy-" -f status="exited")
if [[ -n "$STOPPED_PROXY_CONTAINERS" ]]; then
print_info "Removing proxy containers..."
for container in $STOPPED_PROXY_CONTAINERS; do
docker rm -f "$container" > /dev/null
echo " - removed $container"
done
fi
# remove the temporary hosts
if [[ -f "$TEMP_HOSTS_PATH" ]]; then
rm "$TEMP_HOSTS_PATH"
fi
# start the docker-compose
elif [[ "start" == "$ACTION" ]]; then
# let's see, if a file override for .env was given
if [[ 0 != "${#PARAMETERS[@]}" && -n "${PARAMETERS[0]}" ]]; then
ENV_FILE="${PARAMETERS[0]}"
fi
# test project files
if ! COMPOSE=$(test_files "$ENV_FILE" "$COMPOSE"); then
echo "$COMPOSE"
exit 1
fi
# test, if the proxy is already running
if [[ 0 == $(is_proxy_running) ]]; then
print_error "The proxy is not running, yet. Initialize with 'DockerExec proxy init'" 1
exit 1
fi
# print out all found hosts and start
print_hosts "$COMPOSE"
compose_run "$COMPOSE" "$ENV_FILE"
# stop and remove the containers
elif [[ "stop" == "$ACTION" ]]; then
# let's see, if a file override for .env was given
if [[ 0 != "${#PARAMETERS[@]}" && -n "${PARAMETERS[0]}" ]]; then
ENV_FILE="${PARAMETERS[0]}"
fi
# test project files
if ! COMPOSE=$(test_files "$ENV_FILE" "$COMPOSE"); then
echo "$COMPOSE"
exit 1
fi
update_host_files "$ENV_FILE" "remove"
compose_halt "$COMPOSE" "$ENV_FILE"
# do a dump import for a database container
elif [[ "db-import" == "$ACTION" || "import" == "$ACTION" || "db" == "$ACTION" ]]; then
import_to_database_container
# generate a docker-compose.proxy.yaml with a basic configuration
elif [[ "generate" == "$ACTION" ]]; then
DOMAIN_PREFIX="${PARAMETERS[0]}"
if [[ -z "$DOMAIN_PREFIX" ]]; then
DOMAIN_PREFIX="my-project"
fi
if [[ ! ( -f "./$COMPOSE.yml" || -f "./$COMPOSE.yaml" ) ]]; then
generate_docker_compose_project "docker-compose.proxy.yaml" "$DOMAIN_PREFIX"
print_info "A docker-compose.proxy.yaml was added."
fi
if [[ ! -f "./$ENV_FILE" ]]; then
generate_env_file_project "$ENV_FILE" "$DOMAIN_PREFIX"
print_info "A $ENV_FILE was added."
fi
else
print_error "Unknown option '$ACTION'..." 1
exit 1
fi
elif [[ "spawn" == "$ENVIRONMENT" ]]; then
# print spawn details
if [[ "status" == "$ACTION" ]]; then
print_info "Following spawn containers are available:"
find "$SPAWNS_AVAIL_PATH" -maxdepth 1 -type f,l -exec basename {} \; | sort | awk '{print " - "$1}'
echo
print_info "Following spawn containers are enabled in order:"
find "$SPAWNS_ENABLED_PATH" -maxdepth 1 -type f,l -exec basename {} \; | sort | awk '{print " - "$1}'
echo
# link an existing spawn from available to enabled
elif [[ "enable" == "$ACTION" ]]; then
enable_spawn_container "${PARAMETERS[0]}" "${PARAMETERS[1]}"
# or remove the link from enabled directory
elif [[ "disable" == "$ACTION" ]]; then
disable_spawn_container "${PARAMETERS[0]}"
# create a new spawn file in available spawns directory
elif [[ "create" == "$ACTION" ]]; then
spawn_container_from_questions
# spawn a container for mailcatching
elif [[ "mailcatcher" == "$ACTION" ]]; then
spawn_smt_mailcatcher
# ask and spawn the containers related to Docker-Proxy-Stack v1
elif [[ "legacy" == "$ACTION" ]]; then
spawn_legacy_proxy_stack
fi
elif [[ "do" == "$ENVIRONMENT" ]]; then
# stop, then remove all containers
if [[ "finish" == "$ACTION" ]]; then
REMOVABLE_CONTAINERS=$(docker ps -aq)
if [[ -n "$REMOVABLE_CONTAINERS" ]]; then
print_info "Shutting down..."
# shellcheck disable=SC2086
docker stop $REMOVABLE_CONTAINERS > /dev/null
# shellcheck disable=SC2086
docker rm -f $REMOVABLE_CONTAINERS > /dev/null
else
print_info "No containers to stop."
fi
if [[ -f "$TEMP_HOSTS_PATH" ]]; then
rm "$TEMP_HOSTS_PATH"
fi
# remove all stopped containers
elif [[ "remove" == "$ACTION" ]]; then
EXITED_CONTAINERS=$(docker ps -aq -f status="exited")
if [[ -n "$EXITED_CONTAINERS" ]]; then
# shellcheck disable=SC2086
docker rm -f $EXITED_CONTAINERS
else
print_info "No containers to remove."
fi
# remove all images that are no more in use
elif [[ "cleanup" == "$ACTION" ]]; then
CLEANABLE_CONTAINERS=$(docker images --filter "dangling=true" -q)
if [[ -n "$CLEANABLE_CONTAINERS" ]]; then
# shellcheck disable=SC2086
docker rmi -f $CLEANABLE_CONTAINERS
else
print_info "No unfinished build layers to remove."
fi
# test, if a container is running
elif [[ "watch-running" == "$ACTION" || "watchrunning" == "$ACTION" || "watch" == "$ACTION" ]]; then
CONTAINER="${PARAMETERS[0]}"
if [[ -z "$CONTAINER" ]]; then
print_error "To test a container, please provide its name!" 1
exit 1
fi
STATE=$(docker ps -a --format "table {{.Status}}\t{{.Names}}" | grep "$CONTAINER")
if [[ $STATE == *"Up "* ]]; then
echo "TRUE"
else
echo "FALSE"
fi
# print the current state of Docker into a file
elif [[ "status" == "$ACTION" ]]; then
# let's see, if a file name was given
REAL_STATUS_FILE="$STATUS_FILE_NAME"
if [[ 0 != "${#PARAMETERS[@]}" ]]; then
REAL_STATUS_FILE="${PARAMETERS[0]}"
fi
# create the full path and ask for confirmation
STATUS_PATH="$CURRENT_DIR/$REAL_STATUS_FILE"
read -r -p "A file $REAL_STATUS_FILE will be created (or override existing). Continue? [y/n]" input
case $input in
[yY][eE][sS]|[yY])
# remove the existing file
if [[ -f "$STATUS_PATH" ]]; then
rm -f "$STATUS_PATH"
fi
# print out status info into a static file
echo "### Currently running containers" >> "$STATUS_PATH"
echo "" >> "$STATUS_PATH"
docker ps -a --format "table {{.Image}}\t{{.Status}}\t{{.Ports}}\t{{.Names}}" >> "$STATUS_PATH"
echo "" >> "$STATUS_PATH"
echo "" >> "$STATUS_PATH"
echo "### Currently existing images" >> "$STATUS_PATH"
echo "" >> "$STATUS_PATH"
docker images --format "table {{.ID}}\t{{.Repository}}\t{{.Tag}}\t{{.Size}}" >> "$STATUS_PATH"
echo "" >> "$STATUS_PATH"
;;
*)
print_error "Canceled..." 1
;;
esac
# use prompts to generate certificates for browser usage
# see https://gist.github.com/fntlnz/cf14feb5a46b2eda428e000157447309
# see https://two-oes.medium.com/working-with-openssl-and-dns-alternative-names-367f06a23841
# see https://www.openssl.org/docs/manmaster/man5/x509v3_config.html
# to test a .crt file, try: openssl x509 -in certs/docker.test.crt -text -noout
elif [[ "init-certs" == "$ACTION" || "initcerts" == "$ACTION" ]]; then
command -v openssl > /dev/null 2>&1 || {
print_error "Openssl seems not to be installed on this machine. But it is necessary to generate keys..." 1
exit 1
}
# create the certs path, if not done, yet
if [[ ! -d "$CERTS_PATH" ]]; then
mkdir -p "$CERTS_PATH"
fi
# some security info
print_info "Attention: This is the key used to sign the certificate requests. Anyone holding this can sign certificates on your behalf. So keep it safe!"
# ask the user for some details on the certificate
RSA_KEY_LENGTH=$(match_answer_or_default "Define a key size [4096]: " 4096)
RSA_VALID_DAYS=$(match_answer_or_default "How long shall this certificate be valid (in days) [3650]: " 3650)
RSA_COUNTRY_NAME=$(match_answer_or_default "Country Name (2 letter code) [PS]: " "PS")
RSA_STATE_NAME=$(match_answer_or_default "State or Province Name (full name) [Proxy Environment]: " "Proxy Environment")
RSA_LOCALITY_NAME=$(match_answer_or_default "Locality Name (some city) [Proxy]: " "Proxy")
RSA_ORG_NAME=$(match_answer_or_default "Organization Name (some company) [Docker Proxy Stack]: " "Docker Proxy Stack")
RSA_ORGUNIT_NAME=$(match_answer_or_default "Organizational Unit Name (a section) [Docker Proxy Stack]: " "Docker Proxy Stack")
RSA_EMAIL=$(match_answer_or_default "E-Mail Address []: " "")
generate_openssl_certs "$RSA_KEY_LENGTH" "$RSA_VALID_DAYS" "$RSA_COUNTRY_NAME" "$RSA_STATE_NAME" \
"$RSA_LOCALITY_NAME" "$RSA_ORG_NAME" "$RSA_ORGUNIT_NAME" "$RSA_EMAIL" ""
print_info "Done creating rootCA and certificates in $CERTS_PATH."
print_info "Now register the rootCA.crt in your browser." 1
elif [[ "add-cert" == "$ACTION" || "addcert" == "$ACTION" ]]; then
command -v openssl > /dev/null 2>&1 || {
print_error "Openssl seems not to be installed on this machine. But it is necessary to generate keys..." 1
exit 1
}
# create the certs path, if not done, yet
if [[ ! -d "$CERTS_PATH" ]]; then
mkdir -p "$CERTS_PATH"
fi
print_info "You are about to create a multilevel wildcard cert. Please answer following questions:"
# ask the user for some details on the certificate
RSA_KEY_LENGTH=$(match_answer_or_default "Define a key size [4096]: " 4096)
RSA_VALID_DAYS=$(match_answer_or_default "How long shall this certificate be valid (in days) [3650]: " 3650)
RSA_COUNTRY_NAME=$(match_answer_or_default "Country Name (2 letter code) [PS]: " "PS")
RSA_STATE_NAME=$(match_answer_or_default "State or Province Name (full name) [Proxy Environment]: " "Proxy Environment")
RSA_LOCALITY_NAME=$(match_answer_or_default "Locality Name (some city) [Proxy]: " "Proxy")
RSA_ORG_NAME=$(match_answer_or_default "Organization Name (some company) [Docker Proxy Stack]: " "Docker Proxy Stack")
RSA_ORGUNIT_NAME=$(match_answer_or_default "Organizational Unit Name (a section) [Docker Proxy Stack]: " "Docker Proxy Stack")
RSA_EMAIL=$(match_answer_or_default "E-Mail Address []: " "")
RSA_SUBDOMAIN=$(match_answer_or_default "Additional subdomain, e.g. 'example' to create '*.example.docker.test' []: " "")
if [[ -z "$RSA_SUBDOMAIN" ]]; then
print_error "You need to provide a subdomain to create a multilevel wildcard cert!" 1
exit 1
fi
generate_openssl_certs "$RSA_KEY_LENGTH" "$RSA_VALID_DAYS" "$RSA_COUNTRY_NAME" "$RSA_STATE_NAME" \
"$RSA_LOCALITY_NAME" "$RSA_ORG_NAME" "$RSA_ORGUNIT_NAME" "$RSA_EMAIL" \
"$RSA_SUBDOMAIN"
print_info "Done creating rootCA and certificates in $CERTS_PATH."
print_info "Now register the rootCA.crt in your browser." 1
# shortcut to list all docker containers
elif [[ "self-update" == "$ACTION" || "selfupdate" == "$ACTION" ]]; then
# get_latest_git_tag will NOT cd us into the $BASE_DIR directory
LATEST_TAG=$(get_latest_git_tag)
cd "$BASE_DIR" || (print_error "Could not change directory to $BASE_DIR" && exit 1)
git checkout --quiet "$LATEST_TAG"
print_info "Updated the Docker Proxy Stack. Checked out version $LATEST_TAG." 1
# shortcut to list all docker containers
elif [[ "ps" == "$ACTION" ]]; then
PS_COLUMNS=${DOCKER_PS_COLUMNS:-Image|Status|Ports|Names}
PS_COLUMNS=${PS_COLUMNS//|/ }
PS_COLUMNS_FORMATTED=$(join_by '}}\t{{.' $PS_COLUMNS)
PS_FORMAT="table {{.$PS_COLUMNS_FORMATTED}}"
docker ps -a --format "$PS_FORMAT"
elif [[ "images" == "$ACTION" ]]; then
IMAGES_COLUMNS=${DOCKER_IMAGES_COLUMNS:-Repository|Tag|Size|CreatedSince}
IMAGES_COLUMNS=${IMAGES_COLUMNS//|/ }
IMAGES_COLUMNS_FORMATTED=$(join_by '}}\t{{.' $IMAGES_COLUMNS)
IMAGES_FORMAT="table {{.$IMAGES_COLUMNS_FORMATTED}}"
docker images --all --format "$IMAGES_FORMAT"
else
print_error "Unknown option '$ACTION'..." 1
exit 1
fi
else
print_error "There is no environment named '$ENVIRONMENT'..." 1
exit 1
fi
exit 0