70 lines
1.5 KiB
Bash
Executable file
70 lines
1.5 KiB
Bash
Executable file
#! /bin/bash
|
|
|
|
set -e
|
|
|
|
test "$UID" != "0" || { echo "execute as copr user" && exit 1 ; }
|
|
|
|
something_found=false
|
|
|
|
dump_command ()
|
|
{
|
|
echo >&2 " -> $*"
|
|
"$@"
|
|
}
|
|
|
|
tracked()
|
|
{
|
|
name=$(redis-cli --scan --pattern "copr:backend:vm_instance:hset::$1")
|
|
test -n "$name"
|
|
}
|
|
|
|
old_enough()
|
|
{
|
|
# give them 1 hour
|
|
started=$(date --date="$1" +%s)
|
|
now=$(date +%s)
|
|
old_enough=$(( now - 3600 ))
|
|
test "$started" -le "$old_enough"
|
|
}
|
|
|
|
aws_command=(
|
|
aws ec2 describe-instances
|
|
--query "Reservations[].Instances[].{Id:InstanceId,Name:Tags[?Key=='Name']|[0].Value,Time:LaunchTime}"
|
|
--filters "Name=tag-key,Values=FedoraCopr,Name=tag-value,Values=copr"
|
|
"Name=instance-state-name,Values=running"
|
|
--output text
|
|
)
|
|
|
|
something_found=false
|
|
|
|
prefix=dev
|
|
case $(hostname) in
|
|
copr-be.*)
|
|
prefix=prod
|
|
;;
|
|
esac
|
|
|
|
while read -r aws_id vm_name launch_time; do
|
|
case $vm_name in
|
|
copr-$prefix-builder*)
|
|
something_found=true
|
|
|
|
# skip known VMs
|
|
tracked "$vm_name" && continue
|
|
|
|
# skip recently started VMs
|
|
if ! old_enough "$launch_time"; then
|
|
echo >&2 "$vm_name is not yet old enough: $launch_time"
|
|
continue
|
|
fi
|
|
|
|
# delete the rest
|
|
dump_command aws ec2 terminate-instances --instance-ids "$aws_id"
|
|
;;
|
|
*)
|
|
continue ;;
|
|
esac
|
|
done < <( "${aws_command[@]}" )
|
|
|
|
# fail if no VM was found (weird situation)
|
|
$something_found
|