From 212431cc79d0c2d84cd5fcd175a4327df3ef0bd6 Mon Sep 17 00:00:00 2001 From: Shivam Mathur Date: Fri, 29 Jan 2021 07:52:46 -0800 Subject: [PATCH] [ubuntu] Add Apache and Nginx (#2516) * [ubuntu] Add apache and nginx (#2501) * Use dpkg-query to get nginx version * Fix flaky apache cli test --- .../SoftwareReport.Generator.ps1 | 3 ++ .../SoftwareReport.WebServers.psm1 | 41 +++++++++++++++++++ images/linux/scripts/installers/apache.sh | 14 +++++++ images/linux/scripts/installers/nginx.sh | 14 +++++++ .../linux/scripts/tests/WebServers.Tests.ps1 | 23 +++++++++++ images/linux/ubuntu1604.json | 2 + images/linux/ubuntu1804.json | 2 + images/linux/ubuntu2004.json | 2 + 8 files changed, 101 insertions(+) create mode 100644 images/linux/scripts/SoftwareReport/SoftwareReport.WebServers.psm1 create mode 100644 images/linux/scripts/installers/apache.sh create mode 100644 images/linux/scripts/installers/nginx.sh create mode 100644 images/linux/scripts/tests/WebServers.Tests.ps1 diff --git a/images/linux/scripts/SoftwareReport/SoftwareReport.Generator.ps1 b/images/linux/scripts/SoftwareReport/SoftwareReport.Generator.ps1 index b686122d2..0253016cb 100644 --- a/images/linux/scripts/SoftwareReport/SoftwareReport.Generator.ps1 +++ b/images/linux/scripts/SoftwareReport/SoftwareReport.Generator.ps1 @@ -16,6 +16,7 @@ Import-Module "$PSScriptRoot/../helpers/Common.Helpers.psm1" -DisableNameCheckin Import-Module (Join-Path $PSScriptRoot "SoftwareReport.Java.psm1") -DisableNameChecking Import-Module (Join-Path $PSScriptRoot "SoftwareReport.Rust.psm1") -DisableNameChecking Import-Module (Join-Path $PSScriptRoot "SoftwareReport.Tools.psm1") -DisableNameChecking +Import-Module (Join-Path $PSScriptRoot "SoftwareReport.WebServers.psm1") -DisableNameChecking # Restore file owner in user profile Restore-UserOwner @@ -244,6 +245,8 @@ $markdown += New-MDHeader "PowerShell Modules" -Level 4 $markdown += Get-PowerShellModules | New-MDTable $markdown += New-MDNewLine +$markdown += Build-WebServersSection + $markdown += New-MDHeader "Android" -Level 3 $markdown += Build-AndroidTable | New-MDTable $markdown += New-MDNewLine diff --git a/images/linux/scripts/SoftwareReport/SoftwareReport.WebServers.psm1 b/images/linux/scripts/SoftwareReport/SoftwareReport.WebServers.psm1 new file mode 100644 index 000000000..7cf380766 --- /dev/null +++ b/images/linux/scripts/SoftwareReport/SoftwareReport.WebServers.psm1 @@ -0,0 +1,41 @@ +function Get-ApacheVersion { + $name = "apache2" + $port = 80 + $version = bash -c "apache2 -v | grep -Po 'Apache/(\d+.){2}\d+'" | Take-OutputPart -Part 1 -Delimiter "/" + $serviceStatus = systemctl status apache2 | grep "Active:" | Take-OutputPart -Part 1 + $configFile = "/etc/apache2/apache2.conf" + return [PsCustomObject]@{ + "Name" = $name + "Version" = $version + "ConfigFile" = $configFile + "ServiceStatus" = $serviceStatus + "ListenPort" = $port + } +} + +function Get-NginxVersion { + $name = "nginx" + $port = 80 + $version = (dpkg-query --showformat='${Version}' --show nginx).Split('-')[0] + $serviceStatus = systemctl status nginx | grep "Active:" | Take-OutputPart -Part 1 + $configFile = "/etc/nginx/nginx.conf" + return [PsCustomObject]@{ + "Name" = $name + "Version" = $version + "ConfigFile" = $configFile + "ServiceStatus" = $serviceStatus + "ListenPort" = $port + } +} + +function Build-WebServersSection { + $output = "" + $output += New-MDHeader "Web Servers" -Level 3 + $output += @( + (Get-ApacheVersion), + (Get-NginxVersion) + ) | Sort-Object Name | New-MDTable + + $output += New-MDNewLine + return $output +} \ No newline at end of file diff --git a/images/linux/scripts/installers/apache.sh b/images/linux/scripts/installers/apache.sh new file mode 100644 index 000000000..fef2e555f --- /dev/null +++ b/images/linux/scripts/installers/apache.sh @@ -0,0 +1,14 @@ +#!/bin/bash -e +################################################################################ +## File: apache.sh +## Desc: Installs Apache HTTP Server +################################################################################ + +# Install Apache +apt-get install apache2 -y + +# Disable apache2.service +systemctl is-active --quiet apache2.service && systemctl stop apache2.service +systemctl disable apache2.service + +invoke_tests "WebServers" "Apache" diff --git a/images/linux/scripts/installers/nginx.sh b/images/linux/scripts/installers/nginx.sh new file mode 100644 index 000000000..48b33756c --- /dev/null +++ b/images/linux/scripts/installers/nginx.sh @@ -0,0 +1,14 @@ +#!/bin/bash -e +################################################################################ +## File: nginx.sh +## Desc: Installs Nginx +################################################################################ + +# Install Nginx +apt-get install nginx -y + +# Disable nginx.service +systemctl is-active --quiet nginx.service && systemctl stop nginx.service +systemctl disable nginx.service + +invoke_tests "WebServers" "Nginx" diff --git a/images/linux/scripts/tests/WebServers.Tests.ps1 b/images/linux/scripts/tests/WebServers.Tests.ps1 new file mode 100644 index 000000000..dfdde338f --- /dev/null +++ b/images/linux/scripts/tests/WebServers.Tests.ps1 @@ -0,0 +1,23 @@ +Describe "Apache" { + It "Apache CLI" { + "apache2 -v" | Should -ReturnZeroExitCode + } + + It "Apache Service" { + "sudo systemctl start apache2" | Should -ReturnZeroExitCode + "apachectl configtest" | Should -ReturnZeroExitCode + "sudo systemctl stop apache2" | Should -ReturnZeroExitCode + } +} + +Describe "Nginx" { + It "Nginx CLI" { + "nginx -v" | Should -ReturnZeroExitCode + } + + It "Nginx Service" { + "sudo systemctl start nginx" | Should -ReturnZeroExitCode + "sudo nginx -t" | Should -ReturnZeroExitCode + "sudo systemctl stop nginx" | Should -ReturnZeroExitCode + } +} \ No newline at end of file diff --git a/images/linux/ubuntu1604.json b/images/linux/ubuntu1604.json index 06d7c9c50..911fb6798 100644 --- a/images/linux/ubuntu1604.json +++ b/images/linux/ubuntu1604.json @@ -191,6 +191,7 @@ "{{template_dir}}/scripts/installers/azure-devops-cli.sh", "{{template_dir}}/scripts/installers/basic.sh", "{{template_dir}}/scripts/installers/aliyun-cli.sh", + "{{template_dir}}/scripts/installers/apache.sh", "{{template_dir}}/scripts/installers/aws.sh", "{{template_dir}}/scripts/installers/nvm.sh", "{{template_dir}}/scripts/installers/clang.sh", @@ -238,6 +239,7 @@ "{{template_dir}}/scripts/installers/dpkg-config.sh", "{{template_dir}}/scripts/installers/mongodb.sh", "{{template_dir}}/scripts/installers/netlify.sh", + "{{template_dir}}/scripts/installers/nginx.sh", "{{template_dir}}/scripts/installers/android.sh", "{{template_dir}}/scripts/installers/pypy.sh", "{{template_dir}}/scripts/installers/python.sh" diff --git a/images/linux/ubuntu1804.json b/images/linux/ubuntu1804.json index 31c993fca..01e9119df 100644 --- a/images/linux/ubuntu1804.json +++ b/images/linux/ubuntu1804.json @@ -194,6 +194,7 @@ "{{template_dir}}/scripts/installers/azure-devops-cli.sh", "{{template_dir}}/scripts/installers/basic.sh", "{{template_dir}}/scripts/installers/aliyun-cli.sh", + "{{template_dir}}/scripts/installers/apache.sh", "{{template_dir}}/scripts/installers/aws.sh", "{{template_dir}}/scripts/installers/clang.sh", "{{template_dir}}/scripts/installers/swift.sh", @@ -221,6 +222,7 @@ "{{template_dir}}/scripts/installers/mono.sh", "{{template_dir}}/scripts/installers/mysql.sh", "{{template_dir}}/scripts/installers/mssql-cmd-tools.sh", + "{{template_dir}}/scripts/installers/nginx.sh", "{{template_dir}}/scripts/installers/nvm.sh", "{{template_dir}}/scripts/installers/nodejs.sh", "{{template_dir}}/scripts/installers/bazel.sh", diff --git a/images/linux/ubuntu2004.json b/images/linux/ubuntu2004.json index 68b2da20c..93ae047ac 100644 --- a/images/linux/ubuntu2004.json +++ b/images/linux/ubuntu2004.json @@ -194,6 +194,7 @@ "{{template_dir}}/scripts/installers/azure-devops-cli.sh", "{{template_dir}}/scripts/installers/basic.sh", "{{template_dir}}/scripts/installers/aliyun-cli.sh", + "{{template_dir}}/scripts/installers/apache.sh", "{{template_dir}}/scripts/installers/aws.sh", "{{template_dir}}/scripts/installers/clang.sh", "{{template_dir}}/scripts/installers/swift.sh", @@ -221,6 +222,7 @@ "{{template_dir}}/scripts/installers/mono.sh", "{{template_dir}}/scripts/installers/mysql.sh", "{{template_dir}}/scripts/installers/mssql-cmd-tools.sh", + "{{template_dir}}/scripts/installers/nginx.sh", "{{template_dir}}/scripts/installers/nvm.sh", "{{template_dir}}/scripts/installers/nodejs.sh", "{{template_dir}}/scripts/installers/bazel.sh",