Skip to content

Instantly share code, notes, and snippets.

$ python code-samples/gcloud-python/start_stop_instances_labels_gcloud.py
Executing command "gcloud compute instances stop kube-master --zone us-central1-c --project sj-test-070418"
Stopping instance(s) kube-master...done.
Updated [https://www.googleapis.com/compute/v1/projects/sj-test-070418/zones/us-central1-c/instances/kube-master].
Executing command "gcloud compute instances stop db-server-1 --zone us-central1-c --project sj-test-070218"
Stopping instance(s) db-server-1...done.
Updated [https://www.googleapis.com/compute/v1/projects/sj-test-070218/zones/us-central1-c/instances/db-server-1].
$ python code-samples/gcloud-python/delete_unattached_unused_disks_gcloud.py
Disk kube-master is attached to instance kube-master
Disk orphan-disk-2 is not attached to any instance and is not labeled for delete protection
Executing command "gcloud compute disks delete orphan-disk-2 --zone europe-west1-b --project sj-test-070218"
Disk orphan-disk-3 is not attached to any instance and is labeled for delete protection
@shashyajoshi
shashyajoshi / get_disks_data.output
Created July 2, 2018 22:14
Output of the get_disks_data.sh script
$ bash get_disk_data.sh
project_id,disk_name,user
sj-test-070418,kube-master,kube-master
sj-test-070218,orphan-disk-2,
sj-test-070218,db-server-1,db-server-1
sj-test-062218,orphan-disk-1,
sj-test-062218,app-server-1,app-server-1
sj-test-062218,web-server-1,web-server-1
sj-test-062218,web-server-2,web-server-2
@shashyajoshi
shashyajoshi / get_disks_data.sh
Last active July 2, 2018 23:36
Script to collect disk user data
#!/bin/bash
# Header row for the CSV output
echo "project_id,disk_name,user"
# for every project in the list get the required details. Remove CSV header from the gcloud output. Add the project id to the beginning of each row
for project in $(gcloud projects list --format="value(projectId)")
do
# Because of the nested structure you get '] at the end of the output. Use sed to remove it
gcloud compute disks list --format="csv[no-heading](name,users.basename())" --project $project | sed s/\']//g | sed "s/^/$project,/"
@shashyajoshi
shashyajoshi / get_license_data.output
Created July 2, 2018 21:50
Output of the get_license_data.sh script
$ bash get_license_data.sh
project_id,instance_name,license
sj-test-070418,kube-master,centos-7
sj-test-070218,db-server-1,centos-7
sj-test-062218,app-server-1,centos-7
sj-test-062218,web-server-1,centos-7
sj-test-062218,web-server-2,centos-7
sj-test-062218,test-redhat,rhel-7-server
@shashyajoshi
shashyajoshi / get_license_data.sh
Last active July 2, 2018 23:36
Script to collect OS license data from the instances
#!/bin/bash
# Header row for the CSV output
echo "project_id,instance_name,license"
# for every project in the list get the required details. Remove CSV header from the gcloud output. Add the project id to the beginning of each row
for project in $(gcloud projects list --format="value(projectId)")
do
# Because of the nested structure you get ']] at the end of the output. Use sed to remove it
gcloud compute instances list --format="csv[no-heading](name,disks.licenses.basename())" --project $project | sed s/\']]//g | sed "s/^/$project,/"
@shashyajoshi
shashyajoshi / gcloud_compute_instances_list_csv.output
Created July 2, 2018 21:34
Output of the gcloud instances list command in CSV format
$ gcloud compute instances list - format="csv(name,labels.app-name,labels.biz-unit,labels.env-name,metadata.items.server-role,metadata.items.server-type, metadata.items.os-image)"
name,app-name,biz-unit,env-name,server-role,server-type,os-image
app-server-1,misc,ecom,prod,app,tomcat,centos
web-server-1,misc,ecom,prod,web,nginx,centos
web-server-2,misc,ecom,prod,web,nginx,centos
@shashyajoshi
shashyajoshi / gcloud_compute_instances_list.output
Last active July 2, 2018 22:55
Output of the gcloud instances list command in json format
$ gcloud compute instances list --format json
(~~ part of the response ~~)
"id": "3091147347390362603",
"kind": "compute#instance",
"labels": {
"app-name": "misc",
"biz-unit": "ecom",
"env-name": "prod",
},
"metadata": {
@shashyajoshi
shashyajoshi / gcloud_projects_list_csv.output
Last active July 2, 2018 21:33
Output of gcloud project list command in CSV format
1)
$ gcloud projects list --format="csv(projectId, name, labels)"
project_id,name,labels
sj-test-070418,sj-test-070418,app-name=micro-svc-app;biz-unit=it;env-name=qa;owner=john-smith
sj-test-070218,sj-test-070218,app-name=myapp;biz-unit=engineering;env-name=non-prod;owner=shashank
sj-test-062218,sj-test-062218,app-name=misc;biz-unit=ecom;env-name=prod;owner=jane-doe
secure-moment-207103,My First Project,
2)
@shashyajoshi
shashyajoshi / gcloud_projects_list.output
Last active April 15, 2019 12:01
Output of the gcloud projects list command in json format
$ gcloud projects list --format json
[
{
"createTime": "2018-07-02T14:13:29.262Z",
"labels": {
"app-name": "micro-svc-app",
"biz-unit": "it",
"env-name": "qa",
"owner": "john-smith"
},