Skip to content

Instantly share code, notes, and snippets.

@cmcconnell1
Last active December 2, 2020 19:34
Show Gist options
  • Select an option

  • Save cmcconnell1/222b0e70bef610f15d6e7f311203533c to your computer and use it in GitHub Desktop.

Select an option

Save cmcconnell1/222b0e70bef610f15d6e7f311203533c to your computer and use it in GitHub Desktop.

Revisions

  1. cmcconnell1 revised this gist Dec 2, 2020. 1 changed file with 6 additions and 0 deletions.
    6 changes: 6 additions & 0 deletions gcp-label-project.sh
    Original file line number Diff line number Diff line change
    @@ -1,5 +1,11 @@
    #!/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"
  2. cmcconnell1 created this gist Nov 30, 2020.
    90 changes: 90 additions & 0 deletions gcp-label-project.sh
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,90 @@
    #!/bin/bash

    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"