github.com/GoogleCloudPlatform/testgrid@v0.0.174/cluster/create-subscription.sh (about) 1 #!/usr/bin/env bash 2 # Copyright 2021 The TestGrid Authors. 3 # 4 # Licensed under the Apache License, Version 2.0 (the "License"); 5 # you may not use this file except in compliance with the License. 6 # You may obtain a copy of the License at 7 # 8 # http://www.apache.org/licenses/LICENSE-2.0 9 # 10 # Unless required by applicable law or agreed to in writing, software 11 # distributed under the License is distributed on an "AS IS" BASIS, 12 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 # See the License for the specific language governing permissions and 14 # limitations under the License. 15 16 17 set -o nounset 18 set -o errexit 19 20 ack=10 21 bots=() 22 exp=24h 23 project= 24 role=roles/pubsub.subscriber 25 topic= 26 filter= 27 28 while getopts "t:p:a:e:b:f:" flag; do 29 case "$flag" in 30 a) ack=$OPTARG;; 31 b) bots+=("$OPTARG");; 32 e) exp=$OPTARG;; 33 p) project=$OPTARG;; 34 t) topic=$OPTARG;; 35 f) filter=$OPTARG;; 36 esac 37 done 38 39 shift $((OPTIND -1)) 40 41 if [[ $# -lt 1 || -z "$topic" ]]; then 42 echo "Usage: $(basename "$0") [-a $ack] [-b BOT ] [-e $exp] -c [-p $project] <-t TOPIC> SUBSCRIPTION [EXTRA ...]" >&2 43 echo >&2 44 echo " -a ACK : seconds to allow testgrid to ack a message" >&2 45 echo " -b BIND : add IAM binding for member, such as serviceAccount:foo (repeatable)" >&2 46 echo " -e EXP : number of s/m/h/d to keep unack'd messages" >&2 47 echo " -p PROJ : project to create subscription" >&2 48 echo " -r ROLE : role to bind" >&2 49 echo " -t TOPIC: projects/TOPIC_PROJ/topics/TOPIC" >&2 50 echo " -f FILTER: only include messages matching this filter" >&2 51 echo >&2 52 echo "More info: gcloud pusbsub subscriptions" >&2 53 exit 1 54 fi 55 56 name=$1 57 shift 58 59 log() { 60 ( 61 set -o xtrace 62 "$@" 63 ) 64 } 65 66 # Subscribe to topic 67 68 verb=create 69 create_arg=("--topic=$topic" "$@") 70 filter_args=() 71 filter_arg="" 72 if [ -n "$filter" ];then 73 filter_arg="$filter" 74 filter_args=("--message-filter=$filter_arg") 75 fi 76 77 old=$(gcloud pubsub subscriptions describe "--project=$project" "$name" --format='value(topic)' 2>/dev/null || true) 78 old_filter=$(gcloud pubsub subscriptions describe "--project=$project" "$name" --format='value(filter)' 2>/dev/null || true) 79 80 if [[ -n "$old" ]]; then 81 if [[ "$old" == "$topic" && "$old_filter" == "$filter_arg" ]]; then 82 echo "Subscription already exists, updating." 83 verb=update 84 create_arg=() 85 filter_args=() 86 else 87 echo "WARNING: $project/$name already subscribed to $old (filter: $old_filter)." >&2 88 read -p "Delete and replace with a subscription to $topic (filter: $filter_arg) [yes/NO]: " answer 89 case $answer in 90 y*|Y*) ;; 91 *) exit 1 92 esac 93 log gcloud pubsub subscriptions "--project=$project" delete "$name" 94 fi 95 fi 96 97 log gcloud pubsub subscriptions "$verb" \ 98 "--project=$project" "$name" \ 99 "--ack-deadline=$ack" "--expiration-period=$exp" \ 100 "${filter_args[@]}" "${create_arg[@]}" 101 102 # Add bindings to subscription 103 104 for bot in "${bots[@]}"; do 105 log gcloud pubsub subscriptions add-iam-policy-binding \ 106 "--project=$project" "$name" \ 107 "--member=$bot" "--role=$role" 108 done