def worker()

in buildkite/create_instances.py [0:0]


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)
            health_check = item.pop("health_check", None)
            initial_delay = item.pop("initial_delay", 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:
                kwargs["zone"] = zone
            elif region:
                kwargs["region"] = region
            if health_check:
                kwargs["health_check"] = health_check
            if initial_delay:
                kwargs["initial_delay"] = initial_delay
            gcloud.create_instance_group(instance_group_name, **kwargs)
            print("Created instance group {}".format(instance_group_name))
        finally:
            WORK_QUEUE.task_done()