Created
February 18, 2018 10:08
-
-
Save avivl/ee9e7c11c254e25b890d48c1a873fdc1 to your computer and use it in GitHub Desktop.
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
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