This document describes some helpful usage tips for gcutil. Note that this is not a comprehensive list. For a comprehensive list of all available gcutil flags and commands, run:
$ gcutil
$ gcutil --help
Contents
Listing available gcutil commands
To list all available gcutil commands, run:
$ gcutil
Similarly, you can list all global gcutil flags by running:
$ gcutil --help
For help on a specific command and list non-global flags for that command:
$ gcutil help <command-name>
Filtering list results
When you list resources, you might want to filter your list results based on a set
of criteria. This can help so that you have a shorter list of more relevant
results. To do so, use the
--filter
flag:
--filter="<expression>"
Your
<expression>
must contain the following:
<field-name> <comparison-string> <literal-string>
-
<field-name>
- The name of the field you want to compare. The field name must be valid for the type of resource being filtered. Only atomic field types are supported (string, number, boolean). Array and object fields are not currently supported.
-
<comparison-string>
-
The comparison string, either
eq
(equals) orne
(not equals). -
<literal-string>
-
The literal string value to filter to. The literal value must be valid for the
type of field (string, number, boolean). For string fields, the literal
value is interpreted as a regular expression using
RE2 syntax
. The literal
value must match the entire field. For example, when filtering instances,
name eq my-instance
won't work, butname eq .*my-instance
will work.
For example:
$ gcutil listinstances --filter="status ne RUNNING" --project=my-project
The above filter causes gcutil to return only results whose
status
field does not equal
RUNNING
. Here is a more complex example:
$ gcutil listinstances --filter="name eq '.*/my-instance-[0-9]+'" --project=my-project
This would list all instances whose name matches the given regular expression.
Formatting list results
gcutil allows you format your list results into one of several display options, including:
-
sparse
-
json
-
csv
-
table
-
names
For example, if you wanted to just list the names of a certain resource, you can use the
names
options:
$ gcutil listinstances --format=names --project=<project-id>
Moving instances across zones
gcutil offers a helper function to move instances across zones using the
gcutil moveinstances
command. The command copies your instance configuration,
takes snapshots of any persistent disks attached to the instance, deletes the
existing instances, and then recreates the instances in your destination zone.
Important warnings
This particular feature takes a snapshot of all your existing persistent disk data, shuts down your instance, recreates your persistent disks in the new zone, and starts new instances with your instance configurations. It is possible that this process could fail before it completes your move. For this reason, we encourage that you also backup your data before running this command , so that you can keep track of your backup resources in case you need to perform recovery. In particular, you should create snapshots of any persistent disk data you want to keep and also create custom images of the instances you are moving.
Other warnings to keep in mind:
-
This command deletes and recreates instances for you, but does not preserve any ephemeral IP addresses or contents of memory. Before executing this command, make sure you have backed up any data you want to keep.
-
You should not modify your project while a move is in progress. In particular, any changes that affect quotas, introduce naming conflicts, or deletes required resources, such as snapshots, will cause unpredictable behavior from gcutil.