def worker(): while True: item = WORK_QUEUE.get() if not item: break try: # We take three keys out of the config item. The rest is passed # as-is to create_instance_template() and thus to the gcloud # command line tool. count = item.pop("count") instance_group_name = item.pop("name") zone = item.pop("zone") template_name = instance_group_name + "-template" if gcloud.delete_instance_group(instance_group_name, zone=zone).returncode == 0: print("Deleted existing instance group: {}".format( instance_group_name)) if gcloud.delete_instance_template(template_name).returncode == 0: print("Deleted existing VM template: {}".format(template_name)) gcloud.create_instance_template(template_name, **item) gcloud.create_instance_group( instance_group_name, zone=zone, base_instance_name=instance_group_name, template=template_name, size=count, ) finally: WORK_QUEUE.task_done()
def worker(): while True: item = WORK_QUEUE.get() if not item: break try: # We take a few keys out of the config item. The rest is passed # as-is to create_instance_template() and thus to the gcloud # command line tool. count = item.pop("count") instance_group_name = item.pop("name") project = item.pop("project") zone = item.pop("zone", None) region = item.pop("region", None) if not project: raise Exception("Invalid instance config, no project name set") if not zone and not region: raise Exception( "Invalid instance config, either zone or region must be specified" ) timestamp = datetime.now().strftime("%Y%m%dt%H%M%S") template_name = "{}-{}".format(instance_group_name, timestamp) if zone is not None: if (gcloud.delete_instance_group(instance_group_name, project=project, zone=zone).returncode == 0): print("Deleted existing instance group: {}".format( instance_group_name)) elif region is not None: if (gcloud.delete_instance_group( instance_group_name, project=project, region=region).returncode == 0): print("Deleted existing instance group: {}".format( instance_group_name)) # Create the new instance template. gcloud.create_instance_template(template_name, project=project, **item) print("Created instance template {}".format(template_name)) # Create instance groups with the new template. kwargs = { "project": project, "base_instance_name": instance_group_name, "size": count, "template": template_name, } if zone is not None: kwargs["zone"] = zone elif region is not None: kwargs["region"] = region gcloud.create_instance_group(instance_group_name, **kwargs) print("Created instance group {}".format(instance_group_name)) finally: WORK_QUEUE.task_done()
def instance_group_task(instance_group_name, count, zone, **kwargs): template_name = instance_group_name + '-template' if gcloud.delete_instance_group(instance_group_name, zone=zone).returncode == 0: print( 'Deleted existing instance group: {}'.format(instance_group_name)) if gcloud.delete_instance_template(template_name).returncode == 0: print('Deleted existing VM template: {}'.format(template_name)) gcloud.create_instance_template(template_name, **kwargs) gcloud.create_instance_group(instance_group_name, zone=zone, base_instance_name=instance_group_name, template=template_name, size=count)