Skip to content

Commit

Permalink
chore(DX): added caching speed up shell scripts
Browse files Browse the repository at this point in the history
  • Loading branch information
chapati23 committed Jul 26, 2024
1 parent 17deeb2 commit 9c1bd51
Show file tree
Hide file tree
Showing 5 changed files with 83 additions and 19 deletions.
5 changes: 4 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,7 @@ errored.tfstate
.env.yaml
dist/
function-source.zip
node_modules/
node_modules/

# Local Stuff
.project_vars_cache
4 changes: 2 additions & 2 deletions infra/cloud_function.tf
Original file line number Diff line number Diff line change
@@ -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 {
Expand Down
10 changes: 10 additions & 0 deletions infra/variables.tf
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
"author": "Mento Labs <eng@mentolabs.xyz>",
"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",
Expand Down
82 changes: 66 additions & 16 deletions set-project-vars.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit 9c1bd51

Please sign in to comment.