Skip to content

Commit

Permalink
Merge pull request #1412 from openziti/zititest-housekeeper-aws
Browse files Browse the repository at this point in the history
improve housekeeper for fablab/aws
  • Loading branch information
qrkourier authored Oct 6, 2023
2 parents e3be597 + a59ce80 commit 7a6cf89
Showing 1 changed file with 43 additions and 29 deletions.
72 changes: 43 additions & 29 deletions zititest/scripts/housekeeper-aws.bash
Original file line number Diff line number Diff line change
Expand Up @@ -6,21 +6,22 @@ BASENAME=$(basename "$0")
function describe_instances() {
cd "$(mktemp -d)"
local oldest=$1
local state=$2
for region in us-east-1 us-west-2
do
local outfile="aged-fablab-instances-${region}.json"
aws --region "$region" ec2 describe-instances \
--filters "Name=instance-state-name,Values=running" \
--filters "Name=instance-state-name,Values=${state}" \
"Name=tag:source,Values=fablab" \
--query "Reservations[*].Instances[*].{InstanceId:InstanceId,LaunchTime:LaunchTime,Tags:Tags}" \
--query "Reservations[*].Instances[*].{InstanceId:InstanceId,LaunchTime:LaunchTime,State:State.Name,Tags:Tags}" \
| jq -r \
--arg region "$region" \
--arg oldest "$oldest" '
[
.[]
|.[]
|select(.LaunchTime < $oldest)
|{InstanceId: .InstanceId, Region: $region, LaunchTime: .LaunchTime, Tags: .Tags}
|{InstanceId: .InstanceId, Region: $region, LaunchTime: .LaunchTime, State: .State, Tags: .Tags}
]
' \
| tee $outfile \
Expand Down Expand Up @@ -65,30 +66,43 @@ check_json_file(){
fi
}

case "${1:-}" in
--help|-h)
echo "Usage: $BASENAME [--oldest ISO8601|stop FILE|terminate FILE]"
exit 0
;;
stop)
check_json_file "${2:-}"
stop_instances "${2:-}"
exit
;;
terminate)
check_json_file "${2:-}"
terminate_instances "${2:-}"
exit
;;
--oldest)
OLDEST="${2:-}"
if ! [[ "$OLDEST" =~ ^[0-9]{4}-[0-9]{2}-[0-9]{2} ]];
then
echo "Usage: $BASENAME --oldest ISO8601 (e.g. 2024-01-01 or 2024-01-01T12:00:00Z)"
exit 1
fi
shift 2
;;
esac
while (( $# ))
do
case "${1}" in
stop)
check_json_file "${2:-}"
stop_instances "${2:-}"
exit
;;
terminate)
check_json_file "${2:-}"
terminate_instances "${2:-}"
exit
;;
--age)
AGE="${2:-}"
if ! [[ "$AGE" =~ ^[0-9]+$ ]];
then
echo "Usage: $BASENAME --age DAYS"
exit 1
fi
shift 2
;;
--state)
STATE="${2:-}"
if ! [[ "$STATE" =~ ^(running|stopped)$ ]];
then
echo "Usage: $BASENAME --state (running|stopped)"
exit 1
else
shift 2
fi
;;
--help|\?|*)
echo "Usage: $BASENAME [--age DAYS|--state (running|stopped)|stop FILE|terminate FILE]"
exit 0
;;
esac
done

describe_instances "${OLDEST:-$(date -d '-2 days' -Id)}"
describe_instances "$(date -d "-${AGE:-7} days" -Id)" "${STATE:-running}"

0 comments on commit 7a6cf89

Please sign in to comment.