-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdeploy.sh
More file actions
executable file
·136 lines (109 loc) · 3.66 KB
/
Copy pathdeploy.sh
File metadata and controls
executable file
·136 lines (109 loc) · 3.66 KB
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
#!/bin/bash
# basics
# https://docs.microsoft.com/en-us/azure/azure-resource-manager/resource-group-authoring-templates
# apiversion
# https://www.danielstechblog.info/using-the-latest-api-version-in-your-azure-resource-manager-templates/
# https://github.com/Azure/azure-rest-api-specs
# tags
# https://docs.microsoft.com/en-us/azure/azure-resource-manager/resource-group-using-tags
# https://docs.microsoft.com/en-us/azure/azure-resource-manager/resource-manager-template-tags
# link templates
# https://docs.microsoft.com/en-us/azure/azure-resource-manager/resource-group-linked-templates
# https://blogs.endjin.com/2016/12/advanced-azure-resource-manager-template-patterns-t-shirt-sizing-and-optional-resources/
# http://marcvaneijk.com/2016/03/14/nested.html
# https://blog.kloud.com.au/2016/04/16/break-down-your-templates-with-linked-templates-part-1/
# https://blog.kloud.com.au/2016/04/30/break-down-your-templates-with-linked-templates-part-2/
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 -i <subscriptionId> -g <resourceGroupName> -n <deploymentName> -l <resourceGroupLocation>" 1>&2; exit 1; }
declare subscriptionId=""
declare resourceGroupName=""
declare deploymentName=""
declare resourceGroupLocation=""
# Initialize parameters specified from command line
while getopts ":i:g:n:l:" arg; do
case "${arg}" in
i)
subscriptionId=${OPTARG}
;;
g)
resourceGroupName=${OPTARG}
;;
n)
deploymentName=${OPTARG}
;;
l)
resourceGroupLocation=${OPTARG}
;;
esac
done
shift $((OPTIND-1))
#Prompt for parameters is some required parameters are missing
if [[ -z "$subscriptionId" ]]; then
echo "Subscription Id:"
read subscriptionId
[[ "${subscriptionId:?}" ]]
fi
if [[ -z "$resourceGroupName" ]]; then
echo "ResourceGroupName:"
read resourceGroupName
[[ "${resourceGroupName:?}" ]]
fi
if [[ -z "$deploymentName" ]]; then
echo "DeploymentName:"
read deploymentName
fi
if [[ -z "$resourceGroupLocation" ]]; then
echo "Enter a location below to create a new resource group else skip this"
echo "ResourceGroupLocation:"
read resourceGroupLocation
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
if [ -z "$subscriptionId" ] || [ -z "$resourceGroupName" ] || [ -z "$deploymentName" ]; then
echo "Either one of subscriptionId, resourceGroupName, deploymentName is empty"
usage
fi
#login to azure using your credentials
azure account show 1> /dev/null
if [ $? != 0 ]; then
azure login
fi
#set the default subscription id
azure account set --subscription $subscriptionId
set +e
#Check for existing RG
group_exists=$(azure group exists --name $resourceGroupName)
if [[ $group_exists == "false" ]]; then
echo "Resource group with name" $resourceGroupName "could not be found. Creating new resource group.."
set -e
(
set -x
azure group create --name $resourceGroupName --location $resourceGroupLocation 1> /dev/null
)
else
echo "Using existing resource group..."
fi
#Start deployment
echo "Starting deployment..."
(
set -x
azure group deployment create --name $deploymentName --resource-group $resourceGroupName --template-file $templateFilePath --parameters @$parametersFilePath --verbose --debug
)
if [ $? == 0 ];
then
echo "Template has been successfully deployed"
fi