diff --git a/.gitignore b/.gitignore index 6a71ca3..e684319 100644 --- a/.gitignore +++ b/.gitignore @@ -11,4 +11,7 @@ errored.tfstate .env.yaml dist/ function-source.zip -node_modules/ \ No newline at end of file +node_modules/ + +# Local Stuff +.project_vars_cache \ No newline at end of file diff --git a/infra/cloud_function.tf b/infra/cloud_function.tf index cc4bed5..037e17a 100644 --- a/infra/cloud_function.tf +++ b/infra/cloud_function.tf @@ -1,12 +1,12 @@ resource "google_cloudfunctions2_function" "watchdog_notifications" { project = module.bootstrap.seed_project_id location = var.region - name = "watchdog-notifications" + name = var.function_name description = "A cloud function that receives blockchain event data from QuickAlerts and sends notifications to a Discord channel" build_config { runtime = "nodejs20" - entry_point = "watchdogNotifier" + entry_point = var.function_entry_point service_account = "projects/${module.bootstrap.seed_project_id}/serviceAccounts/${module.bootstrap.terraform_sa_email}" source { diff --git a/infra/variables.tf b/infra/variables.tf index 6a7f236..aafe39d 100644 --- a/infra/variables.tf +++ b/infra/variables.tf @@ -69,3 +69,13 @@ variable "quicknode_api_key" { type = string sensitive = true } + +variable "function_name" { + type = string + default = "watchdog-notifications" +} + +variable "function_entry_point" { + type = string + default = "watchdogNotifier" +} diff --git a/package.json b/package.json index e72a032..150158c 100644 --- a/package.json +++ b/package.json @@ -6,6 +6,7 @@ "author": "Mento Labs ", "main": "dist/index.js", "scripts": { + "cache:clear": "./set-project-vars.sh --no-cache", "deploy": "npm run deploy:via:tf", "deploy:via:gcloud": "./deploy-via-gcloud.sh", "deploy:via:terraform": "npm run deploy:via:tf", diff --git a/set-project-vars.sh b/set-project-vars.sh index df462cf..4b3b9b5 100755 --- a/set-project-vars.sh +++ b/set-project-vars.sh @@ -3,27 +3,77 @@ set -e # Fail on any error set -o pipefail # Ensure piped commands propagate exit codes properly set -u # Treat unset variables as an error when substituting -printf "Looking up project name in variables.tf..." -project_name=$(awk '/variable "project_name"/{f=1} f==1&&/default/{print $3; exit}' ./infra/variables.tf | tr -d '",') -printf ' \033[1m%s\033[0m\n' "${project_name}" - -printf "Looking up region in variables.tf..." -region=$(awk '/variable "region"/{f=1} f==1&&/default/{print $3; exit}' ./infra/variables.tf | tr -d '",') -printf ' \033[1m%s\033[0m\n' "${region}" - +cache_file=".project_vars_cache" current_local_project_id=$(gcloud config get project) +current_tf_state_project_id=$(terraform -chdir=infra state show module.bootstrap.module.seed_project.module.project-factory.google_project.main | grep project_id | awk '{print $3}' | tr -d '"') -if [[ ! ${current_local_project_id} =~ ${project_name} ]]; then +if [[ ${current_local_project_id} != "${current_tf_state_project_id}" ]]; then printf '️\n🚨 Your local gcloud is set to the wrong project: \033[1m%s\033[0m 🚨\n' "${current_local_project_id}" printf "\nRunning ./set-project-id.sh in an attempt to fix this...\n\n" source ./set-project-id.sh printf "\n\n" -else - printf "Looking up the project ID..." - project_id=$(gcloud config get project) - printf ' \033[1m%s\033[0m\n' "${project_id}" fi -printf "Looking up function name..." -function_name=$(gcloud functions list --format="value(name)") -printf ' \033[1m%s\033[0m\n' "${function_name}" +cache_vars() { + if [[ $* != *"--no-cache"* ]]; then + printf "No cache file found at %s.\n\n" "${cache_file}" + fi + + printf "Loading and caching project values...\n\n" + + printf " - Project ID:" + project_id=${current_tf_state_project_id} + printf ' \033[1m%s\033[0m\n' "${project_id}" + + printf " - Project Name:" + project_name=$(awk '/variable "project_name"/{f=1} f==1&&/default/{print $3; exit}' ./infra/variables.tf | tr -d '",') + printf ' \033[1m%s\033[0m\n' "${project_name}" + + printf " - Region:" + region=$(awk '/variable "region"/{f=1} f==1&&/default/{print $3; exit}' ./infra/variables.tf | tr -d '",') + printf ' \033[1m%s\033[0m\n' "${region}" + + printf " - Service Account:" + service_account_email=$(terraform -chdir=infra state show "module.bootstrap.google_service_account.org_terraform[0]" | grep email | awk '{print $3}' | tr -d '"') + printf ' \033[1m%s\033[0m\n' "${service_account_email}" + + printf " - Function Name:" + function_name=$(awk '/variable "function_name"/{f=1} f==1&&/default/{print $3; exit}' ./infra/variables.tf | tr -d '",') + printf ' \033[1m%s\033[0m\n' "${function_name}" + + printf " - Function Entry Point:" + function_entry_point=$(awk '/variable "function_entry_point"/{f=1} f==1&&/default/{print $3; exit}' ./infra/variables.tf | tr -d '",') + printf ' \033[1m%s\033[0m\n' "${function_entry_point}" + + printf "\nCaching values in" + printf ' \033[1m%s\033[0m...' "${cache_file}" + + { + echo "project_id=${project_id}" + echo "project_name=${project_name}" + echo "region=${region}" + echo "service_account_email=${service_account_email}" + echo "function_name=${function_name}" + echo "function_entry_point=${function_entry_point}" + } >>"${cache_file}" + printf "✅\n\n" +} + +if [[ $* == *"--no-cache"* ]]; then + echo "Invalidating cache..." + rm -f "${cache_file}" + cache_vars --no-cache +elif [[ ! -f ${cache_file} ]]; then + cache_vars +else + # shellcheck source=.project_vars_cache + source "${cache_file}" + printf "Using cached values:\n" + printf " - Project ID: \033[1m%s\033[0m\n" "${project_id}" + printf " - Project Name: \033[1m%s\033[0m\n" "${project_name}" + printf " - Region: \033[1m%s\033[0m\n" "${region}" + printf " - Service Account: \033[1m%s\033[0m\n" "${service_account_email}" + printf " - Function Name: \033[1m%s\033[0m\n" "${function_name}" + printf " - Function Entry Point: \033[1m%s\033[0m\n" "${function_entry_point}" + printf "\n" +fi