From 349c66a268a41cb16fd8db7b4c52e1ce63eb318c Mon Sep 17 00:00:00 2001 From: Shivam Mathur Date: Mon, 1 Feb 2021 06:03:49 -0800 Subject: [PATCH] [Windows] Add Apache and Nginx (#2517) * [Windows] Add Apache and Nginx (#2501) * Remove dot-sourcing in web-server tests * Fix web servers readme generation --- .../win/scripts/Installers/Install-Apache.ps1 | 21 ++++++ .../win/scripts/Installers/Install-Nginx.ps1 | 21 ++++++ .../SoftwareReport.Generator.ps1 | 3 + .../SoftwareReport.WebServers.psm1 | 65 +++++++++++++++++++ images/win/scripts/Tests/Apache.Tests.ps1 | 25 +++++++ images/win/scripts/Tests/Nginx.Tests.ps1 | 25 +++++++ images/win/windows2016.json | 4 +- images/win/windows2019.json | 4 +- 8 files changed, 166 insertions(+), 2 deletions(-) create mode 100644 images/win/scripts/Installers/Install-Apache.ps1 create mode 100644 images/win/scripts/Installers/Install-Nginx.ps1 create mode 100644 images/win/scripts/SoftwareReport/SoftwareReport.WebServers.psm1 create mode 100644 images/win/scripts/Tests/Apache.Tests.ps1 create mode 100644 images/win/scripts/Tests/Nginx.Tests.ps1 diff --git a/images/win/scripts/Installers/Install-Apache.ps1 b/images/win/scripts/Installers/Install-Apache.ps1 new file mode 100644 index 000000000..0bcc7a5ee --- /dev/null +++ b/images/win/scripts/Installers/Install-Apache.ps1 @@ -0,0 +1,21 @@ +################################################################################ +## File: Install-Apache.ps1 +## Desc: Install Apache HTTP Server +################################################################################ + +# Stop w3svc service +Stop-Service -Name w3svc | Out-Null + +# Install latest apache in chocolatey +$installDir = "C:\tools" +Choco-Install -PackageName apache-httpd -ArgumentList "--force", "--params", "/installLocation:$installDir /port:80" + +# Stop and disable Apache service +Stop-Service -Name Apache +Set-Service Apache -StartupType Disabled + +# Start w3svc service +Start-Service -Name w3svc | Out-Null + +# Invoke Pester Tests +Invoke-PesterTests -TestFile "Apache" diff --git a/images/win/scripts/Installers/Install-Nginx.ps1 b/images/win/scripts/Installers/Install-Nginx.ps1 new file mode 100644 index 000000000..b0889305a --- /dev/null +++ b/images/win/scripts/Installers/Install-Nginx.ps1 @@ -0,0 +1,21 @@ +################################################################################ +## File: Install-Nginx.ps1 +## Desc: Install Nginx +################################################################################ + +# Stop w3svc service +Stop-Service -Name w3svc | Out-Null + +# Install latest nginx in chocolatey +$installDir = "C:\tools" +Choco-Install -PackageName nginx -ArgumentList "--force", "--params", "/installLocation:$installDir /port:80" + +# Stop and disable Nginx service +Stop-Service -Name nginx +Set-Service nginx -StartupType Disabled + +# Start w3svc service +Start-Service -Name w3svc | Out-Null + +# Invoke Pester Tests +Invoke-PesterTests -TestFile "Nginx" diff --git a/images/win/scripts/SoftwareReport/SoftwareReport.Generator.ps1 b/images/win/scripts/SoftwareReport/SoftwareReport.Generator.ps1 index 3203c5bf9..07d74b378 100644 --- a/images/win/scripts/SoftwareReport/SoftwareReport.Generator.ps1 +++ b/images/win/scripts/SoftwareReport/SoftwareReport.Generator.ps1 @@ -7,6 +7,7 @@ Import-Module (Join-Path $PSScriptRoot "SoftwareReport.Databases.psm1") -Disable Import-Module (Join-Path $PSScriptRoot "SoftwareReport.Helpers.psm1") -DisableNameChecking Import-Module (Join-Path $PSScriptRoot "SoftwareReport.Tools.psm1") -DisableNameChecking Import-Module (Join-Path $PSScriptRoot "SoftwareReport.Java.psm1") -DisableNameChecking +Import-Module (Join-Path $PSScriptRoot "SoftwareReport.WebServers.psm1") -DisableNameChecking Import-Module (Join-Path $PSScriptRoot "SoftwareReport.VisualStudio.psm1") -DisableNameChecking $markdown = "" @@ -195,6 +196,8 @@ $markdown += New-MDList -Style Unordered -Lines (@( ) $markdown += New-MDNewLine +$markdown += Build-WebServersSection + $vs = Get-VisualStudioVersion $markdown += New-MDHeader "$($vs.Name)" -Level 3 $markdown += $vs | New-MDTable diff --git a/images/win/scripts/SoftwareReport/SoftwareReport.WebServers.psm1 b/images/win/scripts/SoftwareReport/SoftwareReport.WebServers.psm1 new file mode 100644 index 000000000..2d09c87e4 --- /dev/null +++ b/images/win/scripts/SoftwareReport/SoftwareReport.WebServers.psm1 @@ -0,0 +1,65 @@ +function Get-ApachePath { + return Join-Path "C:\tools\" (Get-Item C:\tools\apache*).Name +} + +function Get-NginxPath { + return Join-Path "C:\tools\" (Get-Item C:\tools\nginx*).Name +} + +function Get-ApacheVersion { + $apacheBinPath = Join-Path (Get-ApachePath) "\bin\httpd" + (. $apacheBinPath -V | Select-String -Pattern "Apache/") -match "Apache/(?\d+\.\d+\.\d+)" | Out-Null + return $Matches.Version +} + +function Get-NginxVersion { + $nginxBinPath = Join-Path (Get-NginxPath) "\nginx" + (. $nginxBinPath -v 2>&1 | Select-String -Pattern "nginx/") -match "nginx/(?\d+\.\d+\.\d+)" | Out-Null + return $Matches.Version +} + +function Get-ApacheMarkdown +{ + $name = "Apache" + $apachePort = "80" + $apacheService = Get-Service -Name $name + $apacheVersion = Get-ApacheVersion + $apacheConfigFile = Join-Path (Get-ApachePath) "\conf\httpd.conf" + return [PSCustomObject]@{ + Name = $name + Version = $apacheVersion + ConfigFile = $apacheConfigFile + ServiceName = $apacheService.Name + ServiceStatus = $apacheService.Status + ListenPort = $apachePort + } +} + +function Get-NginxMarkdown +{ + $name = "Nginx" + $nginxPort = "80" + $nginxService = Get-Service -Name $name + $nginxVersion = Get-NginxVersion + $nginxConfigFile = Join-Path (Get-NginxPath) "\conf\nginx.conf" + return [PSCustomObject]@{ + Name = $name + Version = $nginxVersion + ConfigFile = $nginxConfigFile + ServiceName = $nginxService.Name + ServiceStatus = $nginxService.Status + ListenPort = $nginxPort + } +} + +function Build-WebServersSection { + $output = "" + $output += New-MDHeader "Web Servers" -Level 3 + $output += @( + (Get-ApacheMarkdown), + (Get-NginxMarkdown) + ) | Sort-Object Name | New-MDTable + + $output += New-MDNewLine + return $output +} \ No newline at end of file diff --git a/images/win/scripts/Tests/Apache.Tests.ps1 b/images/win/scripts/Tests/Apache.Tests.ps1 new file mode 100644 index 000000000..b8efe9cf7 --- /dev/null +++ b/images/win/scripts/Tests/Apache.Tests.ps1 @@ -0,0 +1,25 @@ +Describe "Apache" { + Context "Path" { + It "Apache" { + $apachePath = Join-Path (Join-Path "C:\tools\" (Get-Item C:\tools\apache*).Name) "\bin\httpd" + "$apachePath -V" | Should -ReturnZeroExitCode + } + } + + Context "Service" { + $apacheService = Get-Service -Name Apache + $apacheServiceTests = @{ + Name = $apacheService.Name + Status = $apacheService.Status + StartType = $apacheService.StartType + } + + It " service is stopped" -TestCases $apacheServiceTests { + $Status | Should -Be "Stopped" + } + + It " service is disabled" -TestCases $apacheServiceTests { + $StartType | Should -Be "Disabled" + } + } +} diff --git a/images/win/scripts/Tests/Nginx.Tests.ps1 b/images/win/scripts/Tests/Nginx.Tests.ps1 new file mode 100644 index 000000000..e1e7b6f6c --- /dev/null +++ b/images/win/scripts/Tests/Nginx.Tests.ps1 @@ -0,0 +1,25 @@ +Describe "Nginx" { + Context "Path" { + It "Nginx" { + $nginxPath = Join-Path (Join-Path "C:\tools\" (Get-Item C:\tools\nginx*).Name) "\nginx" + "$nginxPath -v" | Should -ReturnZeroExitCode + } + } + + Context "Service" { + $nginxService = Get-Service -Name nginx + $nginxServiceTests = @{ + Name = $nginxService.Name + Status = $nginxService.Status + StartType = $nginxService.StartType + } + + It " service is stopped" -TestCases $nginxServiceTests { + $Status | Should -Be "Stopped" + } + + It " service is disabled" -TestCases $nginxServiceTests { + $StartType | Should -Be "Disabled" + } + } +} diff --git a/images/win/windows2016.json b/images/win/windows2016.json index 3db5bd6fd..98484d2c8 100644 --- a/images/win/windows2016.json +++ b/images/win/windows2016.json @@ -246,7 +246,9 @@ "{{ template_dir }}/scripts/Installers/Install-Edge.ps1", "{{ template_dir }}/scripts/Installers/Install-Firefox.ps1", "{{ template_dir }}/scripts/Installers/Install-Selenium.ps1", - "{{ template_dir }}/scripts/Installers/Install-IEWebDriver.ps1" + "{{ template_dir }}/scripts/Installers/Install-IEWebDriver.ps1", + "{{ template_dir }}/scripts/Installers/Install-Apache.ps1", + "{{ template_dir }}/scripts/Installers/Install-Nginx.ps1" ] }, { diff --git a/images/win/windows2019.json b/images/win/windows2019.json index a4df7fa20..7c48cb86b 100644 --- a/images/win/windows2019.json +++ b/images/win/windows2019.json @@ -250,7 +250,9 @@ "{{ template_dir }}/scripts/Installers/Install-Edge.ps1", "{{ template_dir }}/scripts/Installers/Install-Firefox.ps1", "{{ template_dir }}/scripts/Installers/Install-Selenium.ps1", - "{{ template_dir }}/scripts/Installers/Install-IEWebDriver.ps1" + "{{ template_dir }}/scripts/Installers/Install-IEWebDriver.ps1", + "{{ template_dir }}/scripts/Installers/Install-Apache.ps1", + "{{ template_dir }}/scripts/Installers/Install-Nginx.ps1" ] }, {