John Mark Causing

System Administrator | Hosting Support Engineer

Bacolod City, Philippines

+639393497018

John Mark Causing

System Administrator | Hosting Support Engineer

Bacolod City, Philippines

+639393497018

Useful commands from Google Cloud Console.

Google Cloud Storage

This will get the list of config like account name, project, etc.

gcloud config list.
[email protected]:~ (esoteric-code-327621)$ gcloud config list
[accessibility]
screen_reader = True[component_manager]
disable_update_check = True
[compute]
gce_metadata_read_timeout_sec = 30
[core]
account = [email protected]
disable_usage_reporting = True
project = esoteric-code-327621
[metrics]
environment = devshell

Get Cloud Storage bucket list

gsutil ls
[email protected]:~ (esoteric-code-327621)$ gsutil ls
gs://storage-lab-cli-johnmark1/
gs://storage-lab-console-johnmark/

List the files and folders from a specific bucket

gsutil ls gs://storage-lab-console/
gsutil ls gs://storage-lab-console/**

Create a Google Cloud Storage bucket (you need to include the GCP region)

gsutil mb --help
gsutil mb -l northamerica-northeast1 gs://storage-lab-cli
gsutil ls

Get current label of a bucket

[email protected]:~ (esoteric-code-327621)$ gsutil label get gs://storage-lab-cli-johnmark1/
{
  "extralabel": "extravalue",
  "function": "learning"
}

Append bucket label to a file

gsutil label get gs://storage-lab-console/ >bucketlabels.json
cat bucketlabels.json

Upload label from a file to a specific bucket

gsutil label set bucketlabels.json gs://storage-lab-cli/
[email protected]:~ (esoteric-code-327621)$ gsutil label set bucketlabels.json gs://storage-lab-cli-johnmark1/
Setting label configuration on gs://storage-lab-cli-johnmark1/...

Please note that this will not work if there’s a SPACE in a key or a value. Example:

[email protected]:~ (esoteric-code-327621)$ vi bucketlabels.json 
[email protected]:~ (esoteric-code-327621)$ cat bucketlabels.json
{
  "label1": "value1 and space"
}
[email protected]:~ (esoteric-code-327621)$ gsutil label set bucketlabels.json gs://storage-lab-cli-johnmark1/
Setting label configuration on gs://storage-lab-cli-johnmark1/...
BadRequestException: 400 Invalid argument.

Update or add a specific label (provide key value)

gsutil label ch -l "extralabel:extravalue" gs://storage-lab-cli

Remove specific label (only provide key not key and value)

[email protected]:~ (esoteric-code-327621)$ gsutil label ch -d "extralabel" gs://storage-lab-cli-johnmark1/Setting label configuration on gs://storage-lab-cli-johnmark1/...

Check versioning if ON or OFF and switch on or off

[email protected]:~ (esoteric-code-327621)$ gsutil versioning get gs://storage-lab-cli-johnmark1/
gs://storage-lab-cli-johnmark1: Enabled

gsutil versioning set on gs://storage-lab-cli/

To view the each file version, you can use ls -a

[email protected]:~ (esoteric-code-327621)$ gsutil ls -a gs://storage-lab-cli-johnmark1/
gs://storage-lab-cli-johnmark1/241459384_201090535305767_7879290509765068430_n.png#1633042188220249
gs://storage-lab-cli-johnmark1/Megaworld Application Form 2020 v2 (1).docx#1633042188889776
gs://storage-lab-cli-johnmark1/README-cloudshell.txt#1633042063811387
gs://storage-lab-cli-johnmark1/signature.png#1633042189665592
gs://storage-lab-cli-johnmark1/tests.txt#1633042190324585
gs://storage-lab-cli-johnmark1/test1/

Copy and remove examples:

gsutil cp README-cloudshell.txt gs://storage-lab-cli/
gsutil rm gs://storage-lab-cli/README-cloudshell.txt
gsutil cp gs://storage-lab-console/** gs://storage-lab-cli/

Set a file permission to public (ch = change, -u users:permission)

[email protected]:~ (esoteric-code-327621)$ gsutil acl ch -u AllUsers:R gs://storage-lab-cli-johnmark1/tests.txt
Updated ACL on gs://storage-lab-cli-johnmark1/tests.txt

Compute Engine

Get the current project name

gcloud config get-value project
[email protected]:~ (esoteric-code-327621)$ gcloud config get-value project
Your active configuration is: [cloudshell-31550]
esoteric-code-327621

Check if project is set

[email protected]:~$ gcloud config get-value project
Your active configuration is: [cloudshell-1880]
(unset)

Above shows no project yet is set. You can set project using this:

[email protected]:~$ gcloud config set project esoteric-code-327621
Updated property [core/project].

[email protected]:~ (esoteric-code-327621)$ gcloud config get-value project
Your active configuration is: [cloudshell-1880]
esoteric-code-327621

Check list of GCP instances

[email protected]:~ (esoteric-code-327621)$ gcloud compute instances list
API [compute.googleapis.com] not enabled on project [163234898054].
Would you like to enable and retry (this will take a few minutes)?
(y/N)?

ERROR: (gcloud.compute.instances.list) PERMISSION_DENIED: Compute Engine API has not been used in project 163234898054 before or it is disabled. Enable it by visiting https://console.developers.google.com/apis/api/compute.googleapis.com/overview?project=163234898054 then retry. If you enabled this API recently, wait a few minutes for the action to propagate to our systems and retry.
[email protected]:~ (esoteric-code-327621)$

The above shows you are not allowed to do that 403. That is because the “compute” api is not yet enabled. You can see the current neabled api services here:

[email protected]:~ (esoteric-code-327621)$ gcloud services list
NAME: bigquery.googleapis.com
TITLE: BigQuery API

NAME: bigquerystorage.googleapis.com
TITLE: BigQuery Storage API

NAME: cloudapis.googleapis.com
TITLE: Google Cloud APIs

NAME: clouddebugger.googleapis.com
TITLE: Cloud Debugger API

NAME: cloudtrace.googleapis.com
TITLE: Cloud Trace API

NAME: datastore.googleapis.com
TITLE: Cloud Datastore API

NAME: logging.googleapis.com
TITLE: Cloud Logging API

NAME: monitoring.googleapis.com
TITLE: Cloud Monitoring API

NAME: servicemanagement.googleapis.com
TITLE: Service Management API

NAME: serviceusage.googleapis.com
TITLE: Service Usage API

NAME: sql-component.googleapis.com
TITLE: Cloud SQL

NAME: storage-api.googleapis.com
TITLE: Google Cloud Storage JSON API

NAME: storage-component.googleapis.com
TITLE: Cloud Storage

NAME: storage.googleapis.com
TITLE: Cloud Storage API

That shows that Compute Engine is not there. You can enable that by Going to GCP site -> Compute Engine and it will enable that API automatically.

Now you can run again the previous command:

[email protected]:~ (esoteric-code-327621)$ gcloud compute instances list
Listed 0 items.

Create a new instances (without providing a zone, it will suggest one for you):

[email protected]:~ (esoteric-code-327621)$ gcloud compute instances create myvm
Did you mean zone [asia-east1-a] for instance: [myvm] (Y/n)?  Y

Created [https://www.googleapis.com/compute/v1/projects/esoteric-code-327621/zones/asia-east1-a/instances/myvm].
NAME: myvm
ZONE: asia-east1-a
MACHINE_TYPE: n1-standard-1
PREEMPTIBLE:
INTERNAL_IP: 10.140.0.2
EXTERNAL_IP: 34.81.242.242
STATUS: RUNNING

Delete an instance

[email protected]:~ (esoteric-code-327621)$ gcloud compute instances delete myvm
Did you mean zone [asia-east1-a] for instance: [myvm] (Y/n)?  y

The following instances will be deleted. Any attached disks configured
 to be auto-deleted will be deleted unless they are attached to any
other instances or the `--keep-disks` flag is given and specifies them
 for keeping. Deleting a disk is irreversible and any data on the disk
 will be lost.
 - [myvm] in [asia-east1-a]

Do you want to continue (Y/n)?

Deleted [https://www.googleapis.com/compute/v1/projects/esoteric-code-327621/zones/asia-east1-a/instances/myvm].
[email protected]:~ (esoteric-code-327621)$ gcloud compute instances list
Listed 0 items.

gcloud syntax

gcloud

Example of :

–available (to view all available services)
gcloud services list --available

–project (specify a project)
gcloud compute instances list --project esoteric-code-327621

–format
cloud services list --format json

-l (location in gsutil) is the flag
and parameter is: gs://this-is-a-test-bucket-creation
gsutil mb -l northamerica-northeast1 gs://this-is-a-test-bucket-creation

Example of label:

label
gsutil label set bucketlabels.json gs://storage-lab-cli-johnmark1/

Above is like:
gsutil
area = label
command = set
parameter1 = bucketlabels.json
parameter2 = gs://storage-lab-cli-johnmark1/

Config Properties

Check current zone for your config

[email protected]:~ (esoteric-code-327621)$ gcloud config get-value zone
Your active configuration is: [cloudshell-755]
ERROR: (gcloud.config.get-value) Section [core] has no property [zone].
[email protected]:~ (esoteric-code-327621)$ gcloud config get-value zone

The above shows there’s no zone set yet.

Set zone in compute Engine
gcloud config set compute/zone us-west4-c

[email protected]:~ (esoteric-code-327621)$ gcloud config get-value compute/zone
Your active configuration is: [cloudshell-755]
us-west4-c

Above is where you saw the current compute/zone location but you can also check that using:
(List all configurations)
gcloud config configurations list

[email protected]:~ (esoteric-code-327621)$ gcloud config configurations list
NAME: cloudshell-755
IS_ACTIVE: True
ACCOUNT:
PROJECT: esoteric-code-327621
COMPUTE_DEFAULT_ZONE: us-west4-c
COMPUTE_DEFAULT_REGION:

List all properties in a configuration:

gcloud config list
[accessibility]
screen_reader = True
[component_manager]
disable_update_check = True
[compute]
gce_metadata_read_timeout_sec = 30
zone = us-west4-c
[core]
account = [email protected]
disable_usage_reporting = True
project = esoteric-code-327621
[metrics]
environment = devshell

Another example of setting a region in compute engine and get the configurations list:

[email protected]:~ (esoteric-code-327621)$ gcloud config set compute/region us-west4
Updated property [compute/region].
[email protected]:~ (esoteric-code-327621)$ gcloud config configurations list
NAME: cloudshell-755
IS_ACTIVE: True
ACCOUNT:
PROJECT: esoteric-code-327621
COMPUTE_DEFAULT_ZONE: us-west4-c
COMPUTE_DEFAULT_REGION: us-west4
[email protected]:~ (esoteric-code-327621)$

Configuration Analogy (compare to other things)

ActionDirectory (example in Linux)Configuration in GCPExplanation
Make Newmkdir newdirgcloud config configurations create new configIn linux you create a new folder, in GCP you create a config (in Linux, there can be files inside a folder. In GCP, there can be parameters inside the config)
Switch tocd newdirgcloud config configurations activate newconfigIn linux, you switch DIR. In GCP you switch configurations
List contentslsgcloud config listIn Linux, list the files inside the current DIR. In GCP list the configurations from the current active config
List non-activels ~/newdirgcloud — configuration=newconfig config list
or
gcloud confi configurations describe newconfig
In linux you list the contents of a different non-current DIR. In GCP, you list the configuration of a different config (newconfig)

Using filters (–filters).

If you run this to check all machine types, you will get a lot of results: gcloud compute machine-types list

So you can use filters instead like this: gcloud compute machine-types list --filter="NAME:c2-standard ” or gcloud compute machine-types list --filter="NAME:c2-standard-30 ZONE~asia-south"

[email protected]:~ (esoteric-code-327621)$ gcloud compute machine-types list --filter="NAME:c2-standard-30 ZONE~asia-south"
NAME: c2-standard-30
ZONE: asia-southeast1-a
CPUS: 30
MEMORY_GB: 120.00
DEPRECATED:

NAME: c2-standard-30
ZONE: asia-southeast1-b
CPUS: 30
MEMORY_GB: 120.00
DEPRECATED:

NAME: c2-standard-30
ZONE: asia-southeast1-c
CPUS: 30
MEMORY_GB: 120.00
DEPRECATED:

NAME: c2-standard-30
ZONE: asia-south1-b
CPUS: 30
MEMORY_GB: 120.00
DEPRECATED: