From 499bacbe019a7641efc7799356b95f4663e1aa42 Mon Sep 17 00:00:00 2001 From: Kenneth Bingham Date: Wed, 6 Mar 2024 15:09:19 -0500 Subject: [PATCH] describe old and odd fablab VPCs --- zititest/scripts/housekeeper-aws.bash | 107 ++++++++++++++++++++++++-- 1 file changed, 100 insertions(+), 7 deletions(-) diff --git a/zititest/scripts/housekeeper-aws.bash b/zititest/scripts/housekeeper-aws.bash index f03f3d737..8465acd8a 100644 --- a/zititest/scripts/housekeeper-aws.bash +++ b/zititest/scripts/housekeeper-aws.bash @@ -9,12 +9,13 @@ function describe_instances() { local state=$2 for region in us-east-1 us-west-2 do - local outfile="aged-fablab-instances-${region}.json" + local old_file="old-fablab-${state}-instances-${region}.json" aws --region "$region" ec2 describe-instances \ --filters "Name=instance-state-name,Values=${state}" \ "Name=tag:source,Values=fablab" \ --query "Reservations[*].Instances[*].{InstanceId:InstanceId,LaunchTime:LaunchTime,State:State.Name,Tags:Tags}" \ - | jq -r \ + | jq \ + --raw-output \ --arg region "$region" \ --arg oldest "$oldest" ' [ @@ -24,15 +25,87 @@ function describe_instances() { |{InstanceId: .InstanceId, Region: $region, LaunchTime: .LaunchTime, State: .State, Tags: .Tags} ] ' \ - | tee $outfile \ - | jq '.|length' | xargs -ILEN echo "Described LEN aged instances in $region in $(realpath $outfile)" + | tee $old_file \ + | jq 'length' | xargs -ILEN echo "Described LEN old instances in $region in $(realpath $old_file)" + done +} + +function describe_vpcs { + cd "$(mktemp -d)" + local oldest=$1 + for region in us-east-1 us-west-2 + do + local old_file="old-fablab-vpcs-${region}.json" + local odd_file="odd-fablab-vpcs-${region}.json" + local -A vpc_create_events=() odd_vpcs=() old_vpcs=() + read -ra all_fablab_vpcs < <( + # shellcheck disable=SC2016 + aws --region "$region" ec2 describe-vpcs \ + --query 'Vpcs[?Tags[?Key==`source` && Value==`fablab`]].VpcId' \ + --output text + ) + if [[ ${#all_fablab_vpcs[@]} -ge 1 ]] + then + for vpc_id in "${all_fablab_vpcs[@]}" + do + vpc_create_events["$vpc_id"]=$( + # shellcheck disable=SC2016 + aws cloudtrail lookup-events \ + --region $region \ + --lookup-attributes "AttributeKey=ResourceName,AttributeValue=${vpc_id}" \ + --query 'Events[?EventName==`CreateVpc`].CloudTrailEvent' + ) + done + + for vpc_id in "${all_fablab_vpcs[@]}" + do + if [[ "$(jq 'length' <<< "${vpc_create_events[$vpc_id]}")" -ne 1 ]] + then + odd_vpcs["$vpc_id"]="true" + else + old_vpcs["$vpc_id"]=$( + jq \ + --raw-output \ + --arg oldest "$oldest" ' + [ + .[] + |fromjson + |select(.eventTime < $oldest) + |{eventName: .eventName, eventTime: .eventTime, awsRegion: .awsRegion, vpcId: .responseElements.vpc.vpcId} + ] + ' <<< "${vpc_create_events[$vpc_id]}" + ) + fi + done + + # for each key in the old_vpcs array + local old_vpcs_json='[]' + for vpc_id in "${!old_vpcs[@]}" + do + if [[ "$(jq 'length' <<< "${old_vpcs[$vpc_id]}")" -eq 1 ]] + then + old_vpcs_json=$(jq --argjson append "${old_vpcs[$vpc_id]}" '. += $append' <<< "${old_vpcs_json}") + fi + done + tee "$old_file" <<< "$old_vpcs_json" \ + | jq 'length' | xargs -ILEN echo "Described LEN old VPCs in $region in $(realpath $old_file)" + + # for each key in the odd_vpcs array + local odd_vpcs_json='[]' + for vpc_id in "${!odd_vpcs[@]}" + do + odd_vpcs_json=$(jq --arg vpc_id "$vpc_id" '. += [{vpcId: $vpc_id}]' <<< "${odd_vpcs_json}") + done + tee "$odd_file" <<< "$odd_vpcs_json" \ + | jq 'length' | xargs -ILEN echo "Described LEN odd VPCs in $region in $(realpath $odd_file)" + fi done } function stop_instances(){ local stopfile onecount region instanceid stopfile=$1 - onecount=$(jq '.|length' "$stopfile") + onecount=$(jq 'length' "$stopfile") for i in $(seq 0 $((onecount-1))) do region=$(jq -r ".[$i].Region" "$stopfile") @@ -45,7 +118,7 @@ function stop_instances(){ function terminate_instances(){ local stopfile onecount region instanceid stopfile=$1 - onecount=$(jq '.|length' "$stopfile") + onecount=$(jq 'length' "$stopfile") for i in $(seq 0 $((onecount-1))) do region=$(jq -r ".[$i].Region" "$stopfile") @@ -79,6 +152,16 @@ do terminate_instances "${2:-}" exit ;; + describe) + if [[ "${2:-}" =~ ^(instance|vpc)s?$ ]] + then + typeset -a DESCRIBE=("${2}") + shift 2 + else + typeset -a DESCRIBE=(instances vpcs) + shift 1 + fi + ;; --age) AGE="${2:-}" if ! [[ "$AGE" =~ ^[0-9]+$ ]]; @@ -105,4 +188,14 @@ do esac done -describe_instances "$(date -d "-${AGE:-7} days" -Id)" "${STATE:-running}" +for describe in "${DESCRIBE[@]}" +do + case "$describe" in + instance*) + describe_instances "$(date -d "-${AGE:-7} days" -Id)" "${STATE:-running}" + ;; + vpc*) + describe_vpcs "$(date -d "-${AGE:-7} days" -Id)" + ;; + esac +done