|
| 1 | +#!/bin/bash |
| 2 | +# |
| 3 | +# Copyright 2023 Google LLC |
| 4 | +# |
| 5 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | +# you may not use this file except in compliance with the License. |
| 7 | +# You may obtain a copy of the License at |
| 8 | +# |
| 9 | +# https://www.apache.org/licenses/LICENSE-2.0 |
| 10 | +# |
| 11 | +# Unless required by applicable law or agreed to in writing, software |
| 12 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | +# See the License for the specific language governing permissions and |
| 15 | +# limitations under the License. |
| 16 | +# |
| 17 | +# This script manages Google Cloud Build scheduled builds. The schedules are |
| 18 | +# defined by JSON files that live in the `schedules/` directory. To create one |
| 19 | +# of these files for an existing schedule use --export. These files can be |
| 20 | +# used to create and update schedules (--import). |
| 21 | +# |
| 22 | +# Usage: schedule.sh [options] |
| 23 | +# |
| 24 | +# Options: |
| 25 | +# --list List all schedules for this project |
| 26 | +# --ids List the IDSs of all schedules this project |
| 27 | +# --export Exports an existing schedule |
| 28 | +# --import Creates or updates a schedule |
| 29 | +# -h|--help Print this help message |
| 30 | + |
| 31 | +set -euo pipefail |
| 32 | + |
| 33 | +source "$(dirname "$0")/../lib/init.sh" |
| 34 | +source module ci/lib/io.sh |
| 35 | + |
| 36 | +function print_usage() { |
| 37 | + # Extracts the usage from the file comment starting at line 17. |
| 38 | + sed -n '17,/^$/s/^# \?//p' "${PROGRAM_PATH}" |
| 39 | +} |
| 40 | + |
| 41 | +readonly CLOUD_PROJECT="cloud-cpp-testing-resources" |
| 42 | +readonly LOCATION=us-central1 |
| 43 | + |
| 44 | +function list_schedules() { |
| 45 | + gcloud scheduler jobs list \ |
| 46 | + --project "${CLOUD_PROJECT}" \ |
| 47 | + --location "${LOCATION}" |
| 48 | +} |
| 49 | + |
| 50 | +function ids_schedules() { |
| 51 | + gcloud scheduler jobs list \ |
| 52 | + --project "${CLOUD_PROJECT}" \ |
| 53 | + --location "${LOCATION}" \ |
| 54 | + --format "value(ID)" |
| 55 | +} |
| 56 | + |
| 57 | +function export_schedule() { |
| 58 | + local name="$1" |
| 59 | + gcloud scheduler jobs describe "${name}" \ |
| 60 | + --project "${CLOUD_PROJECT}" \ |
| 61 | + --location "${LOCATION}" \ |
| 62 | + --format=json |
| 63 | +} |
| 64 | + |
| 65 | +function schedule_flags() { |
| 66 | + local file="$1" |
| 67 | + # jq requires a single argument, but it is too long. |
| 68 | + local args=( |
| 69 | + '"--schedule"' .schedule |
| 70 | + '"--uri"' .httpTarget.uri |
| 71 | + '"--oauth-service-account-email"' .httpTarget.oauthToken.serviceAccountEmail |
| 72 | + '"--oauth2-service-account-scope"' .httpTarget.oauthToken.scope |
| 73 | + ) |
| 74 | + local script |
| 75 | + script="$(print ", %s" "${args[@]}")" |
| 76 | + jq -r "${script:2}" <"${file}" |
| 77 | +} |
| 78 | + |
| 79 | +function import_schedule() { |
| 80 | + local file="$1" |
| 81 | + local id |
| 82 | + id="$(basename "${file}" .json)" |
| 83 | + local -a flags |
| 84 | + mapfile -t flags < <(schedule_flags "${file}") |
| 85 | + |
| 86 | + io::run gcloud scheduler jobs create http "${id}" "${flags[@]}" \ |
| 87 | + --project "${CLOUD_PROJECT}" \ |
| 88 | + --location "${LOCATION}" |
| 89 | +} |
| 90 | + |
| 91 | +# Use getopt to parse and normalize all the args. |
| 92 | +PARSED="$(getopt -a \ |
| 93 | + --options="h" \ |
| 94 | + --longoptions="list,ids,export:,import:,help" \ |
| 95 | + --name="${PROGRAM_NAME}" \ |
| 96 | + -- "$@")" |
| 97 | +eval set -- "${PARSED}" |
| 98 | + |
| 99 | +case "$1" in |
| 100 | + --list) |
| 101 | + list_schedules |
| 102 | + ;; |
| 103 | + --ids) |
| 104 | + ids_schedules |
| 105 | + ;; |
| 106 | + --export) |
| 107 | + export_schedule "$2" |
| 108 | + ;; |
| 109 | + --import) |
| 110 | + import_schedule "$2" |
| 111 | + ;; |
| 112 | + -h | --help) |
| 113 | + print_usage |
| 114 | + ;; |
| 115 | + *) |
| 116 | + print_usage |
| 117 | + exit 1 |
| 118 | + ;; |
| 119 | +esac |
0 commit comments