Skip to content

Commit

Permalink
Fix issues in IIS Windows server installer. Persist additional files …
Browse files Browse the repository at this point in the history
…through installs when extracting ZIP contents.
  • Loading branch information
bitbound committed Apr 26, 2021
1 parent 04d6b0e commit a5be1ca
Show file tree
Hide file tree
Showing 4 changed files with 74 additions and 556 deletions.
1 change: 0 additions & 1 deletion Remotely.sln
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Utilities", "Utilities", "{
Utilities\Example_Nginx_Config.txt = Utilities\Example_Nginx_Config.txt
Utilities\Get-PSCommands.ps1 = Utilities\Get-PSCommands.ps1
Utilities\Get-WindowsCommands.ps1 = Utilities\Get-WindowsCommands.ps1
Utilities\Install-RemotelyServer.ps1 = Utilities\Install-RemotelyServer.ps1
Utilities\Publish.ps1 = Utilities\Publish.ps1
Utilities\signtool.exe = Utilities\signtool.exe
EndProjectSection
Expand Down
208 changes: 58 additions & 150 deletions Server.Installer/Resources/IIS_Windows_Install.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@
Copyright © 2020 Translucency Software. All rights reserved.
#>
param (
# The host name (excluding scheme) for the server that will run Remotely.
[Parameter(Mandatory=$True)]
[string]$HostName,
# The name to use for the IIS Application Pool for the Remotely site.
[Parameter(Mandatory=$True)]
[string]$AppPoolName,
Expand All @@ -24,22 +27,17 @@ param (
[string]$EmailAddress
)

[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12

$ErrorActionPreference = "Stop"
$Host.UI.RawUI.WindowTitle = "Remotely Setup"
Clear-Host

#region Variables
$ServerCmdlets = $false
$FirewallSet = $false
$CopyErrors = $false
if ($PSScriptRoot -eq ""){
$PSScriptRoot = (Get-Location)
}

$Root = (Get-Item -Path $PSScriptRoot).Parent.FullName
$BindingHostname = ""
#endregion

#region Functions
Expand Down Expand Up @@ -137,13 +135,7 @@ if ($OS.Name.ToLower().Contains("home") -or $OS.Caption.ToLower().Contains("home
Do-Pause
return
}
### Test if Windows Feature cmdlets are available. ###
if ((Get-Command -Name "Add-WindowsFeature" -ErrorAction Ignore) -eq $null) {
$ServerCmdlets = $false
}
else {
$ServerCmdlets = $true
}

### Check if PostgreSQL is installed. ###
##if ((Get-Package -Name "*PostgreSQL*" -ErrorAction SilentlyContinue) -eq $null){
## Wrap-Host
Expand All @@ -153,50 +145,8 @@ else {
## return
##}

$HostingPackage = Get-Package -Name "*.NET*Windows Server Hosting*" -ErrorAction SilentlyContinue
$Result = Invoke-WebRequest -Uri "https://dotnet.microsoft.com/download/dotnet/current/runtime" -UseBasicParsing
$BundleLink = $Result.Links | Where-Object {$_.outerHTML -like "*Download Hosting Bundle*"}
$Result = Invoke-WebRequest -Uri ("https://dotnet.microsoft.com$($BundleLink.href)") -UseBasicParsing
$DownloadLink = $Result.Links | Where-Object {$_.outerHTML -like "*Click here to download manually*"}
$Parts = $DownloadLink.href.Split("-")
$RemoteVersionString = $Parts[$Parts.Length - 2]

if ($HostingPackage -ne $null) {
$LocalVersion = [System.Version]::Parse($HostingPackage.Version)

}
$RemoteVersion = [System.Version]::Parse($RemoteVersionString)

if ($HostingPackage -eq $null -or $RemoteVersion -gt $LocalVersion) {
Wrap-Host "Downloading .NET Core Runtime and Hosting Bundle..."
$ProgressPreference = "SilentlyContinue"
Invoke-WebRequest -Uri $DownloadLink.href -OutFile "$env:TEMP\dotnet-hosting-win.exe" -UseBasicParsing
$ProgressPreference = "Continue"
Start-Process -FilePath "$env:TEMP\dotnet-hosting-win.exe" -ArgumentList "/install /quiet /norestart" -Wait
Wrap-Host
Wrap-Host ".NET Runtime installation completed."
}
else {
Wrap-Host ".NET Hosting Bundle is up-to-date."
}

#endregion

### Hosting Requirements ###
Wrap-Host
Wrap-Host "**********************************"
Wrap-Host " IMPORTANT" -ForegroundColor Green
Wrap-Host "**********************************"
Wrap-Host
Wrap-Host "You must have the .NET Core Runtime installed, which includes the IIS hosting module. The SDK doesn't have this."
Wrap-Host
Wrap-Host "If you have not already done so, please download and install it from the following link:"
Wrap-Host
Wrap-Host "https://dotnet.microsoft.com/download"
Wrap-Host
Do-Pause
Clear-Host


### Intro ###
Clear-Host
Expand All @@ -214,78 +164,50 @@ Clear-Host


### Automatic IIS Setup ###
if ($ServerCmdlets) {
$RebootRequired = $false
Wrap-Host
Wrap-Host "Installing IIS components..." -ForegroundColor Green
Write-Progress -Activity "IIS Component Installation" -Status "Installing web server" -PercentComplete (1/7*100)
$Result = Add-WindowsFeature Web-Server
if ($Result.RestartNeeded -like "Yes")
{
$RebootRequired = $true
}
#Write-Progress -Activity "IIS Component Installation" -Status "Installing ASP.NET" -PercentComplete (2/7*100)
#$Result = Add-WindowsFeature Web-Asp-Net
#if ($Result.RestartNeeded -like "Yes")
#{
# $RebootRequired = $true
#}
#Write-Progress -Activity "IIS Component Installation" -Status "Installing ASP.NET 4.5" -PercentComplete (3/7*100)
#$Result = Add-WindowsFeature Web-Asp-Net45
#if ($Result.RestartNeeded -like "Yes")
#{
# $RebootRequired = $true
#}
#Write-Progress -Activity "IIS Component Installation" -Status "Installing web sockets" -PercentComplete (4/7*100)
#$Result = Add-WindowsFeature Web-WebSockets
#if ($Result.RestartNeeded -like "Yes")
#{
# $RebootRequired = $true
#}
Write-Progress -Activity "IIS Component Installation" -Status "Installing IIS management tools" -PercentComplete (5/7*100)
$Result = Add-WindowsFeature Web-Mgmt-Tools
if ($Result.RestartNeeded -like "Yes")
{
$RebootRequired = $true
}
#Write-Progress -Activity "IIS Component Installation" -Status "Installing web filtering" -PercentComplete (6/7*100)
#$Result = Add-WindowsFeature Web-Filtering
#if ($Result.RestartNeeded -like "Yes")
#{
# $RebootRequired = $true
#}

Write-Progress -Activity "IIS Component Installation" -Status "IIS setup completed" -PercentComplete (7/7*100) -Completed
Start-Sleep 2
$RebootRequired = $false
Wrap-Host
Wrap-Host "Installing IIS components..." -ForegroundColor Green
Write-Progress -Activity "IIS Component Installation" -Status "Installing web server" -PercentComplete (1/7*100)
$Result = Add-WindowsFeature Web-Server
if ($Result.RestartNeeded -like "Yes")
{
$RebootRequired = $true
}
else
#Write-Progress -Activity "IIS Component Installation" -Status "Installing ASP.NET" -PercentComplete (2/7*100)
#$Result = Add-WindowsFeature Web-Asp-Net
#if ($Result.RestartNeeded -like "Yes")
#{
# $RebootRequired = $true
#}
#Write-Progress -Activity "IIS Component Installation" -Status "Installing ASP.NET 4.5" -PercentComplete (3/7*100)
#$Result = Add-WindowsFeature Web-Asp-Net45
#if ($Result.RestartNeeded -like "Yes")
#{
# $RebootRequired = $true
#}
Write-Progress -Activity "IIS Component Installation" -Status "Installing web sockets" -PercentComplete (4/7*100)
$Result = Add-WindowsFeature Web-WebSockets
if ($Result.RestartNeeded -like "Yes")
{
Wrap-Host
Wrap-Host "Installing IIS components..." -ForegroundColor Green
Write-Progress -Activity "IIS Component Installation" -Status "Installing web server" -PercentComplete (1/7*100)
DISM /Online /Enable-Feature /FeatureName:IIS-WebServer /All /Quiet

#Write-Progress -Activity "IIS Component Installation" -Status "Installing ASP.NET" -PercentComplete (2/7*100)
#DISM /Online /Enable-Feature /FeatureName:IIS-ASPNET /All /Quiet

#Write-Progress -Activity "IIS Component Installation" -Status "Installing ASP.NET 4.5" -PercentComplete (3/7*100)
#DISM /Online /Enable-Feature /FeatureName:IIS-ASPNET45 /All /Quiet

#Write-Progress -Activity "IIS Component Installation" -Status "Installing web sockets" -PercentComplete (4/7*100)
#DISM /Online /Enable-Feature /FeatureName:IIS-WebSockets /All /Quiet

Write-Progress -Activity "IIS Component Installation" -Status "Installing IIS management tools" -PercentComplete (5/7*100)
DISM /Online /Enable-Feature /FeatureName:IIS-ManagementConsole /All /Quiet

#Write-Progress -Activity "IIS Component Installation" -Status "Installing web filtering" -PercentComplete (6/7*100)
#DISM /Online /Enable-Feature /FeatureName:IIS-RequestFiltering /All /Quiet

Write-Progress -Activity "IIS Component Installation" -Status "IIS setup completed" -PercentComplete (7/7*100) -Completed
Start-Sleep 2

$RebootRequired = $true
}
Write-Progress -Activity "IIS Component Installation" -Status "Installing IIS management tools" -PercentComplete (5/7*100)
$Result = Add-WindowsFeature Web-Mgmt-Tools
if ($Result.RestartNeeded -like "Yes")
{
$RebootRequired = $true
}
Write-Progress -Activity "IIS Component Installation" -Status "Installing web filtering" -PercentComplete (6/7*100)
$Result = Add-WindowsFeature Web-Filtering
if ($Result.RestartNeeded -like "Yes")
{
$RebootRequired = $true
}

Write-Progress -Activity "IIS Component Installation" -Status "IIS setup completed" -PercentComplete (7/7*100) -Completed
Start-Sleep 2
Clear-Host

### Create IIS Site ##

if ((Get-IISAppPool -Name $AppPoolName) -eq $null) {
Expand All @@ -295,37 +217,10 @@ if ((Get-IISAppPool -Name $AppPoolName) -eq $null) {
}

if ((Get-Website -Name $SiteName) -eq $null) {
New-Website -Name $SiteName -PhysicalPath $SitePath -HostHeader $BindingHostname -ApplicationPool $AppPoolName
New-Website -Name $SiteName -PhysicalPath $SitePath -HostHeader $HostName -ApplicationPool $AppPoolName
}


Wrap-Host
Wrap-Host "This will DELETE ALL FILES in the selected website and install Remotely Server. If this is not your intention, close this window now and create a new website where Remotely Server will be installed." -ForegroundColor Red
Wrap-Host
Do-Pause

# Stop site.
Clear-Host
Wrap-Host
Wrap-Host "Stopping website..." -ForegroundColor Green
Stop-Website -Name $SiteName


### File Cleanup ###
Wrap-Host
Wrap-Host "Cleaning up existing files..." -ForegroundColor Green
$Success = $false

while ($Success -eq $false) {
try {
Get-ChildItem -Path "$SitePath" -Recurse | Remove-Item -Force -Recurse
$Success = $true
}
catch {
Start-Sleep -Seconds 1
}
}

### Set ACL on website folders and files ###
Wrap-Host
Wrap-Host "Setting ACLs..." -ForegroundColor Green
Expand Down Expand Up @@ -360,6 +255,7 @@ catch
}

# Start website.
Start-WebAppPool -Name $AppPoolName
Start-Website -Name $SiteName


Expand All @@ -377,7 +273,19 @@ Wrap-Host "**********************************"
Wrap-Host " Server setup complete!" -ForegroundColor Green
Wrap-Host "**********************************"
Wrap-Host
Wrap-Host "If a path to Win-Acme exe path (WacsPath) wasn't provided, SSL/TLS needs to be set up in IIS. I recommend checking out Let's Encrypt for free, automated SSL certificates." -ForegroundColor Green
Wrap-Host
Wrap-Host
Wrap-Host "**********************************"
Wrap-Host " IMPORTANT FOLLOW-UP STEPS" -ForegroundColor Yellow
Wrap-Host "**********************************"
Wrap-Host
Wrap-Host "If you haven't already, you must install the latest .NET Hosting Bundle for IIS."
Wrap-Host "Please download and install it from the following link. Click 'Download .NET Runtime', then 'Download Hosting Bundle'."
Wrap-Host
Wrap-Host "https://dotnet.microsoft.com/download"
Wrap-Host
Wrap-Host
Wrap-Host "You should also install a TLS certificate for your site, if you haven't already. I recommend checking out Let's Encrypt for free, automated SSL certificates." -ForegroundColor Green

if ($RebootRequired) {
Wrap-Host
Expand Down
25 changes: 16 additions & 9 deletions Server.Installer/Services/ServerInstaller.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public ServerInstaller(IGitHubApi githubApi)

public async Task PerformInstall(CliParams cliParams)
{
var filePath = Path.Combine(Path.GetTempPath(), "Remotely_Server.zip");
var zipPath = Path.Combine(Path.GetTempPath(), "Remotely_Server.zip");

if (cliParams.UsePrebuiltPackage == true)
{
Expand All @@ -52,7 +52,7 @@ public async Task PerformInstall(CliParams cliParams)
ConsoleHelper.WriteLine($"Progress: {progress}%");
}
};
await webClient.DownloadFileTaskAsync(releaseFile, filePath);
await webClient.DownloadFileTaskAsync(releaseFile, zipPath);
}
else
{
Expand Down Expand Up @@ -86,7 +86,7 @@ public async Task PerformInstall(CliParams cliParams)
return;
}

var downloadResult = await _githubApi.DownloadArtifact(cliParams, latestBuild.archive_download_url, filePath);
var downloadResult = await _githubApi.DownloadArtifact(cliParams, latestBuild.archive_download_url, zipPath);

if (!downloadResult)
{
Expand All @@ -96,18 +96,25 @@ public async Task PerformInstall(CliParams cliParams)

}


ConsoleHelper.WriteLine("Extracting files.");
if (Directory.Exists(cliParams.InstallDirectory))
// Files in use can't be overwritten in Windows. Stop the
// website process first.
if (cliParams.WebServer == WebServerType.IisWindows)
{
Directory.Delete(cliParams.InstallDirectory, true);
var initialW3wpCount = Process.GetProcessesByName("w3wp").Length;
Process.Start("powershell.exe", "-Command & \"{ Stop-WebAppPool -Name Remotely -ErrorAction SilentlyContinue }\"").WaitForExit();
Process.Start("powershell.exe", "-Command & \"{ Stop-Website -Name Remotely -ErrorAction SilentlyContinue }\"").WaitForExit();
ConsoleHelper.WriteLine("Waiting for w3wp processes to close...");
TaskHelper.DelayUntil(() => Process.GetProcessesByName("w3wp").Length < initialW3wpCount, TimeSpan.FromMinutes(5), 100);
}

ConsoleHelper.WriteLine("Extracting files.");
Directory.CreateDirectory(cliParams.InstallDirectory);
ZipFile.ExtractToDirectory(filePath, cliParams.InstallDirectory);
ZipFile.ExtractToDirectory(zipPath, cliParams.InstallDirectory, true);

await LaunchExternalInstaller(cliParams);
}


private async Task LaunchExternalInstaller(CliParams cliParams)
{
ConsoleHelper.WriteLine("Launching install script for selected reverse proxy type.");
Expand Down Expand Up @@ -140,7 +147,7 @@ private async Task LaunchExternalInstaller(CliParams cliParams)
psi = new ProcessStartInfo("powershell.exe")
{
Arguments = $"-f \"{filePath}\" -AppPoolName Remotely -SiteName Remotely " +
$"-SitePath \"{cliParams.InstallDirectory}\" -Quiet",
$"-SitePath \"{cliParams.InstallDirectory}\" -HostName {cliParams.ServerUrl.Authority} -Quiet",
WorkingDirectory = cliParams.InstallDirectory
};
}
Expand Down

0 comments on commit a5be1ca

Please sign in to comment.