-
Notifications
You must be signed in to change notification settings - Fork 14
/
Add-AzureLocalResource.ps1
116 lines (99 loc) · 4.28 KB
/
Add-AzureLocalResource.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
function Add-AzureLocalResource
{
<#
.Synopsis
Adds an Azure local storage resource to a service definition
.Description
Adds an Azure local storage resource to a service definition.
Azure local storage can create well-known directories on the host machine
.Example
New-AzureServiceDefinition -ServiceName "foo" |
Add-AzureLocalResource -ServiceDefinition
.Link
New-AzureServiceDefinition
#>
[OutputType([xml],[string])]
param(
# The ServiceDefinition XML. This should be created with New-AzureServiceDefinition or retreived with Import-AzureServiceDefinition
[Parameter(Mandatory=$true,
ValueFromPipeline=$true,
ValueFromPipelineByPropertyName=$true)]
[ValidateScript({
$isServiceDefinition = $_.NameTable.Get("http://schemas.microsoft.com/ServiceHosting/2008/10/ServiceDefinition")
if (-not $IsServiceDefinition) {
throw "Input must be a ServiceDefinition XML"
}
return $true
})]
[Xml]
$ServiceDefinition,
# If set, the local resource will only apply to the role named ToRole. If ToRole is not found, or doesn't
# exist, the last role will be used.
[string]
$ToRole,
# The name of the local storage. This will be the path of the name storage element, relative to the root drive.
[Parameter(Mandatory=$true)]
[string]
$Name,
# The size of the storage. Sizes will be rounded up to the nearest megabyte.
[Long]
$Size = 1mb,
# If set, a role will not be cleaned on recycle
[switch]
$DoNotcleanOnRoleRecycle,
# If set, will output results as string rather than XML
[switch]
$AsString
)
process {
#region Resolve the role if it set, create the role if it doesn't exist, and track it if they assume the last item.
$roles = @($ServiceDefinition.ServiceDefinition.WebRole), @($ServiceDefinition.ServiceDefinition.WorkerRole) + @($ServiceDefinition.ServiceDefinition.VirtualMachineRole)
$xmlNamespace = @{'ServiceDefinition'='http://schemas.microsoft.com/ServiceHosting/2008/10/ServiceDefinition'}
$selectXmlParams = @{
XPath = '//ServiceDefinition:WebRole|//ServiceDefinition:WorkerRole|//ServiceDefinition:VirtualMachineRole'
Namespace = $xmlNamespace
}
$roles = @(Select-Xml -Xml $ServiceDefinition @selectXmlParams |
Select-Object -ExpandProperty Node)
if (-not $roles) {
$ServiceDefinition = $ServiceDefinition |
Add-AzureRole -RoleName "WebRole1"
$roles = @(Select-Xml -Xml $ServiceDefinition @selectXmlParams |
Select-Object -ExpandProperty Node)
}
#endregion Resolve the role if it set, create the role if it doesn't exist, and track it if they assume the last item.
if ($roles.Count -gt 1) {
if ($ToRole) {
} else {
$role = $roles[-1]
}
} else {
if ($ToRole) {
if ($roles[0].Name -eq $ToRole) {
$role = $roles[0]
} else {
$role = $null
}
} else {
$role = $roles[0]
}
}
if (-not $role) { return }
$realSize = [Math]::Ceiling($size / 1mb)
if (-not $role.LocalResources) {
$role.InnerXml += "<LocalResources/>"
}
$localResourcesNode = Select-Xml -Xml $role -Namespace $xmlNamespace -XPath '//ServiceDefinition:LocalResources' |
Select-Object -ExpandProperty Node
$localResourcesNode.InnerXml += "<LocalStorage name='$Name' sizeInMB='$realSize' cleanOnRoleRecycle='$($DoNotcleanOnRoleRecycle.ToString().ToLower())'/>"
}
end {
if ($AsString) {
$strWrite = New-Object IO.StringWriter
$serviceDefinition.Save($strWrite)
return "$strWrite"
} else {
$serviceDefinition
}
}
}