GCloud
Gcloud command syntax |
gcloud <global flags> <service/product> <group/area> <command> <flags> <parameters> |
global flags format |
--project myprojid OR --project=myprojid |
Set config property |
gcloud config set <property> <value> |
Check config property |
gcloud config get-value <property> |
Remove config property |
gcloud config unset <property> |
Interactive config creation |
gcloud init |
List all properties in a configuration |
gcloud config list |
List all configurations |
gcloud config configurations list |
Make new configuration |
gcloud config configurations create <NewConfigName> |
Switch to an existing config |
gcloud config configurations activate <NewConfigName> |
Specify config in a command |
--configuration=ITS_NAME ex. gcloud --configuration=newconfig config list |
Global Flags
--help, -h, --project <ProjectID>, --account <Account>, --filter, --format <JSON, YAML, CSV>, --quite, -q
Config Properties
* Set "core/account" or "account" to replace "--account"
* Set "core/project" or "project" to replace "--project"
* Set "compute/region" to replace "--region"
* Set "compute/zone" to replace "--zone"
Global Flags
--account |
User account to use for invocation |
--billing-project |
GCP Object that will be charged quota for operations performed in gcloud. Use this flag when you want to use resources from project, but billed to a different project. |
--configuration |
configuration to be used for this invocation. |
--flags-file |
A YAML or JSON file that specifies a --flag:value dictionary |
--flatten=[KEY,...] |
Flatten output resources slices in KEY into separate records for each item in each slice. |
--format |
Format for printing command output resources. |
--help |
Display detailed help |
--project |
Specify projectId to be used in this invocation |
--quiet |
Disable all interactive prompts. |
--verbosity |
one of debug, info, warning, error, critical, none |
--version, -v |
Print version and exit |
-h |
Print summary help and exit. |
|
|
GCloud 2
Get info on a topic |
gcloud topic filters |
System Administration
Get user |
whoami |
Get Machine name |
hostname |
Get IP address of current machine |
curl api.ipify.org |
Ping with limited repetitions |
ping -c 3 myhappyvm |
SSH to the instance |
gcloud compute ssh myhappyvm |
Get info stored by metadata service |
curl -H "Metadata-Flavor:google" metadata.google.internal/computeMetadata/v1/ |
We can ping a Compute instance only using the external IP of the instance.
It is not possible to SSH directly to an instance. Hence use gcloud compute ssh
We can drill down into the Metadata service file system using the Curl Output.
Project
Create a project |
gcloud projects create [PROJECT_ID] [--folder=FOLDER_ID] [--labels=[KEY=VALUE,...]][--name=NAME][--set-as-default] |
List all projects for active account |
gcloud projects list [--filter=EXPRESSION][--limit=LIMIT][--page-size=PAGE_SIZE][--sort-by=[FIELD,...]][--uri]] |
Delete a project |
gcloud projects delete PROJECT_ID_OR_NUMBER |
Print Metadata of a project |
gcloud projects describe PROJECT_ID_OR_NUMBER |
Undelete a project |
gcloud projects undelete PROJECT_ID_OR_NUMBER |
Update name of a project |
gcloud projects update PROJECT_ID --name=NAME |
You can add Project Wide Flags at the end of all of the above commands.
--accounts, --billing-project, --configuration, --flags-file, --flatten, --format, --help, --impersonate-service-account, --log-http, --project, --quiet, --trace-token, --user-output-enabled, --verbosity
Run $ gcloud help for details
Components, SDK
List all installed components |
gcloud components list |
Install auto-completion tool |
sudo apt-get install google-cloud-sdk |
Enable auto-completion tool |
gcloud beta interactive |
PubSub
Create a topic |
gcloud pubsub topics create myTopic |
List all topics |
gcloud pubsub topics list |
Delete a topic |
gcloud pubsub topics delete Test1 |
Create a subscription for topic myTopic |
gcloud pubsub subscriptions create --topic myTopic mySubscription |
Delete a subscription |
gcloud pubsub subscriptions delete Test1 |
Publish a message to a topic |
gcloud pubsub topics publish myTopic --message "Message" |
Pull a published message |
gcloud pubsub subscriptions pull mySubscription --auto-ack |
Pull all published messages |
gcloud pubsub subscriptions pull mySubscription --auto-ack --limit=3 |
Once a message is pulled, it is deleted from the queue.
Messages are pulled randomly.
Compute
Create an instance |
gcloud compute instances create <myvm> |
Delete an instance |
gcloud compute instances delete <myvm> |
Get List of instances |
gcloud compute instances list |
Get list of machine types |
gcloud compute machine-types list |
Limit list of machine types |
gcloud compute machine-types list --filter="NAME:f1-micro AND ZONE~us-west" |
SSH to an instance |
gcloud compute ssh myhappyvm |
gcloud compute ssh will create the SSH keys if it is being run for the first time for an instance.
Services
Get list of services |
gcloud services list |
Get list of enabled services |
gcloud services list --enabled |
Get list of available services |
gcloud services list --available |
Get list of available services containing "compute" |
gcloud services list --available | grep compute |
Storage
CLI for accessing storage API |
gsutil |
List all storage buckets |
gsutil ls |
List objects in a bucket |
gsutil ls gs://storage-lab-console |
List all objects in a folder |
gsutil ls gs://storage-lab-console/** This lists all objects in a flat structure |
Create Bucket |
gsutil mb [-c class] [-l location] [-p proj_id] gs://new-bucket-name |
Get labels for bucket in JSON format |
gsutil label get gs://storage-lab-cli/ |
Add label to bucket |
gsutil label ch -l "extralabel:extravalue" gs://storage-lab-cli/ |
Set labels for a bucket |
gsutil label set labels.json gs://storage-lab-cli/ |
Check status of versioning on bucket |
gsutil versioning get gs://storage-lab-cli |
Turn on versioning for bucket |
gsutil versioning set on gs://storage-lab-cli |
Turn off versioning for bucket |
gsutil versioning set off gs://storage-lab-cli |
Delete a file from bucket |
gsutil rm gs://storage-lab-cli/README.txt |
Make a file public in a storage bucket |
gsutil acl ch -u AllUsers:R gs://storage-lab-cli/Selfie.jpg |
Remove public access for file |
gsutil acl ch -d AllUsers gs://storage-lab-cli/Selfie.jpg |
BigQuery
Examine schema of the Shakespeare table in samples dataset |
bq show bigquery-public-data:samples.shakespeare |
Get help on query command |
bq help query |
Get list of all commands used by bigquery |
bq help |
List existing projects in the dataset |
bq ls |
List datasets in a public dataset project |
bq ls bigquery-public-data: |
Create a dataset |
bq mk babynames |
Run a query on BigQuery dataset |
bq query --use_legacy=false 'SELECT word FROM ...' |
Create or update a table AND load data |
bq load <dataset_name>.<table_name> <local_file> name:string,gender:string,count:integer |
List tables in a BQ dataset |
bq ls babynames |
Examine schema of a dataset table |
bq show <dataset_name>.<table_name> |
--use_legacy=false is an optional flag.
|