github.com/openshift/installer@v1.4.17/scripts/maintenance/tag-route53-hosted-zones.sh (about) 1 #!/usr/bin/env bash 2 3 usage() { 4 cat <<EOF 5 6 $(basename "$0") tags AWS Route53 Hosted Zones with an 'expirationDate' of tomorrow. 7 Requires that both 'jq' and the AWS CLI are installed. 8 9 Either the AWS_ACCESS_KEY_ID and AWS_SECRET_ACCESS_KEY environmental variables 10 must be set, or ~/.aws/credentials must contain valid AWS credentials. 11 12 Options: 13 14 --force Override user input prompts. Useful for automation. 15 16 --date-override (optional) Date of the format YYYY-MM-DD that overrides the 17 default tag value of tomorrow's date. This script tags resources 18 with 'expirationDate: some-date-string', where some-date-string 19 is replaced with either tomorrow's date or date-override. 20 21 EOF 22 } 23 24 force= 25 date_string= 26 27 while [ $# -gt 0 ]; do 28 case $1 in 29 --help) 30 usage 31 exit 32 ;; 33 --force) 34 force=true 35 ;; 36 --date-override) 37 date_string="${2:-}" 38 shift 39 ;; 40 *) 41 echo "Flag '$1' is not supported." >&2 42 exit 1 43 ;; 44 esac 45 shift 46 done 47 48 if ! command -V jq >/dev/null || ! command -V aws >/dev/null; then 49 echo "Missing required dependencies" >&2 50 exit 1 51 fi 52 53 set -e 54 55 # Tag all Route53 hosted zones that do not already have a tag with the same keys, 56 # in this case 'expirationDate', with tomorrow's date as default, or 57 # with the --date-override value. Format YYYY-MM-DD. 58 if [ -z "$date_string" ]; then 59 date_string="$(date -d tomorrow '+%Y-%m-%d')" 60 fi 61 62 tags="[{\"Key\":\"expirationDate\",\"Value\":\"$date_string\"}]" 63 64 echo "Tagging hosted zones with the following tags:" 65 echo "$tags" 66 67 if [ ! $force ]; then 68 read -rp "Proceed tagging these resources? [y/N]: " yn 69 if [ "$yn" != "y" ]; then 70 echo "Aborting tagging and cleaning up." >&2 71 exit 1 72 fi 73 fi 74 75 private_zones=$(aws route53 list-hosted-zones | \ 76 jq ".HostedZones[] | select(.Config.PrivateZone == true) | .Id" | \ 77 sed "s@\"@@g") 78 79 for key in $(echo "$tags" | jq ".[].Key"); do 80 for zone in $private_zones; do 81 zone="${zone##*/}" 82 is_not_tagged=$(aws route53 list-tags-for-resource \ 83 --resource-type hostedzone \ 84 --resource-id "$zone" | \ 85 jq ".ResourceTagSet | select(.Tags[]? | .Key == $key) | .ResourceId") 86 if [ -z "$is_not_tagged" ]; then 87 if aws route53 change-tags-for-resource \ 88 --resource-type hostedzone \ 89 --add-tags "$tags" \ 90 --resource-id "${zone##*/}"; then 91 echo "Tagged hosted zone ${zone##*/}" 92 else 93 echo "Error tagging hosted zone ${zone##*/}" >&2 94 fi 95 fi 96 done 97 done 98 99 set +e