-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdeploy.azcli
executable file
·197 lines (165 loc) · 6.81 KB
/
deploy.azcli
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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
#!/bin/bash
set -euo pipefail
IFS=$'\n\t'
# -e: immediately exit if any command has a non-zero exit status
# -o: prevents errors in a pipeline from being masked
# IFS new value is less likely to cause confusing bugs when looping arrays or arguments (e.g. $@)
usage() { echo "Usage: $0 -n <deploymentName> -f <installKitFile> -i <subscriptionId> -g <resourceGroupName> -l <resourceGroupLocation>" 1>&2; exit 1; }
declare deploymentName=""
declare installKitFile=""
declare subscriptionId=""
declare resourceGroupName=""
declare resourceGroupLocation=""
# Initialize parameters specified from command line
while getopts ":n:f:i:g:l:" arg; do
case "${arg}" in
n)
deploymentName=${OPTARG}
;;
f)
installKitFile=${OPTARG}
;;
i)
subscriptionId=${OPTARG}
;;
g)
resourceGroupName=${OPTARG}
;;
l)
resourceGroupLocation=${OPTARG}
;;
esac
done
shift $((OPTIND-1))
#Prompt for parameters is some required parameters are missing
if [[ -z "$deploymentName" ]]; then
echo "Enter a name for this deployment:"
read deploymentName
fi
if [[ ! -f "$installKitFile" ]]; then
echo "The IBM DB2 Server Install Kit file is necessary to setup WAS ND."
echo "Enter the path of IBM DB2 Server Install Kit:"
read installKitFile
[[ -f "$installKitFile" ]]
fi
if [[ -z "$subscriptionId" ]]; then
echo "Your subscription ID can be looked up with the CLI using: az account show --out json "
echo "Enter your subscription ID:"
read subscriptionId
[[ "${subscriptionId:?}" ]]
fi
if [[ -z "$resourceGroupName" ]]; then
echo "This script will look for an existing resource group, otherwise a new one will be created "
echo "You can create new resource groups with the CLI using: az group create "
echo "Enter a resource group name"
read resourceGroupName
[[ "${resourceGroupName:?}" ]]
fi
if [[ -z "$resourceGroupLocation" ]]; then
echo "If creating a *new* resource group, you need to set a location "
echo "You can lookup locations with the CLI using: az account list-locations "
echo "Enter resource group location:"
read resourceGroupLocation
fi
if [ -z "$deploymentName" ] || [ ! -f "$installKitFile" ] || [ -z "$subscriptionId" ] || [ -z "$resourceGroupName" ]; then
echo "Either one of deploymentName, installKitFile, subscriptionId and resourceGroupName is empty"
usage
fi
#templateFile Path - template file to be used
templateFilePath="template.json"
if [ ! -f "$templateFilePath" ]; then
echo "$templateFilePath not found"
exit 1
fi
#parameter file path
parametersFilePath="parameters.json"
if [ ! -f "$parametersFilePath" ]; then
echo "$parametersFilePath not found"
exit 1
fi
#login to azure using your credentials
az account show 1> /dev/null
if [ $? != 0 ]; then
az login
fi
#set the default subscription id
az account set --subscription $subscriptionId
set +e
#Check for existing RG
az group show --name $resourceGroupName 1> /dev/null
if [ $? != 0 ]; then
echo "Resource group with name" $resourceGroupName "could not be found. Creating new resource group.."
set -e
(
set -x
az group create --name $resourceGroupName --location $resourceGroupLocation 1> /dev/null
)
else
echo "Using existing resource group..."
fi
resourceGroupLocation=$( az group show --name $resourceGroupName | jq -r '.location' )
#Create storage account and upload IBM DB2 Server install kit to storage account
#Create storage account if not existing
storageAccountName="${subscriptionId//-/}"
storageAccountName="${storageAccountName:0:19}"
storageAccountName="stage$storageAccountName"
if [[ -z $( az storage account list -o json | jq -r --arg accountName "$storageAccountName" '.[].name | select(. == $accountName)' ) ]]; then
echo "Storage account with name" $storageAccountName "could not be found. Creating new storage account..."
set -e
(
set -x
az storage account create -l "$resourceGroupLocation" --sku "Standard_LRS" -g "$resourceGroupName" -n "$storageAccountName" >/dev/null 2>&1
)
else
echo "Using existing storage account..."
fi
#Create storage container if not existing
storageContainerName=${resourceGroupName}"-stageartifacts"
storageContainerName=$( echo "$storageContainerName" | awk '{print tolower($0)}')
storageAccountKey=$( az storage account keys list -g "$resourceGroupName" -n "$storageAccountName" -o json | jq -r '.[0].value' )
if [[ $( az storage container exists --name "$storageContainerName" --account-name "$storageAccountName" --account-key "$storageAccountKey" -o json | jq '.exists') = false ]]; then
echo "Storage container with name" $storageContainerName "could not be found. Creating new storage container..."
set -e
(
set -x
az storage container create -n "$storageContainerName" --account-name "$storageAccountName" --account-key "$storageAccountKey" >/dev/null 2>&1
)
else
echo "Using existing storage container..."
fi
#Upload IBM DB2 Server install kit to storage container
fileName=$(basename "$installKitFile")
if [[ $( az storage blob exists -n "$fileName" -c "$storageContainerName" --account-name "$storageAccountName" --account-key "$storageAccountKey" -o json | jq '.exists') = false ]]; then
echo "Storage blob with name" $fileName "could not be found. Creating new storage blob..."
set -e
(
set -x
az storage blob upload -f "$installKitFile" -c $storageContainerName -n "$fileName" --account-name "$storageAccountName" --account-key "$storageAccountKey"
)
else
echo "Using existing storage blob..."
fi
set +u
#Get a 4-hour SAS Token for the artifacts container. Fall back to OSX date syntax if Linux syntax fails.
plusFourHoursUtc=$(date -u -v+4H +%Y-%m-%dT%H:%MZ 2>/dev/null) || plusFourHoursUtc=$(date -u --date "$dte 4 hour" +%Y-%m-%dT%H:%MZ)
#Generate SAS token
sasToken=$( az storage container generate-sas -n "$storageContainerName" --permissions r --expiry "$plusFourHoursUtc" --account-name "$storageAccountName" --account-key "$storageAccountKey" -o json | sed 's/"//g')
blobEndpoint=$( az storage account show -n "$storageAccountName" -g "$resourceGroupName" -o json | jq -r '.primaryEndpoints.blob' )
installKitUri=$blobEndpoint$storageContainerName/"$fileName"?$sasToken
#parameters JSON
parametersJson=$( cat $parametersFilePath | jq '.parameters' )
parametersJson=$( echo "$parametersJson" | jq --arg uri "$installKitUri" '{"installKitUri": {"value":$uri}} + .' )
parametersJson=$( echo "$parametersJson" | jq -c '.' )
vnetName=$( echo "$parametersJson" | jq -r '.virtualNetworkName.value' )
if [[ $(az network vnet list -g "$resourceGroupName" --query "[?name=='${vnetName}']" -o json | jq -r '. | length') -gt 0 ]]; then
parametersJson=$( echo "$parametersJson" | jq -c '{"virtualNetworkExisted": {"value":true}} + .' )
fi
#Start deployment
echo "Starting deployment..."
(
az deployment group create --name "$deploymentName" --resource-group "$resourceGroupName" \
--template-file "$templateFilePath" --parameters "$parametersJson"
)
if [[ $? -eq 0 ]]; then
echo "Template has been successfully deployed"
fi