|
| 1 | +#! /bin/bash |
| 2 | +# Deploys or deletes broker Cloud Run service |
| 3 | +# This script will not delete Cloud Run services that are in production |
| 4 | + |
| 5 | +# "False" uses production resources |
| 6 | +# any other string will be appended to the names of all resources |
| 7 | +testid="${1:-test}" |
| 8 | +# "True" tearsdown/deletes resources, else setup |
| 9 | +teardown="${2:-False}" |
| 10 | +# name of the survey this broker instance will ingest |
| 11 | +survey="${3:-lvk}" |
| 12 | +region="${4:-us-central1}" |
| 13 | +versiontag="${5:-v1_0}" |
| 14 | +# get the environment variable |
| 15 | +PROJECT_ID=$GOOGLE_CLOUD_PROJECT |
| 16 | +PROJECT_NUMBER=$(gcloud projects describe "$PROJECT_ID" --format="value(projectNumber)") |
| 17 | + |
| 18 | +MODULE_NAME="alerts-to-storage" # lower case required by cloud run |
| 19 | +ROUTE_RUN="/" # url route that will trigger main.run() |
| 20 | + |
| 21 | +define_GCP_resources() { |
| 22 | + local base_name="$1" |
| 23 | + local separator="${2:--}" |
| 24 | + local testid_suffix="" |
| 25 | + |
| 26 | + if [ "$testid" != "False" ] && [ -n "$testid" ]; then |
| 27 | + testid_suffix="${separator}${testid}" |
| 28 | + fi |
| 29 | + echo "${base_name}${testid_suffix}" |
| 30 | +} |
| 31 | + |
| 32 | +#--- GCP resources used in this script |
| 33 | +artifact_registry_repo=$(define_GCP_resources "${survey}-cloud-run-services") |
| 34 | +cr_module_name=$(define_GCP_resources "${survey}-${MODULE_NAME}") # lower case required by cloud run |
| 35 | +gcs_alerts_bucket=$(define_GCP_resources "${PROJECT_ID}-${survey}_alerts") |
| 36 | +ps_deadletter_topic=$(define_GCP_resources "${survey}-deadletter") |
| 37 | +ps_input_subscrip=$(define_GCP_resources "${survey}-alerts_raw") # pub/sub subscription used to trigger cloud run module |
| 38 | +ps_topic_alerts_in_bucket=$(define_GCP_resources "projects/${PROJECT_ID}/topics/${survey}-alerts_in_bucket") |
| 39 | +ps_trigger_topic=$(define_GCP_resources "${survey}-alerts_raw") |
| 40 | +runinvoker_svcact="cloud-run-invoker@${PROJECT_ID}.iam.gserviceaccount.com" |
| 41 | +service_account="service-${PROJECT_NUMBER}@gcp-sa-pubsub.iam.gserviceaccount.com" |
| 42 | + |
| 43 | +if [ "${teardown}" = "True" ]; then |
| 44 | + # ensure that we do not teardown production resources |
| 45 | + if [ "${testid}" != "False" ]; then |
| 46 | + echo |
| 47 | + echo "Deleting resources for ${MODULE_NAME} module..." |
| 48 | + gsutil rm -r "gs://${gcs_alerts_bucket}" |
| 49 | + gcloud pubsub subscriptions delete "${ps_input_subscrip}" |
| 50 | + gcloud pubsub topics delete "${ps_topic_alerts_in_bucket}" |
| 51 | + gcloud run services delete "${cr_module_name}" --region "${region}" |
| 52 | + else |
| 53 | + echo 'ERROR: No testid supplied.' |
| 54 | + echo 'To avoid accidents, this script will not delete production resources.' |
| 55 | + echo 'If that is your intention, you must delete them manually.' |
| 56 | + echo 'Otherwise, please supply a testid.' |
| 57 | + exit 1 |
| 58 | + fi |
| 59 | +else |
| 60 | + echo |
| 61 | + echo "Creating gcs_alert_bucket, uploading files, and setting permissions..." |
| 62 | + if ! gsutil ls -b "gs://${gcs_alerts_bucket}" >/dev/null 2>&1; then |
| 63 | + #--- Create the bucket that will store the alerts |
| 64 | + gsutil mb -b on -l "${region}" "gs://${gcs_alerts_bucket}" |
| 65 | + gsutil uniformbucketlevelaccess set on "gs://${gcs_alerts_bucket}" |
| 66 | + gsutil requesterpays set on "gs://${gcs_alerts_bucket}" |
| 67 | + # set IAM policies on public GCP resources |
| 68 | + if [ "$testid" = "False" ]; then |
| 69 | + gcloud storage buckets add-iam-policy-binding "gs://${gcs_alerts_bucket}" \ |
| 70 | + --member="allUsers" \ |
| 71 | + --role="roles/storage.objectViewer" |
| 72 | + fi |
| 73 | + else |
| 74 | + echo "${gcs_alerts_bucket} already exists." |
| 75 | + fi |
| 76 | + |
| 77 | + echo |
| 78 | + echo "Configuring Pub/Sub notifications on GCS bucket..." |
| 79 | + trigger_event=OBJECT_FINALIZE |
| 80 | + format=json # json or none; if json, file metadata sent in message body |
| 81 | + gsutil notification create \ |
| 82 | + -t "$ps_topic_alerts_in_bucket" \ |
| 83 | + -e "$trigger_event" \ |
| 84 | + -f "$format" \ |
| 85 | + "gs://${gcs_alerts_bucket}" |
| 86 | + |
| 87 | + #--- Deploy Cloud Run service |
| 88 | + echo |
| 89 | + echo "Creating container image for ${MODULE_NAME} module and deploying to Cloud Run..." |
| 90 | + moduledir="." # assumes deploying what's in our current directory |
| 91 | + config="${moduledir}/cloudbuild.yaml" |
| 92 | + # deploy the service and capture the endpoint's URL |
| 93 | + url=$(gcloud builds submit --config="${config}" \ |
| 94 | + --substitutions="_SURVEY=${survey},_TESTID=${testid},_MODULE_NAME=${cr_module_name},_REPOSITORY=${artifact_registry_repo},_VERSIONTAG=${versiontag}" \ |
| 95 | + "${moduledir}" | sed -n 's/^Step #2: Service URL: \(.*\)$/\1/p') |
| 96 | + echo |
| 97 | + echo "Creating trigger subscription for ${MODULE_NAME} Cloud Run service..." |
| 98 | + gcloud pubsub subscriptions create "${ps_input_subscrip}" \ |
| 99 | + --topic "${ps_trigger_topic}" \ |
| 100 | + --topic-project "${PROJECT_ID}" \ |
| 101 | + --ack-deadline=600 \ |
| 102 | + --push-endpoint="${url}${ROUTE_RUN}" \ |
| 103 | + --push-auth-service-account="${runinvoker_svcact}" \ |
| 104 | + --dead-letter-topic="${ps_deadletter_topic}" \ |
| 105 | + --max-delivery-attempts=5 |
| 106 | + gcloud pubsub subscriptions add-iam-policy-binding "${ps_input_subscrip}" \ |
| 107 | + --member="serviceAccount:${service_account}" \ |
| 108 | + --role="roles/pubsub.subscriber" |
| 109 | +fi |
0 commit comments