-
Notifications
You must be signed in to change notification settings - Fork 14
/
Add-AzureWebSite.ps1
261 lines (218 loc) · 10.2 KB
/
Add-AzureWebSite.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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
function Add-AzureWebSite
{
<#
.Synopsis
Adds an Azure web site to a service definition.
.Description
Adds an Azure web site to a service definition.
The site can bind to multiple host
Creates a web role if one does not exist.
.Example
New-AzureServiceDefinition -ServiceName "AService" |
Add-AzureWebSite -SiteName "ASite" -PhysicalDirectory "C:\inetpub\wwwroot\asite" -HostHeader a.subdomain.com, nakeddomain.com, www.fulldomain.com -asString
.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.
[Parameter(ValueFromPipelineByPropertyName=$true)]
[string]
$ToRole,
# The name of the site to create. If Sitename is not set, sites will be named Web1, Web2, Web3, etc
[Parameter(ValueFromPipelineByPropertyName=$true)]
[string]
$SiteName,
# The physical directory of the website. This is where the web site files are located.
[Parameter(ValueFromPipelineByPropertyName=$true)]
[string]
$PhysicalDirectory,
# One or more host headers to use for the site
[Parameter(ValueFromPipelineByPropertyName=$true)]
[string[]]
$HostHeader,
# Additional bindings. Each hashtable can contain an EndpointName, Name, and HostHeader
[Parameter(ValueFromPipelineByPropertyName=$true)]
[Hashtable[]]
[ValidateScript({
Test-SafeDictionary -Dictionary $_ -ThrowOnUnsafe
})]
$Binding,
# Additional virtual directories.
# The keys will be the name of the virtual directories, and the values will be the physical directory on
# the local machine.
[Parameter(ValueFromPipelineByPropertyName=$true)]
[Hashtable]
[ValidateScript({
Test-SafeDictionary -Dictionary $_ -ThrowOnUnsafe
})]
$VirtualDirectory,
# Additional virtual applications.
# The keys will be the name of the virtual applications, and the values will be the physical directory on
# the local machine.
[Parameter(ValueFromPipelineByPropertyName=$true)]
[Hashtable]
[ValidateScript({
Test-SafeDictionary -Dictionary $_ -ThrowOnUnsafe
})]
$VirtualApplication,
# The VMSize
[ValidateSet('ExtraSmall','Small','Medium', 'Large', 'Extra-Large', 'XS', 'XL', 'S', 'M', 'L')]
$VMSize,
# If set, will return values as a string
[switch]
$AsString
)
begin {
$xmlNamespace = @{'ServiceDefinition'='http://schemas.microsoft.com/ServiceHosting/2008/10/ServiceDefinition'}
if (-not $script:siteCount) {$script:SiteCount = 0}
}
process {
$script:siteCount++
# 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)
$selectXmlParams = @{
XPath = '//ServiceDefinition:WebRole'
Namespace = $xmlNamespace
}
$roles = @(Select-Xml -Xml $ServiceDefinition @selectXmlParams |
Select-Object -ExpandProperty Node)
if (-not $roles) {
$params = @{}
if ($vmSize) { $params['vmSize']= $vmSize}
$ServiceDefinition = $ServiceDefinition |
Add-AzureRole -RoleName "WebRole1" @params
$roles = @(Select-Xml -Xml $ServiceDefinition @selectXmlParams |
Select-Object -ExpandProperty Node)
}
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.Sites) {
$role.InnerXml += "<Sites/>"
}
$sitesNode = Select-Xml -Xml $role -Namespace $xmlNamespace -XPath '//ServiceDefinition:Sites' |
Select-Object -ExpandProperty Node
if (-not $siteName) {
if ($physicalPath) {
$SiteName = "WebSite${siteCount}"
} else {
$SiteName = "Web${siteCount}"
}
}
if ($PhysicalDirectory) {
$translatedPhysicalPath = $ExecutionContext.SessionState.Path.GetUnresolvedProviderPathFromPSPath($PhysicalDirectory)
$sitesNode.InnerXml += "<Site name='$SiteName' physicalDirectory='$translatedPhysicalPath' />"
} else {
$sitesNode.InnerXml += "<Site name='$SiteName' />"
}
$siteNode = Select-Xml -Xml $sitesNode -Namespace $xmlNamespace -XPath "//ServiceDefinition:Site"|
Where-Object { $_.Node.Name -eq $siteName } |
Select-Object -ExpandProperty Node -Last 1
if ($psBoundParameters.VirtualDirectory)
{
foreach ($kv in $psBoundParameters.VirtualDirectory.GetEnumerator()) {
$siteNode.InnerXml += " <VirtualDirectory name='$($kv.Key)' physicalDirectory='$($kv.Value)' />"
$role.Sites.InnerXml = $sitesNode.InnerXml.Replace('xmlns=""', '')
}
}
if ($psBoundParameters.VirtualApplication) {
foreach ($kv in $psBoundParameters.VirtualApplication.GetEnumerator()) {
$siteNode.InnerXml += " <VirtualApplication name='$($kv.Key)' physicalDirectory='$($kv.Value)' />"
$role.Sites.InnerXml = $sitesNode.InnerXml.Replace('xmlns=""', '')
}
}
$usedDefaultEndPoint = $false
if (-not $role.Endpoints) {
$usedDefaultEndPoint = $true
$role.InnerXml += "<Endpoints><InputEndpoint name='DefaultWebSiteEndPoint' protocol='http' port='80' />
</Endpoints>"
}
$specifiesBindingEndPoint = $false
if ((-not $psBoundParameters.Binding) -and (-not $psBoundParameters.HostHeader)) {
$endpointsNode = Select-Xml -Xml $role -Namespace $xmlNamespace -XPath "//ServiceDefinition:Endpoints"|
Select-Object -ExpandProperty Node
$endPointName = @($endPointsNode.InputEndPoint)[-1].Name
$siteNode.InnerXml += "<Bindings><Binding endpointName='$endPointName' name='Binding1' /></Bindings>"
$role.Sites.InnerXml = $sitesNode.InnerXml.Replace('xmlns=""', '')
}
if ($psBoundParameters.Binding) {
$bindings = foreach ($ht in $psBoundParameters.Binding) {
$bindingXmlText = "<Binding"
foreach ($kv in $ht.GetEnumerator()) {
if ($kv.Key -eq 'EndpointName') {
$attributeName = 'endpointName'
$specifiesBindingEndPoint = $true
} elseif ($kv.Key -eq 'Name') {
$attributeName = 'name'
} elseif ($key.Key -eq 'HostHeader') {
$attributeName = 'hostHeader'
}
if ($attributeName){
$bindingXmlText+= " $attributeName='$($kv.Value)'"
}
}
"$bindingXmlText />"
}
$ofs = [Environment]::NewLine
$siteNode.InnerXml += "<Bindings>$bindings</Bindings>"
$role.Sites.InnerXml = $sitesNode.InnerXml.Replace('xmlns=""', '')
}
if ($psBoundParameters.HostHeader) {
$endpointsNode = Select-Xml -Xml $role -Namespace $xmlNamespace -XPath "//ServiceDefinition:Endpoints"|
Select-Object -ExpandProperty Node
$endPointName = @($endPointsNode.InputEndPoint)[-1].Name
$bindingCount = 1
$bindings = foreach ($header in $psBoundParameters.HostHeader) {
"<Binding endpointName='$endPointName' name='Binding${BindingCount}' hostHeader='$header'/>"
}
$ofs = [Environment]::NewLine
if ($siteNode.InnerXml) {
$siteNode.InnerXml += "<Bindings>$bindings</Bindings>"
} else {
$siteNode.InnerXml = "<Bindings>$bindings</Bindings>"
}
$role.Sites.InnerXml = $sitesNode.InnerXml.Replace('xmlns=""', '')
}
}
end {
$webRole= Select-Xml -Xml $role -Namespace $xmlNamespace -XPath '//ServiceDefinition:WebRole' |
Select-Object -ExpandProperty Node
if ($AsString) {
$strWrite = New-Object IO.StringWriter
$serviceDefinition.Save($strWrite)
return "$strWrite"
} else {
$serviceDefinition
}
}
}