Skip to content

Commit

Permalink
build: made shell scripts more robust and reusable across projects
Browse files Browse the repository at this point in the history
  • Loading branch information
chapati23 committed Jul 25, 2024
1 parent ccacb5d commit 17deeb2
Show file tree
Hide file tree
Showing 6 changed files with 97 additions and 41 deletions.
19 changes: 6 additions & 13 deletions deploy-via-gcloud.sh
Original file line number Diff line number Diff line change
@@ -1,22 +1,15 @@
#! /bin/bash
set -e # fail on any error
set -o pipefail # ensure non-zero exit codes are propagated in piped commands
#!/bin/bash
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

region="europe-west1"

printf "Looking up function name..."
function_name=$(gcloud functions list --format="value(name)" | grep '^watchdog-notifications')
printf ' \033[1m%s\033[0m\n' "${function_name}"
# Load the project variables
source ./set-project-vars.sh

printf "Looking up entry point..."
entry_point=$(gcloud functions describe "${function_name}" --region="${region}" --format json | jq .buildConfig.entryPoint)
printf ' \033[1m%s\033[0m\n' "${entry_point}"

printf "Looking up project ID..."
project_name="governance-watchdog"
project_id=$(gcloud projects list --filter="name:${project_name}*" --format="value(projectId)")
printf ' \033[1m%s\033[0m\n' "${project_id}"

printf "Looking up service account for function..."
service_account_email=$(gcloud functions describe "${function_name}" --region="${region}" --format="value(serviceConfig.serviceAccountEmail)")
printf ' \033[1m%s\033[0m\n' "${service_account_email}"
Expand Down
14 changes: 8 additions & 6 deletions get-logs-url.sh
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
#!/bin/bash
set -e # fail on any error
set -o pipefail # ensure non-zero exit codes are propagated in piped commands
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

project_id=$(gcloud config get-value project)
region=europe-west1
function_name=watchdog-notifications
echo "https://console.cloud.google.com/functions/details/${region}/${function_name}?project=${project_id}&tab=logs "
# Load the project variables
source ./set-project-vars.sh

logs_url="https://console.cloud.google.com/functions/details/${region}/${function_name}?project=${project_id}&tab=logs "
printf '\n\033[1m%s\033[0m\n' "${logs_url}"
12 changes: 7 additions & 5 deletions get-logs.sh
Original file line number Diff line number Diff line change
@@ -1,18 +1,20 @@
#!/bin/bash
set -e # fail on any error
set -o pipefail # ensure non-zero exit codes are propagated in piped commands
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

# Get the name of the watchdog-notifications function
function_name=$(gcloud functions list --format="value(name)" | grep '^watchdog-notifications')
# Load the project variables
source ./set-project-vars.sh

# Fetch raw logs
raw_logs=$(gcloud functions logs read "${function_name}" \
--region europe-west1 \
--region "${region}" \
--format json \
--limit 50 \
--sort-by TIME_UTC)

# Format logs
printf "\n\n"
echo "${raw_logs}" | jq -r '.[] | if .level == "E" then
"\u001b[31m[\(.level)]\u001b[0m \u001b[33m\(.time_utc)\u001b[0m: \(.log)"
else
Expand Down
57 changes: 42 additions & 15 deletions set-project-id.sh
Original file line number Diff line number Diff line change
@@ -1,23 +1,50 @@
#! /bin/bash
set -e # fail on any error
set -o pipefail # ensure non-zero exit codes are propagated in piped commands
#!/bin/bash
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

# Get the project ID for the governance-watchdog project
printf "Fetching the project ID for the governance-watchdog project:"
project_id=$(gcloud projects list --filter="name:governance-watchdog" --format="value(projectId)")
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 "Fetching the project ID..."
project_id=$(gcloud projects list --filter="name:${project_name}" --format="value(projectId)")
printf ' \033[1m%s\033[0m\n' "${project_id}"

# Set your local default project to the governance-watchdog project
echo "Setting your default project to '${project_id}'..."
gcloud config set project "${project_id}"
# Set your local default project
printf "Setting your default project to %s..." "${project_id}"
{
output=$(gcloud config set project "${project_id}" 2>&1 >/dev/null)
status=$?
}
if [[ ${status} -ne 0 ]]; then
echo "Error: ${output}"
exit "${status}"
fi
printf "✅\n"

# Set the quota project to the governance-watchdog project, some gcloud commands require this to be set
echo "Setting the quota project to '${project_id}'..."
gcloud auth application-default set-quota-project "${project_id}"
printf "Setting the quota project to %s..." "${project_id}"
{
output=$(gcloud auth application-default set-quota-project "${project_id}" 2>&1 >/dev/null)
status=$?
}
if [[ ${status} -ne 0 ]]; then
echo "Error: ${output}"
exit "${status}"
fi
printf "✅\n"

# Update the project ID in your .env file so your cloud function points to the correct project when running locally
printf "\n\nUpdating the project ID in your .env file..."
sed -i '' "s/^GCP_PROJECT_ID=.*/GCP_PROJECT_ID=${project_id}/" .env
printf "Updating the project ID in your .env file..."
# Check if .env file exists
if [[ ! -f .env ]]; then
# If .env doesn't exist, create it with the initial value
echo "GCP_PROJECT_ID=${project_id}" >.env
else
# If .env exists, perform the sed replacement
sed -i '' "s/^GCP_PROJECT_ID=.*/GCP_PROJECT_ID=${project_id}/" .env
fi
printf "✅\n\n"

printf "\n\n✅ All Done!"
exit 0
echo "✅ All Done!"
29 changes: 29 additions & 0 deletions set-project-vars.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
#!/bin/bash
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}"

current_local_project_id=$(gcloud config get project)

if [[ ! ${current_local_project_id} =~ ${project_name} ]]; 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}"
7 changes: 5 additions & 2 deletions test-deployed-function.sh
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
#! /bin/bash
# This only works if the function has been deployed and your `terraform` can access the state backend
#!/bin/bash
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

# This only works if the function has been deployed and your `terraform` can access the state backend
raw_function_url=$(terraform -chdir=infra output -json function_uri)
function_url=$(echo "${raw_function_url}" | jq -r)

Expand Down

0 comments on commit 17deeb2

Please sign in to comment.