forked from Azure/azure-quickstart-templates
-
Notifications
You must be signed in to change notification settings - Fork 0
/
az-group-deploy.sh
executable file
·179 lines (150 loc) · 7.49 KB
/
az-group-deploy.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
#!/bin/bash -e
while getopts "a:l:g:s:f:e:uvdb" opt; do
case $opt in
a)
artifactsStagingDirectory=$OPTARG #the folder or sample to deploy
;;
l)
location=$OPTARG #location for the deployed resource group
;;
g)
resourceGroupName=$OPTARG #name of the resource group to create
;;
u)
uploadArtifacts='true' #set this switch to upload/stage artifacs
;;
s)
storageAccountName=$OPTARG #storage account to use for staging, if not supplied one will be created and reused
;;
f)
templateFile=$OPTARG
;;
e)
parametersFile=$OPTARG
;;
v)
validateOnly='true' #validate the template without deploying
;;
d)
devMode='true' #dev mode automatically selects a different parameter file without explicitly specifying it
;;
b)
bicep='true' #dev mode automatically selects a different parameter file without explicitly specifying it
;;
esac
done
[[ $# -eq 0 || -z $artifactsStagingDirectory || -z $location ]] && { echo "Usage: $0 <-a foldername> <-l location> [-e parameters-file] [-g resource-group-name] [-u] [-s storageAccountName] [-t templateFile] [-v] [-d]"; exit 1; }
export AZURE_HTTP_USER_AGENT="AzureQuickStarts $AZURE_HTTP_USER_AGENT"
# if the switch is set or the file is a bicep file
if [[ $bicep || ($templateFile == *.bicep) ]]
then
isBicep=true
defaultTemplateFile="/main.bicep"
else
isBicep=false
defaultTemplateFile="/azuredeploy.json"
fi
if [[ -z $templateFile ]]
then
templateFile="$artifactsStagingDirectory$defaultTemplateFile"
fi
if [[ $isBicep = true ]]
then
bicep build $templateFile
# after building the script will work with the json file
t="${templateFile/.bicep/.json}"
templateFile=$t
fi
if [[ $devMode ]]
then
parametersFile="$artifactsStagingDirectory/azuredeploy.parameters.dev.json"
if [ ! -f $parametersFile ]
then
parametersFile="$artifactsStagingDirectory/azuredeploy.parameters.1.json"
fi
else
if [[ -z $parametersFile ]]
then
parametersFile="$artifactsStagingDirectory/azuredeploy.parameters.json"
fi
fi
echo "Using parameters file: "$parametersFile
templateName="$( basename "${templateFile%.*}" )"
templateDirectory="$( dirname "$templateFile")"
if [[ -z $resourceGroupName ]]
then
resourceGroupName=$(basename $(cd "${artifactsStagingDirectory}" && pwd))
fi
parameterJson=$( cat "$parametersFile" | jq '.parameters' )
_artifactsLocationParameter=$( cat "$templateFile" | jq '.parameters._artifactsLocation' )
_artifactsLocationSasTokenParameter=$( cat "$templateFile" | jq '.parameters._artifactsLocationSasToken' )
if [[ $uploadArtifacts || $_artifactsLocationParameter != null || $_artifactsLocationSasTokenParameter != null ]]
then
if [[ -z $storageAccountName ]]
then
subscriptionId=$( az account show -o json | jq -r '.id' )
subscriptionId="${subscriptionId//-/}"
subscriptionId="${subscriptionId:0:19}"
artifactsStorageAccountName="stage$subscriptionId"
artifactsResourceGroupName="ARM_Deploy_Staging"
if [[ -z $( az storage account list -o json | jq -r '.[].name | select(. == '\"$artifactsStorageAccountName\"')' ) ]]
then
az group create -n "$artifactsResourceGroupName" -l "$location"
az storage account create -l "$location" --sku "Standard_LRS" -g "$artifactsResourceGroupName" -n "$artifactsStorageAccountName" 2>/dev/null
fi
else
artifactsStorageAccountName=$storageAccountName
artifactsResourceGroupName=$( az storage account list -o json | jq -r '.[] | select(.name == '\"$storageAccountName\"') .resourceGroup' )
if [[ -z $artifactsResourceGroupName ]]
then
echo "Cannot find storageAccount: "$storageAccountName
fi
fi
artifactsStorageContainerName=${resourceGroupName}"-stageartifacts"
artifactsStorageContainerName=$( echo "${artifactsStorageContainerName:0:63}" | awk '{print tolower($0)}')
artifactsStorageAccountKey=$( az storage account keys list -g "$artifactsResourceGroupName" -n "$artifactsStorageAccountName" -o json | jq -r '.[0].value' )
az storage container create -n "$artifactsStorageContainerName" --account-name "$artifactsStorageAccountName" --account-key "$artifactsStorageAccountKey" >/dev/null 2>&1
# Get a 4-hour SAS Token for the artifacts container. Fall back to OSX date syntax if Linux syntax fails.
plusFourHoursUtc=$(date -u -d @$(($(date +%s)+14400)) +%Y-%m-%dT%H:%MZ 2>/dev/null) || plusFourHoursUtc=$(date -u --date "$dte 4 hour" +%Y-%m-%dT%H:%MZ)
sasToken=$( az storage container generate-sas -n "$artifactsStorageContainerName" --permissions r --expiry "$plusFourHoursUtc" --account-name "$artifactsStorageAccountName" --account-key "$artifactsStorageAccountKey" -o json | sed 's/"//g')
blobEndpoint=$( az storage account show -n "$artifactsStorageAccountName" -g "$artifactsResourceGroupName" -o json | jq -r '.primaryEndpoints.blob' )
defaultValue=$( cat "$templateFile" | jq '.parameters._artifactsLocation.defaultValue' )
if [[ $defaultValue != *").properties.templateLink.uri"* ]] # this should really include deployment(). but VS Code has a bug so working around that
then #if the template is not using the templateLink.uri, then add the storage location to the parameters
parameterJson=$( echo "$parameterJson" | jq "{_artifactsLocation: {value: "\"$blobEndpoint$artifactsStorageContainerName/"\"}} + ." )
fi
parameterJson=$( echo "$parameterJson" | jq "{_artifactsLocationSasToken: {value: \"?"$sasToken"\"}} + ." )
artifactsStagingDirectory=$( echo "$artifactsStagingDirectory" | sed 's/\/*$//')
artifactsStagingDirectoryLen=$((${#artifactsStagingDirectory} + 1))
for filepath in $( find "$artifactsStagingDirectory" -type f )
do
relFilePath=${filepath:$artifactsStagingDirectoryLen}
echo "Uploading file $relFilePath..."
az storage blob upload -f $filepath --container $artifactsStorageContainerName -n $relFilePath --account-name "$artifactsStorageAccountName" --account-key "$artifactsStorageAccountKey" --verbose
done
templateUri=$blobEndpoint$artifactsStorageContainerName/$(basename $templateFile)?$sasToken
fi
# Create the resource group only if it doesn't already exist
targetResourceGroup=$( az group list -o json | jq -r '.[] | select(.name == '\"$resourceGroupName\"')'.name )
if [[ -z $targetResourceGroup ]]
then
az group create -n "$resourceGroupName" -l "$location"
fi
# Remove line endings from parameter JSON so it can be passed in to the CLI as a single line
parameterJson=$( echo "$parameterJson" | jq -c '.' )
if [[ $validateOnly ]]
then
if [[ $uploadArtifacts || $_artifactsLocationParameter != null ]]
then
az deployment group validate -g "$resourceGroupName" --template-uri $templateUri --parameters "$parameterJson" --verbose
else
az deployment group validate -g "$resourceGroupName" --template-file $templateFile --parameters "$parameterJson" --verbose
fi
else
if [[ $uploadArtifacts || $_artifactsLocationParameter != null ]]
then
az deployment group create -g "$resourceGroupName" -n AzureRMSamples --template-uri $templateUri --parameters "$parameterJson" --verbose
else
az deployment group create -g "$resourceGroupName" -n AzureRMSamples --template-file $templateFile --parameters "$parameterJson" --verbose
fi
fi