Last active
December 2, 2020 19:34
-
-
Save cmcconnell1/222b0e70bef610f15d6e7f311203533c to your computer and use it in GitHub Desktop.
enable compute.googleapis.com and label all gcp projects
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# gcloud configuration options to reduce output verbosity: | |
# gcloud config set core/log_http False; | |
# gcloud config set core/user_output_enabled True; | |
# debug, info, warning, error, and none | |
# gcloud config set core/verbosity warning; | |
function usage() { | |
printf "\nUsage: $0 [-help ] [-h]\n" | |
printf "\nThis script will enable compute.googleapis.com and add the specified labels in the 'PROJECT_LABELS'\n shell variable to ALL GCP projects within the org.\n" | |
printf "\nWhen ready for a live run disable the 'grep' filter command in the for loop\n" | |
printf "\nNote: Billing account for project should be enabled--else will complain and bark...\n\n" | |
exit | |
} | |
case $1 in | |
--help|-h) | |
usage; | |
exit $?;; | |
esac | |
shift | |
#PROJECT_LABELS="newkey1=newvalue1,newkey2=newvalue2" | |
PROJECT_LABELS="candid=true,audit=true" | |
printf "\nThis script will enable compute.googleapis.com and add the specified labels as set in the PROJECT_LABELS variable:\n \"$PROJECT_LABELS\" to ALL GCP projects within the org\n" | |
read -p "enter 'yes' to continue or any other key to exit" | |
if [ "$REPLY" != "yes" ]; then | |
printf "\nExiting... re-run script again with '-h' or '--help' for usage\n" | |
exit | |
fi | |
printf "\n####################################################" | |
printf "\nStep-1 Validate Google SDK (gcloud) installed..." | |
printf "\n####################################################" | |
uname=$(uname) | |
if [[ "${uname}" == "Darwin" ]]; then | |
platform="darwin" | |
printf "\nRunning on MacOS \n" | |
if which gcloud; then | |
printf "\ngcloud is already installed" | |
else | |
printf "\nPlease install and configure gcloud SDK as described here: https://cloud.google.com/sdk/docs/quickstart-macos" | |
exit 1 | |
fi | |
elif [[ "${uname}" == "Linux" ]]; then | |
platform="linux" | |
lsb_release -a | |
printf "\nWe are running on Linux" | |
printf "\nand dont assume any specific linux flavor please install gcloud SDK per your dists instructions" | |
exit | |
else | |
echo "Unknown, unsupported platform: (${uname})." | |
echo "Supported platforms: Linux, MacOS." | |
echo "Bailing out." | |
exit 2 | |
fi | |
printf "\n####################################################" | |
printf "\nStep-2 Validate GCP project labels BEFORE mods..." | |
printf "\n####################################################" | |
printf "\nValidate PRE processing all projects with desired labels: candid=true && audit=true \n" | |
gcloud projects list --filter='labels.candid:true' --filter='labels.audit:true' | |
printf "\n" | |
printf "\n####################################################" | |
printf "\nStep-3 Add specified PROJECT_LABELS to projectss..." | |
printf "\n####################################################" | |
printf "\nValidate PRE processing all projects with desired labels: candid=true && audit=true \n" | |
#for project in $(gcloud projects list --format="value(projectId)" # enable when ready | |
for project in $(gcloud projects list --format="value(projectId)" | grep "chrism-dev-demo") # only label dev-demo* named projects | |
do | |
printf "\nProjectId: $project" | |
# must enable apis for proj | |
gcloud services enable compute.googleapis.com --project $project | |
# Note: requires alpha version of gcloud | |
gcloud alpha projects update $project --update-labels "${PROJECT_LABELS}" | |
done | |
printf "\n" | |
printf "\n####################################################" | |
printf "\nStep-4 Validate GCP project labels AFTER mods..." | |
printf "\n####################################################" | |
printf "\nValidate POST processing all projects with desired labels: candid=true && audit=true \n" | |
gcloud projects list --filter='labels.candid:true' --filter='labels.audit:true' | |
printf "\n" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment