-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclock_stopper_gcp.sh
31 lines (24 loc) · 1.22 KB
/
clock_stopper_gcp.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
#!/bin/bash
# Set the threshold (in minutes) for instances to be stopped
THRESHOLD_MINUTES=60
# Get the current timestamp
CURRENT_TIMESTAMP=$(date +%s)
# List all instances in the project and their creation timestamps
gcloud compute instances list --format='csv[no-heading](name,zone,creationTimestamp.date("%s"))' --filter="status=RUNNING" > instances.csv
# Iterate through instances and check if they have been running longer than the threshold
while read -r INSTANCE; do
INSTANCE_NAME=$(echo "$INSTANCE" | awk -F',' '{print $1}')
INSTANCE_ZONE=$(echo "$INSTANCE" | awk -F',' '{print $2}')
INSTANCE_TIMESTAMP=$(echo "$INSTANCE" | awk -F',' '{print $3}')
# Calculate the duration in minutes
DURATION_MINUTES=$(( (CURRENT_TIMESTAMP - INSTANCE_TIMESTAMP) / 60 ))
# Check if the duration exceeds the threshold
if [ "$DURATION_MINUTES" -gt "$THRESHOLD_MINUTES" ]; then
echo "Stopping instance $INSTANCE_NAME in zone $INSTANCE_ZONE, running for $DURATION_MINUTES minutes."
gcloud compute instances stop "$INSTANCE_NAME" --zone="$INSTANCE_ZONE"
else
echo "Instance $INSTANCE_NAME in zone $INSTANCE_ZONE is within threshold, running for $DURATION_MINUTES minutes."
fi
done < instances.csv
# Cleanup
rm instances.csv