dotfiles.2022/_home/dot_sz.shrc.d/32_gcp.shortcuts

110 lines
3.4 KiB
Plaintext

function gcp-project() {
gcloud config get-value core/project 2> /dev/null
}
function gcp-get-project-id() {
gcloud projects list \
--filter="$1" \
--format="value(project_id)" \
2> /dev/null
}
function gcp-get-project-number() {
gcloud projects list \
--filter="project_id:$1" \
--format='value(project_number)' \
2> /dev/null
}
function gcp-get-regions() {
gcloud compute regions list --format="value(name)"
}
function gcp-get-region-quotas() {
region_arr_list=( $(gcloud compute regions list --format="value(name)") )
for region in ${region_arr_list[@]}; do
echo -e $region\\\t$(gcloud compute regions describe $region --format="table(quotas:format='table(metric,limit,usage)')" | grep PREEMPTIBLE_CPUS )
done
#for region in {us-central1,us-east1,us-east4,us-west1,us-west2,us-west3,us-west4}; do
# echo -e $region\\\t$(gcloud compute regions describe $region --format="table(quotas:format='table(metric,limit,usage)')" | grep PREEMPTIBLE_CPUS )
#done
}
GCP_SUBNETS_FORMAT='--format="table(
name,
region,
network,
ipCidrRange,
stack_type
)"'
GCP_DISKS_FORMAT='--format="table(
status,
name,
location():label=ZONE,
sizeGb,
type.basename()
)"'
GCP_INSTANCES_FORMAT='--format="table(
status,
name,
zone,
machineType:label=TYPE,
scheduling.preemptible:label=PREE,
deletionProtection:label=DelPr,
INTERNAL_IP:label=IN_IP,
EXTERNAL_IP:label=EX_IP
)"'
# Usage: gcp-get-stack dragonheart 10 22
# Watch the 'dragonheart' stack,
# refresh every 10 seconds,
# set tmux pane to 22 lines high (on every refresh)
function gcp-get-stack() {
local N="${1:-$(basename $PWD)}"
local _cmd=""
local _tmux=''
[ $# -ge 3 ] && [[ -n "$(which-command tmux)" ]] \
&& printf "%s" "$3" > "/tmp/tmux-$1" \
&& _cmd=$( \
printf "%s " \
'tmux resize-pane' \
'-t "$TMUX_PANE"' \
'-y $(cat "/tmp/tmux-' "$1" '") ; ' \
) \
&& eval $_cmd
_cmd="{ $(printf "%s " \
"N='$N'; " \
"${_cmd}" \
"echo 'Listing $N stack:'; " \
'{ gcloud compute networks' \
'list --filter="name:$N"' \
'2>&1 | sed --unbuffered s/^/Networks.:\ /g ; } ' \
'&& { gcloud compute networks subnets' \
'list --filter="name:$N"' ${GCP_SUBNETS_FORMAT} \
'2>&1 | sed --unbuffered s/^/Subnets..:\ /g ; } ' \
'&& { gcloud compute disks' ${GCP_DISKS_FORMAT} \
'list --filter="name:$N"' \
'2>&1 | sed --unbuffered s/^/Disks....:\ /g ; } ' \
'&& { gcloud compute instances' ${GCP_INSTANCES_FORMAT} \
'list --filter="name:$N"' \
'2>&1 | sed --unbuffered s/^/Instances:\ /g ; } ' \
'&& { gcloud compute routers' \
'list --filter="name:$N"' \
'2>&1 | sed --unbuffered s/^/Routers..:\ /g ; } ' \
'&& { gcloud compute network-firewall-policies' \
'get-effective-firewalls --network="$N-network"' \
'2>&1 | sed --unbuffered s/^/Firewall:\ /g ; } ' \
) ;}"
[ $# -ge 2 ] && _cmd="watch -n${2} '${_cmd}'"
[[ -n ${SZ_DEBUG} ]] && printf "Executing: \n\t$_cmd\n\n"
eval $_cmd
}
# printf '\n\n'; gcp-get-stack 'dragonheart' 5
function gcp-region() {
gcloud config get-value compute/region 2> /dev/null
}