Skip to content

Instantly share code, notes, and snippets.

@avivl
Created February 18, 2018 10:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save avivl/ee9e7c11c254e25b890d48c1a873fdc1 to your computer and use it in GitHub Desktop.
Save avivl/ee9e7c11c254e25b890d48c1a873fdc1 to your computer and use it in GitHub Desktop.
def change_status(self, to_status, tagkey, tagvalue):
"""
Stop/start instance based on tags
Args:
to_status: 0 stop 1 start
tagkey: tag key
tagvalue: tag value
Returns:
"""
tag_filter = "labels." + tagkey + "=" + tagvalue
logging.debug("Filter %s", filter)
for zone in gcp.get_zones():
try:
instances = self.list_instances(zone, tag_filter)
for instance in instances:
if int(to_status) == 1:
logging.info(
"Starting %s in project %s tagkey %s tgavalue %s",
instance['name'], self.project, tagkey, tagvalue)
self.start_instance(zone, instance['name'])
else:
logging.info(
"Stopping %s in project %s tagkey %s tgavalue %s",
instance['name'], self.project, tagkey, tagvalue)
self.stop_instance(zone, instance['name'])
except HttpError as http_error:
logging.error(http_error)
return 'Error', 500
return 'ok', 200
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment