1+ #! /bin/bash
2+ set -e
3+
4+ cd " $( dirname " $0 " ) /.."
5+
6+ # Initialize variables
7+ RESOURCE_GROUP=" "
8+ LOCATION=" "
9+ APPLY_ROLE_ASSIGNMENTS=true
10+
11+ # Parse arguments
12+ while [ " $# " -gt 0 ]; do
13+ case " $1 " in
14+ -rg|--resource-group)
15+ RESOURCE_GROUP=" $2 "
16+ shift 2
17+ ;;
18+ -l|--location)
19+ LOCATION=" $2 "
20+ shift 2
21+ ;;
22+ --no-role-assignments)
23+ APPLY_ROLE_ASSIGNMENTS=false
24+ shift
25+ ;;
26+ -h|--help)
27+ # TODO AGB: Clean up
28+ echo " Usage: $0 --resource-group <resource group name> --location <Azure region> [--no-role-assignments]"
29+ echo " or: $0 -rg <resource group name> -l <Azure region> [--no-role-assignments]"
30+ echo " --no-role-assignments: Do not apply role assignments to the managed identities."
31+ exit 0
32+ ;;
33+ * )
34+ echo " Unknown parameter: $1 "
35+ echo " Use --help for usage information."
36+ exit 1
37+ ;;
38+ esac
39+ done
40+
41+ # Check if the resource group exists and create it if it doesn't
42+ echo Checking if resource group " ${RESOURCE_GROUP} " exists...
43+ RG_EXISTS=$( az group exists -n " $RESOURCE_GROUP " | tr -d ' \r\n' )
44+ if [ " $RG_EXISTS " = " false" ]; then
45+ echo " Resource group '$RESOURCE_GROUP ' does not exist. Creating it in location '$LOCATION '."
46+ az group create -n " $RESOURCE_GROUP " -l " $LOCATION "
47+
48+ while RG_CREATED=$( az group exists -n " $RESOURCE_GROUP " | tr -d ' \r\n' ) ; [ " $RG_CREATED " = " false" ]; do
49+ echo " Waiting for resource group '$RESOURCE_GROUP ' to be created..."
50+ sleep 1
51+ done
52+ fi
53+
54+ echo Deploying storage account to resource group " ${RESOURCE_GROUP} " in location " ${LOCATION} " ...
55+ STORAGE_DEPLOYMENT_NAME=" ccw-storage-deployment-${RESOURCE_GROUP} -${LOCATION} "
56+ az deployment group create \
57+ --resource-group " $RESOURCE_GROUP " \
58+ --template-file $( pwd) /bicep/storage-new.bicep \
59+ --parameters location=" $LOCATION " \
60+ --name " $STORAGE_DEPLOYMENT_NAME "
61+
62+ STORAGE_ACCOUNT_NAME=$( az deployment group show -g " $RESOURCE_GROUP " -n " $STORAGE_DEPLOYMENT_NAME " --query " properties.outputs.storageAccountName.value" -o tsv | tr -d ' \r\n' )
63+
64+ echo Creating managed identity for virtual machine scale sets in resource group " ${RESOURCE_GROUP} " in location " ${LOCATION} " ...
65+ if [ " $APPLY_ROLE_ASSIGNMENTS " = true ]; then
66+ echo " Role assignments will be applied after the managed identity is created."
67+ else
68+ echo " Role assignments will NOT be applied as requested."
69+ fi
70+ az deployment group create \
71+ --resource-group " $RESOURCE_GROUP " \
72+ --template-file $( pwd) /bicep/vmssManagedIdentity.bicep \
73+ --parameters name=" ccwLockerManagedIdentity" \
74+ --parameters location=" $LOCATION " \
75+ --parameters storageAccountName=" $STORAGE_ACCOUNT_NAME " \
76+ --parameters applyRoleAssignments=" $APPLY_ROLE_ASSIGNMENTS " \
77+ --name " ccw-vmss-mi-deployment-${RESOURCE_GROUP} -${LOCATION} "
78+
79+ echo Creating managed identity for the CycleCloud virtual machine in resource group " ${RESOURCE_GROUP} " in location " ${LOCATION} " ...
80+ if [ " $APPLY_ROLE_ASSIGNMENTS " = true ]; then
81+ echo " Role assignments will be applied after the managed identity is created."
82+ else
83+ echo " Role assignments will NOT be applied as requested."
84+ fi
85+ az deployment group create \
86+ --resource-group " $RESOURCE_GROUP " \
87+ --template-file $( pwd) /bicep/vmManagedIdentity.bicep \
88+ --parameters name=" ccwCycleCloudVirtualMachineManagedIdentity" \
89+ --parameters location=" $LOCATION " \
90+ --parameters applyRoleAssignments=" $APPLY_ROLE_ASSIGNMENTS " \
91+ --name " ccw-vm-mi-deployment-${RESOURCE_GROUP} -${LOCATION} "
0 commit comments