how to restart instance group via python google cloud library

1k views Asked by At

I am not able to find any code sample or relevant documentation on python library for google cloud

Want to restart managed instance groups all vms via cloud function.

To list instances I am using something like this

import googleapiclient.discovery

def list_instances(compute, project, zone):
    result = compute.instances().list(project=project, zone=zone).execute()
    return result['items'] if 'items' in result else None

in requirement file I have

google-api-python-client==2.31.0
google-auth==2.3.3
google-auth-httplib2==0.1.0

From command line this is possible via SDK -> https://cloud.google.com/sdk/gcloud/reference/compute/instance-groups/managed/rolling-action/restart

gcloud compute instance-groups managed rolling-action restart NAME [--max-unavailable=MAX_UNAVAILABLE] [--region=REGION     | --zone=ZONE] [GCLOUD_WIDE_FLAG …]

But in python I am not able to write any code.

3

There are 3 answers

1
Hitobat On

This is an incomplete answer since the python docs are pretty unreadable to me.

Looking at the gcloud cli code (which I couldn't find an official repo for so I looked here), the restart command is triggered by something called a "minimal action".

minimal_action = (client.messages.InstanceGroupManagerUpdatePolicy.
                  MinimalActionValueValuesEnum.RESTART)

In the Python docs, there's references to these fields in the applyUpdatesToInstances method.

So I think the relevant code is something similar to:

compute.instanceGroupManagers().applyUpdatesToInstances(
  project=project,
  zone=zone,
  instanceGroupManager='NAME',
  body={"allInstances": True, "minimalAction": "RESTART"},
)

There may or may not be a proper Python object for the body, the docs aren't clear. And the result seems to be an Operation object of some kind, but I don't know if there's execute() method or not.

0
Grzenio On

This is confusing, because gcloud compute instance-groups managed rolling-action is syntactic sugar that does two things:

  • It turns on Proactive updater, by setting appropriate UpdatePolicy on the InstanceGroupManager resource
  • And it changes version name on the same resource to trigger an update.

It is covered in the docs in https://cloud.google.com/compute/docs/instance-groups/rolling-out-updates-to-managed-instance-groups#performing_a_rolling_replace_or_restart

Compare the gcloud and API tabs to get the idea.

Unfortunately I am illiterate in Python, so I am not able to translate it into Python code :(.

2
Ricco D On

Using the documentation that @Grzenio provided, use patch() method to restart the instance group. See patch documentation to check its parameters.

This could be written in python using the code below. I provided the required parameters project,zone,instanceGroupManager and body. The value of body is from the example in the documentation.

import googleapiclient.discovery
import json

project = 'your-project-id'
zone = 'us-central1-a' # the zone of your instance group
instanceGroupManager = 'instance-group-1' # instance group name
body = {
    "updatePolicy": {
        "minimalAction": "RESTART",
        "type": "PROACTIVE"
    },
    "versions": [{
        "instanceTemplate": "global/instanceTemplates/instance-template-1",
        "name": "v2"
    }]
}

compute = googleapiclient.discovery.build('compute', 'v1')
rolling_restart = compute.instanceGroupManagers().patch(
        project=project,
        zone=zone,
        instanceGroupManager=instanceGroupManager,
        body=body
        )
restart_operation = rolling_restart.execute() # execute the request
print(json.dumps(restart_operation,indent=2))

This will return an operation object and the instance group should restart in the rolling fashion:

{
  "id": "3206367254887659944",
  "name": "operation-1638418246759-5d221f9977443-33811aed-eed3ee88",
  "zone": "https://www.googleapis.com/compute/v1/projects/your-project-id/zones/us-central1-a",
  "operationType": "patch",
  "targetLink": "https://www.googleapis.com/compute/v1/projects/your-project-id/zones/us-central1-a/instanceGroupManagers/instance-group-1",
  "targetId": "810482163278776898",
  "status": "RUNNING",
  "user": "[email protected]",
  "progress": 0,
  "insertTime": "2021-12-01T20:10:47.654-08:00",
  "startTime": "2021-12-01T20:10:47.670-08:00",
  "selfLink": "https://www.googleapis.com/compute/v1/projects/your-project-id/zones/us-central1-a/operations/operation-1638418246759-5d221f9977443-33811aed-eed3ee88",
  "kind": "compute#operation"
}