1

I need to create a script that deletes instance groups from Google Cloud Platform. This group can be in any zone. When I run command:

gcloud compute instance-groups managed delete [instance-group-name]

(without zone or region) I get prompted to choose a region or a zone. Any help with a script like this?

Grzenio
  • 35,875
  • 47
  • 158
  • 240
Yivgeny
  • 11
  • 1
  • 2

2 Answers2

0

Placing the --zone flag and specifying it in the command would obviously prevent the prompt for the zone. However, if the zone of each instance group is different and you would like to automate this process, you could add some commands to your script that lists your instance group (the output will include the zone) and then filter the output to create a variable that contains the zone information. This variable could then be used in your gcloud compute instance-groups managed delete command.

For example, to isolate the zone of the instance group you would like to delete you could try something like this.

gcloud compute instance-groups list | grep INSTANCE_GROUP_NAME | awk '{print $2;}'

The above command first uses grep to filter a line of information with details of the instance group you would like to delete, so before awk is applied, the information looks like this:

test-instance-group                             us-west1-c  zone   default  Yes      1

The the awk '{print $2;}' filter print out the second non-whitespace substring, which will be the zone so the following is outputted:

us-west1-c

So in terms of your script, before you execute the command to delete the instance group, you would need to set a variable for that instance groups zone. To summarise, here is how your script could look:

#!/bin/bash

### retrieve the zone information of the instance group and store it in a variable.

zonevar="$(gcloud compute instance-groups list | grep INSTANCE_GROUP_NAME  | awk '{print $2;}')"

### use the variable in the gcloud command to delete the instance group.

gcloud compute instance-groups managed delete INSTANCE_GROUP_NAME --zone=$zonevar

This same logic could be applied if you wanted to apply regional information rather than zonal information.

neilH
  • 3,320
  • 1
  • 17
  • 38
  • Thank you, It's vbs script ? How i run it on windows ? – Yivgeny Feb 27 '18 at 16:47
  • Sorry, I am not familiar with vbs so I can't tranlsate this to vbs, although if you are writing a vbs script, you could use the same logic, but obviously with vbs syntax. – neilH Feb 28 '18 at 08:14
  • Also it's probably worth adding a vbscript tag to your question. – neilH Feb 28 '18 at 08:29
0

You can leverage --format to generalize the script and avoid the slice&dice pipeline. A commmand line specific format also future proofs you against table format changes which are not part of the CLI contract. This script also handle zone/region locations.

#!/bin/bash

typeset -A locationsof

function getlocations {
  set -- $(gcloud compute instance-groups list \
           --format="value(name,location(),location_scope())")
  while (( $# >= 3 ))
  do
    locationsof[$1]+=" --$3=$2"
    shift 3
  done
}

getlocations

for name
do
  if [[ $name == -n || $name == --show ]]; then
    show=echo
    continue
  fi
  locations=${locationsof[$name]}
  if [[ ! $locations ]]; then
    echo "$name: unknown instance group" >&2
    continue
  fi
  for location in $locations
  do
    $show gcloud compute instance-groups managed delete $name $location
  done
done

Test the script with -n or --show before really deleting anything. Run any list command with --verbosity=info to see the default output format. That's how the --format incantation was derived.

Glenn Fowler
  • 589
  • 2
  • 6