-
Notifications
You must be signed in to change notification settings - Fork 3
/
gce-functions
75 lines (58 loc) · 1.77 KB
/
gce-functions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
#!/bin/bash
: ${DEBUG:=1}
debug() {
[[ "$DEBUG" ]] && echo "[DEBUG] $*" 1>&2
}
gce_reload_functions() {
source $BASH_SOURCE
}
gce_upgrade_functions() {
curl -Lo $BASH_SOURCE j.mp/gce-functions
source $BASH_SOURCE
}
gce_zones() {
gcloud compute zones list --format json|jq .[].name -r
}
gce_list_disks() {
gcloud compute disks list --format json | jq ".[].name" -r
}
gce_clean_unused_disks() {
for zone in $(gcloud compute zones list --format json|jq .[].name -r); do
gce_clean_unused_disks_in_zone $zone
done
}
gce_delete_instances() {
declare desc="deletes instances matching a regexp"
declare nameRegexp=$1
: ${nameRegexp:? required}
gcloud compute instances list -r ${nameRegexp}.* --format json \
| jq '.[]|.name,.zone' -r | \
while read name && read zone; do
gcloud compute instances delete --quiet --zone $zone $name &
done
}
gce_clean_unused_disks_in_zone() {
declare zone=$1
: ${zone:? required}
debug "[$zone] cleaning unsed disks"
local used=$(
gcloud compute instances list --zone $zone --format json \
| jq '[.[]|.disks[].source]|join("|")' -r \
)
local grepFilter=$(echo "$used"| sed "s:|:\\\\|:g")
gcloud compute disks list --zone $zone --format json \
| jq ".[].name" -r \
| grep -v "$grepFilter" \
| xargs --no-run-if-empty -t gcloud compute disks delete --zone $zone
}
gce_list_running() {
gcloud compute instances list
}
gce_list_by_regexp() {
gcloud compute instances list --regexp ".*$1.*"
}
gce_list_runnings_() {
gcloud compute instances list --format json \
|jq ".[]|.name, .zone, .status, .networkInterfaces[0].accessConfigs[0].natIP, .networkInterfaces[0].networkIP" -r \
|xargs -n 5 printf "%-40s | %-15s | %-10s | %-15s | %-15s \n"
}