Diwali in the Cloud | GSP647

Solution for Diwali in the Cloud | GSP647. 1 lab: GSP647. Fast copy-paste commands for Google Cloud.

GSP647 — Configuring IAM Permissions with gcloud

Estimated time: 25 minutes

# 🚀 Configuring IAM Permissions with gcloud | GSP647 > ⚠️ **Disclaimer:** This is an independent, community-made walkthrough created for educational purposes, hands-on practice, and Google Cloud certification preparation. This guide helps learners understand Identity and Access Management (IAM), custom roles, service accounts, multi-project configurations, and Compute Engine access management using the `gcloud` CLI. Always attempt the lab yourself first and follow Google Cloud Skills Boost / Q

CYAN=

\033[0;96m'
GREEN=

\033[0;92m'
YELLOW=

\033[0;93m'
BLUE=

\033[0;94m'
MAGENTA=

\033[0;95m'
WHITE=

\033[0;97m'
BG_BLUE=

\033[44m'
BG_GREEN=

\033[42m'
BOLD=

\033[1m'
RESET=

\033[0m'

clear
echo "${CYAN}${BOLD}"
echo "   ____        _     _ _              __   ___              "
echo "  / __ \      | |   (_) |            / _| / _ \             "
echo " | |  | |_ __ | |__  _| |_   ___    | |_ | | | |_ __  ___   "
echo " | |  | | '__|| '_ \| | __| / _ \   |  _|| | | | '_ \/ __|  "
echo " | |__| | |   | |_) | | |_ | (_) |  | |  | |_| | |_) \__ \  "
echo "  \____/|_|   |_.__/|_|\__| \___/   |_|   \___/| .__/|___/  "
echo "                                               | |          "
echo "                                               |_|          "
echo "${RESET}"

echo "${CYAN}${BOLD}╔════════════════════════════════════════════════════════════════╗${RESET}"
echo "${CYAN}${BOLD}║         🚀 ORBIT OF OPS — GSP647 AUTOMATION: STAGE 1/3         ║${RESET}"
echo "${CYAN}${BOLD}╚════════════════════════════════════════════════════════════════╝${RESET}"
echo ""
echo "${BG_BLUE}${WHITE}${BOLD} ❖ TASK 1: CONFIGURE THE GCLOUD ENVIRONMENT & CREATE LAB-1      ${RESET}"
echo ""

unset CLOUDSDK_ACTIVE_CONFIG_NAME
gcloud config configurations create default --quiet 2>/dev/null || gcloud config configurations activate default --quiet

echo "${YELLOW}${BOLD}⚡ [STEP 1.1] Verifying active Qwiklabs student account...${RESET}"
export USERID1=$(gcloud auth list --format="value(account)" | grep -v "gserviceaccount.com" | head -n 1)
if [ -z "$USERID1" ]; then
  gcloud auth login --no-launch-browser --quiet
  export USERID1=$(gcloud auth list --format="value(account)" | grep -v "gserviceaccount.com" | head -n 1)
fi
gcloud config set account "$USERID1" --quiet

echo "${BLUE}${BOLD}🔍 [STEP 1.2] Discovering Project 1 and centos-clean VM zone...${RESET}"
for P in $(gcloud projects list --format="value(projectId)" | grep "^qwiklabs-gcp-"); do
  Z=$(gcloud compute instances list --project="$P" --filter="name=centos-clean" --format="value(zone)" 2>/dev/null)
  if [ -n "$Z" ]; then
    export PROJECTID1="$P"
    export ZONE1="$Z"
  fi
done

export REGION1="${ZONE1%-*}"
export ALT_ZONE=$(gcloud compute zones list --project="$PROJECTID1" --filter="name~'^$REGION1'" --format="value(name)" | grep -v "^$ZONE1
\\033[0;96m'\nGREEN=
\\033[0;92m'\nYELLOW=
\\033[0;93m'\nBLUE=
\\033[0;94m'\nMAGENTA=
\\033[0;95m'\nWHITE=
\\033[0;97m'\nBG_BLUE=
\\033[44m'\nBG_GREEN=
\\033[42m'\nBOLD=
\\033[1m'\nRESET=
\\033[0m'\n\nclear\necho \"${CYAN}${BOLD}\"\necho \" ____ _ _ _ __ ___ \"\necho \" / __ \\ | | (_) | / _| / _ \\ \"\necho \" | | | |_ __ | |__ _| |_ ___ | |_ | | | |_ __ ___ \"\necho \" | | | | '__|| '_ \\| | __| / _ \\ | _|| | | | '_ \\/ __| \"\necho \" | |__| | | | |_) | | |_ | (_) | | | | |_| | |_) \\__ \\ \"\necho \" \\____/|_| |_.__/|_|\\__| \\___/ |_| \\___/| .__/|___/ \"\necho \" | | \"\necho \" |_| \"\necho \"${RESET}\"\n\necho \"${CYAN}${BOLD}╔════════════════════════════════════════════════════════════════╗${RESET}\"\necho \"${CYAN}${BOLD}║ 🚀 ORBIT OF OPS — GSP647 AUTOMATION: STAGE 1/3 ║${RESET}\"\necho \"${CYAN}${BOLD}╚════════════════════════════════════════════════════════════════╝${RESET}\"\necho \"\"\necho \"${BG_BLUE}${WHITE}${BOLD} ❖ TASK 1: CONFIGURE THE GCLOUD ENVIRONMENT & CREATE LAB-1 ${RESET}\"\necho \"\"\n\nunset CLOUDSDK_ACTIVE_CONFIG_NAME\ngcloud config configurations create default --quiet 2>/dev/null || gcloud config configurations activate default --quiet\n\necho \"${YELLOW}${BOLD}⚡ [STEP 1.1] Verifying active Qwiklabs student account...${RESET}\"\nexport USERID1=$(gcloud auth list --format=\"value(account)\" | grep -v \"gserviceaccount.com\" | head -n 1)\nif [ -z \"$USERID1\" ]; then\n gcloud auth login --no-launch-browser --quiet\n export USERID1=$(gcloud auth list --format=\"value(account)\" | grep -v \"gserviceaccount.com\" | head -n 1)\nfi\ngcloud config set account \"$USERID1\" --quiet\n\necho \"${BLUE}${BOLD}🔍 [STEP 1.2] Discovering Project 1 and centos-clean VM zone...${RESET}\"\nfor P in $(gcloud projects list --format=\"value(projectId)\" | grep \"^qwiklabs-gcp-\"); do\n Z=$(gcloud compute instances list --project=\"$P\" --filter=\"name=centos-clean\" --format=\"value(zone)\" 2>/dev/null)\n if [ -n \"$Z\" ]; then\n export PROJECTID1=\"$P\"\n export ZONE1=\"$Z\"\n fi\ndone\n\nexport REGION1=\"${ZONE1%-*}\"\nexport ALT_ZONE=$(gcloud compute zones list --project=\"$PROJECTID1\" --filter=\"name~'^$REGION1'\" --format=\"value(name)\" | grep -v \"^$ZONE1$\" | head -n 1)\n\ngcloud config set project \"$PROJECTID1\" --quiet\ngcloud config set compute/region \"$REGION1\" --quiet\ngcloud config set compute/zone \"$ALT_ZONE\" --quiet\n\necho \"${GREEN}${BOLD} ✔ Active User 1 : ${WHITE}${USERID1}${RESET}\"\necho \"${GREEN}${BOLD} ✔ Project 1 ID : ${WHITE}${PROJECTID1}${RESET}\"\necho \"${GREEN}${BOLD} ✔ Initial Zone : ${WHITE}${ZONE1} (${REGION1})${RESET}\"\necho \"${GREEN}${BOLD} ✔ Updated Zone : ${WHITE}${ALT_ZONE}${RESET}\"\necho \"\"\n\necho \"${MAGENTA}${BOLD}💻 [STEP 1.3] Creating VM instance 'lab-1' in ${ZONE1}...${RESET}\"\nif ! gcloud compute instances describe lab-1 --zone=\"$ZONE1\" --project=\"$PROJECTID1\" >/dev/null 2>&1; then\n gcloud compute instances create lab-1 --zone=\"$ZONE1\" --machine-type=e2-standard-2 --project=\"$PROJECTID1\" --quiet\n echo \"${GREEN}${BOLD} ✔ Instance 'lab-1' created successfully.${RESET}\"\nelse\n echo \"${GREEN}${BOLD} ✔ Instance 'lab-1' already exists.${RESET}\"\nfi\necho \"\"\n\nif [[ \"$(hostname)\" != \"centos-clean\" ]]; then\n echo \"${CYAN}${BOLD}🔗 [STEP 1.4] Syncing Task 1 'default' configuration inside centos-clean VM...${RESET}\"\n echo \"${YELLOW}${BOLD}⚠ ACTION: Authenticate as Username 1 (${WHITE}${USERID1}${YELLOW}) when prompted below:${RESET}\"\n gcloud compute ssh centos-clean --zone=\"$ZONE1\" --project=\"$PROJECTID1\" --quiet -- -t \"\n unset CLOUDSDK_ACTIVE_CONFIG_NAME\n gcloud config configurations create default --quiet 2>/dev/null || gcloud config configurations activate default --quiet\n gcloud auth login --no-launch-browser --quiet\n gcloud config set account $USERID1 --quiet\n gcloud config set project $PROJECTID1 --quiet\n gcloud config set compute/region $REGION1 --quiet\n gcloud config set compute/zone $ALT_ZONE --quiet\n gcloud config list\n cat ~/.config/gcloud/configurations/config_default\n \"\nelse\n cat ~/.config/gcloud/configurations/config_default\nfi\n\necho \"\"\necho \"${BG_GREEN}${WHITE}${BOLD} ✔ STAGE 1 COMPLETED SUCCESSFULLY — TASK 1 READY FOR GRADING! ${RESET}\"\necho \"\"\nCYAN=
\\033[0;96m'\nGREEN=
\\033[0;92m'\nYELLOW=
\\033[0;93m'\nBLUE=
\\033[0;94m'\nMAGENTA=
\\033[0;95m'\nWHITE=
\\033[0;97m'\nBG_BLUE=
\\033[44m'\nBG_MAGENTA=
\\033[45m'\nBG_GREEN=
\\033[42m'\nBOLD=
\\033[1m'\nRESET=
\\033[0m'\n\necho \"\"\necho \"${CYAN}${BOLD}╔════════════════════════════════════════════════════════════════╗${RESET}\"\necho \"${CYAN}${BOLD}║ 🚀 ORBIT OF OPS — GSP647 AUTOMATION: STAGE 2/3 ║${RESET}\"\necho \"${CYAN}${BOLD}╚════════════════════════════════════════════════════════════════╝${RESET}\"\necho \"\"\necho \"${BG_MAGENTA}${WHITE}${BOLD} ❖ TASKS 2, 3 & 4: USER2 CONFIG, DEVOPS IAM ROLE & LAB-2 VM ${RESET}\"\necho \"\"\n\nunset CLOUDSDK_ACTIVE_CONFIG_NAME\ngcloud config configurations create default --quiet 2>/dev/null || gcloud config configurations activate default --quiet\n\nread -p \"${YELLOW}${BOLD}🆔 Enter Second Project ID (PROJECTID2 from lab panel) : ${WHITE}\" INPUT_PROJECTID2\nread -p \"${MAGENTA}${BOLD}📧 Enter Username 2 (USERID2 from lab panel) : ${WHITE}\" INPUT_USERID2\nread -p \"${CYAN}${BOLD}📍 Enter Zone 2 (Zone2 from lab panel) : ${WHITE}\" INPUT_ZONE2\necho \"${RESET}\"\n\nexport PROJECTID2=$(echo \"$INPUT_PROJECTID2\" | xargs)\nexport USERID2=$(echo \"$INPUT_USERID2\" | xargs)\nexport ZONE2=$(echo \"$INPUT_ZONE2\" | xargs)\n\nexport USERID1=$(gcloud auth list --format=\"value(account)\" | grep \"@qwiklabs.net\" | grep -v \"$USERID2\" | head -n 1)\ngcloud config set account \"$USERID1\" --quiet\n\nfor P in $(gcloud projects list --format=\"value(projectId)\" | grep \"^qwiklabs-gcp-\"); do\n Z=$(gcloud compute instances list --project=\"$P\" --filter=\"name=centos-clean\" --format=\"value(zone)\" 2>/dev/null)\n if [ -n \"$Z\" ]; then\n export PROJECTID1=\"$P\"\n export ZONE1=\"$Z\"\n fi\ndone\nexport REGION1=\"${ZONE1%-*}\"\nexport ALT_ZONE=$(gcloud compute zones list --project=\"$PROJECTID1\" --filter=\"name~'^$REGION1'\" --format=\"value(name)\" | grep -v \"^$ZONE1$\" | head -n 1)\n\necho \"${BLUE}${BOLD}🔐 [STEP 2.1] Granting roles/viewer to ${WHITE}${USERID2}${BLUE} on ${WHITE}${PROJECTID2}${BLUE}...${RESET}\"\ngcloud projects add-iam-policy-binding \"$PROJECTID2\" --member=\"user:$USERID2\" --role=\"roles/viewer\" --condition=None >/dev/null\necho \"${GREEN}${BOLD} ✔ Viewer role bound successfully.${RESET}\"\n\necho \"${YELLOW}${BOLD}🛠️ [STEP 2.2] Verifying custom role 'devops' in ${WHITE}${PROJECTID2}${YELLOW}...${RESET}\"\nif ! gcloud iam roles describe devops --project \"$PROJECTID2\" >/dev/null 2>&1; then\n gcloud iam roles create devops --project \"$PROJECTID2\" --permissions=\"compute.instances.create,compute.instances.delete,compute.instances.start,compute.instances.stop,compute.instances.update,compute.disks.create,compute.subnetworks.use,compute.subnetworks.useExternalIp,compute.instances.setMetadata,compute.instances.setServiceAccount\"\nfi\necho \"${GREEN}${BOLD} ✔ Custom role 'devops' verified.${RESET}\"\n\necho \"${MAGENTA}${BOLD}🔑 [STEP 2.3] Binding roles/iam.serviceAccountUser and devops role to ${WHITE}${USERID2}${MAGENTA}...${RESET}\"\nexport COMPUTE_SA2=$(gcloud projects describe \"$PROJECTID2\" --format=\"value(projectNumber)\")[email protected]\ngcloud iam service-accounts add-iam-policy-binding \"$COMPUTE_SA2\" --project \"$PROJECTID2\" --member=\"user:$USERID2\" --role=\"roles/iam.serviceAccountUser\" >/dev/null\ngcloud projects add-iam-policy-binding \"$PROJECTID2\" --member=\"user:$USERID2\" --role=\"projects/$PROJECTID2/roles/devops\" --condition=None >/dev/null\necho \"${GREEN}${BOLD} ✔ Least-privilege serviceAccountUser & devops bindings applied.${RESET}\"\necho \"\"\n\nif [[ \"$(hostname)\" != \"centos-clean\" ]]; then\n echo \"${CYAN}${BOLD}👤 [STEP 2.4] Configuring 'user2' and creating 'lab-2' inside centos-clean VM...${RESET}\"\n echo \"${YELLOW}${BOLD}⚠ ACTION: Authenticate as Username 2 (${WHITE}${USERID2}${YELLOW}) when prompted below:${RESET}\"\n gcloud compute ssh centos-clean --zone=\"$ZONE1\" --project=\"$PROJECTID1\" --quiet -- -t \"\n unset CLOUDSDK_ACTIVE_CONFIG_NAME\n sudo yum -y install epel-release >/dev/null 2>&1 || true\n sudo yum -y install jq >/dev/null 2>&1 || true\n sed -i '/export PROJECTID2=/d' ~/.bashrc\n sed -i '/export USERID2=/d' ~/.bashrc\n sed -i '/export ZONE2=/d' ~/.bashrc\n echo 'export PROJECTID2=$PROJECTID2' >> ~/.bashrc\n echo 'export USERID2=$USERID2' >> ~/.bashrc\n echo 'export ZONE2=$ZONE2' >> ~/.bashrc\n\n gcloud config configurations create user2 --quiet 2>/dev/null || gcloud config configurations activate user2 --quiet\n gcloud auth login --no-launch-browser --quiet\n gcloud config set account $USERID2 --quiet\n gcloud config set project $PROJECTID2 --quiet\n gcloud config set compute/region $REGION1 --quiet\n gcloud config set compute/zone $ALT_ZONE --quiet\n gcloud compute instances create lab-2 --zone $ZONE2 --machine-type=e2-standard-2 --project $PROJECTID2 --quiet\n gcloud compute instances list --project $PROJECTID2\n gcloud config configurations activate default --quiet\n \"\nelse\n sudo yum -y install epel-release >/dev/null 2>&1 || true\n sudo yum -y install jq >/dev/null 2>&1 || true\n sed -i '/export PROJECTID2=/d' ~/.bashrc\n sed -i '/export USERID2=/d' ~/.bashrc\n sed -i '/export ZONE2=/d' ~/.bashrc\n echo \"export PROJECTID2=$PROJECTID2\" >> ~/.bashrc\n echo \"export USERID2=$USERID2\" >> ~/.bashrc\n echo \"export ZONE2=$ZONE2\" >> ~/.bashrc\n gcloud config configurations create user2 --quiet 2>/dev/null || gcloud config configurations activate user2 --quiet\n gcloud auth login --no-launch-browser --quiet\n gcloud config set account \"$USERID2\" --quiet\n gcloud config set project \"$PROJECTID2\" --quiet\n gcloud config set compute/region \"$REGION1\" --quiet\n gcloud config set compute/zone \"$ALT_ZONE\" --quiet\n gcloud compute instances create lab-2 --zone \"$ZONE2\" --machine-type=e2-standard-2 --project \"$PROJECTID2\" --quiet\n gcloud compute instances list --project \"$PROJECTID2\"\n gcloud config configurations activate default --quiet\nfi\n\necho \"\"\necho \"${BG_GREEN}${WHITE}${BOLD} ✔ STAGE 2 COMPLETED SUCCESSFULLY — TASKS 2–4 VERIFIED! ${RESET}\"\necho \"\"\nCYAN=
\\033[0;96m'\nGREEN=
\\033[0;92m'\nYELLOW=
\\033[0;93m'\nBLUE=
\\033[0;94m'\nMAGENTA=
\\033[0;95m'\nWHITE=
\\033[0;97m'\nBG_BLUE=
\\033[44m'\nBG_GREEN=
\\033[42m'\nBOLD=
\\033[1m'\nRESET=
\\033[0m'\n\necho \"\"\necho \"${CYAN}${BOLD}╔════════════════════════════════════════════════════════════════╗${RESET}\"\necho \"${CYAN}${BOLD}║ 🚀 ORBIT OF OPS — GSP647 AUTOMATION: STAGE 3/3 ║${RESET}\"\necho \"${CYAN}${BOLD}╚════════════════════════════════════════════════════════════════╝${RESET}\"\necho \"\"\necho \"${BG_BLUE}${WHITE}${BOLD} ❖ TASKS 5, 6 & 7: DEVOPS SERVICE ACCOUNT, LAB-3 & LAB-4 VMs ${RESET}\"\necho \"\"\n\nunset CLOUDSDK_ACTIVE_CONFIG_NAME\ngcloud config configurations activate default --quiet\ngcloud config set account \"$USERID1\" --quiet\ngcloud config set project \"$PROJECTID2\" --quiet\n\necho \"${BLUE}${BOLD}🤖 [STEP 3.1] Locating 'devops' service account in ${WHITE}${PROJECTID2}${BLUE}...${RESET}\"\nSA=$(gcloud iam service-accounts list --project \"$PROJECTID2\" --format=\"value(email)\" --filter=\"displayName=devops\" | head -n 1)\nif [ -z \"$SA\" ]; then\n gcloud iam service-accounts create devops --display-name=\"devops\" --project \"$PROJECTID2\"\n sleep 10\n SA=$(gcloud iam service-accounts list --project \"$PROJECTID2\" --format=\"value(email)\" --filter=\"displayName=devops\" | head -n 1)\nfi\necho \"${GREEN}${BOLD} ✔ Service Account Email : ${WHITE}${SA}${RESET}\"\n\necho \"${YELLOW}${BOLD}🔐 [STEP 3.2] Binding roles/iam.serviceAccountUser & roles/compute.instanceAdmin...${RESET}\"\nexport COMPUTE_SA2=$(gcloud projects describe \"$PROJECTID2\" --format=\"value(projectNumber)\")[email protected]\ngcloud iam service-accounts add-iam-policy-binding \"$COMPUTE_SA2\" --project \"$PROJECTID2\" --member=\"serviceAccount:$SA\" --role=\"roles/iam.serviceAccountUser\" >/dev/null\ngcloud projects add-iam-policy-binding \"$PROJECTID2\" --member=\"serviceAccount:$SA\" --role=\"roles/compute.instanceAdmin\" --condition=None >/dev/null\necho \"${GREEN}${BOLD} ✔ Service Account IAM roles bound.${RESET}\"\n\necho \"${CYAN}${BOLD}⏳ [STEP 3.3] Waiting 15 seconds for IAM policy propagation...${RESET}\"\nsleep 15\n\necho \"${MAGENTA}${BOLD}🚀 [STEP 3.4] Creating instance 'lab-3' in ${WHITE}${ZONE2}${MAGENTA} with service account attached...${RESET}\"\ngcloud compute instances create lab-3 --zone \"$ZONE2\" --project \"$PROJECTID2\" --machine-type=e2-standard-2 --service-account \"$SA\" --scopes \"https://www.googleapis.com/auth/compute\" --quiet\necho \"${GREEN}${BOLD} ✔ Instance 'lab-3' is RUNNING.${RESET}\"\n\necho \"${CYAN}${BOLD}⏳ [STEP 3.5] Waiting 25 seconds for lab-3 SSH daemon to initialize...${RESET}\"\nsleep 25\n\necho \"${YELLOW}${BOLD}🔗 [STEP 3.6] Connecting to 'lab-3' via IAP tunnel to create instance 'lab-4'...${RESET}\"\ngcloud compute ssh lab-3 --zone \"$ZONE2\" --project \"$PROJECTID2\" --tunnel-through-iap --quiet --command=\"gcloud config list && gcloud compute instances create lab-4 --zone $ZONE2 --machine-type=e2-standard-2 --quiet && gcloud compute instances list\"\necho \"${GREEN}${BOLD} ✔ Instance 'lab-4' created via 'lab-3' service account credentials.${RESET}\"\n\necho \"${BLUE}${BOLD}🔄 [STEP 3.7] Restoring 'default' configuration to Project 1 (${WHITE}${PROJECTID1}${BLUE}) & Zone (${WHITE}${ALT_ZONE}${BLUE})...${RESET}\"\ngcloud config configurations activate default --quiet\ngcloud config set account \"$USERID1\" --quiet\ngcloud config set project \"$PROJECTID1\" --quiet\ngcloud config set compute/region \"$REGION1\" --quiet\ngcloud config set compute/zone \"$ALT_ZONE\" --quiet\n\nif [[ \"$(hostname)\" != \"centos-clean\" ]]; then\n gcloud compute ssh centos-clean --zone=\"$ZONE1\" --project=\"$PROJECTID1\" --quiet --command=\"\n gcloud config configurations activate default --quiet\n gcloud config set account $USERID1 --quiet\n gcloud config set project $PROJECTID1 --quiet\n gcloud config set compute/region $REGION1 --quiet\n gcloud config set compute/zone $ALT_ZONE --quiet\n \"\nfi\n\necho \"\"\necho \"${GREEN}${BOLD}╔════════════════════════════════════════════════════════════════╗${RESET}\"\necho \"${GREEN}${BOLD}║ 🏆 ALL GSP647 LAB TASKS COMPLETED 100% SUCCESSFULLY! ║${RESET}\"\necho \"${GREEN}${BOLD}╚════════════════════════════════════════════════════════════════╝${RESET}\"\necho \"\""}]}
quot; | head -n 1) gcloud config set project "$PROJECTID1" --quiet gcloud config set compute/region "$REGION1" --quiet gcloud config set compute/zone "$ALT_ZONE" --quiet echo "${GREEN}${BOLD} ✔ Active User 1 : ${WHITE}${USERID1}${RESET}" echo "${GREEN}${BOLD} ✔ Project 1 ID : ${WHITE}${PROJECTID1}${RESET}" echo "${GREEN}${BOLD} ✔ Initial Zone : ${WHITE}${ZONE1} (${REGION1})${RESET}" echo "${GREEN}${BOLD} ✔ Updated Zone : ${WHITE}${ALT_ZONE}${RESET}" echo "" echo "${MAGENTA}${BOLD}💻 [STEP 1.3] Creating VM instance 'lab-1' in ${ZONE1}...${RESET}" if ! gcloud compute instances describe lab-1 --zone="$ZONE1" --project="$PROJECTID1" >/dev/null 2>&1; then gcloud compute instances create lab-1 --zone="$ZONE1" --machine-type=e2-standard-2 --project="$PROJECTID1" --quiet echo "${GREEN}${BOLD} ✔ Instance 'lab-1' created successfully.${RESET}" else echo "${GREEN}${BOLD} ✔ Instance 'lab-1' already exists.${RESET}" fi echo "" if [[ "$(hostname)" != "centos-clean" ]]; then echo "${CYAN}${BOLD}🔗 [STEP 1.4] Syncing Task 1 'default' configuration inside centos-clean VM...${RESET}" echo "${YELLOW}${BOLD}⚠ ACTION: Authenticate as Username 1 (${WHITE}${USERID1}${YELLOW}) when prompted below:${RESET}" gcloud compute ssh centos-clean --zone="$ZONE1" --project="$PROJECTID1" --quiet -- -t " unset CLOUDSDK_ACTIVE_CONFIG_NAME gcloud config configurations create default --quiet 2>/dev/null || gcloud config configurations activate default --quiet gcloud auth login --no-launch-browser --quiet gcloud config set account $USERID1 --quiet gcloud config set project $PROJECTID1 --quiet gcloud config set compute/region $REGION1 --quiet gcloud config set compute/zone $ALT_ZONE --quiet gcloud config list cat ~/.config/gcloud/configurations/config_default " else cat ~/.config/gcloud/configurations/config_default fi echo "" echo "${BG_GREEN}${WHITE}${BOLD} ✔ STAGE 1 COMPLETED SUCCESSFULLY — TASK 1 READY FOR GRADING! ${RESET}" echo ""
CYAN=

\033[0;96m'
GREEN=

\033[0;92m'
YELLOW=

\033[0;93m'
BLUE=

\033[0;94m'
MAGENTA=

\033[0;95m'
WHITE=

\033[0;97m'
BG_BLUE=

\033[44m'
BG_MAGENTA=

\033[45m'
BG_GREEN=

\033[42m'
BOLD=

\033[1m'
RESET=

\033[0m'

echo ""
echo "${CYAN}${BOLD}╔════════════════════════════════════════════════════════════════╗${RESET}"
echo "${CYAN}${BOLD}║         🚀 ORBIT OF OPS — GSP647 AUTOMATION: STAGE 2/3         ║${RESET}"
echo "${CYAN}${BOLD}╚════════════════════════════════════════════════════════════════╝${RESET}"
echo ""
echo "${BG_MAGENTA}${WHITE}${BOLD} ❖ TASKS 2, 3 & 4: USER2 CONFIG, DEVOPS IAM ROLE & LAB-2 VM     ${RESET}"
echo ""

unset CLOUDSDK_ACTIVE_CONFIG_NAME
gcloud config configurations create default --quiet 2>/dev/null || gcloud config configurations activate default --quiet

read -p "${YELLOW}${BOLD}🆔 Enter Second Project ID (PROJECTID2 from lab panel) : ${WHITE}" INPUT_PROJECTID2
read -p "${MAGENTA}${BOLD}📧 Enter Username 2 (USERID2 from lab panel)          : ${WHITE}" INPUT_USERID2
read -p "${CYAN}${BOLD}📍 Enter Zone 2 (Zone2 from lab panel)                : ${WHITE}" INPUT_ZONE2
echo "${RESET}"

export PROJECTID2=$(echo "$INPUT_PROJECTID2" | xargs)
export USERID2=$(echo "$INPUT_USERID2" | xargs)
export ZONE2=$(echo "$INPUT_ZONE2" | xargs)

export USERID1=$(gcloud auth list --format="value(account)" | grep "@qwiklabs.net" | grep -v "$USERID2" | head -n 1)
gcloud config set account "$USERID1" --quiet

for P in $(gcloud projects list --format="value(projectId)" | grep "^qwiklabs-gcp-"); do
  Z=$(gcloud compute instances list --project="$P" --filter="name=centos-clean" --format="value(zone)" 2>/dev/null)
  if [ -n "$Z" ]; then
    export PROJECTID1="$P"
    export ZONE1="$Z"
  fi
done
export REGION1="${ZONE1%-*}"
export ALT_ZONE=$(gcloud compute zones list --project="$PROJECTID1" --filter="name~'^$REGION1'" --format="value(name)" | grep -v "^$ZONE1
\\033[0;96m'\nGREEN=
\\033[0;92m'\nYELLOW=
\\033[0;93m'\nBLUE=
\\033[0;94m'\nMAGENTA=
\\033[0;95m'\nWHITE=
\\033[0;97m'\nBG_BLUE=
\\033[44m'\nBG_GREEN=
\\033[42m'\nBOLD=
\\033[1m'\nRESET=
\\033[0m'\n\nclear\necho \"${CYAN}${BOLD}\"\necho \" ____ _ _ _ __ ___ \"\necho \" / __ \\ | | (_) | / _| / _ \\ \"\necho \" | | | |_ __ | |__ _| |_ ___ | |_ | | | |_ __ ___ \"\necho \" | | | | '__|| '_ \\| | __| / _ \\ | _|| | | | '_ \\/ __| \"\necho \" | |__| | | | |_) | | |_ | (_) | | | | |_| | |_) \\__ \\ \"\necho \" \\____/|_| |_.__/|_|\\__| \\___/ |_| \\___/| .__/|___/ \"\necho \" | | \"\necho \" |_| \"\necho \"${RESET}\"\n\necho \"${CYAN}${BOLD}╔════════════════════════════════════════════════════════════════╗${RESET}\"\necho \"${CYAN}${BOLD}║ 🚀 ORBIT OF OPS — GSP647 AUTOMATION: STAGE 1/3 ║${RESET}\"\necho \"${CYAN}${BOLD}╚════════════════════════════════════════════════════════════════╝${RESET}\"\necho \"\"\necho \"${BG_BLUE}${WHITE}${BOLD} ❖ TASK 1: CONFIGURE THE GCLOUD ENVIRONMENT & CREATE LAB-1 ${RESET}\"\necho \"\"\n\nunset CLOUDSDK_ACTIVE_CONFIG_NAME\ngcloud config configurations create default --quiet 2>/dev/null || gcloud config configurations activate default --quiet\n\necho \"${YELLOW}${BOLD}⚡ [STEP 1.1] Verifying active Qwiklabs student account...${RESET}\"\nexport USERID1=$(gcloud auth list --format=\"value(account)\" | grep -v \"gserviceaccount.com\" | head -n 1)\nif [ -z \"$USERID1\" ]; then\n gcloud auth login --no-launch-browser --quiet\n export USERID1=$(gcloud auth list --format=\"value(account)\" | grep -v \"gserviceaccount.com\" | head -n 1)\nfi\ngcloud config set account \"$USERID1\" --quiet\n\necho \"${BLUE}${BOLD}🔍 [STEP 1.2] Discovering Project 1 and centos-clean VM zone...${RESET}\"\nfor P in $(gcloud projects list --format=\"value(projectId)\" | grep \"^qwiklabs-gcp-\"); do\n Z=$(gcloud compute instances list --project=\"$P\" --filter=\"name=centos-clean\" --format=\"value(zone)\" 2>/dev/null)\n if [ -n \"$Z\" ]; then\n export PROJECTID1=\"$P\"\n export ZONE1=\"$Z\"\n fi\ndone\n\nexport REGION1=\"${ZONE1%-*}\"\nexport ALT_ZONE=$(gcloud compute zones list --project=\"$PROJECTID1\" --filter=\"name~'^$REGION1'\" --format=\"value(name)\" | grep -v \"^$ZONE1$\" | head -n 1)\n\ngcloud config set project \"$PROJECTID1\" --quiet\ngcloud config set compute/region \"$REGION1\" --quiet\ngcloud config set compute/zone \"$ALT_ZONE\" --quiet\n\necho \"${GREEN}${BOLD} ✔ Active User 1 : ${WHITE}${USERID1}${RESET}\"\necho \"${GREEN}${BOLD} ✔ Project 1 ID : ${WHITE}${PROJECTID1}${RESET}\"\necho \"${GREEN}${BOLD} ✔ Initial Zone : ${WHITE}${ZONE1} (${REGION1})${RESET}\"\necho \"${GREEN}${BOLD} ✔ Updated Zone : ${WHITE}${ALT_ZONE}${RESET}\"\necho \"\"\n\necho \"${MAGENTA}${BOLD}💻 [STEP 1.3] Creating VM instance 'lab-1' in ${ZONE1}...${RESET}\"\nif ! gcloud compute instances describe lab-1 --zone=\"$ZONE1\" --project=\"$PROJECTID1\" >/dev/null 2>&1; then\n gcloud compute instances create lab-1 --zone=\"$ZONE1\" --machine-type=e2-standard-2 --project=\"$PROJECTID1\" --quiet\n echo \"${GREEN}${BOLD} ✔ Instance 'lab-1' created successfully.${RESET}\"\nelse\n echo \"${GREEN}${BOLD} ✔ Instance 'lab-1' already exists.${RESET}\"\nfi\necho \"\"\n\nif [[ \"$(hostname)\" != \"centos-clean\" ]]; then\n echo \"${CYAN}${BOLD}🔗 [STEP 1.4] Syncing Task 1 'default' configuration inside centos-clean VM...${RESET}\"\n echo \"${YELLOW}${BOLD}⚠ ACTION: Authenticate as Username 1 (${WHITE}${USERID1}${YELLOW}) when prompted below:${RESET}\"\n gcloud compute ssh centos-clean --zone=\"$ZONE1\" --project=\"$PROJECTID1\" --quiet -- -t \"\n unset CLOUDSDK_ACTIVE_CONFIG_NAME\n gcloud config configurations create default --quiet 2>/dev/null || gcloud config configurations activate default --quiet\n gcloud auth login --no-launch-browser --quiet\n gcloud config set account $USERID1 --quiet\n gcloud config set project $PROJECTID1 --quiet\n gcloud config set compute/region $REGION1 --quiet\n gcloud config set compute/zone $ALT_ZONE --quiet\n gcloud config list\n cat ~/.config/gcloud/configurations/config_default\n \"\nelse\n cat ~/.config/gcloud/configurations/config_default\nfi\n\necho \"\"\necho \"${BG_GREEN}${WHITE}${BOLD} ✔ STAGE 1 COMPLETED SUCCESSFULLY — TASK 1 READY FOR GRADING! ${RESET}\"\necho \"\"\nCYAN=
\\033[0;96m'\nGREEN=
\\033[0;92m'\nYELLOW=
\\033[0;93m'\nBLUE=
\\033[0;94m'\nMAGENTA=
\\033[0;95m'\nWHITE=
\\033[0;97m'\nBG_BLUE=
\\033[44m'\nBG_MAGENTA=
\\033[45m'\nBG_GREEN=
\\033[42m'\nBOLD=
\\033[1m'\nRESET=
\\033[0m'\n\necho \"\"\necho \"${CYAN}${BOLD}╔════════════════════════════════════════════════════════════════╗${RESET}\"\necho \"${CYAN}${BOLD}║ 🚀 ORBIT OF OPS — GSP647 AUTOMATION: STAGE 2/3 ║${RESET}\"\necho \"${CYAN}${BOLD}╚════════════════════════════════════════════════════════════════╝${RESET}\"\necho \"\"\necho \"${BG_MAGENTA}${WHITE}${BOLD} ❖ TASKS 2, 3 & 4: USER2 CONFIG, DEVOPS IAM ROLE & LAB-2 VM ${RESET}\"\necho \"\"\n\nunset CLOUDSDK_ACTIVE_CONFIG_NAME\ngcloud config configurations create default --quiet 2>/dev/null || gcloud config configurations activate default --quiet\n\nread -p \"${YELLOW}${BOLD}🆔 Enter Second Project ID (PROJECTID2 from lab panel) : ${WHITE}\" INPUT_PROJECTID2\nread -p \"${MAGENTA}${BOLD}📧 Enter Username 2 (USERID2 from lab panel) : ${WHITE}\" INPUT_USERID2\nread -p \"${CYAN}${BOLD}📍 Enter Zone 2 (Zone2 from lab panel) : ${WHITE}\" INPUT_ZONE2\necho \"${RESET}\"\n\nexport PROJECTID2=$(echo \"$INPUT_PROJECTID2\" | xargs)\nexport USERID2=$(echo \"$INPUT_USERID2\" | xargs)\nexport ZONE2=$(echo \"$INPUT_ZONE2\" | xargs)\n\nexport USERID1=$(gcloud auth list --format=\"value(account)\" | grep \"@qwiklabs.net\" | grep -v \"$USERID2\" | head -n 1)\ngcloud config set account \"$USERID1\" --quiet\n\nfor P in $(gcloud projects list --format=\"value(projectId)\" | grep \"^qwiklabs-gcp-\"); do\n Z=$(gcloud compute instances list --project=\"$P\" --filter=\"name=centos-clean\" --format=\"value(zone)\" 2>/dev/null)\n if [ -n \"$Z\" ]; then\n export PROJECTID1=\"$P\"\n export ZONE1=\"$Z\"\n fi\ndone\nexport REGION1=\"${ZONE1%-*}\"\nexport ALT_ZONE=$(gcloud compute zones list --project=\"$PROJECTID1\" --filter=\"name~'^$REGION1'\" --format=\"value(name)\" | grep -v \"^$ZONE1$\" | head -n 1)\n\necho \"${BLUE}${BOLD}🔐 [STEP 2.1] Granting roles/viewer to ${WHITE}${USERID2}${BLUE} on ${WHITE}${PROJECTID2}${BLUE}...${RESET}\"\ngcloud projects add-iam-policy-binding \"$PROJECTID2\" --member=\"user:$USERID2\" --role=\"roles/viewer\" --condition=None >/dev/null\necho \"${GREEN}${BOLD} ✔ Viewer role bound successfully.${RESET}\"\n\necho \"${YELLOW}${BOLD}🛠️ [STEP 2.2] Verifying custom role 'devops' in ${WHITE}${PROJECTID2}${YELLOW}...${RESET}\"\nif ! gcloud iam roles describe devops --project \"$PROJECTID2\" >/dev/null 2>&1; then\n gcloud iam roles create devops --project \"$PROJECTID2\" --permissions=\"compute.instances.create,compute.instances.delete,compute.instances.start,compute.instances.stop,compute.instances.update,compute.disks.create,compute.subnetworks.use,compute.subnetworks.useExternalIp,compute.instances.setMetadata,compute.instances.setServiceAccount\"\nfi\necho \"${GREEN}${BOLD} ✔ Custom role 'devops' verified.${RESET}\"\n\necho \"${MAGENTA}${BOLD}🔑 [STEP 2.3] Binding roles/iam.serviceAccountUser and devops role to ${WHITE}${USERID2}${MAGENTA}...${RESET}\"\nexport COMPUTE_SA2=$(gcloud projects describe \"$PROJECTID2\" --format=\"value(projectNumber)\")[email protected]\ngcloud iam service-accounts add-iam-policy-binding \"$COMPUTE_SA2\" --project \"$PROJECTID2\" --member=\"user:$USERID2\" --role=\"roles/iam.serviceAccountUser\" >/dev/null\ngcloud projects add-iam-policy-binding \"$PROJECTID2\" --member=\"user:$USERID2\" --role=\"projects/$PROJECTID2/roles/devops\" --condition=None >/dev/null\necho \"${GREEN}${BOLD} ✔ Least-privilege serviceAccountUser & devops bindings applied.${RESET}\"\necho \"\"\n\nif [[ \"$(hostname)\" != \"centos-clean\" ]]; then\n echo \"${CYAN}${BOLD}👤 [STEP 2.4] Configuring 'user2' and creating 'lab-2' inside centos-clean VM...${RESET}\"\n echo \"${YELLOW}${BOLD}⚠ ACTION: Authenticate as Username 2 (${WHITE}${USERID2}${YELLOW}) when prompted below:${RESET}\"\n gcloud compute ssh centos-clean --zone=\"$ZONE1\" --project=\"$PROJECTID1\" --quiet -- -t \"\n unset CLOUDSDK_ACTIVE_CONFIG_NAME\n sudo yum -y install epel-release >/dev/null 2>&1 || true\n sudo yum -y install jq >/dev/null 2>&1 || true\n sed -i '/export PROJECTID2=/d' ~/.bashrc\n sed -i '/export USERID2=/d' ~/.bashrc\n sed -i '/export ZONE2=/d' ~/.bashrc\n echo 'export PROJECTID2=$PROJECTID2' >> ~/.bashrc\n echo 'export USERID2=$USERID2' >> ~/.bashrc\n echo 'export ZONE2=$ZONE2' >> ~/.bashrc\n\n gcloud config configurations create user2 --quiet 2>/dev/null || gcloud config configurations activate user2 --quiet\n gcloud auth login --no-launch-browser --quiet\n gcloud config set account $USERID2 --quiet\n gcloud config set project $PROJECTID2 --quiet\n gcloud config set compute/region $REGION1 --quiet\n gcloud config set compute/zone $ALT_ZONE --quiet\n gcloud compute instances create lab-2 --zone $ZONE2 --machine-type=e2-standard-2 --project $PROJECTID2 --quiet\n gcloud compute instances list --project $PROJECTID2\n gcloud config configurations activate default --quiet\n \"\nelse\n sudo yum -y install epel-release >/dev/null 2>&1 || true\n sudo yum -y install jq >/dev/null 2>&1 || true\n sed -i '/export PROJECTID2=/d' ~/.bashrc\n sed -i '/export USERID2=/d' ~/.bashrc\n sed -i '/export ZONE2=/d' ~/.bashrc\n echo \"export PROJECTID2=$PROJECTID2\" >> ~/.bashrc\n echo \"export USERID2=$USERID2\" >> ~/.bashrc\n echo \"export ZONE2=$ZONE2\" >> ~/.bashrc\n gcloud config configurations create user2 --quiet 2>/dev/null || gcloud config configurations activate user2 --quiet\n gcloud auth login --no-launch-browser --quiet\n gcloud config set account \"$USERID2\" --quiet\n gcloud config set project \"$PROJECTID2\" --quiet\n gcloud config set compute/region \"$REGION1\" --quiet\n gcloud config set compute/zone \"$ALT_ZONE\" --quiet\n gcloud compute instances create lab-2 --zone \"$ZONE2\" --machine-type=e2-standard-2 --project \"$PROJECTID2\" --quiet\n gcloud compute instances list --project \"$PROJECTID2\"\n gcloud config configurations activate default --quiet\nfi\n\necho \"\"\necho \"${BG_GREEN}${WHITE}${BOLD} ✔ STAGE 2 COMPLETED SUCCESSFULLY — TASKS 2–4 VERIFIED! ${RESET}\"\necho \"\"\nCYAN=
\\033[0;96m'\nGREEN=
\\033[0;92m'\nYELLOW=
\\033[0;93m'\nBLUE=
\\033[0;94m'\nMAGENTA=
\\033[0;95m'\nWHITE=
\\033[0;97m'\nBG_BLUE=
\\033[44m'\nBG_GREEN=
\\033[42m'\nBOLD=
\\033[1m'\nRESET=
\\033[0m'\n\necho \"\"\necho \"${CYAN}${BOLD}╔════════════════════════════════════════════════════════════════╗${RESET}\"\necho \"${CYAN}${BOLD}║ 🚀 ORBIT OF OPS — GSP647 AUTOMATION: STAGE 3/3 ║${RESET}\"\necho \"${CYAN}${BOLD}╚════════════════════════════════════════════════════════════════╝${RESET}\"\necho \"\"\necho \"${BG_BLUE}${WHITE}${BOLD} ❖ TASKS 5, 6 & 7: DEVOPS SERVICE ACCOUNT, LAB-3 & LAB-4 VMs ${RESET}\"\necho \"\"\n\nunset CLOUDSDK_ACTIVE_CONFIG_NAME\ngcloud config configurations activate default --quiet\ngcloud config set account \"$USERID1\" --quiet\ngcloud config set project \"$PROJECTID2\" --quiet\n\necho \"${BLUE}${BOLD}🤖 [STEP 3.1] Locating 'devops' service account in ${WHITE}${PROJECTID2}${BLUE}...${RESET}\"\nSA=$(gcloud iam service-accounts list --project \"$PROJECTID2\" --format=\"value(email)\" --filter=\"displayName=devops\" | head -n 1)\nif [ -z \"$SA\" ]; then\n gcloud iam service-accounts create devops --display-name=\"devops\" --project \"$PROJECTID2\"\n sleep 10\n SA=$(gcloud iam service-accounts list --project \"$PROJECTID2\" --format=\"value(email)\" --filter=\"displayName=devops\" | head -n 1)\nfi\necho \"${GREEN}${BOLD} ✔ Service Account Email : ${WHITE}${SA}${RESET}\"\n\necho \"${YELLOW}${BOLD}🔐 [STEP 3.2] Binding roles/iam.serviceAccountUser & roles/compute.instanceAdmin...${RESET}\"\nexport COMPUTE_SA2=$(gcloud projects describe \"$PROJECTID2\" --format=\"value(projectNumber)\")[email protected]\ngcloud iam service-accounts add-iam-policy-binding \"$COMPUTE_SA2\" --project \"$PROJECTID2\" --member=\"serviceAccount:$SA\" --role=\"roles/iam.serviceAccountUser\" >/dev/null\ngcloud projects add-iam-policy-binding \"$PROJECTID2\" --member=\"serviceAccount:$SA\" --role=\"roles/compute.instanceAdmin\" --condition=None >/dev/null\necho \"${GREEN}${BOLD} ✔ Service Account IAM roles bound.${RESET}\"\n\necho \"${CYAN}${BOLD}⏳ [STEP 3.3] Waiting 15 seconds for IAM policy propagation...${RESET}\"\nsleep 15\n\necho \"${MAGENTA}${BOLD}🚀 [STEP 3.4] Creating instance 'lab-3' in ${WHITE}${ZONE2}${MAGENTA} with service account attached...${RESET}\"\ngcloud compute instances create lab-3 --zone \"$ZONE2\" --project \"$PROJECTID2\" --machine-type=e2-standard-2 --service-account \"$SA\" --scopes \"https://www.googleapis.com/auth/compute\" --quiet\necho \"${GREEN}${BOLD} ✔ Instance 'lab-3' is RUNNING.${RESET}\"\n\necho \"${CYAN}${BOLD}⏳ [STEP 3.5] Waiting 25 seconds for lab-3 SSH daemon to initialize...${RESET}\"\nsleep 25\n\necho \"${YELLOW}${BOLD}🔗 [STEP 3.6] Connecting to 'lab-3' via IAP tunnel to create instance 'lab-4'...${RESET}\"\ngcloud compute ssh lab-3 --zone \"$ZONE2\" --project \"$PROJECTID2\" --tunnel-through-iap --quiet --command=\"gcloud config list && gcloud compute instances create lab-4 --zone $ZONE2 --machine-type=e2-standard-2 --quiet && gcloud compute instances list\"\necho \"${GREEN}${BOLD} ✔ Instance 'lab-4' created via 'lab-3' service account credentials.${RESET}\"\n\necho \"${BLUE}${BOLD}🔄 [STEP 3.7] Restoring 'default' configuration to Project 1 (${WHITE}${PROJECTID1}${BLUE}) & Zone (${WHITE}${ALT_ZONE}${BLUE})...${RESET}\"\ngcloud config configurations activate default --quiet\ngcloud config set account \"$USERID1\" --quiet\ngcloud config set project \"$PROJECTID1\" --quiet\ngcloud config set compute/region \"$REGION1\" --quiet\ngcloud config set compute/zone \"$ALT_ZONE\" --quiet\n\nif [[ \"$(hostname)\" != \"centos-clean\" ]]; then\n gcloud compute ssh centos-clean --zone=\"$ZONE1\" --project=\"$PROJECTID1\" --quiet --command=\"\n gcloud config configurations activate default --quiet\n gcloud config set account $USERID1 --quiet\n gcloud config set project $PROJECTID1 --quiet\n gcloud config set compute/region $REGION1 --quiet\n gcloud config set compute/zone $ALT_ZONE --quiet\n \"\nfi\n\necho \"\"\necho \"${GREEN}${BOLD}╔════════════════════════════════════════════════════════════════╗${RESET}\"\necho \"${GREEN}${BOLD}║ 🏆 ALL GSP647 LAB TASKS COMPLETED 100% SUCCESSFULLY! ║${RESET}\"\necho \"${GREEN}${BOLD}╚════════════════════════════════════════════════════════════════╝${RESET}\"\necho \"\""}]}
quot; | head -n 1) echo "${BLUE}${BOLD}🔐 [STEP 2.1] Granting roles/viewer to ${WHITE}${USERID2}${BLUE} on ${WHITE}${PROJECTID2}${BLUE}...${RESET}" gcloud projects add-iam-policy-binding "$PROJECTID2" --member="user:$USERID2" --role="roles/viewer" --condition=None >/dev/null echo "${GREEN}${BOLD} ✔ Viewer role bound successfully.${RESET}" echo "${YELLOW}${BOLD}🛠️ [STEP 2.2] Verifying custom role 'devops' in ${WHITE}${PROJECTID2}${YELLOW}...${RESET}" if ! gcloud iam roles describe devops --project "$PROJECTID2" >/dev/null 2>&1; then gcloud iam roles create devops --project "$PROJECTID2" --permissions="compute.instances.create,compute.instances.delete,compute.instances.start,compute.instances.stop,compute.instances.update,compute.disks.create,compute.subnetworks.use,compute.subnetworks.useExternalIp,compute.instances.setMetadata,compute.instances.setServiceAccount" fi echo "${GREEN}${BOLD} ✔ Custom role 'devops' verified.${RESET}" echo "${MAGENTA}${BOLD}🔑 [STEP 2.3] Binding roles/iam.serviceAccountUser and devops role to ${WHITE}${USERID2}${MAGENTA}...${RESET}" export COMPUTE_SA2=$(gcloud projects describe "$PROJECTID2" --format="value(projectNumber)")[email protected] gcloud iam service-accounts add-iam-policy-binding "$COMPUTE_SA2" --project "$PROJECTID2" --member="user:$USERID2" --role="roles/iam.serviceAccountUser" >/dev/null gcloud projects add-iam-policy-binding "$PROJECTID2" --member="user:$USERID2" --role="projects/$PROJECTID2/roles/devops" --condition=None >/dev/null echo "${GREEN}${BOLD} ✔ Least-privilege serviceAccountUser & devops bindings applied.${RESET}" echo "" if [[ "$(hostname)" != "centos-clean" ]]; then echo "${CYAN}${BOLD}👤 [STEP 2.4] Configuring 'user2' and creating 'lab-2' inside centos-clean VM...${RESET}" echo "${YELLOW}${BOLD}⚠ ACTION: Authenticate as Username 2 (${WHITE}${USERID2}${YELLOW}) when prompted below:${RESET}" gcloud compute ssh centos-clean --zone="$ZONE1" --project="$PROJECTID1" --quiet -- -t " unset CLOUDSDK_ACTIVE_CONFIG_NAME sudo yum -y install epel-release >/dev/null 2>&1 || true sudo yum -y install jq >/dev/null 2>&1 || true sed -i '/export PROJECTID2=/d' ~/.bashrc sed -i '/export USERID2=/d' ~/.bashrc sed -i '/export ZONE2=/d' ~/.bashrc echo 'export PROJECTID2=$PROJECTID2' >> ~/.bashrc echo 'export USERID2=$USERID2' >> ~/.bashrc echo 'export ZONE2=$ZONE2' >> ~/.bashrc gcloud config configurations create user2 --quiet 2>/dev/null || gcloud config configurations activate user2 --quiet gcloud auth login --no-launch-browser --quiet gcloud config set account $USERID2 --quiet gcloud config set project $PROJECTID2 --quiet gcloud config set compute/region $REGION1 --quiet gcloud config set compute/zone $ALT_ZONE --quiet gcloud compute instances create lab-2 --zone $ZONE2 --machine-type=e2-standard-2 --project $PROJECTID2 --quiet gcloud compute instances list --project $PROJECTID2 gcloud config configurations activate default --quiet " else sudo yum -y install epel-release >/dev/null 2>&1 || true sudo yum -y install jq >/dev/null 2>&1 || true sed -i '/export PROJECTID2=/d' ~/.bashrc sed -i '/export USERID2=/d' ~/.bashrc sed -i '/export ZONE2=/d' ~/.bashrc echo "export PROJECTID2=$PROJECTID2" >> ~/.bashrc echo "export USERID2=$USERID2" >> ~/.bashrc echo "export ZONE2=$ZONE2" >> ~/.bashrc gcloud config configurations create user2 --quiet 2>/dev/null || gcloud config configurations activate user2 --quiet gcloud auth login --no-launch-browser --quiet gcloud config set account "$USERID2" --quiet gcloud config set project "$PROJECTID2" --quiet gcloud config set compute/region "$REGION1" --quiet gcloud config set compute/zone "$ALT_ZONE" --quiet gcloud compute instances create lab-2 --zone "$ZONE2" --machine-type=e2-standard-2 --project "$PROJECTID2" --quiet gcloud compute instances list --project "$PROJECTID2" gcloud config configurations activate default --quiet fi echo "" echo "${BG_GREEN}${WHITE}${BOLD} ✔ STAGE 2 COMPLETED SUCCESSFULLY — TASKS 2–4 VERIFIED! ${RESET}" echo ""
CYAN=

\033[0;96m'
GREEN=

\033[0;92m'
YELLOW=

\033[0;93m'
BLUE=

\033[0;94m'
MAGENTA=

\033[0;95m'
WHITE=

\033[0;97m'
BG_BLUE=

\033[44m'
BG_GREEN=

\033[42m'
BOLD=

\033[1m'
RESET=

\033[0m'

echo ""
echo "${CYAN}${BOLD}╔════════════════════════════════════════════════════════════════╗${RESET}"
echo "${CYAN}${BOLD}║         🚀 ORBIT OF OPS — GSP647 AUTOMATION: STAGE 3/3         ║${RESET}"
echo "${CYAN}${BOLD}╚════════════════════════════════════════════════════════════════╝${RESET}"
echo ""
echo "${BG_BLUE}${WHITE}${BOLD} ❖ TASKS 5, 6 & 7: DEVOPS SERVICE ACCOUNT, LAB-3 & LAB-4 VMs    ${RESET}"
echo ""

unset CLOUDSDK_ACTIVE_CONFIG_NAME
gcloud config configurations activate default --quiet
gcloud config set account "$USERID1" --quiet
gcloud config set project "$PROJECTID2" --quiet

echo "${BLUE}${BOLD}🤖 [STEP 3.1] Locating 'devops' service account in ${WHITE}${PROJECTID2}${BLUE}...${RESET}"
SA=$(gcloud iam service-accounts list --project "$PROJECTID2" --format="value(email)" --filter="displayName=devops" | head -n 1)
if [ -z "$SA" ]; then
  gcloud iam service-accounts create devops --display-name="devops" --project "$PROJECTID2"
  sleep 10
  SA=$(gcloud iam service-accounts list --project "$PROJECTID2" --format="value(email)" --filter="displayName=devops" | head -n 1)
fi
echo "${GREEN}${BOLD}  ✔ Service Account Email : ${WHITE}${SA}${RESET}"

echo "${YELLOW}${BOLD}🔐 [STEP 3.2] Binding roles/iam.serviceAccountUser & roles/compute.instanceAdmin...${RESET}"
export COMPUTE_SA2=$(gcloud projects describe "$PROJECTID2" --format="value(projectNumber)")[email protected]
gcloud iam service-accounts add-iam-policy-binding "$COMPUTE_SA2" --project "$PROJECTID2" --member="serviceAccount:$SA" --role="roles/iam.serviceAccountUser" >/dev/null
gcloud projects add-iam-policy-binding "$PROJECTID2" --member="serviceAccount:$SA" --role="roles/compute.instanceAdmin" --condition=None >/dev/null
echo "${GREEN}${BOLD}  ✔ Service Account IAM roles bound.${RESET}"

echo "${CYAN}${BOLD}⏳ [STEP 3.3] Waiting 15 seconds for IAM policy propagation...${RESET}"
sleep 15

echo "${MAGENTA}${BOLD}🚀 [STEP 3.4] Creating instance 'lab-3' in ${WHITE}${ZONE2}${MAGENTA} with service account attached...${RESET}"
gcloud compute instances create lab-3 --zone "$ZONE2" --project "$PROJECTID2" --machine-type=e2-standard-2 --service-account "$SA" --scopes "https://www.googleapis.com/auth/compute" --quiet
echo "${GREEN}${BOLD}  ✔ Instance 'lab-3' is RUNNING.${RESET}"

echo "${CYAN}${BOLD}⏳ [STEP 3.5] Waiting 25 seconds for lab-3 SSH daemon to initialize...${RESET}"
sleep 25

echo "${YELLOW}${BOLD}🔗 [STEP 3.6] Connecting to 'lab-3' via IAP tunnel to create instance 'lab-4'...${RESET}"
gcloud compute ssh lab-3 --zone "$ZONE2" --project "$PROJECTID2" --tunnel-through-iap --quiet --command="gcloud config list && gcloud compute instances create lab-4 --zone $ZONE2 --machine-type=e2-standard-2 --quiet && gcloud compute instances list"
echo "${GREEN}${BOLD}  ✔ Instance 'lab-4' created via 'lab-3' service account credentials.${RESET}"

echo "${BLUE}${BOLD}🔄 [STEP 3.7] Restoring 'default' configuration to Project 1 (${WHITE}${PROJECTID1}${BLUE}) & Zone (${WHITE}${ALT_ZONE}${BLUE})...${RESET}"
gcloud config configurations activate default --quiet
gcloud config set account "$USERID1" --quiet
gcloud config set project "$PROJECTID1" --quiet
gcloud config set compute/region "$REGION1" --quiet
gcloud config set compute/zone "$ALT_ZONE" --quiet

if [[ "$(hostname)" != "centos-clean" ]]; then
  gcloud compute ssh centos-clean --zone="$ZONE1" --project="$PROJECTID1" --quiet --command="
    gcloud config configurations activate default --quiet
    gcloud config set account $USERID1 --quiet
    gcloud config set project $PROJECTID1 --quiet
    gcloud config set compute/region $REGION1 --quiet
    gcloud config set compute/zone $ALT_ZONE --quiet
  "
fi

echo ""
echo "${GREEN}${BOLD}╔════════════════════════════════════════════════════════════════╗${RESET}"
echo "${GREEN}${BOLD}║      🏆 ALL GSP647 LAB TASKS COMPLETED 100% SUCCESSFULLY!      ║${RESET}"
echo "${GREEN}${BOLD}╚════════════════════════════════════════════════════════════════╝${RESET}"
echo ""