-
Notifications
You must be signed in to change notification settings - Fork 33
/
cloud
executable file
·928 lines (885 loc) · 37.1 KB
/
cloud
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
#!/usr/bin/env bash
#
# Cloud installation management tool.
# Assumes its location is the top directory of KCIDB source tree.
# Deploying creates or updates an installation, withdrawing removes it.
#
# Conventions:
# - *_deploy functions create or update an installation;
# - *_withdraw functions delete installation if it exists;
# - no output should be produced unless something fails;
# - no error/message output on stdout, only stderr.
set -euCo pipefail
shopt -s extglob
shopt -s inherit_errexit
PATH="$(dirname "$(realpath "$0")")/kcidb/cloud:$PATH"
. artifacts.sh
. run.sh
. submitters.sh
. sections.sh
. bigquery.sh
. psql.sh
. storage.sh
. pubsub.sh
. firestore.sh
. functions.sh
. scheduler.sh
. password.sh
. secrets.sh
. iam.sh
. misc.sh
# Enable Google Cloud services using their short names
# Args: project [name...]
function services_enable() {
declare -r project="$1"; shift
declare -a names=("$@")
for ((i = 0; i < ${#names[@]}; i++)); do
names[$i]="${names[$i]}.googleapis.com"
done
mute gcloud services enable --quiet --project="$project" "${names[@]}"
}
# Check if the App Engine app exists.
# Args: project
# Output: "true" if the app exists, "false" otherwise.
function app_exists() {
declare -r project="$1"; shift
declare output
if output=$(
gcloud app describe --quiet --project="$project" 2>&1
); then
echo "true"
elif [[ $output == *\ does\ not\ contain\ * ]]; then
echo "false"
else
echo "$output" >&2
false
fi
}
# Create the App Engine app, if it doesn't exist.
# Args: project
function app_deploy() {
declare -r project="$1"; shift
exists=$(app_exists "$project")
if "$exists"; then
return
fi
mute gcloud app create --quiet --project="$project" --region=us-central
}
# Execute a deploy/env/shell/withdraw command
# Args: --command=NAME
# --format=yaml|sh
# --sections=EXTGLOB
# --project=NAME
# --namespace=STRING
# --extra-cc=ADDRS
# --smtp-to-addrs=STRING
# --smtp-mocked=true|false
# --test=true|false
# --log-level=NAME
# --optimize=PYTHONOPTIMIZE
# --heavy-asserts=true|false
# --updated-publish=true|false
# --submitters=WORDS
# --cost-thresholds=JSON
# --grafana-url=URL
# --grafana-public=true|false
# --grafana-anonymous=true|false
# --argv=WORDS
function execute_command() {
declare params
params="$(getopt_vars command \
format \
project \
sections \
namespace \
extra_cc \
smtp_to_addrs \
smtp_mocked \
test \
log_level \
optimize \
heavy_asserts \
updated_publish \
submitters \
cost_thresholds \
grafana_url \
grafana_public \
grafana_anonymous \
argv \
-- "$@")"
eval "$params"
# Convert submitters and extra args from a string of words to an array
eval "declare -a submitters=($submitters)"
eval "declare -a argv=($argv)"
declare -r prefix="${namespace}_"
declare -r dashed_prefix="${prefix//[^a-z0-9-]/-}"
declare -r load_queue_trigger_topic="${prefix}load_queue_trigger"
declare -r cache_bucket_name="${project}_${prefix}cache"
declare -r pick_notifications_trigger_topic="${prefix}pick_notifications_trigger"
declare -r purge_db_trigger_topic="${prefix}purge_db_trigger"
declare -r archive_trigger_topic="${prefix}archive_trigger"
declare -r cache_redirect_function_name="cache_redirect"
declare cache_redirector_url="https://${FUNCTION_REGION}"
declare cache_redirector_url+="-${project}.cloudfunctions.net/"
declare cache_redirector_url+="${prefix}"
declare -r cache_redirector_url+="${cache_redirect_function_name}"
declare -r updated_urls_topic="${prefix}updated_urls"
declare -r new_topic="${prefix}new"
declare -r new_load_subscription="${prefix}new_load"
declare -r new_debug_subscription="${prefix}new_debug"
declare -r updated_topic="${prefix}updated"
declare -r updated_debug_subscription="${prefix}updated_debug"
declare -r cost_topic="${prefix}cost"
declare -r spool_collection_path="${prefix}notifications"
declare -r psql_conn=$(
echo -n "${project}:"
echo -n "${PSQL_INSTANCE_REGION}:"
echo -n "${PSQL_INSTANCE}"
)
declare -r psql_socket_dir="/cloudsql/${psql_conn}"
declare -r psql_grafana_database="${prefix}grafana"
declare -r psql_grafana_user="${prefix}grafana"
declare -r psql_database="${namespace}"
declare -r psql_clean_test_database="${prefix}clean_test"
declare -r psql_empty_test_database="${prefix}empty_test"
declare -r psql_editor="${psql_database}_editor"
declare -r psql_pgpass_secret="${prefix}psql_pgpass"
declare -r psql_kcidb_db=$(
echo -n "postgresql: "
# Use the local proxy for the shell
if [ "$command" != "shell" ]; then
echo -n "host=${psql_socket_dir}" | escape_whitespace
echo -n " "
echo -n "user=${psql_editor}"
echo -n " "
fi
echo -n "dbname=${psql_database}" | escape_whitespace
)
declare -r psql_clean_test_kcidb_db=$(
echo -n "postgresql:"
echo -n "dbname=${psql_clean_test_database}" | escape_whitespace
)
declare -r psql_empty_test_kcidb_db=$(
echo -n "postgresql:"
echo -n "dbname=${psql_empty_test_database}" | escape_whitespace
)
declare -a psql_args=(
"$project"
"$psql_grafana_database" "$psql_grafana_user"
"$cache_redirector_url"
"$psql_database" "$psql_editor"
)
if "$test"; then
psql_args+=(
# Do not initialize the clean database
"#$psql_clean_test_database"
"${psql_clean_test_database}_editor"
"$psql_empty_test_database"
"${psql_empty_test_database}_editor"
)
fi
declare -a -r psql_args
# Enable fetching PostgreSQL passwords from their secrets
password_secret_set "psql_superuser" "$project" "kcidb_psql_superuser"
password_secret_set "psql_viewer" "$project" "kcidb_psql_viewer"
password_secret_set "psql_editor" "$project" "${prefix}psql_editor"
password_secret_set "psql_grafana" "$project" "${prefix}psql_grafana"
declare -r bigquery_dataset="${namespace}"
declare -r bigquery_clean_test_dataset="${prefix}clean_test"
declare -r bigquery_empty_test_dataset="${prefix}empty_test"
declare -r bigquery_kcidb_db="bigquery:${project}.${bigquery_dataset}"
declare -r bigquery_sample_dataset="${prefix}sample"
declare -r bigquery_sample_kcidb_db="bigquery:${project}.${bigquery_sample_dataset}"
declare bigquery_clean_test_kcidb_db="bigquery:${project}."
declare -r bigquery_clean_test_kcidb_db+="${bigquery_clean_test_dataset}"
declare bigquery_empty_test_kcidb_db="bigquery:${project}."
declare -r bigquery_empty_test_kcidb_db+="${bigquery_empty_test_dataset}"
declare -a bigquery_args=(
"$project" "$bigquery_dataset" "$bigquery_sample_dataset"
)
if "$test"; then
bigquery_args+=(
# Do not initialize the clean dataset
"#$bigquery_clean_test_dataset"
"$bigquery_empty_test_dataset"
)
fi
declare -a -r bigquery_args
declare -r database="$psql_kcidb_db"
if "$test"; then
declare -r sqlite_clean_test_file="$TMPDIR/clean.sqlite3"
touch "$sqlite_clean_test_file"
declare clean_test_databases=$(
echo -n "sqlite:$sqlite_clean_test_file" | escape_whitespace
echo -n " "
echo -n "$psql_clean_test_kcidb_db" | escape_whitespace
echo -n " "
echo -n "$bigquery_clean_test_kcidb_db" | escape_whitespace
)
declare -r clean_test_databases=$(
echo -n "$clean_test_databases"
echo -n " "
echo -n "mux:$clean_test_databases" | escape_whitespace
)
else
declare -r clean_test_databases=""
fi
if "$test"; then
declare -r sqlite_empty_test_kcidb_db="sqlite:$TMPDIR/empty.sqlite3"
mute kcidb-db-init -lDEBUG -d "$sqlite_empty_test_kcidb_db"
declare empty_test_databases=$(
echo -n "$sqlite_empty_test_kcidb_db" | escape_whitespace
echo -n " "
echo -n "$psql_empty_test_kcidb_db" | escape_whitespace
echo -n " "
echo -n "$bigquery_empty_test_kcidb_db" | escape_whitespace
)
declare -r empty_test_databases=$(
echo -n "$empty_test_databases"
echo -n " "
echo -n "mux:$empty_test_databases" | escape_whitespace
)
else
declare -r empty_test_databases=""
fi
# Register SMTP password secret
password_secret_set "smtp" "$project" "$SECRETS_SMTP_PASSWORD"
declare -r smtp_topic=$("$smtp_mocked" && echo "${prefix}smtp" || true)
declare -r smtp_subscription=$(
"$smtp_mocked" && echo "${prefix}smtp_received" || true
)
# Docker artifact repository name (must match [a-z0-9-]{0,62}[a-z0-9])
declare -r docker_repo="${dashed_prefix}docker"
# Cost monitor Cloud Run service name
declare -r cost_mon_service="${dashed_prefix}cost-mon"
# Cost monitor docker image
declare cost_mon_image="${ARTIFACTS_REGION}-docker.pkg.dev/"
cost_mon_image+="$project/$docker_repo/cost-mon"
declare -r cost_mon_image
# Cost Updater service account name (must match [a-z0-9-]{0,62}[a-z0-9])
declare -r cost_upd="${dashed_prefix}cost-upd"
# Cost updater serice account
declare cost_upd_service_account="$cost_upd"
cost_upd_service_account+="@$project.iam.gserviceaccount.com"
declare -r cost_upd_service_account
# Grafana Cloud Run service name (must match [a-z0-9-]{0,62}[a-z0-9])
declare -r grafana_service="${dashed_prefix}grafana"
# Issue editor Cloud Run service name
declare -r iss_ed_service="${dashed_prefix}iss-ed"
# Issue editor docker image
declare iss_ed_image="${ARTIFACTS_REGION}-docker.pkg.dev/"
iss_ed_image+="$project/$docker_repo/iss-ed"
declare -r iss_ed_image
declare -r -a env_args=(
--project="$project"
--log-level="$log_level"
--cache-bucket-name="$cache_bucket_name"
--cache-redirector-url="$cache_redirector_url"
--optimize="$optimize"
--heavy-asserts="$heavy_asserts"
--new-topic="$new_topic"
--new-load-subscription="$new_load_subscription"
--updated-publish="$updated_publish"
--updated-topic="$updated_topic"
--load-queue-trigger-topic="$load_queue_trigger_topic"
--purge-db-trigger-topic="$purge_db_trigger_topic"
--archive-trigger-topic="$archive_trigger_topic"
--updated-urls-topic="$updated_urls_topic"
--spool-collection-path="$spool_collection_path"
--extra-cc="$extra_cc"
--smtp-to-addrs="$smtp_to_addrs"
--smtp-password-secret="$SECRETS_SMTP_PASSWORD"
--smtp-topic="$smtp_topic"
--smtp-subscription="$smtp_subscription"
--pgpass-secret="$psql_pgpass_secret"
--op-database="$psql_kcidb_db"
--ar-database="$bigquery_kcidb_db"
--sm-database="$bigquery_sample_kcidb_db"
--database="$database"
--clean-test-databases="$clean_test_databases"
--empty-test-databases="$empty_test_databases"
)
# Handle "env" command
if [ "$command" == "env" ]; then
functions_env --format="$format" "${env_args[@]}"
return
# Handle "shell" command
elif [ "$command" == "shell" ]; then
export GCP_PROJECT="$project"
source <(functions_env --format=sh "${env_args[@]}")
export KCIDB_DEPLOYMENT="${KCIDB_DEPLOYMENT:-1}"
if (( ${#argv[@]} )); then
argv=("$SHELL" "-c" "${argv[*]@Q}")
else
argv=("$SHELL" "-i")
fi
psql_proxy_session "$project" "$PSQL_INSTANCE" "${argv[@]}"
return
fi
declare -r -a env_yaml=$(
functions_env --format=yaml "${env_args[@]}"
)
# Make sure requisite services are enabled
if [ "$command" != "shutdown" ]; then
app_deploy "$project"
services_enable "$project" appengine secretmanager cloudfunctions \
cloudbuild cloudscheduler firestore \
sqladmin storage run iam bigquery
# Enable generating PostgreSQL superuser password (if not specified),
# if the instance doesn't exist yet
declare exists
exists=$(psql_instance_exists "$project" "$PSQL_INSTANCE")
if ! "$exists"; then
password_set_generate psql_superuser true
fi
fi
# Handle "deploy" command
if [ "$command" == "deploy" ]; then
sections_run "$sections" iam_deploy \
"$project" "$grafana_service" \
"$cost_upd" "$cost_mon_service" "$iss_ed_service"
sections_run "$sections" secrets_deploy "$project" \
"$psql_pgpass_secret" "$psql_editor" "$grafana_service"
sections_run "$sections" bigquery_deploy "${bigquery_args[@]}"
sections_run "$sections" psql_deploy "${psql_args[@]}"
sections_run "$sections" artifacts_deploy "$project" \
"$docker_repo" "$cost_mon_image" "$iss_ed_image"
sections_run "$sections" run_deploy \
--project="$project" \
--grafana-service="$grafana_service" \
--grafana-url="$grafana_url" \
--grafana-public="$grafana_public" \
--grafana-anonymous="$grafana_anonymous" \
--psql-conn="$psql_conn" \
--psql-grafana-user="$psql_grafana_user" \
--psql-grafana-database="$psql_grafana_database" \
--cost-thresholds="$cost_thresholds" \
--cost-mon-service="$cost_mon_service" \
--cost-mon-image="$cost_mon_image" \
--cost-upd-service-account="$cost_upd_service_account" \
--iss-ed-service="$iss_ed_service" \
--iss-ed-image="$iss_ed_image" \
--new-topic="$new_topic"
sections_run "$sections" pubsub_deploy \
--project="$project" \
--load-queue-trigger-topic="$load_queue_trigger_topic" \
--new-topic="$new_topic" \
--new-load-subscription="$new_load_subscription" \
--new-debug-subscription="$new_debug_subscription" \
--updated-topic="$updated_topic" \
--updated-urls-topic="$updated_urls_topic" \
--updated-debug-subscription="$updated_debug_subscription" \
--pick-notifications-trigger-topic \
"$pick_notifications_trigger_topic" \
--purge-db-trigger-topic "$purge_db_trigger_topic" \
--archive-trigger-topic "$archive_trigger_topic" \
--smtp-topic="$smtp_topic" \
--smtp-subscription="$smtp_subscription" \
--cost-topic="$cost_topic" \
--cost-upd-service-account="$cost_upd_service_account" \
--cost-mon-service="$cost_mon_service" \
--iss-ed-service="$iss_ed_service"
sections_run "$sections" firestore_deploy "$project"
sections_run "$sections" storage_deploy "$project" "$cache_bucket_name"
functions_deploy \
--sections="$sections" \
--project="$project" \
--prefix="$prefix" \
--source="$(dirname "$(realpath "$0")")" \
--load-queue-trigger-topic="$load_queue_trigger_topic" \
--pick-notifications-trigger-topic \
"$pick_notifications_trigger_topic" \
--purge-db-trigger-topic "$purge_db_trigger_topic" \
--archive-trigger-topic "$archive_trigger_topic" \
--updated-urls-topic="$updated_urls_topic" \
--updated-topic="$updated_topic" \
--cache-redirect-function-name="$cache_redirect_function_name" \
--spool-collection-path="$spool_collection_path" \
--env-yaml="${env_yaml}"
sections_run "$sections" scheduler_deploy \
--project="$project" \
--prefix="$prefix" \
--load-queue-trigger-topic="$load_queue_trigger_topic" \
--pick-notifications-trigger-topic \
"$pick_notifications_trigger_topic" \
--purge-db-trigger-topic="$purge_db_trigger_topic" \
--archive-trigger-topic="$archive_trigger_topic"
sections_run "$sections" submitters_deploy \
"$project" "$new_topic" "${submitters[@]}"
# Handle "shutdown" command
elif [ "$command" == "shutdown" ]; then
sections_run "$sections" submitters_shutdown \
"$project" "$new_topic" "${submitters[@]}"
functions_shutdown \
--sections="$sections" \
--project="$project" \
--prefix="$prefix" \
--cache-redirect-function-name="$cache_redirect_function_name"
sections_run "$sections" run_shutdown \
--project="$project" \
--grafana-service="$grafana_service"
# Handle "withdraw" command
elif [ "$command" == "withdraw" ]; then
sections_run "$sections" submitters_withdraw \
"$project" "$new_topic" "${submitters[@]}"
sections_run "$sections" scheduler_withdraw "$project" "$prefix"
functions_withdraw \
--sections="$sections" \
--project="$project" \
--prefix="$prefix" \
--cache-redirect-function-name="$cache_redirect_function_name"
sections_run "$sections" storage_withdraw "$cache_bucket_name"
sections_run "$sections" firestore_withdraw \
"$project" "$spool_collection_path"
sections_run "$sections" pubsub_withdraw \
--project="$project" \
--load-queue-trigger-topic="$load_queue_trigger_topic" \
--pick-notifications-trigger-topic \
"$pick_notifications_trigger_topic" \
--purge-db-trigger-topic "$purge_db_trigger_topic" \
--archive-trigger-topic "$archive_trigger_topic" \
--new-topic="$new_topic" \
--new-load-subscription="$new_load_subscription" \
--new-debug-subscription="$new_debug_subscription" \
--updated-urls-topic="$updated_urls_topic" \
--updated-topic="$updated_topic" \
--updated-debug-subscription="$updated_debug_subscription" \
--smtp-topic="$smtp_topic" \
--smtp-subscription="$smtp_subscription" \
--cost-topic="$cost_topic" \
--cost-upd-service-account="$cost_upd_service_account"
sections_run "$sections" run_withdraw \
--project="$project" \
--grafana-service="$grafana_service" \
--cost-mon-service="$cost_mon_service" \
--cost-upd-service-account="$cost_upd_service_account" \
--iss-ed-service="$iss_ed_service"
sections_run "$sections" artifacts_withdraw "$project" "$docker_repo"
sections_run "$sections" psql_withdraw "${psql_args[@]}"
sections_run "$sections" bigquery_withdraw "${bigquery_args[@]}"
sections_run "$sections" secrets_withdraw "$project" \
"$psql_pgpass_secret"
sections_run "$sections" iam_withdraw \
"$project" "$grafana_service" \
"$cost_upd" "$cost_mon_service" "$iss_ed_service"
fi
}
# Output usage information
function usage() {
echo "Usage: $(basename "$0") [OPTION...] COMMAND [COMMAND_ARGUMENT...]"
echo "Manage KCIDB installation in a Google Cloud project."
echo ""
echo "Commands:"
echo ""
echo " deploy Deploy an installation to the cloud."
echo " env Output environment YAML for Cloud Functions."
echo " shell Execute a shell with deployment environment."
echo " shutdown Shutdown an installation in the cloud."
echo " withdraw Withdraw an installation from the cloud."
echo " list-sections List sections of the installation."
echo ""
echo "Options:"
echo ""
echo " -h, --help"
echo " Display this usage message and exit."
echo " To get command usage message run "
echo " $(basename "$0") COMMAND -h/--help."
echo ""
}
# Output deploy command usage information
function usage_deploy() {
echo "Usage: $(basename "$0") deploy [OPTION...] PROJECT NAMESPACE"
echo "Deploy a KCIDB installation to a Google Cloud project."
echo ""
echo "Options:"
echo ""
echo " -h, --help"
echo " Display this usage message and exit."
echo " -v, --verbose"
echo " Output the deployment steps being executed."
echo " -s, --sections=EXTGLOB"
echo " Specify an extended shell glob matching the"
echo " installation sections to limit deployment to."
echo " See output of \"$(basename "$0") list-sections\""
echo " for a list of available sections."
echo " --extra-cc=ADDRS"
echo " Add specified addresses to CC of all emails."
echo " --smtp-to-addrs=ADDRS"
echo " Specify a comma-separated list of addresses"
echo " to override recipients of email notifications."
echo " --smtp-password-file=FILE"
echo " Specify a file with the SMTP server password,"
echo " or \"-\" to read it from stdin."
echo " --smtp-mocked"
echo " Post notification messages to a PubSub topic,"
echo " instead of sending them to the SMTP server."
echo " Used when testing deployments."
echo " --psql-password-file=FILE"
echo " Specify a file with the password for the PostgreSQL"
echo " superuser, or \"-\" to read it from stdin."
echo " --log-level=NAME"
echo " Specify Python log level NAME for Cloud Functions."
echo " Default is INFO."
echo " --optimize=PYTHONOPTIMIZE"
echo " Specify a value for PYTHONOPTIMIZE to be added to the "
echo " environment. Default is an empty string."
echo " --heavy-asserts"
echo " Enable heavy assertion checking in deployment."
echo " --mute-updates"
echo " Disable posting updates about loaded data."
echo " --test"
echo " Deploy test resources in various sections."
echo " --submitter=NAME"
echo " Specify a service account to permit submissions for."
echo " Repeat to add more submitters."
echo " --cost-thresholds=JSON"
echo " Specify thresholds JSON for cost monitor."
echo " Must be an array of arrays, each subarray containing a"
echo " threshold cost and an optional shell command to execute."
echo " A subarray without command can be just the cost instead."
echo " --grafana-url=URL"
echo " Assume Grafana is served from the specified URL."
echo " Default is \"http://localhost:3000\"."
echo " --grafana-public"
echo " Permit public access to Grafana deployment in Cloud Run."
echo " --grafana-anonymous"
echo " Permit anonymous access to Grafana."
echo ""
echo "Positional arguments:"
echo ""
echo " PROJECT Google Cloud project ID, e.g. \"kernelci-production\"."
echo " NAMESPACE Namespace for all objects, e.g. \"test\"."
echo ""
}
# Output env command usage information
function usage_env() {
echo "Usage: $(basename "$0") env [OPTION...] PROJECT NAMESPACE"
echo "Output environment YAML used by KCIDB Cloud Functions."
echo ""
echo "Options:"
echo ""
echo " -h, --help"
echo " Display this usage message and exit."
echo " --format=FORMAT"
echo " Specify either \"yaml\" or \"sh\" as the output"
echo " format. Default is \"yaml\"."
echo " --extra-cc=ADDRS"
echo " Add specified addresses to CC of all emails."
echo " --smtp-to-addrs=ADDRS"
echo " Specify a comma-separated list of addresses"
echo " to override recipients of email notifications."
echo " --smtp-mocked"
echo " Post notification messages to a PubSub topic,"
echo " instead of sending them to the SMTP server."
echo " Used when testing deployments."
echo " --log-level=NAME"
echo " Specify Python log level NAME for Cloud Functions."
echo " Default is INFO."
echo " --optimize=PYTHONOPTIMIZE"
echo " Specify a value for PYTHONOPTIMIZE to be added to the "
echo " environment. Default is an empty string."
echo " --heavy-asserts"
echo " Enable heavy assertion checking in deployment."
echo " --mute-updates"
echo " Disable posting updates about loaded data."
echo " --test"
echo " Enable various test resources."
echo ""
echo "Positional arguments:"
echo ""
echo " PROJECT Google Cloud project ID, e.g. \"kernelci-production\"."
echo " NAMESPACE Namespace for all objects, e.g. \"test\"."
echo ""
}
# Output shell command usage information
function usage_shell() {
echo "Usage: $(basename "$0") shell [OPTION...]" \
"PROJECT NAMESPACE [CMD [ARG...]]"
echo "Execute a shell with deployment environment."
echo ""
echo "Options:"
echo ""
echo " -h, --help"
echo " Display this usage message and exit."
echo " --extra-cc=ADDRS"
echo " Add specified addresses to CC of all emails."
echo " --smtp-to-addrs=ADDRS"
echo " Specify a comma-separated list of addresses"
echo " to override recipients of email notifications."
echo " --smtp-mocked"
echo " Expect notification messages to be posted to a"
echo " PubSub topic, instead of being sent to the SMTP"
echo " server."
echo " --log-level=NAME"
echo " Specify Python log level NAME for Cloud Functions."
echo " Default is INFO."
echo " --optimize=PYTHONOPTIMIZE"
echo " Specify a value for PYTHONOPTIMIZE to be added to the "
echo " environment. Default is an empty string."
echo " --heavy-asserts"
echo " Enable heavy assertion checking in deployment."
echo " --mute-updates"
echo " Expect updates about loaded data to be disabled."
echo " --test"
echo " Expect various test resources to be deployed."
echo ""
echo "Positional arguments:"
echo ""
echo " PROJECT Google Cloud project ID, e.g. \"kernelci-production\"."
echo " NAMESPACE Namespace for all objects, e.g. \"test\"."
echo " CMD The command to execute inside the shell."
echo " If not specified, an interactive shell is started."
echo " ARG An argument to pass to the command, if specified."
echo ""
}
# Output shutdown command usage information
function usage_shutdown() {
echo "Usage: $(basename "$0") shutdown [OPTION...] PROJECT NAMESPACE"
echo "Shutdown a KCIDB installation in a Google Cloud project."
echo ""
echo "Options:"
echo ""
echo " -h, --help"
echo " Display this usage message and exit."
echo " -v, --verbose"
echo " Output the shutdown steps being executed."
echo " -s, --sections=EXTGLOB"
echo " Specify an extended shell glob matching the"
echo " installation sections to limit shutdown to."
echo " See output of \"$(basename "$0") list-sections\""
echo " for a list of available sections."
echo " --smtp-mocked"
echo " The project being shut down uses a PubSub topic for"
echo " receiving notification messages instead of the SMTP"
echo " server"
echo " --test"
echo " The project being shut down has test resources"
echo " deployed."
echo " --submitter=NAME"
echo " Specify a service account to shutdown submissions"
echo " for. Repeat to add more submitters."
echo ""
echo "Positional arguments:"
echo ""
echo " PROJECT Google Cloud project ID, e.g. \"kernelci-production\"."
echo " NAMESPACE Namespace for all objects, e.g. \"test\"."
echo ""
}
# Output withdraw command usage information
function usage_withdraw() {
echo "Usage: $(basename "$0") withdraw [OPTION...] PROJECT NAMESPACE"
echo "Withdraw a KCIDB installation from a Google Cloud project."
echo ""
echo "Options:"
echo ""
echo " -h, --help"
echo " Display this usage message and exit."
echo " -v, --verbose"
echo " Output the withdrawal steps being executed."
echo " -s, --sections=EXTGLOB"
echo " Specify an extended shell glob matching the"
echo " installation sections to limit withdrawal to."
echo " See output of \"$(basename "$0") list-sections\""
echo " for a list of available sections."
echo " --smtp-mocked"
echo " Withdraw the PubSub topic receiving notification"
echo " messages instead of the SMTP server"
echo " Used when testing deployments."
echo " --test"
echo " Withdraw test resources from various sections."
echo " --submitter=NAME"
echo " Specify a service account to deny submissions for."
echo " Repeat to add more submitters."
echo ""
echo "Positional arguments:"
echo ""
echo " PROJECT Google Cloud project ID, e.g. \"kernelci-production\"."
echo " NAMESPACE Namespace for all objects, e.g. \"test\"."
echo ""
}
# Output list-sections command usage information
function usage_list_sections() {
echo "Usage: $(basename "$0") list-sections [OPTION...] [EXTGLOB]"
echo "List sections of a KCIDB installation."
echo ""
echo "Options:"
echo ""
echo " -h, --help"
echo " Display this usage message and exit."
echo ""
echo "Positional arguments:"
echo ""
echo " EXTGLOB Extended shell glob matching the installation"
echo " sections to list. Default is \"*\"."
echo ""
}
# Execute
# Args: [argument...]
function execute() {
declare args_expr
# Make sure getopt compatibility isn't enforced
unset GETOPT_COMPATIBLE
# Check if getopt is enhanced and supports quoting
if getopt --test >/dev/null; [ $? != 4 ]; then
echo "Enhanced getopt not found" >&2
exit 1
fi
# Parse global command-line arguments
args_expr=$(getopt --name $(basename "$0") \
--options "+h" --longoptions "help" \
-- "$@")
eval set -- "$args_expr"
while true; do
case "$1" in
-h|--help) usage; exit 0;;
--) shift; break;;
*) echo "Unknown option: $1" >&2; exit 1;;
esac
done
if (( $# < 1 )); then
echo "Command is not specified" >&2
usage >&2
exit 1
fi
# Parse command and its arguments
declare -r command="$1"; shift
declare getopt_shortopts="h"
declare getopt_longopts="help"
if [[ $command == @(deploy|env|shell|shutdown|withdraw) ]]; then
getopt_longopts+=",smtp-mocked,test"
if [[ $command == @(deploy|shutdown|withdraw) ]]; then
getopt_shortopts+="vs:"
getopt_longopts+=",verbose,sections:,submitter:"
fi
if [[ $command == @(deploy|env|shell) ]]; then
getopt_longopts+=",extra-cc:,smtp-to-addrs:"
getopt_longopts+=",log-level:,optimize:,heavy-asserts,mute-updates"
if [[ $command == env ]]; then
getopt_longopts+=",format:"
fi
fi
if [[ $command == @(deploy) ]]; then
getopt_longopts+=",smtp-password-file:"
getopt_longopts+=",psql-password-file:"
getopt_longopts+=",cost-thresholds:"
getopt_longopts+=",grafana-url:"
getopt_longopts+=",grafana-public"
getopt_longopts+=",grafana-anonymous"
fi
elif [ "$command" == "list-sections" ]; then
:
else
echo "Unknown command: ${command@Q}" >&2
usage >&2
exit 1
fi
# Parse command-line arguments
args_expr=$(getopt --name $(basename "$0") \
--options "$getopt_shortopts" \
--longoptions "$getopt_longopts" \
-- "$@")
eval set -- "$args_expr"
# Read option arguments
declare sections="*"
declare extra_cc=""
declare smtp_to_addrs=""
declare smtp_mocked="false"
declare test="false"
declare log_level="INFO"
declare optimize=""
declare heavy_asserts="false"
declare updated_publish="true"
declare -a submitters=()
declare format="yaml"
declare cost_thresholds="[]"
declare grafana_url="http://localhost:3000"
declare grafana_public="false"
declare grafana_anonymous="false"
while true; do
case "$1" in
-h|--help) "usage_${command//-/_}"; exit 0;;
-v|--verbose) VERBOSE="true"; shift;;
-s|--sections) sections="$2"; shift 2;;
--extra-cc) extra_cc="$2"; shift 2;;
--smtp-to-addrs) smtp_to_addrs="$2"; shift 2;;
--smtp-password-file) password_set_file smtp "$2"; shift 2;;
--smtp-mocked) smtp_mocked="true"; shift;;
--psql-password-file)
password_set_file psql_superuser "$2"; shift 2;;
--log-level) log_level="$2"; shift 2;;
--optimize) optimize="$2"; shift 2;;
--heavy-asserts) heavy_asserts="true"; shift;;
--mute-updates) updated_publish="false"; shift;;
--test) test="true"; shift;;
--format) format="$2"; shift 2;;
--submitter) submitters+=("$2"); shift 2;;
--cost-thresholds) cost_thresholds="$2"; shift 2;;
--grafana-url) grafana_url="$2"; shift 2;;
--grafana-public) grafana_public="true"; shift;;
--grafana-anonymous) grafana_anonymous="true"; shift;;
--) shift; break;;
*) echo "Unknown option: $1" >&2; exit 1;;
esac
done
# Check the thresholds schema is valid
if ! json_validate kcidb/cloud/cost-thresholds.schema.json \
<<<"$cost_thresholds"; then
echo "Invalid cost thresholds JSON provided: $cost_thresholds" >&2
exit 1
fi
# Process positional arguments according to the command invoked
if [ "$command" == "list-sections" ]; then
if (( $# > 1 )); then
echo "Invalid number of positional arguments" >&2
"usage_${command//-/_}" >&2
exit 1
fi
declare glob="${1:-*}"
declare name
printf '%s\n' "${!SECTIONS[@]}" | sort |
while read -r name; do
if [[ $name == $glob ]]; then
printf "%-${SECTIONS_NAME_LEN_MAX}s %s\\n" \
"$name" "${SECTIONS[$name]}"
fi
done
exit 0
elif [ "$command" == "shell" ]; then
if (( $# < 2 )); then
echo "Invalid number of positional arguments" >&2
"usage_${command//-/_}" >&2
exit 1
fi
else
if (( $# != 2 )); then
echo "Invalid number of positional arguments" >&2
"usage_${command//-/_}" >&2
exit 1
fi
fi
# Read and normalize positional arguments
declare -r project="$1"; shift
declare -r namespace="$1"; shift
declare -r -a argv=("$@")
# Execute the command
execute_command --command="$command" \
--format="$format" \
--sections="$sections" \
--project="$project" \
--namespace="${namespace:-}${namespace:+_}kcidb" \
--extra-cc="$extra_cc" \
--smtp-to-addrs="$smtp_to_addrs" \
--smtp-mocked="$smtp_mocked" \
--test="$test" \
--log-level="$log_level" \
--optimize="$optimize" \
--heavy-asserts="$heavy_asserts" \
--updated-publish="$updated_publish" \
--submitters="${submitters[*]@Q}" \
--cost-thresholds="$cost_thresholds" \
--grafana-url="$grafana_url" \
--grafana-public="$grafana_public" \
--grafana-anonymous="$grafana_anonymous" \
--argv="${argv[*]@Q}"
}
execute "$@"