/
deploy.ps1
85 lines (74 loc) · 3.06 KB
/
deploy.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
Param (
[string] $ResourceGroupName = "ubuntudocker-local-rg",
[string] $Location = "North Europe",
[string] $Template = "$PSScriptRoot\azuredeploy.json",
[string] $TemplateParameters = "$PSScriptRoot\azuredeploy.parameters.json",
[string] $VaultOwner,
[string] $VaultName = "ubuntudocker-local-kv",
[string] $VaultSecretName = "VirtualMachineAdminPassword",
[string] $AdminUsername = "azureuser"
)
$ErrorActionPreference = "Stop"
if ([string]::IsNullOrEmpty($env:RELEASE_DEFINITIONNAME))
{
Write-Host (@"
Not executing inside VSTS Release Management.
Make sure you have done "Login-AzureRmAccount" and
"Select-AzureRmSubscription -SubscriptionName name"
so that script continues to work correctly for you.
"@)
}
if ((Get-AzureRmResourceGroup -Name $ResourceGroupName -Location $Location -ErrorAction SilentlyContinue) -eq $null)
{
Throw "Resource group '$ResourceGroupName' doesn't exist which means 'deploy-initial.ps1' is not called correctly."
}
# Get password from key vault
$secret = Get-AzureKeyVaultSecret -VaultName $VaultName -Name $VaultSecretName
# Create additional parameters that we pass to the template deployment
$additionalParameters = New-Object -TypeName hashtable
$additionalParameters['adminUsername'] = $AdminUsername
$additionalParameters['adminPassword'] = $secret.SecretValue
$result = New-AzureRmResourceGroupDeployment `
-ResourceGroupName $ResourceGroupName `
-TemplateFile $Template `
-TemplateParameterFile $TemplateParameters `
@additionalParameters `
-Verbose
$result
if ($result.Outputs.fqdn -eq $null)
{
Throw "Template deployment didn't return FQDN correctly and therefore deployment is cancelled."
}
$fqdn = $result.Outputs.fqdn.value
#
# As an _added_ bonus you can do all sorts of scripting
# since we have username and password available!
#
# Below example grabs "secure copy" tools from web and uses them to push
# simple script to the vm and then executes it. This enables
# any custom deployments you wish.
#
cd $PSScriptRoot
Invoke-WebRequest -Uri "https://the.earth.li/~sgtatham/putty/latest/x86/pscp.exe" -OutFile pscp.exe
Invoke-WebRequest -Uri "https://the.earth.li/~sgtatham/putty/latest/x86/plink.exe" -OutFile plink.exe
del log.txt -ErrorAction SilentlyContinue
$hostKey = ""
$ErrorActionPreference = "Continue"
.\pscp.exe -sshlog log.txt `
-l $AdminUsername -pw $secret.SecretValueText `
-batch *.sh ($fqdn + ":.") 2> $null
if ($LastExitCode -eq 1)
{
# Most likely the host key check failed so we need to grab the
# host key from log and pass that on the command-line:
$hostKeyFind = (Get-Content .\log.txt | Select-String -Pattern "^Event Log: ssh-rsa \d* ([\d|a-z|:]*)")
$hostKey = $hostKeyFind.Matches[0].Groups[1].Value
.\pscp.exe `
-l $AdminUsername -pw $secret.SecretValueText `
-batch -hostkey "$hostKey" *.sh ($fqdn + ":.")
}
# Below command will execute script and print out
# "This is script coming from repository" text in console
.\plink.exe `
-l $AdminUsername -pw $secret.SecretValueText `
-batch -hostkey "$hostKey" $fqdn "bash deploy.sh"