Skip to content

Commit

Permalink
[Windows] Unify services handling (actions#8871)
Browse files Browse the repository at this point in the history
* [Windows] Unify services handling

* Fix Set-Service usage
  • Loading branch information
vpolikarpov-akvelon authored Nov 23, 2023
1 parent 6efbc46 commit 79c3477
Show file tree
Hide file tree
Showing 8 changed files with 21 additions and 102 deletions.
8 changes: 4 additions & 4 deletions images/windows/scripts/build/Configure-System.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -105,10 +105,10 @@ $servicesToDisable = @(
'gupdate'
'gupdatem'
'StorSvc'
)

$servicesToDisable | Stop-SvcWithErrHandling
$servicesToDisable | Set-SvcWithErrHandling -Arguments @{StartupType = "Disabled"}
) | Get-Service -ErrorAction SilentlyContinue
Stop-Service $servicesToDisable
$servicesToDisable.WaitForStatus('Stopped', "00:01:00")
$servicesToDisable | Set-Service -StartupType Disabled

# Disable scheduled tasks
$allTasksInTaskPath = @(
Expand Down
6 changes: 3 additions & 3 deletions images/windows/scripts/build/Install-Apache.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,18 @@
################################################################################

# Stop w3svc service
Stop-Service -Name w3svc | Out-Null
Stop-Service -Name w3svc

# Install latest apache in chocolatey
$installDir = "C:\tools"
Install-ChocoPackage apache-httpd -ArgumentList "--force", "--params", "/installLocation:$installDir /port:80"

# Stop and disable Apache service
Stop-Service -Name Apache
Set-Service Apache -StartupType Disabled
Set-Service -Name Apache -StartupType Disabled

# Start w3svc service
Start-Service -Name w3svc | Out-Null
Start-Service -Name w3svc

# Invoke Pester Tests
Invoke-PesterTests -TestFile "Apache"
7 changes: 4 additions & 3 deletions images/windows/scripts/build/Install-Chrome.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,10 @@ Install-Binary `
Write-Host "Adding the firewall rule for Google update blocking..."
New-NetFirewallRule -DisplayName "BlockGoogleUpdate" -Direction Outbound -Action Block -Program "C:\Program Files (x86)\Google\Update\GoogleUpdate.exe"

$GoogleSvcs = ('gupdate','gupdatem')
$GoogleSvcs | Stop-SvcWithErrHandling -StopOnError
$GoogleSvcs | Set-SvcWithErrHandling -Arguments @{StartupType = "Disabled"}
$googleServices = @('gupdate', 'gupdatem') | Get-Service
Stop-Service $googleServices
$googleServices.WaitForStatus('Stopped', "00:01:00")
$googleServices | Set-Service -StartupType Disabled

$regGoogleUpdatePath = "HKLM:\SOFTWARE\Policies\Google\Update"
$regGoogleUpdateChrome = "HKLM:\SOFTWARE\Policies\Google\Chrome"
Expand Down
11 changes: 5 additions & 6 deletions images/windows/scripts/build/Install-MongoDB.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -26,17 +26,16 @@ Install-Binary `
-ExpectedSignature (Get-ToolsetContent).mongodb.signature

# Add mongodb to the PATH
$mongodbService = "mongodb"
$mongoPath = (Get-CimInstance Win32_Service -Filter "Name LIKE '$mongodbService'").PathName
$mongoPath = (Get-CimInstance Win32_Service -Filter "Name LIKE 'mongodb'").PathName
$mongoBin = Split-Path -Path $mongoPath.split('"')[1]
Add-MachinePathItem "$mongoBin"

# Wait for mongodb service running
$svc = Get-Service $mongodbService
$svc.WaitForStatus('Running','00:01:00')
$mongodbService = Get-Service "mongodb"
$mongodbService.WaitForStatus('Running', '00:01:00')

# Stop and disable mongodb service
Stop-Service -Name $mongodbService
Set-Service $mongodbService -StartupType Disabled
Stop-Service $mongodbService
$mongodbService | Set-Service -StartupType Disabled

Invoke-PesterTests -TestFile "Databases" -TestName "MongoDB"
6 changes: 3 additions & 3 deletions images/windows/scripts/build/Install-Nginx.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,18 @@
################################################################################

# Stop w3svc service
Stop-Service -Name w3svc | Out-Null
Stop-Service -Name w3svc

# Install latest nginx in chocolatey
$installDir = "C:\tools"
Install-ChocoPackage nginx -ArgumentList "--force", "--params", "/installLocation:$installDir /port:80"

# Stop and disable Nginx service
Stop-Service -Name nginx
Set-Service nginx -StartupType Disabled
Set-Service -Name nginx -StartupType Disabled

# Start w3svc service
Start-Service -Name w3svc | Out-Null
Start-Service -Name w3svc

# Invoke Pester Tests
Invoke-PesterTests -TestFile "Nginx"
4 changes: 2 additions & 2 deletions images/windows/scripts/build/Install-PostgreSQL.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ if ($exitCode -ne 0) {

# Stop and disable PostgreSQL service
$pgService = Get-Service -Name postgresql*
Stop-Service -InputObject $pgService
Set-Service -InputObject $pgService -StartupType Disabled
Stop-Service $pgService
$pgService | Set-Service -StartupType Disabled

Invoke-PesterTests -TestFile "Databases" -TestName "PostgreSQL"
2 changes: 0 additions & 2 deletions images/windows/scripts/helpers/ImageHelpers.psm1
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,6 @@ Export-ModuleMember -Function @(
'Get-ToolsetContent'
'Get-TCToolVersionPath'
'Get-TCToolPath'
'Stop-SvcWithErrHandling'
'Set-SvcWithErrHandling'
'Start-DownloadWithRetry'
'Get-VsixExtenstionFromMarketplace'
'Install-VSIXFromFile'
Expand Down
79 changes: 0 additions & 79 deletions images/windows/scripts/helpers/InstallHelpers.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -130,85 +130,6 @@ function Install-Binary {
}
}

function Stop-SvcWithErrHandling {
<#
.DESCRIPTION
Function for stopping the Windows Service with error handling
.PARAMETER ServiceName
The name of stopping service
.PARAMETER StopOnError
Switch for stopping the script and exit from PowerShell if one service is absent
#>
Param (
[Parameter(Mandatory, ValueFromPipeLine = $true)]
[string] $ServiceName,
[switch] $StopOnError
)

Process {
$service = Get-Service $ServiceName -ErrorAction SilentlyContinue
if (-not $service) {
Write-Warning "[!] Service [$ServiceName] is not found"
if ($StopOnError) {
exit 1
}
} else {
Write-Host "Try to stop service [$ServiceName]"
try {
Stop-Service -Name $ServiceName -Force
$service.WaitForStatus("Stopped", "00:01:00")
Write-Host "Service [$ServiceName] has been stopped successfuly"
} catch {
Write-Error "[!] Failed to stop service [$ServiceName] with error:"
$_ | Out-String | Write-Error
}
}
}
}

function Set-SvcWithErrHandling {
<#
.DESCRIPTION
Function for setting the Windows Service parameter with error handling
.PARAMETER ServiceName
The name of stopping service
.PARAMETER Arguments
Hashtable for service arguments
.PARAMETER StopOnError
Switch for stopping the script and exit from PowerShell if one service is absent
#>

Param (
[Parameter(Mandatory, ValueFromPipeLine = $true)]
[string] $ServiceName,
[Parameter(Mandatory)]
[hashtable] $Arguments,
[switch] $StopOnError
)

Process {
$service = Get-Service $ServiceName -ErrorAction SilentlyContinue
if (-not $service) {
Write-Warning "[!] Service [$ServiceName] is not found"
if ($StopOnError) {
exit 1
}
} else {
try {
Set-Service $serviceName @Arguments
} catch {
Write-Error "[!] Failed to set service [$ServiceName] arguments with error:"
$_ | Out-String | Write-Error
}
}
}
}

function Start-DownloadWithRetry {
Param
(
Expand Down

0 comments on commit 79c3477

Please sign in to comment.