forked from linuxfoundation/crowd.dev
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcli
More file actions
executable file
·999 lines (858 loc) · 27.5 KB
/
Copy pathcli
File metadata and controls
executable file
·999 lines (858 loc) · 27.5 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
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
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
#!/usr/bin/env bash
set -eo pipefail
PROJECT_NAME="crowd"
DOCKER_NETWORK_SUBNET="${CROWD_NETWORK_SUBNET:-10.90.0.0/24}"
DOCKER_NETWORK_GATEWAY="${CROWD_NETWORK_GATEWAY:-10.90.0.1}"
DOCKET_TEST_NETWORK_SUBNET="${CROWD_TEST_NETWORK_SUBNET:-10.91.0.0/24}"
DOCKER_TEST_NETWORK_GATEWAY="${CROWD_TEST_NETWORK_GATEWAY:-10.91.0.1}"
CLI_HOME="$(cd "$(dirname "${BASH_SOURCE[0]}")" >/dev/null 2>&1 && pwd)"
source $CLI_HOME/utils
source $CLI_HOME/scaffold/kafka-connect/build-docker-image.sh
CONTAINERS=$(ls -p $CLI_HOME/services | grep -v / | sed 's/\.[^.]*$//')
BUILDERS=$(ls -p $CLI_HOME/builders | grep -v / | sed 's/\.[^.]*$//')
DOCKER_COMPOSE_PROFILE=''
DOCKER_COMPOSE_SCAFFOLD_FILES="-f $CLI_HOME/scaffold.yaml"
if [[ -n "${WITH_NGINX}" ]]; then
DOCKER_COMPOSE_PROFILE='--profile nginx'
elif [[ "$WITH_INSIGHTS" == "1" ]]; then
DOCKER_COMPOSE_PROFILE='--profile insights'
DOCKER_COMPOSE_SCAFFOLD_FILES="-f $CLI_HOME/scaffold.yaml -f $CLI_HOME/scaffold.insights.yaml"
fi
_DC="docker compose"
arch=$(uname -m)
if [ "$arch" == 'arm64' ]; then
DOCKER_PLATFORM_FLAGS="--platform=linux/arm64/v8"
else
DOCKER_PLATFORM_FLAGS="--platform=linux/amd64"
fi
function prepare_dev_env() {
if [[ ! -f "$CLI_HOME/../backend/.env.override.local" ]]; then
echo "# Here you can put environment variables that you would like to override when using local environment" >"$CLI_HOME/../backend/.env.override.local"
fi
if [[ ! -f "$CLI_HOME/../backend/.env.override.composed" ]]; then
echo "# Here you can put environment variables that you would like to override when using local environment" >"$CLI_HOME/../backend/.env.override.composed"
fi
if [[ ! -f "$CLI_HOME/../frontend/.env.override.local" ]]; then
echo "# Here you can put environment variables that you would like to override when using local environment" >"$CLI_HOME/../frontend/.env.override.local"
fi
if [[ ! -f "$CLI_HOME/../frontend/.env.override.composed" ]]; then
echo "# Here you can put environment variables that you would like to override when using local environment" >"$CLI_HOME/../frontend/.env.override.composed"
fi
set +eo pipefail
$_DC >>/dev/null 2>&1
if [[ ! $? -eq 0 ]]; then
_DC="docker-compose"
$_DC >>/dev/null 2>&1
if [[ ! $? -eq 0 ]]; then
error "Docker compose not detected!"
exit 1
fi
fi
set -eo pipefail
mkdir -p $CLI_HOME/scaffold/kafka-connect/tmp/kafka-connect-http
mkdir -p $CLI_HOME/scaffold/kafka-connect/tmp/questdb-connector
}
prepare_dev_env
function prepare_service_string() {
if [ $1 == "HELP" ]; then
delimiter1=" | "
delimiter2=" => | "
fi
if [ $1 == "LIST" ]; then
delimiter1="\|"
delimiter2="\|"
fi
for cmd in $SERVICE_CMD; do string+="$cmd$delimiter1"; done
for container in $2; do string+="$container$delimiter2"; done
echo ${string%??}
}
HELP_STRING=$(prepare_service_string HELP "$CONTAINERS")
LIST_STRING=$(prepare_service_string LIST "$CONTAINERS")
HELP_BUILD_STRING=$(prepare_service_string HELP "$BUILDERS")
function multiselect {
# little helpers for terminal print control and key input
ESC=$(printf "\033")
cursor_blink_on() { printf "$ESC[?25h"; }
cursor_blink_off() { printf "$ESC[?25l"; }
cursor_to() { printf "$ESC[$1;${2:-1}H"; }
print_inactive() { printf "$2 $1 "; }
print_active() { printf "$2 $ESC[7m $1 $ESC[27m"; }
get_cursor_row() {
IFS=';' read -sdR -p $'\E[6n' ROW COL
echo ${ROW#*[}
}
local return_value=$1
local options=("${!2}")
local defaults=()
local selected=()
for ((i = 0; i < ${#options[@]}; i++)); do
if [[ ${defaults[i]} == "true" ]]; then
selected+=("true")
else
selected+=("false")
fi
printf "\n"
done
local lastrow=$(get_cursor_row)
local startrow=$(($lastrow - ${#options[@]}))
trap "cursor_blink_on; stty echo; printf '\n'; exit" 2
cursor_blink_off
key_input() {
local key
IFS= read -rsn1 key
if [[ -z $key ]]; then
echo enter
return
fi # Handle Enter key (when key is a newline)
case "$key" in
$'\x20') echo space ;; # Space key
k) echo up ;; # 'k' for up
j) echo down ;; # 'j' for down
$'\x1b') # Handle arrow keys
read -rsn2 key
if [[ $key == "[A" ]]; then echo up; fi # Up arrow
if [[ $key == "[B" ]]; then echo down; fi # Down arrow
;;
*) echo "unknown" ;; # Handle other keys
esac
}
toggle_option() {
local option=$1
if [[ ${selected[option]} == true ]]; then
selected[option]=false
else
selected[option]=true
fi
}
print_options() {
local idx=0
for option in "${options[@]}"; do
local prefix="[ ]"
if [[ ${selected[idx]} == true ]]; then
prefix="[\e[38;5;46m✔\e[0m]"
fi
cursor_to $(($startrow + $idx))
if [ $idx -eq $1 ]; then
print_active "$option" "$prefix"
else
print_inactive "$option" "$prefix"
fi
((idx++))
done
}
local active=0
while true; do
print_options $active
case $(key_input) in
space) toggle_option $active ;;
enter)
print_options -1
break
;;
up)
((active--))
if [ $active -lt 0 ]; then active=$((${#options[@]} - 1)); fi
;;
down)
((active++))
if [ $active -ge ${#options[@]} ]; then active=0; fi
;;
esac
done
cursor_to $lastrow
printf "\n"
cursor_blink_on
# Construct the result array based on selections
local result=()
for i in "${!selected[@]}"; do
if [[ ${selected[i]} == true ]]; then
result+=(${options[$i]})
fi
done
# Return the result
eval $return_value='("${result[@]}")'
}
SELECTED_SERVICES=()
function select_services() {
# Get all services
SERVICES=()
while IFS= read -r file; do
filename=$(basename "$file" .yaml)
SERVICES+=("$filename")
done < <(find "$CLI_HOME/services" -name '*.yaml')
# Show multiselect and store results in SELECTED_SERVICES
multiselect SELECTED_SERVICES SERVICES[@]
for service in "${SELECTED_SERVICES[@]}"; do
selected_services+="$service, "
done
selected_services=${selected_services%, }
echo "Selected services: $selected_services"
}
function deploy_staging() {
REPOSITORY="CrowdDotDev/crowd.dev"
WORKFLOW_FILE="lf-staging-deploy.yaml"
CURRENT_BRANCH=$(git branch --show-current)
SERVICES="$(
IFS=" "
echo "${SELECTED_SERVICES[*]}"
)"
gh workflow run $WORKFLOW_FILE --repo $REPOSITORY --ref $CURRENT_BRANCH -f services="$SERVICES"
}
function deploy_production() {
REPOSITORY="CrowdDotDev/crowd.dev"
WORKFLOW_FILE="lf-production-deploy.yaml"
CURRENT_BRANCH=$(git branch --show-current)
SERVICES="$(
IFS=" "
echo "${SELECTED_SERVICES[*]}"
)"
gh workflow run $WORKFLOW_FILE --repo $REPOSITORY --ref $CURRENT_BRANCH -f services="$SERVICES"
}
function reset_selected_services() {
for SERVICE in "${SELECTED_SERVICES[@]}"; do
echo "Restarting service $SERVICE."
kill_containers $SERVICE && start_service $SERVICE
done
}
function build() {
HELP="${RESET}\nUsage:\n ./cli build [ $HELP_BUILD_STRING ]\n"
[[ -z "$1" ]] && say "$HELP" && exit 1
if [[ $BUILDERS =~ (^|[[:space:]])"$1"($|[[:space:]]) ]]; then
build_and_publish "$@"
else
error "Invalid command '$1'" && say "$HELP"
exit 1
fi
}
function create_migration() {
MIG_NAME="$1"
MIG_VERSION=$(date +%s)
UP_MIG_FILE="${CLI_HOME}/../backend/src/database/migrations/V${MIG_VERSION}__${MIG_NAME}.sql"
DOWN_MIG_FILE="${CLI_HOME}/../backend/src/database/migrations/U${MIG_VERSION}__${MIG_NAME}.sql"
touch $UP_MIG_FILE
touch $DOWN_MIG_FILE
yell "Created ${MIG_FILE}"
}
function create_product_migration() {
MIG_NAME="$1"
MIG_VERSION=$(date +%s)
UP_MIG_FILE="${CLI_HOME}/../backend/src/product/migrations/V${MIG_VERSION}__${MIG_NAME}.sql"
DOWN_MIG_FILE="${CLI_HOME}/../backend/src/product/migrations/U${MIG_VERSION}__${MIG_NAME}.sql"
touch $UP_MIG_FILE
touch $DOWN_MIG_FILE
yell "Created ${MIG_FILE}"
}
function build_and_publish() {
VERSION="$2"
if [[ -z "${VERSION}" ]]; then
COMMIT_HASH=$(git rev-parse --short HEAD)
TS_VERSION=$(date +%s)
VERSION="$TS_VERSION.$COMMIT_HASH"
fi
source $CLI_HOME/builders/$1.env
if [[ ${BUILD} ]]; then
say "Building $REPO version $VERSION with dockerfile '$DOCKERFILE' and context '$CONTEXT' and pushing it to $REPO:$VERSION"
docker build --platform linux/amd64 --tag "$REPO:$VERSION" -f "$DOCKERFILE" "$CONTEXT"
fi
if [[ ${PUSH} ]]; then
say "Pushing image $REPO version $VERSION to $REPO:$VERSION"
docker push "$REPO:$VERSION"
fi
}
function db_backup() {
[[ -z "$1" ]] && error "Dump name has to be provided as first parameter!" && exit 1
mkdir -p $CLI_HOME/db_dumps
say "Cloning local database to $CLI_HOME/db_dumps/$1.dump!"
docker exec -t ${PROJECT_NAME}_db_1 bash -c "PGPASSWORD=example pg_dump -F c -d crowd-web -U postgres > /$1.dump"
docker cp ${PROJECT_NAME}_db_1:/$1.dump $CLI_HOME/db_dumps/$1.dump
say "All done!"
}
function restore_db_backup() {
[[ -z "$1" ]] && error "Dump name has to be provided as first parameter!" && exit 1
say "First we need to clean up the scaffold!"
scaffold_destroy
up_scaffold
say "Sleeping for 5 seconds for the database container to start up!"
sleep 5
say "Restoring dump from $CLI_HOME/db_dumps/$1.dump"
docker cp $CLI_HOME/db_dumps/$1.dump ${PROJECT_NAME}_db_1:/backup.dump
# docker exec -t ${PROJECT_NAME}_db_1 bash -c "PGPASSWORD=example dropdb -U postgres crowd-web && PGPASSWORD=example createdb -U postgres crowd-web"
set +eo pipefail
docker exec -t ${PROJECT_NAME}_db_1 bash -c "PGPASSWORD=example pg_restore --clean -U postgres -d crowd-web backup.dump && rm -f backup.dump"
set -eo pipefail
post_up_scaffold
say "All done!"
}
function scaffold() {
HELP="${RESET}\nUsage:\n ./cli scaffold [ up | down | destroy | reset ]\n"
[[ -z "$1" ]] && say "$HELP" && exit 1
while test $# -gt 0; do
case "$1" in
up)
up_scaffold
post_up_scaffold
exit
;;
down)
down_scaffold
exit
;;
destroy)
scaffold_destroy
exit
;;
reset)
scaffold_reset
exit
;;
create-migration)
create_migration $2
exit
;;
create-product-migration)
create_product_migration $2
exit
;;
migrate-up)
migrate_local
exit
;;
up-test)
up_test_scaffold
exit
;;
*)
error "Invalid command '$1'" && say "$HELP"
exit 1
;;
esac
shift
done
}
function service() {
HELP="${RESET}\nUsage:\n ./cli service [ $HELP_STRING ]\n"
[[ -z "$1" ]] && say "$HELP" && exit 1
while test $# -gt 0; do
case "$1" in
list)
docker container ls | grep $LIST_STRING
exit
;;
up-all)
start_all_containers
exit
;;
*)
if [[ $CONTAINERS =~ (^|[[:space:]])"$1"($|[[:space:]]) ]]; then
service_manipulator $1 $2 $3
exit
else
error "Invalid command '$1'" && say "$HELP"
exit 1
fi
;;
esac
shift
done
}
function service_manipulator() {
HELP="${RESET}\nUsage:\n ./cli service $1 [ up | down | restart | status | logs | id ]\n"
[[ -z "$2" ]] && say "$HELP" && exit 1
while test $# -gt 0; do
case "$2" in
up)
start_service "$1"
exit
;;
down)
kill_containers "$1"
exit
;;
restart)
kill_containers "$1" && start_service "$1"
exit
;;
status)
print_container_status "$1"
exit
;;
logs)
get_logs "$1"
exit
;;
id)
get_container_id "$1"
exit
;;
*)
error "Invalid command '$2'" && say "$HELP"
exit 1
;;
esac
shift
done
}
function start_service() {
export USER_ID=$(id -u)
export GROUP_ID=$(id -g)
if [[ ${DEV} ]]; then
$_DC --compatibility -p $PROJECT_NAME -f "$CLI_HOME/services/${1}.yaml" up --build -d ${1}-dev
else
$_DC --compatibility -p $PROJECT_NAME -f "$CLI_HOME/services/${1}.yaml" up --build -d ${1}
fi
}
function kill_containers() {
$_DC --compatibility -p $PROJECT_NAME -f "$CLI_HOME/services/${1}.yaml" rm -fs ${1} ${1}-dev
}
function get_logs() {
if [[ ${DEV} ]]; then
docker container logs -f $(get_container_id "$1-dev")
else
docker container logs -f $(get_container_id "$1")
fi
}
function print_container_status() {
CONTAINER_STATUS=$(check_container_status $1)
if [[ ${CONTAINER_STATUS} ]]; then
check_container_status "$1"
else
error "Down."
exit 1
fi
}
function get_container_id() {
docker container ls -a | grep "${PROJECT_NAME}_${1}_" | tr " " "\n" | head -n 1
}
function check_container_status() {
docker container ls -a | grep "${PROJECT_NAME}_${1}_"
}
# <network-name> <subnet> <gateway>
function scaffold_set_up_network() {
NETWORK_NAME="$1"
NETWORK_SUBNET="$2"
NETWORK_GATEWAY="$3"
set +e pipefail
NETWORK_ID=$(docker network ls | grep -F -e "$NETWORK_NAME " | tr " " "\n" | head -n 1)
set -e pipefail
if [[ ${NETWORK_ID} ]]; then
say "The $NETWORK_NAME network is up and running."
else
docker network create -d bridge --subnet "$NETWORK_SUBNET" --gateway "$NETWORK_GATEWAY" "$NETWORK_NAME"
fi
}
function migrate_env() {
if [[ -z "$CROWD_DB_WRITE_HOST" ]]; then
error "No CROWD_DB_WRITE_HOST env variable set!"
exit 1
fi
say "Building flyway migration image..."
docker build $DOCKER_PLATFORM_FLAGS -t crowd_flyway -f $CLI_HOME/../backend/src/database/Dockerfile.flyway $CLI_HOME/../backend/src/database --load
say "Applying PostgreSQL migrations to $CROWD_DB_WRITE_HOST!"
docker run --rm \
-e PGHOST=$CROWD_DB_WRITE_HOST \
-e PGPORT=$CROWD_DB_PORT \
-e PGUSER=$CROWD_DB_USERNAME \
-e PGPASSWORD=$CROWD_DB_PASSWORD \
-e PGDATABASE=$CROWD_DB_DATABASE \
crowd_flyway
say "Applying QuestDB migrations to $CROWD_QUESTDB_SQL_HOST!"
docker run --rm --network "${PROJECT_NAME}-bridge" \
-v $CLI_HOME/../services/migrations/questdb/:/tmp/migrations \
postgres psql postgresql://$CROWD_QUESTDB_SQL_USERNAME:$CROWD_QUESTDB_SQL_PASSWORD@$CROWD_QUESTDB_SQL_HOST:$CROWD_QUESTDB_SQL_PORT/qdb \
-f /tmp/migrations/V1716382997__init-questdb.sql
}
function wait_for_tinybird() {
say "Waiting for Tinybird to get ready.."
MAX_RETRIES=10
RETRY_DELAY=5
attempt=1
while [ $attempt -le $MAX_RETRIES ]; do
OUTPUT=$(tb local status)
echo "$OUTPUT"
if [[ "$OUTPUT" == *"✓ Tinybird Local is ready!"* ]]; then
say "Tinybird is ready!"
break
else
whisper "Not ready yet. Retrying in $RETRY_DELAY seconds..."
sleep $RETRY_DELAY
fi
attempt=$((attempt + 1))
done
if [ $attempt -gt $MAX_RETRIES ]; then
error "Tinybird was not ready after $MAX_RETRIES attempts. Exiting."
return 1
fi
}
function migrate_tinybird_local() {
set +e +o pipefail
say "Waiting for Tinybird to be ready..."
wait_for_tinybird
say "Authenticating to tinybird staging..."
cd "$CLI_HOME/../services/libs/tinybird" && tb auth --region us-west-2
set -eo pipefail
return 1
}
function migrate_postgres_local() {
say "Building crowd flyway migration image..."
docker build $DOCKER_PLATFORM_FLAGS -t crowd_flyway -f $CLI_HOME/../backend/src/database/Dockerfile.flyway $CLI_HOME/../backend/src/database --load
say "Applying PostgreSQL migrations!"
docker run --rm --network "${PROJECT_NAME}-bridge" \
-e PGHOST=db \
-e PGPORT=5432 \
-e PGUSER=postgres \
-e PGPASSWORD=example \
-e PGDATABASE=crowd-web \
crowd_flyway
}
function migrate_questdb_local() {
say "Applying QuestDB migrations!"
docker run --rm --network "${PROJECT_NAME}-bridge" \
-v $CLI_HOME/../services/migrations/questdb/:/tmp/migrations \
postgres psql postgresql://admin:quest@questdb:8812/qdb \
-f /tmp/migrations/V1716382997__init-questdb.sql
}
function migrate_productdb_local() {
say "Building product flyway migration image..."
docker build $DOCKER_PLATFORM_FLAGS -t product_flyway -f $CLI_HOME/../backend/src/product/Dockerfile.flyway $CLI_HOME/../backend/src/product
say "Applying product database migrations!"
docker run --rm --network "${PROJECT_NAME}-bridge" \
-e PGHOST=product \
-e PGPORT=5432 \
-e PGUSER=postgres \
-e PGPASSWORD=example \
-e PGDATABASE=product-db \
product_flyway
}
function migrate_local() {
migrate_postgres_local
migrate_questdb_local
migrate_productdb_local
if [[ "$WITH_INSIGHTS" == "1" ]]; then
migrate_tinybird_local
fi
}
function up_test_scaffold() {
scaffold_set_up_network "${PROJECT_NAME}-bridge-test" $DOCKET_TEST_NETWORK_SUBNET $DOCKER_TEST_NETWORK_GATEWAY
$_DC -p "$PROJECT_NAME-test" -f $CLI_HOME/../backend/docker-compose.test.yaml down
$_DC -p "$PROJECT_NAME-test" -f $CLI_HOME/../backend/docker-compose.test.yaml up -d
migrate_test
}
function migrate_test() {
say "Building flyway migration image..."
docker build -t crowd_flyway -f $CLI_HOME/../backend/src/database/Dockerfile.flyway $CLI_HOME/../backend/src/database
say "Applying database migrations!"
docker run --rm --network "${PROJECT_NAME}-bridge-test" \
-e PGHOST=db-test \
-e PGPORT=5432 \
-e PGUSER=postgres \
-e PGPASSWORD=example \
-e PGDATABASE=crowd-web \
crowd_flyway
}
function source_edition() {
__CROWD_EDITION=$(source $CLI_HOME/../backend/.env.dist.local && source $CLI_HOME/../backend/.env.override.local && echo $CROWD_EDITION)
nl
say "Crowd edition detected: $__CROWD_EDITION"
}
function install_libs() {
(cd $CLI_HOME/.. && pnpm i --frozen-lockfile)
}
function install_tinybird_cli() {
if ! tb --version &> /dev/null; then
echo "Tinybird CLI not found. Installing..."
curl -s https://tinybird.co | sh
fi
}
function set_tinybird_workspace_admin_token() {
export CROWD_TINYBIRD_WORKSPACE_ADMIN_TOKEN=$(tb --local token ls --match "admin token" | awk '/^token: / {print $2}')
}
function up_scaffold() {
scaffold_set_up_network "$PROJECT_NAME-bridge" $DOCKER_NETWORK_SUBNET $DOCKER_NETWORK_GATEWAY
download_kafka_connect_questdb_connector "$CLI_HOME/scaffold/kafka-connect"
if [[ "$WITH_INSIGHTS" == "1" ]]; then
download_kafka_connect_http "$CLI_HOME/scaffold/kafka-connect"
install_tinybird_cli
fi
if [[ "$WITH_INSIGHTS" == "1" ]]; then
# first up tinybird, then wait for it to be ready, then set the token env for kafka connect
$_DC --compatibility -p $PROJECT_NAME $DOCKER_COMPOSE_SCAFFOLD_FILES $DOCKER_COMPOSE_PROFILE up -d --build tinybird
wait_for_tinybird
set_tinybird_workspace_admin_token
fi
$_DC --compatibility -p $PROJECT_NAME $DOCKER_COMPOSE_SCAFFOLD_FILES $DOCKER_COMPOSE_PROFILE up -d --build --no-recreate
}
function post_up_scaffold() {
migrate_local
bash $CLI_HOME/nango-integrations.sh
}
function down_scaffold() {
$_DC --compatibility -p $PROJECT_NAME $DOCKER_COMPOSE_SCAFFOLD_FILES $DOCKER_COMPOSE_PROFILE down
}
function scaffold_destroy() {
say "\nWill delete all local crowd state data (docker volumes) if you have any. Are you sure?"
select reset_system_condition in "Yes" "No"; do
case $reset_system_condition in
'Yes')
scaffold_destroy_confirmed
break
;;
'No')
yell "Canceled!"
break
;;
esac
done
}
function scaffold_reset() {
scaffold_destroy
up_scaffold
post_up_scaffold
}
function kill_all_containers() {
for i in $CONTAINERS; do
say "Killing service $i."
if [[ $(check_container_status ${i}) ]]; then
docker rm -f $(get_container_id ${i})
yell "Service $i killed."
elif [[ $(check_container_status ${i}-dev) ]]; then
docker rm -f $(get_container_id ${i}-dev)
yell "Service $i-dev killed."
else
error "Service $i not running."
fi
nl
done
}
function service_start() {
for SERVICE in $CONTAINERS; do
if [[ ${#INGORED_SERVICES[@]} -ne 0 ]]; then
for IGNORED_SERVICE in "${INGORED_SERVICES[@]}"; do
if [[ "$SERVICE" == "${IGNORED_SERVICE}" ]]; then
SKIP=1
break
fi
done
fi
if [[ -z "$SKIP" ]]; then
say "Starting service $SERVICE."
start_service $SERVICE
nl
fi
unset SKIP
done
}
function scaffold_destroy_confirmed() {
kill_all_containers
$_DC --compatibility -p $PROJECT_NAME $DOCKER_COMPOSE_SCAFFOLD_FILES $DOCKER_COMPOSE_PROFILE down
# needed because if there are no volumes this might cause the script to exit
set +eo pipefail
VOLUMES=$(docker volume ls | tail -n +2 | tr -s " " | cut -d' ' -f2 | grep $PROJECT_NAME)
set -eo pipefail
if [[ ${VOLUMES} ]]; then
_IFS=$IFS
IFS=$' '
NAMES=$VOLUMES
IFS=$_IFS
for name in $NAMES; do
say "Destroying volume $name!"
docker volume rm -f $name
done
fi
}
function wait_for_db() {
say "Waiting for scaffold to start!"
sleep 3
while [[ ! $(docker container ls | grep $PROJECT_NAME | grep db | grep Up) ]]; do
sleep 1
done
say "Scaffold is up and running!"
}
function start() {
if [[ -z "$CLEAN_START" ]]; then
up_scaffold
post_up_scaffold
else
scaffold_reset
fi
service_start
}
SCRIPT_USAGE="${YELLOW}${PROJECT_NAME} CLI ${RESET}\n\nExample usage: ./cli [ start, start-dev, clean-start, clean-start-dev, start-backend, start-backend-dev, clean-start-backend, clean-start-backend-dev, scaffold =>, service =>, build =>, build-and-push ]"
[[ -z "$1" ]] && say "$SCRIPT_USAGE" && exit 1
while test $# -gt 0; do
case "$1" in
scaffold)
scaffold $2 $3 $4 $5
exit
;;
service)
service $2 $3 $4 $5
exit
;;
services)
service $2 $3 $4 $5
exit
;;
start)
start
exit
;;
start-e2e)
declare -a INGORED_SERVICES=("job-generator" "data-sink-worker" "integration-run-worker" "integration-stream-worker")
start
exit
;;
start-be)
declare -a INGORED_SERVICES=("frontend")
start
exit
;;
start-dev)
INGORED_SERVICES=("python-worker" "job-generator" "discord-ws" "webhook-api" "profiles-worker" "organizations-enrichment-worker" "merge-suggestions-worker" "members-enrichment-worker" "exports-worker" "entity-merging-worker")
DEV=1
start
exit
;;
clean-start)
CLEAN_START=1
start
exit
;;
clean-start-dev)
# INGORED_SERVICES=("python-worker" "job-generator" "discord-ws" "webhook-api" "profiles-worker" "organizations-enrichment-worker" "merge-suggestions-worker" "members-enrichment-worker" "exports-worker" "entity-merging-worker")
CLEAN_START=1
DEV=1
start
exit
;;
start-backend)
declare -a INGORED_SERVICES=("frontend")
start
exit
;;
start-backend-dev)
declare -a INGORED_SERVICES=("frontend")
DEV=1
start
exit
;;
clean-start-backend)
declare -a INGORED_SERVICES=("frontend")
CLEAN_START=1
start
exit
;;
clean-start-backend-dev)
declare -a INGORED_SERVICES=("frontend")
CLEAN_START=1
DEV=1
start
exit
;;
service-restart)
kill_all_containers
service_start
exit
;;
service-restart-be)
declare -a INGORED_SERVICES=("frontend")
kill_all_containers
service_start
exit
;;
service-restart-dev)
DEV=1
kill_all_containers
service_start
exit
;;
service-restart-fe-dev)
IGNORED_SERVICES=("frontend" "python-worker" "job-generator" "discord-ws" "webhook-api" "profiles-worker" "organizations-enrichment-worker" "merge-suggestions-worker" "members-enrichment-worker" "exports-worker" "entity-merging-worker")
DEV=1
kill_all_containers
service_start
exit
;;
clean-start-fe-dev)
INGORED_SERVICES=("frontend" "python-worker" "job-generator" "discord-ws" "webhook-api" "profiles-worker" "organizations-enrichment-worker" "merge-suggestions-worker" "members-enrichment-worker" "exports-worker" "entity-merging-worker")
CLEAN_START=1
DEV=1
start
exit
;;
deploy-staging)
select_services
deploy_staging
exit
;;
deploy-production)
select_services
deploy_production
exit
;;
reset-selected-services)
select_services
reset_selected_services
exit
;;
reset-selected-services-dev)
DEV=1
select_services
reset_selected_services
exit
;;
build)
BUILD=1
build $2 $3
exit
;;
build-and-push)
BUILD=1
PUSH=1
build $2 $3
exit
;;
build-and-push-multiple)
BUILD=1
PUSH=1
COMMIT_HASH=$(git rev-parse --short HEAD)
TS_VERSION=$(date +%s)
VERSION="$TS_VERSION.$COMMIT_HASH"
say "Building images with version tag $VERSION"
shift
for IMAGE in "$@"; do
build $IMAGE $VERSION
done
exit
;;
push)
PUSH=1
build $2 $3
exit
;;
migrate-env)
migrate_env
exit
;;
db-backup)
db_backup $2
exit
;;
db-restore)
restore_db_backup $2
exit
;;
lint-backend)
(cd $CLI_HOME/../backend && pnpm run lint && pnpm run format-check && pnpm run tsc-check)
exit
;;
lint-services)
(cd $CLI_HOME/../services/scripts && ./lint_apps.sh && ./lint_libs.sh)
exit
;;
*)
error "Invalid command '$1'" && say "$SCRIPT_USAGE"
exit 1
;;
esac
shift
done