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

add tags to old VPCs #1844

Merged
merged 1 commit into from
Mar 25, 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
28 changes: 19 additions & 9 deletions zititest/scripts/housekeeper-aws.bash
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ function describe_instances() {
|{InstanceId: .InstanceId, Region: $region, LaunchTime: .LaunchTime, State: .State, Tags: .Tags}
]
' \
| tee $old_file \
| tee "$old_file" \
| jq 'length' | xargs -ILEN echo "Described LEN old instances in $region in $(realpath $old_file)"
done
}
Expand All @@ -38,15 +38,20 @@ function describe_vpcs {
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 < <(
all_fablab_vpcs_json="$(
# 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 ]]
--query 'Vpcs[?Tags[?Key==`source` && Value==`fablab`]]' \
--output json
)"
local -a all_fablab_vpcs_ids=()
while read -r; do
all_fablab_vpcs_ids+=("$REPLY")
done < <(jq --raw-output '.[].VpcId' <<< "$all_fablab_vpcs_json")
# echo "DEBUG: found $(jq 'length' <<< "${all_fablab_vpcs_json}") fablab VPCs: ${all_fablab_vpcs_ids[*]}"
if [[ ${#all_fablab_vpcs_ids[@]} -ge 1 ]]
then
for vpc_id in "${all_fablab_vpcs[@]}"
for vpc_id in "${all_fablab_vpcs_ids[@]}"
do
vpc_create_events["$vpc_id"]=$(
# shellcheck disable=SC2016
Expand All @@ -57,7 +62,7 @@ function describe_vpcs {
)
done

for vpc_id in "${all_fablab_vpcs[@]}"
for vpc_id in "${all_fablab_vpcs_ids[@]}"
do
if [[ "$(jq 'length' <<< "${vpc_create_events[$vpc_id]}")" -ne 1 ]]
then
Expand All @@ -84,6 +89,10 @@ function describe_vpcs {
do
if [[ "$(jq 'length' <<< "${old_vpcs[$vpc_id]}")" -eq 1 ]]
then
# append the tags from describe all VPCs as a new key "tags" in the current VPC
local tags='{}'
tags="$(jq --arg vpc_id "${vpc_id}" '.[]|select(.VpcId == $vpc_id)|.Tags' <<< "${all_fablab_vpcs_json}")"
old_vpcs[$vpc_id]="$(jq --argjson tags "${tags}" '.[0] += {"tags": $tags}' <<< "${old_vpcs[$vpc_id]}")"
old_vpcs_json=$(jq --argjson append "${old_vpcs[$vpc_id]}" '. += $append' <<< "${old_vpcs_json}")
fi
done
Expand Down Expand Up @@ -182,7 +191,8 @@ do
fi
;;
--help|\?|*)
echo "Usage: $BASENAME [--age DAYS|--state (running|stopped)|stop FILE|terminate FILE]"
echo "Usage: $BASENAME [describe instance --age DAYS --state (running|stopped) | describe vpc ] | stop FILE | terminate FILE]"\
"where FILE is a JSON file created by the describe command"
exit 0
;;
esac
Expand Down
Loading