Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

aws housekeeping - describe old fablab VPCs #1802

Merged
merged 1 commit into from
Mar 6, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
107 changes: 100 additions & 7 deletions zititest/scripts/housekeeper-aws.bash
Original file line number Diff line number Diff line change
Expand Up @@ -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" '
[
Expand All @@ -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")
Expand All @@ -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")
Expand Down Expand Up @@ -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]+$ ]];
Expand All @@ -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
Loading