Skip to content

Instantly share code, notes, and snippets.

@ajanderson1
Last active July 25, 2023 23:54
Show Gist options
  • Save ajanderson1/ea8bf03cc54ec5d418cec7b0b3b9c868 to your computer and use it in GitHub Desktop.
Save ajanderson1/ea8bf03cc54ec5d418cec7b0b3b9c868 to your computer and use it in GitHub Desktop.
Cloud Scheduler Scripts

GCP Cloud Scheduler Scripts

Overview

A collection of useful scripts for managing GCP Cloud Scheduler jobs.

Scripts:

Usage:

Install

To download particular script, run the following command:

curl -O  https://gist.githubusercontent.com/ajanderson1/ea8bf03cc54ec5d418cec7b0b3b9c868/raw/92473253b2e0b0211f7e1238ff5adbbe5e838f04/<script_name.sh>

or

FILE_URL="https://gist.githubusercontent.com/ajanderson1/ea8bf03cc54ec5d418cec7b0b3b9c868/raw/92473253b2e0b0211f7e1238ff5adbbe5e838f04/<script_name.sh>"
SCRIPT_NAME=$(basename "$FILE_URL") && curl -O "$FILE_URL" && chmod +x "$SCRIPT_NAME"

Run

To run a script, run the following command:

./<script_name.sh>

or add to your PATH:

export PATH=$PATH:<path_to_script>

Requires:

#!/bin/bash
# Creates a Cloud Scheduler job using the Google Cloud CLI tool, allowing users to specify the
# project ID, scheduler region, schedule, job URI, job description, message body, time zone, and
# job name as input parameters.
read -p "Enter the project ID (default: $PROJECT_ID): " PROJECT_ID
PROJECT_ID=${PROJECT_ID:-$PROJECT_ID}
read -p "Enter the scheduler region (default: europe-west2): " SCHEDULER_REGION
SCHEDULER_REGION=${SCHEDULER_REGION:-europe-west2}
read -p "Enter the schedule (default: 0 */2 * * *): " SCHEDULE
SCHEDULE=${SCHEDULE:-"0 */2 * * *"}
read -p "Enter the job URI: " JOB_URI
read -p "Enter the scheduler job description: " SCHEDULER_JOB_DESC
read -p "Enter the message body: " MESSAGE_BODY
read -p "Enter the time zone (default: Europe/London): " TIME_ZONE
TIME_ZONE=${TIME_ZONE:-Europe/London}
read -p "Enter the scheduler job name: " SCHEDULER_JOB_NAME
echo "Creating Cloud Scheduler job..."
gcloud scheduler jobs create http "$SCHEDULER_JOB_NAME" \
--project "$PROJECT_ID" \
--location "$SCHEDULER_REGION" \
--schedule="$SCHEDULE" \
--uri="$JOB_URI" \
--description="$SCHEDULER_JOB_DESC" \
--headers="Content-Type:application/json" \
--http-method=POST \
--message-body="$MESSAGE_BODY" \
--time-zone="$TIME_ZONE"
echo "Cloud Scheduler job created successfully."
#!/bin/bash
# List and delete Cloud Scheduler jobs based on user selection
# NB: Requires:
#  - jq command-line JSON processor (https://stedolan.github.io/jq/)
# - gcloud command-line tool (https://cloud.google.com/sdk/gcloud/)
# - tput command-line tool (https://linux.die.net/man/1/tput)
# Set the default scheduler location
DEFAULT_LOCATION="europe-west2"
SCHEDULER_LOCATION="$DEFAULT_LOCATION"
# Request the scheduler location or use the default
read -p "Enter the scheduler location (default: $DEFAULT_LOCATION): " location
SCHEDULER_LOCATION=${location:-$DEFAULT_LOCATION}
# List the scheduler jobs and store the output in a temporary file
gcloud scheduler jobs list --location "$SCHEDULER_LOCATION" > scheduler_jobs.txt
# Print the scheduler jobs with line numbers, excluding column titles
awk 'NR==1 {print} NR>1 {print NR-1 ". " $0}' scheduler_jobs.txt
# Prompt for selection of jobs to delete
read -p "Enter the job numbers you want to delete (separated by spaces) or 'all' to delete all: " job_numbers
# Check if the user made a valid selection
if [[ -z "$job_numbers" ]]; then
echo "No job numbers entered. Exiting."
exit 1
fi
# Define the formatting codes for bold and reset
bold=$(tput bold)
reset=$(tput sgr0)
# Delete the selected jobs
if [[ "$job_numbers" == "all" ]]; then
read -p "Are you sure you want to delete all jobs? This action cannot be undone. (Y/n): " confirm
confirm=${confirm:-Y}
if [[ "$confirm" == [yY] || "$confirm" == [yY][eE][sS] ]]; then
while read -r line; do
job_name=$(echo "$line" | awk '{print $1}')
echo "${bold}Deleting job: $job_name${reset}"
gcloud scheduler jobs delete "$job_name" --location "$SCHEDULER_LOCATION" --quiet
done < scheduler_jobs.txt
else
echo "Operation cancelled. No jobs were deleted."
fi
else
for number in $job_numbers; do
job_name=$(awk 'NR==line_num+1 {print $1}' line_num="$number" scheduler_jobs.txt)
echo "${bold}Deleting job: $job_name${reset}"
gcloud scheduler jobs delete "$job_name" --location "$SCHEDULER_LOCATION" --quiet
done
fi
# Clean up the temporary file
rm scheduler_jobs.txt
#!/bin/bash
# Describe Cloud Scheduler jobs based on user selection from list of jobs (in a given location)
# NB: Requires:
#  - jq command-line JSON processor (https://stedolan.github.io/jq/)
# - gcloud command-line tool (https://cloud.google.com/sdk/gcloud/)
# - tput command-line tool (https://linux.die.net/man/1/tput)
# Set the default scheduler location
DEFAULT_LOCATION="europe-west2"
SCHEDULER_LOCATION="$DEFAULT_LOCATION"
# Request the scheduler location or use the default
read -p "Enter the scheduler location (default: $DEFAULT_LOCATION): " location
SCHEDULER_LOCATION=${location:-$DEFAULT_LOCATION}
# List the scheduler jobs and store the output in a temporary file
gcloud scheduler jobs list --location "$SCHEDULER_LOCATION" > scheduler_jobs.txt
# Print the scheduler jobs with line numbers, excluding column titles
awk 'NR==1 {print} NR>1 {print NR-1 ". " $0}' scheduler_jobs.txt
# Prompt for selection of jobs to describe
read -p "Enter the job numbers you want to describe (separated by spaces): " job_numbers
# Check if the user made a valid selection
if [[ -z "$job_numbers" ]]; then
echo "No job numbers entered. Exiting."
exit 1
fi
# Define the formatting codes for bold and reset
bold=$(tput bold)
reset=$(tput sgr0)
# Describe the selected jobs
for number in $job_numbers; do
job_name=$(awk 'NR==line_num+1 {print $1}' line_num="$number" scheduler_jobs.txt)
echo
echo "${bold}Describing job: $job_name${reset}"
gcloud scheduler jobs describe "$job_name" --location "$SCHEDULER_LOCATION" --format=json | jq '.'
done
# Clean up the temporary file
rm scheduler_jobs.txt
#!/bin/bash
# Force run Cloud Scheduler jobs based on user selection
# NB: Requires:
#  - jq command-line JSON processor (https://stedolan.github.io/jq/)
# - gcloud command-line tool (https://cloud.google.com/sdk/gcloud/)
# - tput command-line tool (https://linux.die.net/man/1/tput)
# Set the default scheduler location
DEFAULT_LOCATION="europe-west2"
SCHEDULER_LOCATION="$DEFAULT_LOCATION"
# Request the scheduler location or use the default
read -p "Enter the scheduler location (default: $DEFAULT_LOCATION): " location
SCHEDULER_LOCATION=${location:-$DEFAULT_LOCATION}
# List the scheduler jobs and store the output in a temporary file
gcloud scheduler jobs list --location "$SCHEDULER_LOCATION" > scheduler_jobs.txt
# Print the scheduler jobs with line numbers, excluding column titles
awk 'NR==1 {print} NR>1 {print NR-1 ". " $0}' scheduler_jobs.txt
# Prompt for selection of jobs to run
read -p "Enter the job numbers you want to run (separated by spaces), or type 'all' to run all jobs: " job_numbers
# Check if the user typed 'all'
if [ "$job_numbers" = "all" ]; then
# Get all job numbers
job_numbers=$(awk 'NR>1 {print NR-1}' scheduler_jobs.txt)
fi
# Run the selected jobs
for number in $job_numbers; do
job_name=$(awk 'NR==line_num+1 {print $1}' line_num="$number" scheduler_jobs.txt)
echo "Running job: $job_name"
gcloud scheduler jobs run "$job_name" --location "$SCHEDULER_LOCATION"
done
# Clean up the temporary file
rm scheduler_jobs.txt
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment