-
Notifications
You must be signed in to change notification settings - Fork 2
/
IISSiteWithServerFarm.install.ps1
98 lines (79 loc) · 3.99 KB
/
IISSiteWithServerFarm.install.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
# SETUP
# a short guid is generated and used to uniquely identify the new release, could also be the current version number
$ShortGUID = Get-ShortGUID
# Define other variables used later
$PackageName = "Sample.Package"
$InstPath = $ENV:InstPath + "\$PackageName.$ShortGUID"
$IisRoot = $InstPath + "\bin"
$ProductZip = $PSScriptRoot + "\$PackageName.zip"
$ServerFarmName = "$PackageName.ServerFarm"
# get more variables depending on the enviroment
switch ($ENV:Environment) {
"Development" {
$SiteName = "$ENV:Environment.$PackageName"
$AppPoolName = $SiteName
$Url = "dev.sample.package.com"
$User = ".\MyUser"
$Password = "MyPassword"
}
"Staging" {
$SiteName = "$ENV:Environment.$PackageName"
$AppPoolName = $SiteName
$Url = "test.sample.package.com"
$User = "MyUser"
$Password = "MyPassword"
}
"Production" {
$SiteName = "$PackageName"
$AppPoolName = $SiteName
$Url = "sample.package.com"
$User = ".\MyUser"
$Password = "MyPassword"
}
"Local" {
$SiteName = "$PackageName"
$AppPoolName = $SiteName
$Url = "sample.package.com"
$User = ".\MyUser"
$Password = "MyPassword"
}
default {
Write-Output "Installation of $PackageName not possible environment is unkown"
exit
}
}
# add a default website which listens to the main url and port normally www.example.com and 80
New-IISWebsite -name $SiteName -root $IisRoot -bindings @{protocol='http';bindingInformation="*:80:$Url"}
# append the guid to the variables for the next steps
$AppPoolName = "$AppPoolName.$ShortGUID"
$SiteName = "$SiteName.$ShortGUID"
$UrlWithGuid = "$ShortGUID.$Url"
# We get the next free port from Windows under which we will host the new release
$Port = Get-FreePortFromRange (8100..8199)
# make sure the directory where our solution should be installed exists
If(!(test-path $InstPath))
{
New-Item -ItemType Directory -Force -Path $InstPath
}
# Unzip the file containing the artifact
Expand-Archive -Path $ProductZip -destinationpath $InstPath
# Install the IISSite and AppPool for the new release
New-IISApplicationPool -Name $AppPoolName -StartMode "AlwaysRunning" -IdentityType 4
New-IISWebsite -name $SiteName -root $IisRoot -appPool $AppPoolName
Use-WebAdministration
Remove-WebBinding -Name $SiteName -Protocol "http" -Port 80 -IPAddress "*"
# do not add a binding to an url to the site
New-WebBinding -Name $SiteName -Protocol "http" -Port $Port -IPAddress "*" -HostHeader ""
Add-WebConfiguration -Value @{Name="ASPNETCORE_ENVIRONMENT"; Value=$ENV:Environment} -Filter system.webServer/aspNetCore/environmentVariables -PSPath "MACHINE/WEBROOT/APPHOST" -Location "$SiteName"
Start-Website -Name $SiteName
# We set a global URL Rewrite from our URL to the Server Farm which then deals with all requests, the URL for a server farm is http://SERVERFARMNAME or https://SERVERFARMNAME
Add-IISGlobalRouting -RuleName "Rewrite $($Url) to ServerFarm $($ServerFarmName)" -MatchUrl ".*" -Conditions @(@{"input"="{HTTP_HOST}"; "pattern"="^$Url$"},@{"input"="{SERVER_PORT}"; "pattern"="^80$"}) -Action @{"type"="Rewrite";url="http://$ServerFarmName/{R:0}"} -StopProcessing $false
# Create Server Farm will only create a new one if it does not exist so we can run it every time
Create-IISServerFarm -ServerFarmName $ServerFarmName
Set-IISServerFarmhealthCheck -ServerFarmName $ServerFarmName -Url "http://$Url/up.html" -ResponseMatch "up"
# Now we add a new enty pointing to 127.0.0.1 to the local hosts file of windows so that we do not need to worry
# about network wide dns resolution
Add-WindowsHostsRecord -DnsName $UrlWithGuid -IPAddress "127.0.0.1"
Add-IISServerToServerFarm -ServerFarmName $ServerFarmName -ServerAddress $UrlWithGuid -HttpPort $Port
Set-IISServerFarmServerAvailability -ServerFarmName $ServerFarmName -Filter "*.$($Url)" -Exclude $UrlWithGuid -Availability 1
#Todo: disable / remove old servers / releases