There are a couple of challenges:
instances describe
works best with the instance name and zone
machine-types describe
is needed (!?) to get CPU|RAM for non-custom
- custom machine types are self-describing
On Linux|Bash, here's the basic info:
# Get instance name,zone for `${PROJECT}
for PAIR in $(
gcloud compute instances list
--project=${PROJECT}
--format="csv[no-heading](name,zone.scope(zones))")
do
# Parse result from above into instance and zone vars
IFS=, read INSTANCE ZONE <<< ${PAIR}
# Get the machine type value only
MACHINE_TYPE=$(
gcloud compute instances describe ${INSTANCE}
--project=${PROJECT}
--zone=${ZONE}
--format="value(machineType.scope(machineTypes))")
# If it's custom-${vCPUs}-${RAM} we've sufficient info
if [[ ${MACHINE_TYPE}} == custom* ]]
then
IFS=- read CUSTOM CPU MEM <<< ${MACHINE_TYPE}
printf "%s: vCPUs: %s; Mem: %s
" ${INSTANCE} ${CPU} ${MEM}
else
# Otherwise, we need to call `machine-types describe`
CPU_MEMORY=$(
gcloud compute machine-types describe ${MACHINE_TYPE}
--project=${PROJECT}
--zone=${ZONE}
--format="csv[no-heading](guestCpus,memoryMb)")
IFS=, read CPU MEM <<< ${CPU_MEMORY}
printf "%s: vCPUs: %s; Mem: %s
" ${INSTANCE} ${CPU} ${MEM}
fi
done
I'll leave it to you to combine with the instances describe
data as you wish.
There is undoubtedly another (and possibly better) way to do this.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…