-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcool-deploy.sh
executable file
·2039 lines (1884 loc) · 72.1 KB
/
cool-deploy.sh
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
1000
#!/bin/bash
COOL_DEPLOY_VERSION="1.1.2"
# Some project variables
WASP_PROJECT_DIR=$(CDPATH= cd -- "$(dirname -- "$0")" && pwd)
PARENT_DIR=$(dirname "$WASP_PROJECT_DIR")
FIRST_TIME_RUN=0
NEED_DB_SETUP=0
NEED_DEV_DB_SETUP=0
BEARER=""
COOLIFY_GITHUB_APP_UUID=""
COOLIFY_GITHUB_PRIVATE_KEY_UUID=""
COOLIFY_GIT_COMMIT_SHA="HEAD"
# Deploy directories
DEPLOY_DIR=$WASP_PROJECT_DIR/deploy
CLIENT_DEPLOY_DIR=$DEPLOY_DIR/client
SERVER_DEPLOY_DIR=$DEPLOY_DIR/server
# Get the app name and version of the Wasp App
cd $WASP_PROJECT_DIR
WASP_APP_NAME=$(grep -o 'app \w\+' main.wasp | cut -d' ' -f2)
WASP_VERSION=$(awk '/wasp: {/,/}/ {if ($1 == "version:") {gsub(/[",]/, "", $2); sub(/^\^/, "", $2); print $2; exit}}' main.wasp)
if [ -z "$WASP_APP_NAME" ]; then
WASP_APP_NAME="unknownWaspApp"
fi
if [ -z "$WASP_VERSION" ]; then
WASP_VERSION="unknownVersion"
fi
# TODO: CLI Args
# - init (let script continue as normal and it will configure. If .env.coolify exists, error out)
# - start db (start dev db on Coolify)
# - stop db (stop dev db on Coolify)
# - status (show status of Frontend and Backend on Coolify)
# - restart (trigger manual redeployment of containers)
# - msg "x" (set a commit message for the deployment)
# ------------------------------------------------------------------------------
# Parsing the JSON from Coolify's API requires the `jq` command line tool.
# Make sure it is installed and available on the system.
# ------------------------------------------------------------------------------
detect_jq() {
if ! command -v jq &> /dev/null; then
echo -e "\033[1;31mERROR:\033[0m \033[31m\`jq\` is not installed. Please install it using the following commands:\033[0m"
case "$OSTYPE" in
darwin*) # macOS
echo -e " \033[33mbrew install jq\033[0m"
;;
linux*)
echo -e " \033[33msudo apt-get install jq\033[0m (Ubuntu/Debian-based systems)"
echo -e " \033[33msudo yum install jq\033[0m (RHEL/CentOS-based systems)"
echo -e " \033[33msudo dnf install jq\033[0m (Fedora-based systems)"
;;
*)
echo -e "Sorry, we don't have installation instructions for your platform."
echo -e "Please install jq manually by referring to: https://github.com/jqlang/jq"
;;
esac
exit 1
fi
}
# ------------------------------------------------------------------------------
# GET_COOLIFY_VERSION
# ------------------------------------------------------------------------------
get_coolify_version() {
local version=$(curl -s --request GET \
--url $COOLIFY_BASE_URL/api/v1/version \
--header "$BEARER")
echo "$version"
}
# ------------------------------------------------------------------------------
# GET_COOLIFY_SERVERS
# Optional: $1 = if already retrieved UUID, check to make sure it is valid.
# ------------------------------------------------------------------------------
get_coolify_servers() {
if [ ! -z "$1" ]; then # $1 is not empty, get ready to do the check!
valid_server=0
fi
local servers=$(curl -s --request GET \
--url $COOLIFY_BASE_URL/api/v1/servers \
--header "$BEARER")
local server_count=$(jq '. | length' <<< "$servers")
for ((i = 0; i < server_count; i++)); do # Loop through all servers
local uuid=$(jq -r ".[$i].uuid" <<< "$servers")
local description=$(jq -r ".[$i].description" <<< "$servers")
local name=$(jq -r ".[$i].name" <<< "$servers")
if [ -z "$1" ]; then
echo -e "\033[33mServer-$((i+1)):\033[0m \033[1;31m$name\033[0m"
echo -e " UUID: \033[1;37m$uuid\033[0m"
echo -e " Description: $description"
else
if [ "$uuid" == "$1" ]; then
valid_server=1
fi
fi
done # End of server loop
if [ ! -z "$1" ]; then
if [ $valid_server -eq 0 ]; then
echo -e "\033[1;31mERROR: Server with UUID \`$1\` not found!\033[0m"
return 1
fi
return 0
fi
return 0
}
# ------------------------------------------------------------------------------
# GET_COOLIFY_PROJECTS
# Optional: $1 = if already retrieved UUID, check to make sure it is valid.
# ------------------------------------------------------------------------------
get_coolify_projects() {
if [ ! -z "$1" ]; then
valid_project=0
fi
local projects=$(curl -s --request GET \
--url $COOLIFY_BASE_URL/api/v1/projects \
--header "$BEARER")
local project_count=$(jq '. | length' <<< "$projects")
for ((i = 0; i < project_count; i++)); do # Loop through all projects
local uuid=$(jq -r ".[$i].uuid" <<< "$projects")
if [ -z "$1" ]; then # $1 is empty, just print the data
local name=$(jq -r ".[$i].name" <<< "$projects")
# local id=$(jq -r ".[$i].id" <<< "$projects")
project_info=$(curl -s --request GET \
--url $COOLIFY_BASE_URL/api/v1/projects/$uuid \
--header "$BEARER")
local description=$(jq -r ".description" <<< "$project_info")
# local created_at=$(jq -r ".created_at" <<< "$project_info")
# local updated_at=$(jq -r ".updated_at" <<< "$project_info")
echo -e "\033[33mProject-$((i+1)):\033[0m \033[1;31m$name\033[0m"
echo -e " Description: $description"
echo -e " UUID: \033[1;37m$uuid\033[0m"
# echo -e " ID: $id"
# echo -e " Created At: $created_at"
# echo -e " Updated At: $updated_at"
else # Check if the UUID matches the one provided
if [ "$uuid" == "$1" ]; then
valid_project=1
fi
fi
done # End of project loop
if [ ! -z "$1" ]; then
if [ $valid_project -eq 0 ]; then
echo -e "\033[1;31mERROR: Project with UUID \`$1\` not found!\033[0m"
return 1
fi
return 0
fi
return 0
}
# ------------------------------------------------------------------------------
# NEW_COOLIFY_PROJECT
# ------------------------------------------------------------------------------
new_coolify_project() {
local project_name="$1"
local project_description="$2"
local project_info=$(curl -s --request POST \
--url $COOLIFY_BASE_URL/api/v1/projects \
--header "$BEARER" \
--header 'Content-Type: application/json' \
--data '{
"name": "'"$project_name"'",
"description": "'"$project_description"'"
}')
local project_uuid=$(jq -r ".uuid" <<< "$project_info")
echo "$project_uuid"
}
# ------------------------------------------------------------------------------
# NEW_COOLIFY_PRIVATE_KEY
# ------------------------------------------------------------------------------
new_coolify_private_key() {
local pk_name="$1"
local pk_description="$2"
# Create a temporary file in the system's temporary directory. The file is
# created with read and write permissions for the current owner only.
TMPFILE=$(mktemp)
rm -f "$TMPFILE"
trap "rm -f '$TMPFILE'" EXIT
# Use the temporary file and get the private key from it
ssh-keygen -t rsa -b 4096 -C "deploy_key" -N "" -f "$TMPFILE"
local pk_key=$(cat "$TMPFILE")
local pub_key=$(cat "$TMPFILE.pub")
local pk_key=${pk_key//$'\n'/\\n} # replace all newlines with `\n`
rm -f "$TMPFILE"
local pk_payload=$(cat <<EOF
{
"name": "$pk_name",
"description": "$pk_description",
"private_key": "$pk_key"
}
EOF
)
local pk_info=$(curl -s --request POST \
--url $COOLIFY_BASE_URL/api/v1/security/keys \
--header "$BEARER" \
--header 'Content-Type: application/json' \
--data "$pk_payload"
)
local pk_uuid=$(jq -r ".uuid" <<< "$pk_info")
local possible_pk_error=$(jq -r ".error" <<< "$pk_info")
local possible_pk_errors=$(jq -r ".errors" <<< "$pk_info")
local possible_pk_msg=$(jq -r ".message" <<< "$pk_info")
if [ ! "$possible_pk_error" == "null" ]; then
echo -e "$pk_payload"
echo
if [ ! "$possible_pk_error" == "null" ]; then
echo -e "$possible_pk_error"
fi
if [ ! "$possible_pk_errors" == "null" ]; then
echo -e "$possible_pk_errors"
fi
echo -e "$possible_pk_msg"
echo
echo -e "\033[1;31m💀 --- COOLIFY ERROR: Private Key Creation Failed! See above for possible details... ---\033[0m"
echo
exit 1
else
if [ "$pk_uuid" == "null" ]; then # If the UUID is null, we can assume the setup failed
echo -e "$pk_info" # May not even be JSON, print it out
echo
echo -e "\033[1;31m💀 --- COOLIFY ERROR: Could not create new Private Key! See above for possible details... ---\033[0m"
echo
exit 1
else # If we got here, we can assume a successful setup
COOLIFY_GITHUB_PRIVATE_KEY_UUID="$pk_uuid"
echo
echo -e "\033[33m✅ --- Private Key '$COOLIFY_GITHUB_PRIVATE_KEY_UUID' created successfully ---\033[0m"
echo
echo -e "\033[1;31mREMEMBER:\033[0m \033[33mCopy your Public Key (below) and add it to your GitHub Repo Settings under 'Deploy Keys':\033[0m"
echo -e "$pub_key"
echo
echo -e "TODO: Make sure to confirm if user copy/pasted Public Key before continuing!"
echo
fi
fi
}
# ------------------------------------------------------------------------------
# GET_COOLIFY_GITHUB_KEY
# Optional: $1 = if already retrieved UUID, check to make sure it is valid.
# ------------------------------------------------------------------------------
get_coolify_github_key() {
if [ ! -z "$1" ]; then
valid_key=0
fi
local keys=$(curl -s --request GET \
--url $COOLIFY_BASE_URL/api/v1/security/keys \
--header "$BEARER")
local key_found=0
local key_count=$(jq '. | length' <<< "$keys")
for ((i = 0; i < key_count; i++)); do # Loop through all keys
local key_is_git_related=$(jq -r ".[$i].is_git_related" <<< "$keys")
if [ "$key_is_git_related" == "true" ]; then
local key_uuid=$(jq -r ".[$i].uuid" <<< "$keys")
if [ -z "$1" ]; then # $1 is empty, just print the data
((key_found++))
local key_id=$(jq -r ".[$i].id" <<< "$keys")
local key_name=$(jq -r ".[$i].name" <<< "$keys")
local key_description=$(jq -r ".[$i].description" <<< "$keys")
echo -e "\033[33mGithub App Key-$key_found:\033[0m \033[1;31m$key_name\033[0m"
echo -e " Description: $key_description"
echo -e " UUID: \033[1;37m$key_uuid\033[0m"
else # Check if the UUID matches the one provided
if [ "$key_uuid" == "$1" ]; then
valid_key=1
fi
fi
fi
done # End of project loop
if [ ! -z "$1" ]; then
if [ $valid_key -eq 0 ]; then
echo -e "\033[1;31mERROR: Github App Key with UUID \`$1\` not found!\033[0m"
return 1
fi
return 0
fi
return 0
}
# ------------------------------------------------------------------------------
# GET_COOLIFY_PRIVATE_KEY
# Optional: $1 = if already retrieved UUID, check to make sure it is valid.
# ------------------------------------------------------------------------------
get_coolify_private_key() {
if [ ! -z "$1" ]; then
valid_private_key=0
fi
local keys=$(curl -s --request GET \
--url $COOLIFY_BASE_URL/api/v1/security/keys \
--header "$BEARER")
local key_found=0
local key_count=$(jq '. | length' <<< "$keys")
for ((i = 0; i < key_count; i++)); do # Loop through all keys
local key_is_git_related=$(jq -r ".[$i].is_git_related" <<< "$keys")
if [ "$key_is_git_related" == "false" ]; then
local key_uuid=$(jq -r ".[$i].uuid" <<< "$keys")
if [ -z "$1" ]; then # $1 is empty, just print the data
((key_found++))
local key_id=$(jq -r ".[$i].id" <<< "$keys")
local key_name=$(jq -r ".[$i].name" <<< "$keys")
local key_description=$(jq -r ".[$i].description" <<< "$keys")
echo -e "\033[33mPrivate Key-$key_found:\033[0m \033[1;31m$key_name\033[0m"
echo -e " Description: $key_description"
echo -e " UUID: \033[1;37m$key_uuid\033[0m"
else # Check if the UUID matches the one provided
if [ "$key_uuid" == "$1" ]; then
valid_private_key=1
fi
fi
fi
done # End of project loop
if [ ! -z "$1" ]; then
if [ $valid_private_key -eq 0 ]; then
echo -e "\033[1;31mERROR: Private Key with UUID \`$1\` not found!\033[0m"
return 1
fi
return 0
fi
return 0
}
# ------------------------------------------------------------------------------
# SET_SERVER_ENV
# Required: $1 = Key
# Required: $2 = Value
# Optional: $3 = Is Preview?
# ------------------------------------------------------------------------------
set_server_env() {
if [ -z "$1" ]; then
if [ ! -z "$2" ]; then
echo -e "ERROR: No 'Key' Provided for Environment Variable Value '$2'!"
else
echo -e "ERROR: No 'Key' or 'Value' Provided for Environment Variable!"
fi
echo
echo -e "\033[1;31m💀 --- COOLIFY ERROR: Server Set Env Variable Failed! See above for possible details... ---\033[0m"
exit 1
fi
if [ -z "$2" ]; then
echo "ERROR: No 'Value' Provided for Environment Variable Key '$1'!"
echo
echo -e "\033[1;31m💀 --- COOLIFY ERROR: Server Set Env Variable Failed! See above for possible details... ---\033[0m"
exit 1
fi
if [ -z "$3" ]; then
local is_preview="false"
else
local is_preview="$3"
fi
local env_payload=$(cat <<EOF
{
"key": "$1",
"value": "$2",
"is_preview": $is_preview
}
EOF
)
local env_return=$(curl -s --request POST \
--url $COOLIFY_BASE_URL/api/v1/applications/$configured_server_uuid/envs \
--header "$BEARER" \
--header 'Content-Type: application/json' \
-d "$env_payload")
local possible_env_uuid=$(jq -r ".uuid" <<< "$env_return")
local possible_env_error=$(jq -r ".error" <<< "$env_return")
local possible_env_msg=$(jq -r ".message" <<< "$env_return")
if [ ! "$possible_env_error" == "null" ]; then
echo -e "$env_payload"
echo
echo -e "$possible_env_error"
echo -e "$possible_env_msg"
echo
echo -e "\033[1;31m💀 --- COOLIFY ERROR: Server Set Env Variable Failed! See above for possible details... ---\033[0m"
echo
exit 1
else
if [ "$possible_env_uuid" == "null" ]; then # If the UUID is null, we may need to update the env
local update_env_return=$(curl -s --request PATCH \
--url $COOLIFY_BASE_URL/api/v1/applications/$configured_server_uuid/envs \
--header "$BEARER" \
--header 'Content-Type: application/json' \
-d "$env_payload")
local possible_update_env_uuid=$(jq -r ".uuid" <<< "$update_env_return")
local possible_update_env_error=$(jq -r ".error" <<< "$update_env_return")
local possible_update_env_msg=$(jq -r ".message" <<< "$update_env_return")
if [ ! "$possible_update_env_error" == "null" ]; then
echo -e "$env_payload"
echo
echo -e "$possible_update_env_error"
echo -e "$possible_update_env_msg"
echo
echo -e "\033[1;31m💀 --- COOLIFY ERROR: Server Update Env Variable Failed! See above for possible details... ---\033[0m"
echo
exit 1
else
if [ "$possible_update_env_uuid" == "null" ]; then # If the UUID is null, NOW we can assume it all failed
echo -e "$update_env_return" # May not even be JSON, print it out
echo
echo -e "\033[1;31m💀 --- COOLIFY ERROR: Server Update Env Variable Failed! See above for possible details... ---\033[0m"
echo
exit 1
else # If we got here, we can assume a successful setup
return 0
fi
fi
# If we got here, error out. Original set env call return may not have even been JSON
echo -e "$env_return" # May not even be JSON, print it out
echo
echo -e "\033[1;31m💀 --- COOLIFY ERROR: Server Set Env Variable Failed! See above for possible details... ---\033[0m"
echo
exit 1
else # If we got here, we can assume a successful setup
return 0
fi
fi
return 1
}
# ------------------------------------------------------------------------------
# configure_some_coolify_settings
# TODO
# ------------------------------------------------------------------------------
configure_some_coolify_settings() {
echo
local do_header=0
if [ -z "$COOLIFY_SERVER_UUID" ]; then
do_header=1
fi
if [ -z "$COOLIFY_PROJECT_UUID" ]; then
do_header=1
fi
if [ -z "$GH_PRIVATE" ]; then
do_header=1
else
if [ $GH_PRIVATE -eq 1 ]; then
if [ -z "$COOLIFY_GITHUB_APP_UUID" ]; then
do_header=1
fi
elif [ $GH_PRIVATE -eq 2 ]; then
if [ -z "$COOLIFY_GITHUB_PRIVATE_KEY_UUID" ]; then
do_header=1
fi
fi
fi
if [ -z "$COOLIFY_GIT_REPOSITORY" ]; then
do_header=1
fi
if [ -z "$COOLIFY_GIT_BRANCH" ]; then
do_header=1
fi
if [ -z "$COOLIFY_CLIENT_DESCRIPTION" ]; then
do_header=1
fi
if [ -z "$COOLIFY_SERVER_DESCRIPTION" ]; then
do_header=1
fi
if [ -z "$COOLIFY_GIT_COMMIT_SHA" ]; then
COOLIFY_GIT_COMMIT_SHA="HEAD"
fi
if [ -z "$COOLIFY_ENVIRONMENT_NAME" ]; then
COOLIFY_ENVIRONMENT_NAME="production"
fi
if [ $do_header -eq 1 ]; then
echo -e "\033[1;32m🤖 --- CONFIGURING SOME COOLIFY SETTINGS & VARIABLES... ---\033[0m"
echo
fi
# Get the UUID of the server to deploy to:
if [ -z "$COOLIFY_SERVER_UUID" ]; then
get_coolify_servers
while true; do
read -p $'\033[33mEnter the UUID of the Server to Deploy to:\033[0m ' COOLIFY_SERVER_UUID
if [ -z "$COOLIFY_SERVER_UUID" ]; then
echo -e "\033[31mPlease enter a valid UUID!\033[0m"
else
get_coolify_servers "$COOLIFY_SERVER_UUID"
if [ $? -eq 0 ]; then
break
fi
fi
done
fi
# Now, we need to select the Coolify Project to house the deployments:
if [ -z "$COOLIFY_PROJECT_UUID" ]; then
while true; do # New/List/Add Loop
read -p $'\033[33mDeploy to New Project or Add to existing one? \033[31mDefault: New \033[33m[New/List/Add]:\033[0m ' new_list_or_add
if [ -z "$new_list_or_add" ]; then
new_list_or_add="NEW"
fi
new_list_or_add=$(echo "$new_list_or_add" | tr '[:lower:]' '[:upper:]')
if [ "$new_list_or_add" == "NEW" ]; then # Create a new Project and get its UUID
while true; do # Get Project Name loop
read -p $'\033[33mEnter the Project Name:\033[0m ' new_project_name
if [ -z "$new_project_name" ]; then
echo -e "\033[31mPlease enter a valid Project Name!\033[0m"
else
break
fi
done # End of Project Name loop
while true; do # Get Project Description loop
read -p $'\033[33mEnter the Project Description:\033[0m ' new_project_description
if [ -z "$new_project_description" ]; then
echo -e "\033[31mPlease enter a valid Project Description!\033[0m"
else
break
fi
done # End of Project Description loop
COOLIFY_PROJECT_UUID=$(new_coolify_project "$new_project_name" "$new_project_description")
break
elif [ "$new_list_or_add" == "LIST" ]; then
get_coolify_projects
elif [ "$new_list_or_add" == "ADD" ]; then
# Get the UUID of the project:
get_coolify_projects
while true; do
read -p $'\033[33mEnter the UUID of the Project to use:\033[0m ' COOLIFY_PROJECT_UUID
if [ -z "$COOLIFY_PROJECT_UUID" ]; then
echo -e "\033[31mPlease enter a valid UUID!\033[0m"
else
get_coolify_projects "$COOLIFY_PROJECT_UUID"
if [ $? -eq 0 ]; then
break
fi
fi
done
break
fi
done # End of New/List/Add Loop
fi
# Get the Source to clone from
if [ -z "$GH_PRIVATE" ]; then
while true; do
read -p $'\033[33mEnter the Source Type (0 = Public Github; 1 = Private Github App; 2 = Private Key):\033[0m ' GH_PRIVATE
if [ -z "$GH_PRIVATE" ]; then
echo -e "\033[31mPlease enter something valid!\033[0m"
elif ! [[ "$GH_PRIVATE" =~ ^[0-9]+$ ]]; then
echo -e "\033[31mPlease enter a WHOLE number in the range of 0-2 inclusive!\033[0m"
elif [ "$GH_PRIVATE" -gt 2 ]; then
echo -e "\033[31mPlease enter a number in the range of 0-2 inclusive!\033[0m"
elif [ "$GH_PRIVATE" -lt 0 ]; then
echo -e "\033[31mNow you're just being a brat\033[0m"
elif [ "$GH_PRIVATE" -eq 1 ]; then
echo -e "\033[31mERROR:\033[0m \033[33mPrivate Github App is currently not supported! Please choose either Public Repo or Private Key Deployment.\033[0m"
else
break
fi
done
fi
if [ $GH_PRIVATE -eq 1 ]; then
# Grab the UUID of the Github App Key, so we can actually deploy:
if [ -z "$COOLIFY_GITHUB_APP_UUID" ]; then
get_coolify_github_key
while true; do # Get Github Key loop
read -p $'\033[33mEnter the UUID of the Github App Key to use:\033[0m ' COOLIFY_GITHUB_APP_UUID
if [ -z "$COOLIFY_GITHUB_APP_UUID" ]; then
echo -e "\033[31mPlease enter a valid UUID!\033[0m"
else
get_coolify_github_key "$COOLIFY_GITHUB_APP_UUID"
if [ $? -eq 0 ]; then
break
else
get_coolify_github_key
fi
fi
done
fi
fi
if [ $GH_PRIVATE -eq 2 ]; then
# Grab the UUID of the Private Key
if [ -z "$COOLIFY_GITHUB_PRIVATE_KEY_UUID" ]; then
get_coolify_private_key
while true; do # Get Github Key loop
read -p $'\033[33mEnter the UUID of the Private Key to use, or type NEW to create a new key:\033[0m ' COOLIFY_GITHUB_PRIVATE_KEY_UUID
if [ -z "$COOLIFY_GITHUB_PRIVATE_KEY_UUID" ]; then
echo -e "\033[31mPlease enter a valid Private Key UUID!\033[0m"
else
make_new_key=$(echo "$COOLIFY_GITHUB_PRIVATE_KEY_UUID" | tr '[:lower:]' '[:upper:]')
if [ "$make_new_key" == "NEW" ]; then
# Get a name for the new Private Key
while true; do
read -p $'\033[33mEnter a name for the Private Key:\033[0m ' new_private_key_name
if [ -z "$new_private_key_name" ]; then
echo -e "\033[31mPlease enter a valid Private Key Name!\033[0m"
else
break
fi
done
local new_key_description="Auto-Generated by Cool-Deploy on '$(date +%Y-%m-%d)'"
new_coolify_private_key "$new_private_key_name" "$new_key_description"
break
else
get_coolify_private_key "$COOLIFY_GITHUB_PRIVATE_KEY_UUID"
if [ $? -eq 0 ]; then
break
else
get_coolify_private_key
fi
fi
fi
done
fi
fi
# Let's get the Git repository...
if [ -z "$COOLIFY_GIT_REPOSITORY" ]; then
while true; do
if [ $GH_PRIVATE -eq 1 ]; then
read -p $'\033[33mEnter the Github App Repository (format: username/my-wasp-project):\033[0m ' COOLIFY_GIT_REPOSITORY
elif [ $GH_PRIVATE -eq 0 ]; then
read -p $'\033[33mEnter the Public Git Repository URL (format: https://github.com/username/my-wasp-project.git):\033[0m ' COOLIFY_GIT_REPOSITORY
elif [ $GH_PRIVATE -eq 2 ]; then
read -p $'\033[33mEnter the Private Git Repository SSH (format: [email protected]:username/my-wasp-project.git):\033[0m ' COOLIFY_GIT_REPOSITORY
fi
if [ -z "$COOLIFY_GIT_REPOSITORY" ]; then
echo -e "\033[31mPlease enter a valid Git Repository URL!\033[0m"
else
break
fi
done
fi
# ...and its branch
if [ -z "$COOLIFY_GIT_BRANCH" ]; then
while true; do
read -p $'\033[33mEnter the Git Branch (Default: main):\033[0m ' COOLIFY_GIT_BRANCH
if [ -z "$COOLIFY_GIT_BRANCH" ]; then
COOLIFY_GIT_BRANCH="main"
break
else
break
fi
done
fi
# Frontend Description for the Coolify UI
if [ -z "$COOLIFY_CLIENT_DESCRIPTION" ]; then
while true; do
read -p $'\033[33mEnter a Client App description for the Coolify UI (Default: "Wasp Frontend"):\033[0m ' COOLIFY_CLIENT_DESCRIPTION
if [ -z "$COOLIFY_CLIENT_DESCRIPTION" ]; then
COOLIFY_CLIENT_DESCRIPTION="Wasp Frontend"
break
else
break
fi
done
fi
# Backend Description for the Coolify UI
if [ -z "$COOLIFY_SERVER_DESCRIPTION" ]; then
while true; do
read -p $'\033[33mEnter a Server App description for the Coolify UI (Default: "Wasp Backend"):\033[0m ' COOLIFY_SERVER_DESCRIPTION
if [ -z "$COOLIFY_SERVER_DESCRIPTION" ]; then
COOLIFY_SERVER_DESCRIPTION="Wasp Backend"
break
else
break
fi
done
fi
return 0
}
# ------------------------------------------------------------------------------
# run_coolify_healthcheck
# Let's make sure Coolify is up and running!
# ------------------------------------------------------------------------------
run_coolify_healthcheck() {
local test_call=$(get_coolify_version)
if [ -z "$test_call" ]; then
test_call="\033[1;41m FAILED \033[0m"
else
test_call="\033[1;42m SUCCESS \033[0m"
fi
echo
echo -e "\033[1;37mCoolify Healthcheck:\033[0m $test_call"
if [ "$test_call" == "\033[1;41m FAILED \033[0m" ]; then
echo -e "\033[1;37mOh, no! Something went wrong and we couldn't connect to Coolify!\033[0m"
echo
echo -e "\033[1;31m🛑 --- ERROR: 'COOLIFY_BASE_URL' and/or 'COOLIFY_API_KEY' not correctly configured! ---\033[0m"
echo
echo -e "COOLIFY_BASE_URL: $COOLIFY_BASE_URL"
echo -e "COOLIFY_API_KEY: $COOLIFY_API_KEY"
echo
exit 1
elif [ "$test_call" == "\033[1;42m SUCCESS \033[0m" ]; then
echo -e "\033[1;37mIf you can see this, we can successfully connect to Coolify!\033[0m"
fi
}
# ------------------------------------------------------------------------------
# CREATE PROJECTS AND DEPLOY DBS
# ------------------------------------------------------------------------------
create_projects_and_deploy_dbs() {
# DEFINE SERVER PAYLOADS
if [ $GH_PRIVATE -eq 0 ]; then
# Deploying from Public GitHub Repo
server_payload=$(cat <<EOF
{
"project_uuid": "$project_uuid",
"server_uuid": "$server_uuid",
"environment_name": "$environment_name",
"git_repository": "$git_repository",
"git_branch": "$git_branch",
"git_commit_sha": "$git_commit_sha",
"ports_exposes": "$server_ports_exposes",
"build_pack": "$server_build_pack",
"description": "$server_description",
"domains": "$server_domains",
"base_directory": "$server_base_directory",
"instant_deploy": $server_instant_deploy
}
EOF
)
elif [ $GH_PRIVATE -eq 1 ]; then
# Deploying from Private GitHub App
server_payload=$(cat <<EOF
{
"project_uuid": "$project_uuid",
"server_uuid": "$server_uuid",
"environment_name": "$environment_name",
"github_app_uuid": "$github_app_uuid",
"git_repository": "$git_repository",
"git_branch": "$git_branch",
"git_commit_sha": "$git_commit_sha",
"ports_exposes": "$server_ports_exposes",
"build_pack": "$server_build_pack",
"description": "$server_description",
"domains": "$server_domains",
"base_directory": "$server_base_directory",
"instant_deploy": $server_instant_deploy
}
EOF
)
elif [ $GH_PRIVATE -eq 2 ]; then
# Deploying from Private Key Git Repo
server_payload=$(cat <<EOF
{
"project_uuid": "$project_uuid",
"server_uuid": "$server_uuid",
"environment_name": "$environment_name",
"private_key_uuid": "$github_private_key_uuid",
"git_repository": "$git_repository",
"git_branch": "$git_branch",
"git_commit_sha": "$git_commit_sha",
"ports_exposes": "$server_ports_exposes",
"build_pack": "$server_build_pack",
"description": "$server_description",
"domains": "$server_domains",
"base_directory": "$server_base_directory",
"instant_deploy": $server_instant_deploy
}
EOF
)
fi
# DEFINE CLIENT PAYLOADS
if [ $GH_PRIVATE -eq 0 ]; then
# Deploying from Public GitHub Repo
client_payload=$(cat <<EOF
{
"project_uuid": "$project_uuid",
"server_uuid": "$server_uuid",
"environment_name": "$environment_name",
"git_repository": "$git_repository",
"git_branch": "$git_branch",
"git_commit_sha": "$git_commit_sha",
"ports_exposes": "$client_ports_exposes",
"build_pack": "$client_build_pack",
"description": "$client_description",
"domains": "$client_domains",
"base_directory": "$client_base_directory",
"instant_deploy": $client_instant_deploy
}
EOF
)
elif [ $GH_PRIVATE -eq 1 ]; then
# Deploying from Private GitHub App
client_payload=$(cat <<EOF
{
"project_uuid": "$project_uuid",
"server_uuid": "$server_uuid",
"environment_name": "$environment_name",
"github_app_uuid": "$github_app_uuid",
"git_repository": "$git_repository",
"git_branch": "$git_branch",
"git_commit_sha": "$git_commit_sha",
"ports_exposes": "$client_ports_exposes",
"build_pack": "$client_build_pack",
"description": "$client_description",
"domains": "$client_domains",
"base_directory": "$client_base_directory",
"instant_deploy": $client_instant_deploy
}
EOF
)
elif [ $GH_PRIVATE -eq 2 ]; then
# Deploying from Private GitHub App
client_payload=$(cat <<EOF
{
"project_uuid": "$project_uuid",
"server_uuid": "$server_uuid",
"environment_name": "$environment_name",
"private_key_uuid": "$github_private_key_uuid",
"git_repository": "$git_repository",
"git_branch": "$git_branch",
"git_commit_sha": "$git_commit_sha",
"ports_exposes": "$client_ports_exposes",
"build_pack": "$client_build_pack",
"description": "$client_description",
"domains": "$client_domains",
"base_directory": "$client_base_directory",
"instant_deploy": $client_instant_deploy
}
EOF
)
fi
# Check if we need to setup a dB
if [ $NEED_DB_SETUP -eq 1 ]; then
# Production db payload
create_prod_db_payload=$(cat <<EOF
{
"server_uuid": "$server_uuid",
"project_uuid": "$project_uuid",
"environment_name": "$environment_name",
"description": "Production dB",
"is_public": false,
"instant_deploy": true
}
EOF
)
fi
# Similarly, do we need a dev dB?
if [ $NEED_DEV_DB_SETUP -eq 1 ]; then
# Development db payload
create_dev_db_payload=$(cat <<EOF
{
"server_uuid": "$server_uuid",
"project_uuid": "$project_uuid",
"environment_name": "$environment_name",
"description": "Development dB",
"is_public": true,
"public_port": $dev_db_port,
"instant_deploy": true
}
EOF
)
fi
echo -e "\033[1;32m🤖 --- SETTING UP & CONFIGURING COOLIFY APPS for FRONTEND and BACKEND... ---\033[0m"
echo
# Setup the Server on Coolify
if [ $GH_PRIVATE -eq 0 ]; then
# Deploying from Public GitHub Repo
coolify_server_return=$(curl -s --request POST \
--url $COOLIFY_BASE_URL/api/v1/applications/public \
--header "$BEARER" \
--header 'Content-Type: application/json' \
-d "$server_payload")
elif [ $GH_PRIVATE -eq 1 ]; then
# Deploying from Private GitHub App
coolify_server_return=$(curl -s --request POST \
--url $COOLIFY_BASE_URL/api/v1/applications/private-github-app \
--header "$BEARER" \
--header 'Content-Type: application/json' \
-d "$server_payload")
elif [ $GH_PRIVATE -eq 2 ]; then
# Deploying from Private Key Git Repo
coolify_server_return=$(curl -s --request POST \
--url $COOLIFY_BASE_URL/api/v1/applications/private-deploy-key \
--header "$BEARER" \
--header 'Content-Type: application/json' \
-d "$server_payload")
fi
configured_server_uuid=$(jq -r ".uuid" <<< "$coolify_server_return")
possible_server_error=$(jq -r ".error" <<< "$coolify_server_return")
possible_server_msg=$(jq -r ".message" <<< "$coolify_server_return")
possible_server_uuid=$(jq -r ".uuid" <<< "$coolify_server_return")
if [ ! "$possible_server_error" == "null" ]; then
echo -e "$server_payload"
echo
echo -e "$possible_server_error"
echo -e "$possible_server_msg"
echo
echo -e "\033[1;31m💀 --- COOLIFY ERROR: Backend App Setup Failed! See above for possible details... ---\033[0m"
echo
exit 1
else
if [ "$possible_server_uuid" == "null" ]; then # If the UUID is null, we can assume the setup failed
echo -e "$coolify_server_return" # May not even be JSON, print it out
echo
echo -e "\033[1;31m💀 --- COOLIFY ERROR: Could not create new Server App! See above for possible details... ---\033[0m"
echo
exit 1
else # If we got here, we can assume a successful setup
echo -e "- SERVER UUID: $possible_server_uuid"
echo
echo -e "\033[33m✅ --- New SERVER App Successfully Created on Coolify! ---\033[0m"
echo
fi
fi
# Next, setup the Client
if [ $GH_PRIVATE -eq 0 ]; then
# Deploying from Public GitHub Repo
coolify_client_return=$(curl -s --request POST \
--url $COOLIFY_BASE_URL/api/v1/applications/public \
--header "$BEARER" \
--header 'Content-Type: application/json' \
-d "$client_payload")
elif [ $GH_PRIVATE -eq 1 ]; then
# Deploying from Private GitHub App
coolify_client_return=$(curl -s --request POST \
--url $COOLIFY_BASE_URL/api/v1/applications/private-github-app \
--header "$BEARER" \
--header 'Content-Type: application/json' \
-d "$client_payload")
elif [ $GH_PRIVATE -eq 2 ]; then
# Deploying from Private Key Git Repo
coolify_client_return=$(curl -s --request POST \
--url $COOLIFY_BASE_URL/api/v1/applications/private-deploy-key \
--header "$BEARER" \
--header 'Content-Type: application/json' \
-d "$client_payload")
fi
configured_client_uuid=$(jq -r ".uuid" <<< "$coolify_client_return")
possible_client_error=$(jq -r ".error" <<< "$coolify_client_return")
possible_client_message=$(jq -r ".message" <<< "$coolify_client_return")
possible_client_uuid=$(jq -r ".uuid" <<< "$coolify_client_return")
if [ ! "$possible_client_error" == "null" ]; then
echo -e "$client_payload"
echo
echo -e "$possible_client_error"
echo -e "$possible_client_message"
echo
echo -e "\033[1;31m💀 --- COOLIFY ERROR: Frontend App Setup Failed! See above for possible details... ---\033[0m"
echo
exit 1
else
if [ "$possible_client_uuid" == "null" ]; then # If the UUID is null, we can assume the setup failed
echo -e "$coolify_client_return" # May not even be JSON, print it out
echo
echo -e "\033[1;31m💀 --- COOLIFY ERROR: Could not create new Client App! See above for possible details... ---\033[0m"
echo
exit 1
else
echo -e "- CLIENT UUID: $possible_client_uuid"
echo
echo -e "\033[33m✅ --- New CLIENT App Successfully Created on Coolify! ---\033[0m"
echo
fi
fi
db_header=0
if [ $NEED_DB_SETUP -eq 1 ]; then
db_header=1
fi
if [ $NEED_DEV_DB_SETUP -eq 1 ]; then
db_header=1
fi
if [ $db_header -eq 1 ]; then
echo -e "\033[1;32m🤖 --- DEPLOYING ANY DATABASES REQUIRED ON COOLIFY... ---\033[0m"
echo
fi
# Create the Production Database and bring it online
if [ $NEED_DB_SETUP -eq 1 ]; then
create_prod_db_return=$(curl -s --request POST \
--url $COOLIFY_BASE_URL/api/v1/databases/postgresql \
--header "$BEARER" \
--header 'Content-Type: application/json' \
-d "$create_prod_db_payload")
possible_prod_db_uuid=$(jq -r ".uuid" <<< "$create_prod_db_return")
if [ "$possible_prod_db_uuid" == "null" ]; then # If the UUID is null, we can assume the setup failed
echo -e "$create_prod_db_return" # May not even be JSON, print it out
echo
echo -e "\033[1;31m💀 --- COOLIFY ERROR: Production Database Setup Failed! ---\033[0m"
echo
exit 1
else
prod_db_url=$(jq -r ".internal_db_url" <<< "$create_prod_db_return")
WASP_DATABASE_URL="$prod_db_url"
echo -e "- PROD DB UUID: $possible_prod_db_uuid"