1

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.

Grzenio
  • 35,875
  • 47
  • 158
  • 240

3 Answers3

0

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.

Hitobat
  • 2,847
  • 1
  • 16
  • 12
  • Note that applyUpdatesToInstances method will restart instances in the MIG, but *not* in a rolling fashion (it will do them all at once), which is different from the gcloud command the OP is referring to. – Grzenio Dec 01 '21 at 16:29
0

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 :(.

Grzenio
  • 35,875
  • 47
  • 158
  • 240
0

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": "serviceaccountused@your-project-id.iam.gserviceaccount.com",
  "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"
}
Donnald Cucharo
  • 3,866
  • 1
  • 10
  • 17
Ricco D
  • 6,873
  • 1
  • 8
  • 18
  • @GauravTomar I suggest that you open a new question with regards to targeting unmanaged instance groups since the original question is intended for managed instance groups. – Ricco D Dec 06 '21 at 01:57
  • @GauravTomar if this answered your original question, please consider accepting it by clicking the check icon on the left side. This will help other community members if they encounter the same problem. – Donnald Cucharo Dec 07 '21 at 03:05