/
deploy-arm.ps1
executable file
·140 lines (110 loc) · 4.02 KB
/
deploy-arm.ps1
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
# Copyright (C) Microsoft Corporation. All rights reserved.
# Licensed under the MIT License. See LICENSE in project root for information.
<#
.SYNOPSIS
Deploys a template to Azure
.DESCRIPTION
Deploys an Azure Resource Manager template with a given parameters file.
.PARAMETER subscriptionId
The Subscription ID where the template will be deployed.
.PARAMETER resourceGroupName
The Resource Group where the template will be deployed.
Can be the name of an existing resource group or a new one which will be
created.
.PARAMETER resourceGroupLocation
A resource group location.
If the resourceGroupName does not exist, this parameter is required for the
creation of the group, specifying its location.
.PARAMETER deploymentName
The deployment name.
.PARAMETER templateLocation
URL of the template to deploy.
Optional, defaults to the one corresponding to this script.
.PARAMETER parametersFilePath
Path of the parameters file to use for the template, use
deploy-parameters.template to create this file.
If file is not found, will prompt for parameter values based on
template.
.EXAMPLE
Deploy-Arm
Interactively read values and run.
.EXAMPLE
Deploy-Arm deploy-main-template.json -resourceGroupName MyCluster -parametersFilePath MyParameters.json
Deploy the Cluster + GPU template, in the default subscription, under
the existing "MyCluster" group with the parameters in MyParameters.json
#>
param(
[Parameter(Mandatory=$True)]
[string]
$subscriptionId,
[Parameter(Mandatory=$True)]
[string]
$resourceGroupName,
[Parameter(Mandatory=$False)]
[string]
$resourceGroupLocation,
[Parameter(Mandatory=$False)]
[string]
$deploymentName,
[Parameter(Mandatory=$False)]
[string]
$templateLocation,
[Parameter(Mandatory=$True)]
[string]
$parametersFilePath
)
# <=<= this line is replaced with variables defined with `defvar -X` =>=>
$DOWNLOAD_URL = "$STORAGE_URL/$MML_VERSION"
# TODO: throw an error if $MML_VERSION is not defined
<#
.SYNOPSIS
Registers RPs
#>
Function RegisterRP {
Param(
[string]$ResourceProviderNamespace
)
Write-Host "Registering resource provider '$ResourceProviderNamespace'";
Register-AzureRmResourceProvider -ProviderNamespace $ResourceProviderNamespace;
}
#******************************************************************************
# Script body
# Execution begins here
#******************************************************************************
if (!$templateLocation) {
$templateLocation = $DOWNLOAD_URL + "/deploy-main-template.json";
}
$ErrorActionPreference = "Stop"
# sign in
Write-Host "Logging in...";
Login-AzureRmAccount;
# select subscription
Write-Host "Selecting subscription '$subscriptionId'";
Select-AzureRmSubscription -SubscriptionID $subscriptionId;
# Register RPs
$resourceProviders = @("microsoft.hdinsight");
if ($resourceProviders.length) {
Write-Host "Registering resource providers"
foreach ($resourceProvider in $resourceProviders) {
RegisterRP($resourceProvider);
}
}
#Create or check for existing resource group
$resourceGroup = Get-AzureRmResourceGroup -Name $resourceGroupName -ErrorAction SilentlyContinue
if (!$resourceGroup) {
Write-Host "Resource group '$resourceGroupName' does not exist. To create a new resource group, please enter a location.";
if (!$resourceGroupLocation) {
$resourceGroupLocation = Read-Host "resourceGroupLocation";
}
Write-Host "Creating resource group '$resourceGroupName' in location '$resourceGroupLocation'";
New-AzureRmResourceGroup -Name $resourceGroupName -Location $resourceGroupLocation
} else {
Write-Host "Using existing resource group '$resourceGroupName'";
}
# Start the deployment
Write-Host "Starting deployment...";
if (Test-Path $parametersFilePath) {
New-AzureRmResourceGroupDeployment -ResourceGroupName $resourceGroupName -TemplateUri $templateLocation -TemplateParameterFile $parametersFilePath;
} else {
New-AzureRmResourceGroupDeployment -ResourceGroupName $resourceGroupName -TemplateUri $templateLocation;
}