This repository has been archived by the owner on Aug 13, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 8
/
deploy.sh
81 lines (71 loc) · 2.66 KB
/
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
#!/bin/bash
##### deploy.sh ################################################################
# Purpose: General purpose ARM template deployment script for Azure CLI 2.0
# Author: Ben Coleman
# Date: 23-06-2017
# Version: 1.1
################################################################################
# Defaults
LOC='westeurope'
SUB='Microsoft Azure Internal Consumption'
HELP=false
VALIDATE=false;
DATE=`date +%Y%m%d_%H.%M.%S`
# Display usage
usage(){
echo "\
deploy.sh - Deploy an Azure resource template
Parameters:
-t, --template Input template file (required)
[-g, --group] Resource group to deploy to, will be created
[-l, --location] Region or location for the resource group, default: westeurope
[-p, --parameters] Parameter file, if ommited then <inputfile>.parameters.json is used
[-s, --subscription] Azure subscription to use, default: 'Microsoft Azure Internal Consumption'
[-h] Show this help text
" | column -t -s ";"
}
# Param handling stuff
OPTS=`getopt -o t:g:p:l:s:h --long template:,group:,location:,subscription:,parameters:,help -n 'parse-options' -- "$@"`
if [ $? != 0 ] ; then echo "Failed parsing options." >&2 ; usage; exit 1 ; fi
eval set -- "$OPTS"
# Param handling stuff
while true; do
case "$1" in
-t | --template ) FILE="$2"; shift; shift;;
-g | --group ) GROUP="$2"; shift; shift;;
-p | --parameters ) PARAMS="$2"; shift; shift;;
-l | --location ) LOC="$2"; shift; shift;;
-s | --subscription ) SUB="$2"; shift; shift;;
-h | --help ) HELP=true; shift ;;
-- ) shift; break ;;
* ) break ;;
esac
done
if [ ${HELP} = true ]; then
usage
exit 0
fi
# We need the template param at a minimum
if [ -z ${FILE} ]; then
usage
exit 1
fi
# Make up a res group name if it's not supplied, Temp.GroupXXXXX where X is random number
if [ -z ${GROUP} ]; then
GROUP=Temp.Group$(shuf -i 1-100000 -n 1)
echo "WARN: Group name not supplied, $GROUP will be used"
fi
# Parameter file can be inferred from the input template
PARAMS="${PARAMS:-${FILE::-5}.parameters.json}"
# Check Azure CLI is installed
if [ ! -x "$(command -v az)" ]; then
echo "ERROR: Command 'az' not found, please install Azure CLI v2"
exit 1
fi
# Create resource group and start deployment
echo "Will use Azure subscription '$SUB'..."
az account set -s "$SUB"
echo "Creating resource group '$GROUP' in region '$LOC'..."
az group create -n $GROUP -l $LOC -o table
echo "Deploying template '$FILE'..."
az group deployment create -g $GROUP --template-file "$FILE" --parameters "@${PARAMS}" --verbose --name "deployment_$DATE" -o jsonc --query "[properties.{outputs: outputs},properties.{status: provisioningState}]"