-
-
Save rajathithan/3cd858198cafef22f684a9cb42ad4dcd to your computer and use it in GitHub Desktop.
#!/bin/bash | |
# Script to retrieve compute engine details. | |
# Author - Rajathithan Rajasekar - 03/03/2020 | |
echo "PROJECT NAME, INSTANCE NAME , ZONE , MACHINE-TYPE , OPERATING SYSTEM , CPU , MEMORY , DISK SIZE" > compute-engine-details.csv | |
prjs=( $(gcloud projects list | tail -n +2 | awk {'print $1'}) ) | |
for i in "${prjs[@]}" | |
do | |
echo "@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@" >> list.txt | |
echo "Setting Project: $i" >> list.txt | |
echo $(gcloud config set project $i) | |
if [[ $(gcloud services list --enabled --filter="NAME:compute.googleapis.com" --format="value(NAME)" | grep compute) ]]; then | |
echo "Compute API is enabled is in project - $i" | |
echo $(gcloud compute instances list | awk '{print $1,$2}' | tail -n +2| while read line; do echo "$i $line"; done |xargs -n3 sh -c 'python3 retrieve-compute-engine-details.py $1 $2 $3 >> compute-engine-details.csv' sh) | |
else | |
echo "Compute API is not enabled in project - $i" | |
fi | |
done | |
403 is a forbidden error, you dont have access to that project.
@rajathithan While trying to execute the script, I'm getting the below error. ModuleNotFoundError: No module named 'googleapiclient'. Any thoughts?
test@ubuntu:~/Downloads$ ./compute-engine-details.sh
Updated property [core/project].
Compute API is enabled is in project - ate-bastion
Traceback (most recent call last):
File "retrieve-compute-engine-details.py", line 4, in
from googleapiclient import discovery
ModuleNotFoundError: No module named 'googleapiclient'
Updated property [core/project].
@rajathithan While trying to execute the script, I'm getting the below error. credentials = GoogleCredentials.get_application_default(). Any thoughts?
root@ubuntu:/home/test/Downloads# ./compute-engine-details.sh
Updated property [core/project].
Compute API is enabled is in project - ate-bastion
Traceback (most recent call last):
File "retrieve-compute-engine-details.py", line 10, in
credentials = GoogleCredentials.get_application_default()
File "/usr/local/lib/python3.8/dist-packages/oauth2client/client.py", line 1271, in get_application_default
return GoogleCredentials._get_implicit_credentials()
File "/usr/local/lib/python3.8/dist-packages/oauth2client/client.py", line 1261, in _get_implicit_credentials
raise ApplicationDefaultCredentialsError(ADC_HELP_MSG)
oauth2client.client.ApplicationDefaultCredentialsError: The Application Default Credentials are not available. They are available if running in Google Compute Engine. Otherwise, the environment variable GOOGLE_APPLICATION_CREDENTIALS must be defined pointing to a file defining the credentials. See https://developers.google.com/accounts/docs/application-default-credentials for more information.
Hi,
I hope you are running it from cloud shell, there is a bug with gcloud SDK to display the output in rows instead of columnar format. update your gcloud sdk in cloud shell and rerun the script .
Thanks, Raj
@rajathithan Thank you. Can you let me know how to get the hostname and Private IP of the machines through this script (both Linux & Windows)?
Thanks, Vishnu
While trying to execute the script, I'm getting the below:
File "/home/xpto/compute_details/retrieve-compute-engine-details.py", line 19, in <module>
request = service.instances().get(project=project, zone=zone, instance=instance)
File "/usr/local/lib/python3.9/dist-packages/googleapiclient/discovery.py", line 1073, in method
raise TypeError(
TypeError: Parameter "zone" value "NAME:" does not match the pattern "[a-z](?:[-a-z0-9]{0,61}[a-z0-9])?"
Traceback (most recent call last):
File "/home/xpto/compute_details/retrieve-compute-engine-details.py", line 19, in <module>
request = service.instances().get(project=project, zone=zone, instance=instance)
File "/usr/local/lib/python3.9/dist-packages/googleapiclient/discovery.py", line 1073, in method
@rajathithan - While trying to execute the script, I'm getting the below error. I'm running it on the compute engine. Any thoughts?