|
| 1 | +#!/usr/bin/env bash |
| 2 | +set -euo pipefail |
| 3 | + |
| 4 | +# Azure AKS + ACR quick setup script |
| 5 | +# Requirements: az CLI installed and logged in; subscription selected |
| 6 | + |
| 7 | +RG_NAME=${RG_NAME:-whanos-rg} |
| 8 | +# Default region for the resource group and as a fallback |
| 9 | +LOCATION=${LOCATION:-westeurope} |
| 10 | +# Allow separate locations if your subscription limits services differently |
| 11 | +ACR_LOCATION=${ACR_LOCATION:-$LOCATION} |
| 12 | +AKS_LOCATION=${AKS_LOCATION:-$LOCATION} |
| 13 | + |
| 14 | +ACR_NAME=${ACR_NAME:-whanos} |
| 15 | +AKS_NAME=${AKS_NAME:-whanos-aks} |
| 16 | +NODE_COUNT=${NODE_COUNT:-2} |
| 17 | +NODE_SIZE=${NODE_SIZE:-Standard_B2s} |
| 18 | + |
| 19 | +# Fail fast by default on the first error (disable by setting FAIL_FAST=0) |
| 20 | +FAIL_FAST=${FAIL_FAST:-1} |
| 21 | + |
| 22 | +# Ensure required resource providers are registered (idempotent) |
| 23 | +ensure_provider_registered() { |
| 24 | + local ns="$1" |
| 25 | + local state |
| 26 | + state=$(az provider show --namespace "$ns" --query registrationState -o tsv 2>/dev/null || echo "NotRegistered") |
| 27 | + if [ "$state" != "Registered" ]; then |
| 28 | + echo " -> registering provider: $ns (current state: $state)" |
| 29 | + az provider register --namespace "$ns" 1>/dev/null || true |
| 30 | + # Wait up to ~5 minutes, polling every 5s |
| 31 | + local tries=0 |
| 32 | + while true; do |
| 33 | + state=$(az provider show --namespace "$ns" --query registrationState -o tsv 2>/dev/null || echo "NotRegistered") |
| 34 | + if [ "$state" = "Registered" ]; then |
| 35 | + echo " -> $ns is Registered" |
| 36 | + break |
| 37 | + fi |
| 38 | + tries=$((tries+1)) |
| 39 | + if [ $tries -ge 60 ]; then |
| 40 | + echo " !! timeout waiting for $ns to register (last state: $state)" >&2 |
| 41 | + if [ "$FAIL_FAST" = "1" ]; then |
| 42 | + exit 1 |
| 43 | + else |
| 44 | + break |
| 45 | + fi |
| 46 | + fi |
| 47 | + sleep 5 |
| 48 | + done |
| 49 | + else |
| 50 | + echo " -> $ns already Registered" |
| 51 | + fi |
| 52 | +} |
| 53 | + |
| 54 | +echo "[1/7] Ensuring Azure resource providers are registered" |
| 55 | +ensure_provider_registered "Microsoft.ContainerRegistry" |
| 56 | +ensure_provider_registered "Microsoft.ContainerService" |
| 57 | +ensure_provider_registered "Microsoft.Network" |
| 58 | + |
| 59 | +echo "[2/7] Creating resource group: $RG_NAME" |
| 60 | +# If the RG already exists, reuse its location to avoid conflicts |
| 61 | +if az group exists -n "$RG_NAME" >/dev/null; then |
| 62 | + EXISTING_LOC=$(az group show -n "$RG_NAME" --query location -o tsv) |
| 63 | + echo "Resource group already exists in $EXISTING_LOC; reusing it." |
| 64 | + LOCATION="$EXISTING_LOC" |
| 65 | +else |
| 66 | + az group create -n "$RG_NAME" -l "$LOCATION" 1>/dev/null |
| 67 | +fi |
| 68 | + |
| 69 | +# Ensure service-specific locations default to the (possibly updated) RG location |
| 70 | +ACR_LOCATION=${ACR_LOCATION:-$LOCATION} |
| 71 | +AKS_LOCATION=${AKS_LOCATION:-$LOCATION} |
| 72 | + |
| 73 | +echo "[3/7] Creating Azure Container Registry: $ACR_NAME in $ACR_LOCATION" |
| 74 | +create_acr() { |
| 75 | + local rg="$1" name="$2" location="$3" |
| 76 | + az acr create -g "$rg" -n "$name" -l "$location" --sku Standard --admin-enabled false 1>/dev/null |
| 77 | +} |
| 78 | + |
| 79 | +set +e |
| 80 | +EXISTING_ACR_LOC=$(az acr show -n "$ACR_NAME" -g "$RG_NAME" --query location -o tsv 2>/dev/null || true) |
| 81 | +if [ -n "$EXISTING_ACR_LOC" ]; then |
| 82 | + ACR_RC=0 |
| 83 | + ACR_LOCATION="$EXISTING_ACR_LOC" |
| 84 | + echo "ACR '$ACR_NAME' already exists in $ACR_LOCATION; reusing it." |
| 85 | +else |
| 86 | + create_acr "$RG_NAME" "$ACR_NAME" "$ACR_LOCATION" |
| 87 | + ACR_RC=$? |
| 88 | +fi |
| 89 | +set -e |
| 90 | + |
| 91 | +if [ $ACR_RC -ne 0 ]; then |
| 92 | + if [ "$FAIL_FAST" = "1" ]; then |
| 93 | + echo "ACR creation failed in '$ACR_LOCATION'. Set ACR_LOCATION to an allowed region and retry." >&2 |
| 94 | + exit 1 |
| 95 | + else |
| 96 | + echo "Primary ACR location '$ACR_LOCATION' failed; trying fallbacks..." |
| 97 | + # Allow override via env, else use a sane default shortlist |
| 98 | + ACR_FALLBACKS=${ACR_FALLBACKS:-"francecentral eastus2 eastus northeurope uksouth germanywestcentral swedencentral polandcentral"} |
| 99 | + for loc in $ACR_FALLBACKS; do |
| 100 | + echo " -> trying $loc" |
| 101 | + set +e |
| 102 | + create_acr "$RG_NAME" "$ACR_NAME" "$loc" |
| 103 | + ACR_RC=$? |
| 104 | + set -e |
| 105 | + if [ $ACR_RC -eq 0 ]; then |
| 106 | + ACR_LOCATION="$loc" |
| 107 | + echo "ACR created in $ACR_LOCATION" |
| 108 | + break |
| 109 | + fi |
| 110 | + done |
| 111 | + if [ $ACR_RC -ne 0 ]; then |
| 112 | + echo "Failed to create ACR in all attempted regions. Set ACR_LOCATION to an allowed region and retry." >&2 |
| 113 | + exit 1 |
| 114 | + fi |
| 115 | + fi |
| 116 | +fi |
| 117 | + |
| 118 | +echo "[4/7] Creating AKS cluster: $AKS_NAME in $AKS_LOCATION ($NODE_COUNT nodes)" |
| 119 | +create_aks() { |
| 120 | + local rg="$1" name="$2" location="$3" |
| 121 | + az aks create -g "$rg" -n "$name" -l "$location" \ |
| 122 | + --node-count "$NODE_COUNT" \ |
| 123 | + --node-vm-size "$NODE_SIZE" \ |
| 124 | + --enable-managed-identity \ |
| 125 | + --generate-ssh-keys \ |
| 126 | + --attach-acr "$ACR_NAME" 1>/dev/null |
| 127 | +} |
| 128 | + |
| 129 | +set +e |
| 130 | +# If AKS already exists, reuse it instead of creating |
| 131 | +az aks show -g "$RG_NAME" -n "$AKS_NAME" 1>/dev/null 2>&1 |
| 132 | +EXISTS_RC=$? |
| 133 | +if [ $EXISTS_RC -eq 0 ]; then |
| 134 | + AKS_RC=0 |
| 135 | + EXISTING_AKS_LOC=$(az aks show -g "$RG_NAME" -n "$AKS_NAME" --query location -o tsv) |
| 136 | + echo "AKS '$AKS_NAME' already exists in $EXISTING_AKS_LOC; reusing it." |
| 137 | + AKS_LOCATION="$EXISTING_AKS_LOC" |
| 138 | +else |
| 139 | + create_aks "$RG_NAME" "$AKS_NAME" "$AKS_LOCATION" |
| 140 | + AKS_RC=$? |
| 141 | +fi |
| 142 | +set -e |
| 143 | + |
| 144 | +if [ $AKS_RC -ne 0 ]; then |
| 145 | + if [ "$FAIL_FAST" = "1" ]; then |
| 146 | + echo "AKS creation failed in '$AKS_LOCATION'. If you see MissingSubscriptionRegistration, the script will register providers on the next run. Otherwise, set AKS_LOCATION to an allowed region and retry." >&2 |
| 147 | + exit 1 |
| 148 | + else |
| 149 | + echo "Primary AKS location '$AKS_LOCATION' failed; trying fallbacks..." |
| 150 | + AKS_FALLBACKS=${AKS_FALLBACKS:-"francecentral eastus2 eastus northeurope uksouth germanywestcentral swedencentral polandcentral"} |
| 151 | + for loc in $AKS_FALLBACKS; do |
| 152 | + echo " -> trying $loc" |
| 153 | + set +e |
| 154 | + create_aks "$RG_NAME" "$AKS_NAME" "$loc" |
| 155 | + AKS_RC=$? |
| 156 | + set -e |
| 157 | + if [ $AKS_RC -eq 0 ]; then |
| 158 | + AKS_LOCATION="$loc" |
| 159 | + echo "AKS created in $AKS_LOCATION" |
| 160 | + break |
| 161 | + fi |
| 162 | + done |
| 163 | + if [ $AKS_RC -ne 0 ]; then |
| 164 | + echo "Failed to create AKS in all attempted regions. Set AKS_LOCATION to an allowed region and retry." >&2 |
| 165 | + exit 1 |
| 166 | + fi |
| 167 | + fi |
| 168 | +fi |
| 169 | + |
| 170 | +echo "[5/7] Getting kubeconfig" |
| 171 | +az aks get-credentials -g "$RG_NAME" -n "$AKS_NAME" --overwrite-existing 1>/dev/null |
| 172 | + |
| 173 | +echo "[6/7] Verifying cluster access" |
| 174 | +kubectl get nodes -o wide |
| 175 | + |
| 176 | +echo "[7/7] Optional: install NGINX Ingress (comment out if not needed)" |
| 177 | +kubectl apply -f https://raw.githubusercontent.com/kubernetes/ingress-nginx/main/deploy/static/provider/cloud/deploy.yaml |
| 178 | + |
| 179 | +echo "Done. Use ACR: $ACR_NAME.azurecr.io" |
0 commit comments