-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnge
More file actions
executable file
·640 lines (530 loc) · 18.2 KB
/
nge
File metadata and controls
executable file
·640 lines (530 loc) · 18.2 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
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
#!/bin/bash
set -Eeo pipefail
set -o errexit # Used to exit upon error, avoiding cascading errors
IFS=$'\n\t'
RED="31"
GREEN="32"
YELLOW="33"
BOLDGREEN="\e[1;${GREEN}m"
ITALICRED="\e[3;${RED}m"
BOLDYELLOW="\e[1;${YELLOW}m"
NC="\e[0m"
# read .env file
set -o allexport
source .env set
set +o allexport
DOCKER_USER="${DOCKER_USER:-appuser}"
DOCKER_PUID="${DOCKER_PUID:-1000}"
DOCKER_PGID="${DOCKER_PGID:-1000}"
ENABLE_WEB="${ENABLE_WEB:-true}"
ENABLE_HORIZON="${ENABLE_HORIZON:-true}"
ENABLE_SCHEDULER="${ENABLE_SCHEDULER:-true}"
ENABLE_PATCH="${ENABLE_PATCH:-false}"
MYSQL_CLI_IMAGE="bitnami/mysql-client:latest"
POSTGRES_CLI_IMAGE="bitnami/postgresql-client:latest"
REDIS_CLI_IMAGE="bitnami/redis-cli:latest"
NGE_NONINTERACTIVE="${NGE_NONINTERACTIVE:-false}"
COMPOSE_WAIT_TIMEOUT="${COMPOSE_WAIT_TIMEOUT:-120}"
# usage: file_env VAR [DEFAULT]
# ie: file_env 'XYZ_DB_PASSWORD' 'example'
# (will allow for "$XYZ_DB_PASSWORD_FILE" to fill in the value of
# "$XYZ_DB_PASSWORD" from a file, especially for Docker's secrets feature)
file_env() {
local var="$1"
local fileVar="${var}_FILE"
local def="${2:-}"
if [ "${!var:-}" ] && [ "${!fileVar:-}" ]; then
echo >&2 "error: both $var and $fileVar are set (but are exclusive)"
exit 1
fi
local val="$def"
if [ "${!var:-}" ]; then
val="${!var}"
elif [ "${!fileVar:-}" ]; then
val="$(< "${!fileVar}")"
fi
export "$var"="$val"
unset "$fileVar"
}
install() {
echo "installing in 5 seconds"
sleep 5
compose:pull
start
}
start() {
local active=$(get_active_color)
local idle=$(get_idle_color)
printf "${BOLDGREEN}→${NC} Starting application on active service: \e[${GREEN}m$active${NC}\n"
local services=()
if [ "$ENABLE_WEB" = "true" ]; then
services+=("gateway" "$active")
fi
if [ "$ENABLE_HORIZON" = "true" ]; then
services+=("horizon")
fi
if [ "$ENABLE_SCHEDULER" = "true" ]; then
services+=("scheduler")
fi
# Start specific services
if [ ${#services[@]} -gt 0 ]; then
compose:up:detached "${services[@]}"
fi
reconcile_worker_services
# Ensure idle service is stopped (only if web is enabled)
if [ "$ENABLE_WEB" = "true" ]; then
printf "${BOLDYELLOW}→${NC} Ensuring idle service ($idle) is stopped...\n"
compose stop $idle
fi
}
compose() {
printf "${BOLDYELLOW}→${NC} run docker compose\n"
local compose_args=()
if [ "$NGE_NONINTERACTIVE" = "true" ]; then
compose_args+=(--ansi never --progress plain)
fi
docker compose "${compose_args[@]}" "$@"
}
_compose() {
local compose_args=()
if [ "$NGE_NONINTERACTIVE" = "true" ]; then
compose_args+=(--ansi never --progress plain)
fi
docker compose "${compose_args[@]}" "$@"
}
compose:pull() {
if [ "$NGE_NONINTERACTIVE" = "true" ]; then
printf "${BOLDYELLOW}→${NC} run docker compose\n"
docker compose --ansi never --progress plain pull -q "$@"
return
fi
compose pull "$@"
}
compose:up:detached() {
local args=(-d "$@")
if [ "$NGE_NONINTERACTIVE" = "true" ]; then
args=(-d --quiet-pull --wait --wait-timeout "$COMPOSE_WAIT_TIMEOUT" "$@")
fi
compose up "${args[@]}"
}
reconcile_worker_services() {
local disabled=()
if [ "$ENABLE_HORIZON" != "true" ]; then disabled+=("horizon"); fi
if [ "$ENABLE_SCHEDULER" != "true" ]; then disabled+=("scheduler"); fi
if [ ${#disabled[@]} -eq 0 ]; then
return
fi
printf "${BOLDYELLOW}→${NC} Ensuring disabled worker services are stopped: %s\n" "${disabled[*]}"
compose rm --stop --force "${disabled[@]}"
}
get_active_color() {
# Ensure upstream config exists, default to blue
if [ ! -f config/gateway/upstream.conf ]; then
mkdir -p config/gateway
printf "upstream core {\n server core-blue:80;\n keepalive 64;\n}\n" > config/gateway/upstream.conf
fi
if grep -q "core-blue" config/gateway/upstream.conf; then
echo "core-blue"
else
echo "core-green"
fi
}
get_idle_color() {
if [ "$(get_active_color)" == "core-blue" ]; then
echo "core-green"
else
echo "core-blue"
fi
}
composer() {
local service=$(get_active_color)
printf "${BOLDGREEN}→${NC} run composer on user : ${DOCKER_USER} (service: $service)\n"
compose run --rm --user=${DOCKER_USER} $service composer $@
}
shell() {
local service=$(get_active_color)
printf "${BOLDGREEN}→${NC} run shell on user : ${DOCKER_USER} (service: $service)\n"
compose run --rm --user=${DOCKER_USER} $service bash
}
php() {
local service=$(get_active_color)
printf "${BOLDGREEN}→${NC} run php on user : ${DOCKER_USER} (service: $service)\n"
compose run --rm --user=${DOCKER_USER} $service php $@
}
artisan() {
local service=$(get_active_color)
printf "${BOLDGREEN}→${NC} run artisan on user : ${DOCKER_USER} (service: $service)\n"
# docker compose exec -e HOME=/home/${DOCKER_USER:-dokar} core runuser --preserve-environment -u ${DOCKER_USER:-dokar} /usr/bin/php /var/www/artisan $@
php artisan $@
}
a() {
artisan $@
}
up() {
echo "Nge up service $@..."
compose:up:detached "$@"
}
down() {
echo "Nge down all service..."
compose rm --stop --force horizon scheduler
compose down
}
check() {
compose ps
}
code:update() {
printf "${BOLDYELLOW}Does this update require downtime? (y/N)${NC} "
read -r response
if [[ "$response" =~ ^([yY][eE][sS]|[yY])$ ]]; then
update:downtime
else
update:zero_downtime
fi
}
update:downtime() {
local active=$(get_active_color)
printf "${BOLDYELLOW}→${NC} Update with downtime on $active...\n"
compose:pull "$active" artisan
artisan down
# Recreate the active container
compose:up:detached --force-recreate "$active"
# Run migrations
compose run --rm --user=${DOCKER_USER} artisan php artisan migrate --force
if [ "$ENABLE_PATCH" = "true" ]; then
compose run --rm --user=${DOCKER_USER} artisan php artisan patcher:run --force
fi
# Restart workers
local workers=()
if [ "$ENABLE_HORIZON" = "true" ]; then workers+=("horizon"); fi
if [ "$ENABLE_SCHEDULER" = "true" ]; then workers+=("scheduler"); fi
if [ ${#workers[@]} -gt 0 ]; then
compose:pull "${workers[@]}"
compose:up:detached "${workers[@]}"
fi
reconcile_worker_services
artisan up
if [ "$ENABLE_WEB" = "true" ]; then
compose restart gateway
fi
}
update:zero_downtime() {
local active=$(get_active_color)
local idle=$(get_idle_color)
printf "${BOLDYELLOW}→${NC} Zero-downtime update initiated.\n"
printf "Active: \e[${GREEN}m$active${NC}, Target: \e[${GREEN}m$idle${NC}\n"
# 1. Pull and Start Idle
if [ "$ENABLE_WEB" = "true" ]; then
printf "${BOLDYELLOW}→${NC} Pulling and starting $idle...\n"
compose:pull "$idle" artisan
printf "${BOLDYELLOW}→${NC} Waiting for $idle to be ready...\n"
compose:up:detached "$idle"
fi
# 3. Run Migrations (on the NEW code)
printf "${BOLDYELLOW}→${NC} Running migrations...\n"
compose run --rm --user=${DOCKER_USER} artisan php artisan migrate --force
if [ "$ENABLE_PATCH" = "true" ]; then
compose run --rm --user=${DOCKER_USER} artisan php artisan patcher:run --force
fi
# 4. Switch Traffic
if [ "$ENABLE_WEB" = "true" ]; then
printf "${BOLDYELLOW}→${NC} Switching traffic to $idle...\n"
printf "upstream core {\n server $idle:80;\n keepalive 64;\n}\n" > config/gateway/upstream.conf
_compose exec -T gateway nginx -s reload
fi
# 5. Restart Workers
local workers=()
if [ "$ENABLE_HORIZON" = "true" ]; then workers+=("horizon"); fi
if [ "$ENABLE_SCHEDULER" = "true" ]; then workers+=("scheduler"); fi
if [ ${#workers[@]} -gt 0 ]; then
compose:pull "${workers[@]}"
compose:up:detached "${workers[@]}"
fi
reconcile_worker_services
# 6. Stop Old Active
if [ "$ENABLE_WEB" = "true" ]; then
printf "${BOLDYELLOW}→${NC} Stopping old service $active...\n"
compose stop $active
fi
if [ "$ENABLE_WEB" = "true" ]; then
printf "${BOLDGREEN}→${NC} Update complete! Now running on $idle.\n"
else
printf "${BOLDGREEN}→${NC} Update complete! Workers updated.\n"
fi
}
status() {
compose ps --format "table {{.Name}}\t{{.State}}\t{{.Ports}}"
}
code:reload() {
local active=$(get_active_color)
artisan down
local services=()
if [ "$ENABLE_WEB" = "true" ]; then services+=("$active"); fi
if [ "$ENABLE_HORIZON" = "true" ]; then services+=("horizon"); fi
if [ "$ENABLE_SCHEDULER" = "true" ]; then services+=("scheduler"); fi
if [ ${#services[@]} -gt 0 ]; then
compose restart "${services[@]}"
fi
reconcile_worker_services
artisan up
if [ "$ENABLE_WEB" = "true" ]; then
compose restart gateway
fi
}
image:upgrade() {
compose:up:detached --force-recreate --remove-orphans
local workers=()
if [ "$ENABLE_HORIZON" = "true" ]; then workers+=("horizon"); fi
if [ "$ENABLE_SCHEDULER" = "true" ]; then workers+=("scheduler"); fi
if [ ${#workers[@]} -gt 0 ]; then
compose:up:detached --force-recreate "${workers[@]}"
fi
reconcile_worker_services
}
redis:cli() {
docker run --rm -it \
--network host \
${REDIS_CLI_IMAGE} \
redis-cli -h ${REDIS_HOST:-127.0.0.1} -p ${REDIS_PORT:-6379} ${REDIS_PASSWORD:+-a ${REDIS_PASSWORD}}
}
mysql:cli() {
db:cli
}
db:cli() {
case "${DB_CONNECTION}" in
mysql|mariadb|"")
docker run --rm -it \
--network host \
${MYSQL_CLI_IMAGE} \
mysql -h ${DB_HOST:-127.0.0.1} -P ${DB_PORT:-3306} -u ${DB_USERNAME:-root} -p${DB_PASSWORD} ${DB_DATABASE}
;;
pgsql|postgres|postgresql)
docker run --rm -it \
--network host \
-e PGPASSWORD=${DB_PASSWORD} \
${POSTGRES_CLI_IMAGE} \
psql -h ${DB_HOST:-127.0.0.1} -p ${DB_PORT:-5432} -U ${DB_USERNAME} -d ${DB_DATABASE}
;;
*)
printf "${ITALICRED}Error: Unsupported DB_CONNECTION '${DB_CONNECTION}'.${NC}\n"
exit 1
;;
esac
}
mysql:cli() {
db:cli
}
db:import() {
if [ -z "$1" ]; then
echo "Please provide a filename to import."
echo "Usage: db:import filename.sql[.gz] [--db=database_name] [additional args]"
exit 1
fi
local file="$1"
shift # Remove filename from arguments
# Check if the file exists
if [ ! -f "$file" ]; then
printf "${ITALICRED}Error: File '$file' not found.${NC}\n"
exit 1
fi
# Parse arguments for database name
local database="${DB_DATABASE}"
local db_args=()
for arg in "$@"; do
if [[ "$arg" == --db=* ]]; then
database="${arg#--db=}"
else
db_args+=("$arg")
fi
done
printf "${BOLDGREEN}→${NC} Importing to database ${database}\n"
# Check file extension to determine if it's compressed
if [[ "$file" == *.gz ]]; then
printf "${BOLDYELLOW}→${NC} Detected gzip compressed file\n"
# Create a named pipe for streaming
local pipe=$(mktemp -u)
mkfifo "$pipe"
# Start decompression in background
gzip -dc "$file" > "$pipe" &
decompress_pid=$!
printf "${BOLDYELLOW}→${NC} Starting import (this might take a while)...\n"
# Import from the pipe
case "${DB_CONNECTION}" in
mysql|mariadb|"")
docker run --rm -i \
--network host \
${MYSQL_CLI_IMAGE} \
mysql -h ${DB_HOST:-127.0.0.1} -P ${DB_PORT:-3306} -u ${DB_USERNAME:-root} -p${DB_PASSWORD} "$database" "${db_args[@]}" < "$pipe"
;;
pgsql|postgres|postgresql)
docker run --rm -i \
--network host \
-e PGPASSWORD=${DB_PASSWORD} \
${POSTGRES_CLI_IMAGE} \
psql -h ${DB_HOST:-127.0.0.1} -p ${DB_PORT:-5432} -U ${DB_USERNAME} -d "$database" "${db_args[@]}" < "$pipe"
;;
*)
printf "${ITALICRED}Error: Unsupported DB_CONNECTION '${DB_CONNECTION}'.${NC}\n"
rm "$pipe"
exit 1
;;
esac
import_result=$?
# Wait for decompression to finish and clean up
wait $decompress_pid
rm "$pipe"
else
printf "${BOLDYELLOW}→${NC} Starting import from SQL file...\n"
case "${DB_CONNECTION}" in
mysql|mariadb|"")
docker run --rm -i \
--network host \
${MYSQL_CLI_IMAGE} \
mysql -h ${DB_HOST:-127.0.0.1} -P ${DB_PORT:-3306} -u ${DB_USERNAME:-root} -p${DB_PASSWORD} "$database" "${db_args[@]}" < "$file"
;;
pgsql|postgres|postgresql)
docker run --rm -i \
--network host \
-e PGPASSWORD=${DB_PASSWORD} \
${POSTGRES_CLI_IMAGE} \
psql -h ${DB_HOST:-127.0.0.1} -p ${DB_PORT:-5432} -U ${DB_USERNAME} -d "$database" "${db_args[@]}" < "$file"
;;
*)
printf "${ITALICRED}Error: Unsupported DB_CONNECTION '${DB_CONNECTION}'.${NC}\n"
exit 1
;;
esac
import_result=$?
fi
# Check the result
if [ $import_result -eq 0 ]; then
printf "${BOLDGREEN}→${NC} Import completed successfully!\n"
else
printf "${ITALICRED}Error: Import failed with code $import_result${NC}\n"
exit $import_result
fi
}
mysql:import() {
db:import "$@"
}
postgres:import() {
DB_CONNECTION=pgsql db:import "$@"
}
db:export() {
if [ -z "$1" ]; then
echo "Please provide a filename prefix for the export."
echo "Usage: db:export filename_prefix [--db=database_name] [additional args]"
exit 1
fi
# file name add timestamp
local timestamp=$(date +"%Y%m%d_%H%M%S")
local filename="${1}_${timestamp}.sql.gz"
# Parse arguments for database name
local database="${DB_DATABASE}"
local dump_args=()
shift # Remove filename prefix from arguments
for arg in "$@"; do
if [[ "$arg" == --db=* ]]; then
database="${arg#--db=}"
else
dump_args+=("$arg")
fi
done
printf "${BOLDGREEN}→${NC} run database export on ${database}\n"
printf "${BOLDGREEN}→${NC} export to file $filename\n"
# check gzip exists
if ! command -v gzip &> /dev/null; then
echo "gzip could not be found. Please install gzip."
exit 1
fi
local temp_file=$(mktemp)
printf "${BOLDYELLOW}→${NC} Starting database export"
case "${DB_CONNECTION}" in
mysql|mariadb|"")
if [ ${#dump_args[@]} -eq 0 ]; then
dump_args=(--skip-lock-tables --skip-add-locks --no-tablespaces)
fi
docker run --rm \
--network host \
${MYSQL_CLI_IMAGE} \
mysqldump -h ${DB_HOST:-127.0.0.1} -P ${DB_PORT:-3306} -u ${DB_USERNAME:-root} -p${DB_PASSWORD} "${dump_args[@]}" "$database" > "$temp_file" &
;;
pgsql|postgres|postgresql)
docker run --rm \
--network host \
-e PGPASSWORD=${DB_PASSWORD} \
${POSTGRES_CLI_IMAGE} \
pg_dump -h ${DB_HOST:-127.0.0.1} -p ${DB_PORT:-5432} -U ${DB_USERNAME} "${dump_args[@]}" "$database" > "$temp_file" &
;;
*)
printf "${ITALICRED}Error: Unsupported DB_CONNECTION '${DB_CONNECTION}'.${NC}\n"
rm -f "$temp_file"
exit 1
;;
esac
export_pid=$!
# Show loading animation
spin='-\|/'
i=0
while kill -0 $export_pid 2>/dev/null; do
i=$(( (i+1) % 4 ))
printf "\r${BOLDYELLOW}→${NC} Exporting database... ${spin:$i:1}"
sleep .1
done
printf "\r${BOLDGREEN}→${NC} Database export completed! \n"
# Check if the file is empty
if [ -s "$temp_file" ]; then
printf "${BOLDYELLOW}→${NC} Compressing export file..."
cat "$temp_file" | gzip > "$filename"
printf "\r${BOLDGREEN}→${NC} Export completed: $filename\n"
else
printf "${ITALICRED}Error: export produced no output. Database might be empty or an error occurred.${NC}\n"
rm -f "$temp_file"
exit 1
fi
# Clean up
rm -f "$temp_file"
}
mysql:export() {
db:export "$@"
}
postgres:export() {
DB_CONNECTION=pgsql db:export "$@"
}
help() {
echo "Usage: $0 [COMMAND]"
echo "Available commands:"
echo " install Installs and starts the containers"
echo " start Starts the app respecting blue/green state"
echo " compose Wraps docker compose commands"
echo " composer Runs composer inside the core container"
echo " shell Opens a bash shell in the core container"
echo " php Runs a PHP command inside the core container"
echo " artisan, a Shortcut for artisan commands"
echo " up Starts specified service(s)"
echo " down Stops all services"
echo " check Checks docker-compose status"
echo " status Prints docker-compose status in a table"
echo " code:update Update core image, reload code, then run migrate and patch"
echo " code:reload Puts app in maintenance mode and restarts services"
echo " image:upgrade Force-recreates all (nginx,mysql,redis,...) containers using latest images"
echo " redis:cli Opens Redis CLI"
echo " db:cli Opens database CLI (MySQL/Postgres)"
echo " mysql:cli Alias for db:cli"
echo " db:import Imports SQL into MySQL or Postgres"
echo " mysql:import Alias for db:import"
echo " postgres:import Import SQL into Postgres"
echo " db:export Export SQL from MySQL or Postgres"
echo " mysql:export Alias for db:export"
echo " postgres:export Export SQL from Postgres"
echo
}
# Check if the function exists (bash specific)
if declare -f "$1" > /dev/null
then
# call arguments verbatim
"$@"
else
# Show a helpful error
echo "'$1' is not a known function name. See '$0 help' for usage."
exit 1
fi