Schedule GCP VMs to start or stop

  • Listing all projects
  • listing VMs, with metainfo
  • Stopping and starting VMs
[u”run-24–7", u”run-24–5", u”run-16–5"]
def program_list_all_projects(self):
p_args = ["gcloud", "projects", "list", "--format", "json",
"--quiet"]
o = subprocess.check_output(p_args)
return json.loads(o)
def program_list_all_compute(self):
project_tag = "--project={}".format(self.project)
p_args = ["gcloud", "compute", "instances", "list", "--format"
, "json", project_tag, "--quiet"]
o = subprocess.check_output(p_args)
return json.loads(o)
def program_stop_vm(self, instance_name, instance_zone):
project_tag = "--project={}".format(self.project)
p_args = ["gcloud", "compute", "instances", "stop", "--format",
"json", instance_name, project_tag,
"--zone={}".format(instance_zone), "--quiet"]
if self.dry_run:
return json.loads("[]")
o = subprocess.check_output(p_args)
return json.loads(o)
def program_start_vm(self, instance_name, instance_zone):
project_tag = "--project={}".format(self.project)
p_args = ["gcloud", "compute", "instances", "start", "--format",
"json", instance_name, project_tag,
"--zone={}".format(instance_zone), "--quiet"]
if self.dry_run:
return json.loads("[]")
o = subprocess.check_output(p_args)
return json.loads(o)

--

--

--

Software developer and cloud enthusiast

Love podcasts or audiobooks? Learn on the go with our new app.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Anders Elton

Anders Elton

Software developer and cloud enthusiast

More from Medium

A Simple Analogy to understand Google Cloud Platform’s Identity and Access Management IAM

Getting started with Google Cloud Endpoints OpenAPI

Maximize your cloud investment with FinOps on Google Cloud

Cloud Deployment Models