Skip to content

Commit

Permalink
ci: fix cleanup error when there is no instance
Browse files Browse the repository at this point in the history
Signed-off-by: Yang Chiu <yang.chiu@suse.com>
  • Loading branch information
yangchiu committed May 1, 2024
1 parent 2b3f6f5 commit dfbd44f
Showing 1 changed file with 16 additions and 12 deletions.
28 changes: 16 additions & 12 deletions cleanup/scripts/cleanup.sh
Original file line number Diff line number Diff line change
Expand Up @@ -162,12 +162,14 @@ for INSTANCE in ${ALL_INSTANCES[@]}; do
echo " Launch Time: ${LAUNCH_TIME} (${TIMESTAMP}), Diff: ${TIME_DIFF}"
if [[ $TIME_DIFF -gt $THRESHOLD_IN_SEC ]]; then INSTANCE_IDS+=("$INSTANCE_ID"); fi
done
aws ec2 terminate-instances --instance-ids "${INSTANCE_IDS[@]}"
echo " instances ${INSTANCE_IDS[*]} shutting-down"
while [[ -n $(aws ec2 describe-instances --instance-ids "${INSTANCE_IDS[@]}" | jq '.Reservations[].Instances[].State.Name' | grep -v "terminated") ]]; do
echo "Wait for instances terminated ..."
sleep 5s
done
if [[ -n "${INSTANCE_IDS}" ]]; then
aws ec2 terminate-instances --instance-ids "${INSTANCE_IDS[@]}"
echo " instances ${INSTANCE_IDS[*]} shutting-down"
while [[ -n $(aws ec2 describe-instances --instance-ids "${INSTANCE_IDS[@]}" | jq '.Reservations[].Instances[].State.Name' | grep -v "terminated") ]]; do
echo "Wait for instances terminated ..."
sleep 5s
done
fi

echo "[Step 5] List long-running resources with owner:"
THRESHOLD_IN_SEC=$((86400 * 3))
Expand Down Expand Up @@ -203,9 +205,11 @@ for INSTANCE in ${ALL_INSTANCES[@]}; do
echo " Launch Time: ${LAUNCH_TIME} (${TIMESTAMP}), Diff: ${TIME_DIFF}"
if [[ $TIME_DIFF -gt $THRESHOLD_IN_SEC ]]; then INSTANCE_IDS+=("$INSTANCE_ID"); fi
done
aws ec2 terminate-instances --instance-ids "${INSTANCE_IDS[@]}"
echo " instance ${INSTANCE_IDS[*]} shutting-down"
while [[ -n $(aws ec2 describe-instances --instance-ids "${INSTANCE_IDS[@]}" | jq '.Reservations[].Instances[].State.Name' | grep -v "terminated") ]]; do
echo "Wait for instances terminated ..."
sleep 5s
done
if [[ -n "${INSTANCE_IDS}" ]]; then
aws ec2 terminate-instances --instance-ids "${INSTANCE_IDS[@]}"
echo " instance ${INSTANCE_IDS[*]} shutting-down"
while [[ -n $(aws ec2 describe-instances --instance-ids "${INSTANCE_IDS[@]}" | jq '.Reservations[].Instances[].State.Name' | grep -v "terminated") ]]; do
echo "Wait for instances terminated ..."
sleep 5s
done
fi

0 comments on commit dfbd44f

Please sign in to comment.