From 93f9880993df512de0469b706ee1fc7ccec9dcc1 Mon Sep 17 00:00:00 2001 From: Hongkai Liu Date: Tue, 23 Jul 2019 16:46:06 -0400 Subject: [PATCH 1/3] Check if AWS_SHARED_CREDENTIALS_FILE is set Follow-up of https://github.com/openshift/release/pull/4489 --- cmd/ipi-deprovision/ipi-deprovision.sh | 2 ++ 1 file changed, 2 insertions(+) diff --git a/cmd/ipi-deprovision/ipi-deprovision.sh b/cmd/ipi-deprovision/ipi-deprovision.sh index 6d051f0ace5..cb34f940fe1 100644 --- a/cmd/ipi-deprovision/ipi-deprovision.sh +++ b/cmd/ipi-deprovision/ipi-deprovision.sh @@ -1,5 +1,7 @@ #!/bin/sh +if [ -z "${AWS_SHARED_CREDENTIALS_FILE}" ]; then echo "ENV VAR AWS_SHARED_CREDENTIALS_FILE is not set"; exit 1; fi + ### expirationDate is always with timezone +00:00, eg, "2019-07-23T22:35+0000" MY_DATE=$(TZ=":Africa/Abidjan" date '+%Y-%m-%d') declare -a regions=("us-east-1" "us-east-2" "us-west-1") From 706c6477d14d2c2d99383e74dfe9ab1eee283dab Mon Sep 17 00:00:00 2001 From: Hongkai Liu Date: Tue, 23 Jul 2019 17:38:58 -0400 Subject: [PATCH 2/3] Add comments on ipi-deprovision.sh --- cmd/ipi-deprovision/ipi-deprovision.sh | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/cmd/ipi-deprovision/ipi-deprovision.sh b/cmd/ipi-deprovision/ipi-deprovision.sh index cb34f940fe1..a1be098d851 100644 --- a/cmd/ipi-deprovision/ipi-deprovision.sh +++ b/cmd/ipi-deprovision/ipi-deprovision.sh @@ -1,15 +1,23 @@ #!/bin/sh +set -o errexit +set -o nounset +set -o pipefail + if [ -z "${AWS_SHARED_CREDENTIALS_FILE}" ]; then echo "ENV VAR AWS_SHARED_CREDENTIALS_FILE is not set"; exit 1; fi ### expirationDate is always with timezone +00:00, eg, "2019-07-23T22:35+0000" MY_DATE=$(TZ=":Africa/Abidjan" date '+%Y-%m-%d') +### TODO: ask @abhinav.dahiya the right list of regions we should focus on +### here is only for test declare -a regions=("us-east-1" "us-east-2" "us-west-1") handle_cluster () { local cluster_name; cluster_name=$1 echo "handling cluster: ${cluster_name} ..." + ### TODO: will test hiveutil later + #./bin/hiveutil aws-tag-deprovision "${cluster_name}=owned" } for r in "${regions[@]}" From 9dbcc5e5e0e04a0abdbd8957b879f70341f1bc87 Mon Sep 17 00:00:00 2001 From: Hongkai Liu Date: Tue, 23 Jul 2019 20:12:47 -0400 Subject: [PATCH 3/3] Take params of ipi-deprovision.sh from env. var. --- cmd/ipi-deprovision/ipi-deprovision.sh | 35 ++++++++++++++++++-------- 1 file changed, 25 insertions(+), 10 deletions(-) diff --git a/cmd/ipi-deprovision/ipi-deprovision.sh b/cmd/ipi-deprovision/ipi-deprovision.sh index a1be098d851..51c10f91dd9 100644 --- a/cmd/ipi-deprovision/ipi-deprovision.sh +++ b/cmd/ipi-deprovision/ipi-deprovision.sh @@ -1,27 +1,42 @@ -#!/bin/sh +#!/bin/bash set -o errexit set -o nounset set -o pipefail -if [ -z "${AWS_SHARED_CREDENTIALS_FILE}" ]; then echo "ENV VAR AWS_SHARED_CREDENTIALS_FILE is not set"; exit 1; fi +### cluster_age_cutoff is going to be used as argument in the aws command below +### to filter out the expired clusters with the tags of the key expirationDate +### which is always with timezone +00:00, eg, "2019-07-23T22:35+0000". +### CLUSTER_TTL controlls the relative time to the running time of this script. +### eg, export CLUSTER_TTL="22 hours ago" +echo "Searching for clusters with a TTL of ${CLUSTER_TTL}" +cluster_age_cutoff="$(TZ=":Africa/Abidjan" date --date="${CLUSTER_TTL}" '+%Y-%m-%dT%H:%M+0000')" -### expirationDate is always with timezone +00:00, eg, "2019-07-23T22:35+0000" -MY_DATE=$(TZ=":Africa/Abidjan" date '+%Y-%m-%d') -### TODO: ask @abhinav.dahiya the right list of regions we should focus on -### here is only for test -declare -a regions=("us-east-1" "us-east-2" "us-west-1") +echo "cluster_age_cutoff: ${cluster_age_cutoff}" + +### eg, export AWS_REGIONS="us-east-1;us-west-1" +echo "take regions from env. var.: AWS_REGIONS: ${AWS_REGIONS}" +IFS=';' read -r -a regions <<< "$AWS_REGIONS" handle_cluster () { - local cluster_name; + local cluster_name cluster_name=$1 - echo "handling cluster: ${cluster_name} ..." + local expirationDateValue + expirationDateValue=$2 + echo "handling cluster: ${cluster_name} with expirationDate: ${expirationDateValue}" ### TODO: will test hiveutil later #./bin/hiveutil aws-tag-deprovision "${cluster_name}=owned" } + + for r in "${regions[@]}" do echo "doing region ${r} ..." - aws ec2 describe-vpcs --output json --region "${r}" | jq --arg date "${MY_DATE}" -r -S '.Vpcs[] | select (.Tags[]? | (.Key == "expirationDate" and .Value < $date)) | .Tags[] | select (.Value == "owned") | .Key' | while read line; do handle_cluster ${line}; done + json_output=$(aws ec2 describe-vpcs --output json --region "${r}") + for cluster in $(echo ${json_output} | jq --arg date "${cluster_age_cutoff}" -r -S '.Vpcs[] | select (.Tags[]? | (.Key == "expirationDate" and .Value < $date)) | .Tags[] | select (.Value == "owned") | .Key') + do + expirationDateValue=$(echo ${json_output} | jq --arg cl "${cluster}" -r -S '.Vpcs[] | select (.Tags[]? | (.Key == $cl and .Value == "owned")) | .Tags[] | select (.Key == "expirationDate") | .Value') + handle_cluster "${cluster}" "${expirationDateValue}" + done done