Last active
June 9, 2019 10:11
-
-
Save shal/cce40c8c46f6eb209d20c081cefdf4e0 to your computer and use it in GitHub Desktop.
Google Cloud Scheduler for VMs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash -ex | |
# Author: Ali Shanaakh <hi@shal.dev> | |
check_vms() { | |
local PROJECT="gcloud-project" | |
local time=`date +%k%M` | |
local output=`gcloud compute --project "${PROJECT}" instances list --filter="labels.infinite: false" --format='table(name)'` | |
for instance in ${output[@]:4}; do | |
echo "Time now: $time" | |
if [[ $(eval "$time") -ge 1900 ]] && [[ $(eval "$time") -le 0600 ]]; then | |
echo "Starting virtual machinies with label infinite=false..." | |
gcloud compute --project "${PROJECT}" instances start $instance --zone "europe-west1-b" | |
else | |
echo "Stopping virtual machinies with label infinite=false..." | |
gcloud compute --project "${PROJECT}" instances stop $instance --zone "europe-west1-b" | |
fi | |
done | |
} | |
check_vms |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment