11#! /bin/bash
22# Create and configure GCP resources needed to run the nightly broker.
33
4- testid=" ${1:- test} "
54# "False" uses production resources
65# any other string will be appended to the names of all resources
7- teardown =" ${2 :- False } "
6+ testid =" ${1 :- test } "
87# "True" tearsdown/deletes resources, else setup
9- survey =" ${3 :- lvk } "
8+ teardown =" ${2 :- False } "
109# name of the survey this broker instance will ingest
11- region=" ${4:- us-central1} "
10+ survey=" ${3:- lvk} "
11+ schema_version=" ${4:- 1.0} "
12+ versiontag=v$( echo " ${schema_version} " | tr . _) # 1.0 -> v1_0
13+ region=" ${5:- us-central1} "
1214zone=" ${region} -a" # just use zone "a" instead of adding another script arg
1315
1416PROJECT_ID=$GOOGLE_CLOUD_PROJECT # get the environment variable
2022echo " GOOGLE_CLOUD_PROJECT = ${PROJECT_ID} "
2123echo " survey = ${survey} "
2224echo " testid = ${testid} "
25+ echo " schema_version = ${schema_version} "
2326echo " teardown = ${teardown} "
2427echo
2528echo " Continue? [y/(n)]: "
3437
3538# --- GCP resources used directly in this script
3639broker_bucket=" ${PROJECT_ID} -${survey} -broker_files"
40+ bq_dataset=" ${survey} "
3741topic_alerts=" ${survey} -alerts"
42+ topic_storebigquery=" ${survey} -bigquery"
43+
3844# use test resources, if requested
3945if [ " $testid " != " False" ]; then
4046 broker_bucket=" ${broker_bucket} -${testid} "
47+ bq_dataset=" ${bq_dataset} _${testid} "
4148 topic_alerts=" ${topic_alerts} -${testid} "
49+ topic_storebigquery=" ${topic_storebigquery} -${testid} "
4250fi
4351
44- # --- Create (or delete) GCS, Pub/Sub resources
52+ alerts_table=" alerts_${versiontag} "
53+
54+ # --- Create (or delete) BigQuery, GCS, Pub/Sub resources
55+ echo
56+ echo " Configuring BigQuery, GCS, Pub/Sub resources..."
4557if [ " ${teardown} " != " True" ]; then
58+ # create bigquery dataset and table
59+ bq --location=" ${region} " mk --dataset " ${bq_dataset} "
60+
61+ cd templates || exit 5
62+ bq mk --table " ${PROJECT_ID} :${bq_dataset} .${alerts_table} " " bq_${survey} _${alerts_table} _schema.json" || exit 5
63+ bq update --description " Alert data from LIGO/Virgo/KAGRA. This table is an archive of the lvk-alerts Pub/Sub stream. It has the same schema as the original alert bytes, including nested and repeated fields." " ${PROJECT_ID} :${bq_dataset} .${alerts_table} "
64+ cd .. || exit 5
65+
4666 # create broker bucket and upload files
4767 echo " Creating broker_bucket and uploading files..."
4868 gsutil mb -b on -l " ${region} " " gs://${broker_bucket} "
@@ -51,22 +71,37 @@ if [ "${teardown}" != "True" ]; then
5171 # create pubsub
5272 echo " Configuring Pub/Sub resources..."
5373 gcloud pubsub topics create " ${topic_alerts} "
74+ gcloud pubsub topics create " ${topic_storebigquery} "
5475
5576 # Set IAM policies on resources
5677 user=" allUsers"
5778 roleid=" projects/${GOOGLE_CLOUD_PROJECT} /roles/userPublic"
5879 gcloud pubsub topics add-iam-policy-binding " ${topic_alerts} " --member=" ${user} " --role=" ${roleid} "
80+ gcloud pubsub topics add-iam-policy-binding " ${topic_storebigquery} " --member=" ${user} " --role=" ${roleid} "
5981
6082else
6183 # ensure that we do not teardown production resources
6284 if [ " ${testid} " != " False" ]; then
6385 o=" GSUtil:parallel_process_count=1" # disable multiprocessing for Macs
6486 gsutil -m -o " ${o} " rm -r " gs://${broker_bucket} "
87+ bq rm -r -f " ${PROJECT_ID} :${bq_dataset} "
6588 gcloud pubsub topics delete " ${topic_alerts} "
89+ gcloud pubsub topics delete " ${topic_storebigquery} "
6690 fi
6791fi
6892
6993# --- Create VM instances
7094echo
7195echo " Configuring VMs..."
7296./create_vms.sh " ${broker_bucket} " " ${testid} " " ${teardown} " " ${survey} " " ${zone} "
97+
98+ # --- Deploy Cloud Functions
99+ echo
100+ echo " Configuring Cloud Functions..."
101+ cd .. && cd .. && cd cloud_functions && cd lvk || exit 5
102+
103+ # --- BigQuery storage cloud function
104+ cd store_BigQuery && ./deploy.sh " $testid " " $teardown " " $survey " " $versiontag " || exit 5
105+
106+ # --- return to setup_broker/lvk directory
107+ cd .. && cd .. && cd .. && cd setup_broker && cd lvk || exit 5
0 commit comments