From 80bd15e47feed7051c24c88cd29baceed4784534 Mon Sep 17 00:00:00 2001 From: Lord Hepipud Date: Thu, 31 Oct 2019 14:33:56 +0100 Subject: [PATCH] Extend setup wizard with powershell framework service --- .../Get-IcingaFrameworkServiceBinary.psm1 | 38 ++++++++++--------- .../misc/Start-IcingaAgentInstallWizard.psm1 | 25 +++++++++++- 2 files changed, 44 insertions(+), 19 deletions(-) diff --git a/lib/core/framework/Get-IcingaFrameworkServiceBinary.psm1 b/lib/core/framework/Get-IcingaFrameworkServiceBinary.psm1 index a80cc72..97c8396 100644 --- a/lib/core/framework/Get-IcingaFrameworkServiceBinary.psm1 +++ b/lib/core/framework/Get-IcingaFrameworkServiceBinary.psm1 @@ -1,37 +1,37 @@ function Get-IcingaFrameworkServiceBinary() { param( - [string]$DownloadUrl, - [string]$InstallDir + [string]$FrameworkServiceUrl, + [string]$ServiceDirectory ); $ProgressPreference = "SilentlyContinue"; - if ([string]::IsNullOrEmpty($DownloadUrl)) { + if ([string]::IsNullOrEmpty($FrameworkServiceUrl)) { if ((Get-IcingaAgentInstallerAnswerInput -Prompt 'Do you provide a custom source of the service binary?' -Default 'n').result -eq 1) { - $LatestRelease = (Invoke-WebRequest -Uri 'https://github.com/LordHepipud/icinga-windows-service/releases/latest' -UseBasicParsing).BaseResponse.ResponseUri.AbsoluteUri; - $DownloadUrl = $LatestRelease.Replace('/tag/', '/download/'); - $Tag = $DownloadUrl.Split('/')[-1]; - $DownloadUrl = [string]::Format('{0}/icinga-service-{1}.zip', $DownloadUrl, $Tag); + $LatestRelease = (Invoke-WebRequest -Uri 'https://github.com/LordHepipud/icinga-windows-service/releases/latest' -UseBasicParsing).BaseResponse.ResponseUri.AbsoluteUri; + $FrameworkServiceUrl = $LatestRelease.Replace('/tag/', '/download/'); + $Tag = $FrameworkServiceUrl.Split('/')[-1]; + $FrameworkServiceUrl = [string]::Format('{0}/icinga-service-{1}.zip', $FrameworkServiceUrl, $Tag); } else { - $DownloadUrl = (Get-IcingaAgentInstallerAnswerInput -Prompt 'Please enter the full path to your service binary repository' -Default 'v').answer; + $FrameworkServiceUrl = (Get-IcingaAgentInstallerAnswerInput -Prompt 'Please enter the full path to your service binary repository' -Default 'v').answer; } } - if ([string]::IsNullOrEmpty($InstallDir)) { - $InstallDir = (Get-IcingaAgentInstallerAnswerInput -Prompt 'Please enter the path you wish to install the service to' -Default 'v' -DefaultInput 'C:\Program Files\icinga-framework-service\').answer; + if ([string]::IsNullOrEmpty($ServiceDirectory)) { + $ServiceDirectory = (Get-IcingaAgentInstallerAnswerInput -Prompt 'Please enter the path you wish to install the service to' -Default 'v' -DefaultInput 'C:\Program Files\icinga-framework-service\').answer; } - if ((Test-Path $InstallDir) -eq $FALSE) { - New-Item -Path $InstallDir -Force -ItemType Directory | Out-Null; + if ((Test-Path $ServiceDirectory) -eq $FALSE) { + New-Item -Path $ServiceDirectory -Force -ItemType Directory | Out-Null; } - $ZipArchive = Join-Path -Path $InstallDir -ChildPath ($DownloadUrl.Split('/')[-1]); - $ServiceBin = Join-Path -Path $InstallDir -ChildPath 'icinga-service.exe'; + $ZipArchive = Join-Path -Path $ServiceDirectory -ChildPath ($FrameworkServiceUrl.Split('/')[-1]); + $ServiceBin = Join-Path -Path $ServiceDirectory -ChildPath 'icinga-service.exe'; - Invoke-WebRequest -Uri $DownloadUrl -UseBasicParsing -OutFile $ZipArchive; + Invoke-WebRequest -Uri $FrameworkServiceUrl -UseBasicParsing -OutFile $ZipArchive; - if ((Expand-IcingaZipArchive -Path $ZipArchive -Destination $InstallDir) -eq $FALSE) { + if ((Expand-IcingaZipArchive -Path $ZipArchive -Destination $ServiceDirectory) -eq $FALSE) { throw 'Failed to expand the downloaded ZIP archive'; } @@ -39,5 +39,9 @@ function Get-IcingaFrameworkServiceBinary() throw 'The checksum of the downloaded file and the required MD5 hash are not matching'; } - return $ServiceBin; + return @{ + 'FrameworkServiceUrl' = $FrameworkServiceUrl; + 'ServiceDirectory' = $ServiceDirectory; + 'ServiceBin' = $ServiceBin; + }; } diff --git a/lib/core/icingaagent/misc/Start-IcingaAgentInstallWizard.psm1 b/lib/core/icingaagent/misc/Start-IcingaAgentInstallWizard.psm1 index 40f6c08..dc9cc37 100644 --- a/lib/core/icingaagent/misc/Start-IcingaAgentInstallWizard.psm1 +++ b/lib/core/icingaagent/misc/Start-IcingaAgentInstallWizard.psm1 @@ -24,7 +24,11 @@ function Start-IcingaAgentInstallWizard() [switch]$RunInstaller, [switch]$Reconfigure, [string]$ServiceUser, - [securestring]$ServicePass = $null + [securestring]$ServicePass = $null, + $InstallFrameworkService = $null, + $FrameworkServiceUrl = $null, + $ServiceDirectory = $null, + $ServiceBin = $null ); [array]$InstallerArguments = @(); @@ -249,6 +253,20 @@ function Start-IcingaAgentInstallWizard() } } + if ($null -eq $InstallFrameworkService) { + if ((Get-IcingaAgentInstallerAnswerInput -Prompt 'Do you want to install the PowerShell Framework as Service?' -Default 'y').result -eq 1) { + $result = Get-IcingaFrameworkServiceBinary; + $InstallerArguments += "-InstallFrameworkService 1"; + $InstallerArguments += [string]::Format("-FrameworkServiceUrl '{0}'", $result.FrameworkServiceUrl); + $InstallerArguments += [string]::Format("-ServiceDirectory '{0}'", $result.ServiceDirectory); + $InstallerArguments += [string]::Format("-ServiceBin '{0}'", $result.ServiceBin); + $ServiceBin = $result.ServiceBin; + } + } elseif ($InstallFrameworkService -eq $TRUE) { + $result = Get-IcingaFrameworkServiceBinary -FrameworkServiceUrl $FrameworkServiceUrl -ServiceDirectory $ServiceDirectory; + $ServiceBin = $result.ServiceBin; + } + if ($InstallerArguments.Count -ne 0) { $InstallerArguments += "-RunInstaller"; Write-Host 'The wizard is complete. These are the configured settings:'; @@ -276,12 +294,15 @@ function Start-IcingaAgentInstallWizard() Set-IcingaAcl "$Env:ProgramData\icinga2\etc"; Set-IcingaAcl "$Env:ProgramData\icinga2\var"; Set-IcingaAcl (Get-IcingaCacheDir); + Install-IcingaFrameworkService -Path $ServiceBin -User $ServiceUser -Password $ServicePass | Out-Null; + Register-IcingaBackgroundDaemon -Command 'Start-IcingaServiceCheckDaemon'; Install-IcingaAgentBaseFeatures; Install-IcingaAgentCertificates -Hostname $Hostname -Endpoint $CAEndpoint -Port $CAPort -CACert $CAFile -Ticket $Ticket | Out-Null; Write-IcingaAgentApiConfig -Port $CAPort; Write-IcingaAgentZonesConfig -Endpoints $Endpoints -EndpointConnections $EndpointConnections -ParentZone $ParentZone -GlobalZones $GlobalZoneConfig -Hostname $Hostname; Test-IcingaAgent; - Restart-Service icinga2; + Restart-IcingaService 'icingapowershell'; + Restart-IcingaService 'icinga2'; } } }