diff --git a/doc/installation/01-KickstartScript.md b/doc/installation/01-KickstartScript.md index 7c76ec5..b3e347f 100644 --- a/doc/installation/01-KickstartScript.md +++ b/doc/installation/01-KickstartScript.md @@ -12,7 +12,10 @@ Getting Started [Net.ServicePointManager]::SecurityProtocol = "tls12, tls11"; $ProgressPreference = "SilentlyContinue"; -$Script = (Invoke-WebRequest -UseBasicParsing -Uri 'https://raw.githubusercontent.com/LordHepipud/icinga-framework-kickstart/master/script/icinga-framework-kickstart.ps1').Content; +$global:IcingaFrameworkKickstartSource = 'https://raw.githubusercontent.com/LordHepipud/icinga-framework-kickstart/master/script/icinga-framework-kickstart.ps1'; + +$Script = (Invoke-WebRequest -UseBasicParsing -Uri $global:IcingaFrameworkKickstartSource).Content; +$Script += "`r`n`r`n Start-IcingaFrameworkWizard;"; Invoke-Command -ScriptBlock ([Scriptblock]::Create($Script)); ``` diff --git a/icinga-module-windows.psd1 b/icinga-module-windows.psd1 index fdb32d1..e8e36fe 100644 --- a/icinga-module-windows.psd1 +++ b/icinga-module-windows.psd1 @@ -25,7 +25,7 @@ Description = 'Icinga 2 Windows Agent Module, which allows to entirely monitor t PowerShellVersion = '3.0' # Aus diesem Modul zu exportierende Funktionen. Um optimale Leistung zu erzielen, verwenden Sie keine Platzhalter und löschen den Eintrag nicht. Verwenden Sie ein leeres Array, wenn keine zu exportierenden Funktionen vorhanden sind. -FunctionsToExport = @( 'Use-Icinga', 'Import-IcingaLib', 'Publish-IcingaModuleManifests', 'Get-IcingaPluginDir', 'Get-IcingaCustomPluginDir', 'Get-IcingaCacheDir', 'Get-IcingaPowerShellConfigDir' ) +FunctionsToExport = @( 'Use-Icinga', 'Import-IcingaLib', 'Publish-IcingaModuleManifests', 'Get-IcingaPluginDir', 'Get-IcingaCustomPluginDir', 'Get-IcingaCacheDir', 'Get-IcingaPowerShellConfigDir', 'Get-IcingaPowerShellModuleFile' ) # Aus diesem Modul zu exportierende Cmdlets. Um optimale Leistung zu erzielen, verwenden Sie keine Plat'zhalter und löschen den Eintrag nicht. Verwenden Sie ein leeres Array, wenn keine zu exportierenden Cmdlets vorhanden sind. CmdletsToExport = @() diff --git a/icinga-module-windows.psm1 b/icinga-module-windows.psm1 index c50c163..77f45d8 100644 --- a/icinga-module-windows.psm1 +++ b/icinga-module-windows.psm1 @@ -165,3 +165,8 @@ function Get-IcingaPowerShellConfigDir() { return (Join-Path -Path $PSScriptRoot -ChildPath 'config'); } + +function Get-IcingaPowerShellModuleFile() +{ + return (Join-Path -Path $PSScriptRoot -ChildPath 'icinga-module-windows.psm1'); +} diff --git a/lib/core/framework/Expand-IcingaZipArchive.psm1 b/lib/core/framework/Expand-IcingaZipArchive.psm1 new file mode 100644 index 0000000..658e63a --- /dev/null +++ b/lib/core/framework/Expand-IcingaZipArchive.psm1 @@ -0,0 +1,23 @@ +function Expand-IcingaZipArchive() +{ + param( + $Path, + $Destination + ); + + if ((Test-Path $Path) -eq $FALSE -Or (Test-Path $Destination) -eq $FALSE) { + Write-Host 'The path to the zip archive or the destination path do not exist'; + return $FALSE; + } + + Add-Type -AssemblyName System.IO.Compression.FileSystem; + + try { + [System.IO.Compression.ZipFile]::ExtractToDirectory($Path, $Destination); + return $TRUE; + } catch { + throw $_.Exception; + } + + return $FALSE; +} diff --git a/lib/core/framework/Get-IcingaFrameworkServiceBinary.psm1 b/lib/core/framework/Get-IcingaFrameworkServiceBinary.psm1 new file mode 100644 index 0000000..97c8396 --- /dev/null +++ b/lib/core/framework/Get-IcingaFrameworkServiceBinary.psm1 @@ -0,0 +1,47 @@ +function Get-IcingaFrameworkServiceBinary() +{ + param( + [string]$FrameworkServiceUrl, + [string]$ServiceDirectory + ); + + $ProgressPreference = "SilentlyContinue"; + + 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; + $FrameworkServiceUrl = $LatestRelease.Replace('/tag/', '/download/'); + $Tag = $FrameworkServiceUrl.Split('/')[-1]; + $FrameworkServiceUrl = [string]::Format('{0}/icinga-service-{1}.zip', $FrameworkServiceUrl, $Tag); + } else { + $FrameworkServiceUrl = (Get-IcingaAgentInstallerAnswerInput -Prompt 'Please enter the full path to your service binary repository' -Default 'v').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 $ServiceDirectory) -eq $FALSE) { + New-Item -Path $ServiceDirectory -Force -ItemType Directory | Out-Null; + } + + $ZipArchive = Join-Path -Path $ServiceDirectory -ChildPath ($FrameworkServiceUrl.Split('/')[-1]); + $ServiceBin = Join-Path -Path $ServiceDirectory -ChildPath 'icinga-service.exe'; + + Invoke-WebRequest -Uri $FrameworkServiceUrl -UseBasicParsing -OutFile $ZipArchive; + + if ((Expand-IcingaZipArchive -Path $ZipArchive -Destination $ServiceDirectory) -eq $FALSE) { + throw 'Failed to expand the downloaded ZIP archive'; + } + + if ((Test-IcingaZipBinaryChecksum -Path $ServiceBin) -eq $FALSE) { + throw 'The checksum of the downloaded file and the required MD5 hash are not matching'; + } + + return @{ + 'FrameworkServiceUrl' = $FrameworkServiceUrl; + 'ServiceDirectory' = $ServiceDirectory; + 'ServiceBin' = $ServiceBin; + }; +} diff --git a/lib/core/framework/Install-IcingaFrameworkService.psm1 b/lib/core/framework/Install-IcingaFrameworkService.psm1 new file mode 100644 index 0000000..71abdb3 --- /dev/null +++ b/lib/core/framework/Install-IcingaFrameworkService.psm1 @@ -0,0 +1,26 @@ +function Install-IcingaFrameworkService() +{ + param( + $Path, + $User, + [SecureString]$Password + ); + + if ((Test-Path $Path) -eq $FALSE) { + throw 'Please specify the path directly to the service binary'; + } + + $Path = [string]::Format( + '{0} \"{1}\"', + $Path, + (Get-IcingaPowerShellModuleFile) + ); + + $ServiceCreation = Start-IcingaProcess -Executable 'sc.exe' -Arguments ([string]::Format('create icingapowershell binPath= "{0}" DisplayName= "Icinga PowerShell Service" start= auto', $Path)); + + if ($ServiceCreation.ExitCode -ne 0) { + throw ([string]::Format('Failed to install Icinga PowerShell Service: {0}{1}', $ServiceCreation.Message, $ServiceCreation.Error)); + } + + return (Set-IcingaAgentServiceUser -User $User -Password $Password -Service 'icingapowershell'); +} diff --git a/lib/core/framework/Restart-IcingaService.psm1 b/lib/core/framework/Restart-IcingaService.psm1 new file mode 100644 index 0000000..ce0f009 --- /dev/null +++ b/lib/core/framework/Restart-IcingaService.psm1 @@ -0,0 +1,10 @@ +function Restart-IcingaService() +{ + param( + $Service + ); + + if (Get-Service $Service -ErrorAction SilentlyContinue) { + Restart-Service $Service; + } +} diff --git a/lib/core/framework/Start-IcingaService.psm1 b/lib/core/framework/Start-IcingaService.psm1 new file mode 100644 index 0000000..ec4b5a7 --- /dev/null +++ b/lib/core/framework/Start-IcingaService.psm1 @@ -0,0 +1,10 @@ +function Start-IcingaService() +{ + param( + $Service + ); + + if (Get-Service $Service -ErrorAction SilentlyContinue) { + Start-Service $Service; + } +} diff --git a/lib/core/framework/Stop-IcingaService.psm1 b/lib/core/framework/Stop-IcingaService.psm1 new file mode 100644 index 0000000..ef91fa8 --- /dev/null +++ b/lib/core/framework/Stop-IcingaService.psm1 @@ -0,0 +1,10 @@ +function Stop-IcingaService() +{ + param( + $Service + ); + + if (Get-Service $Service -ErrorAction SilentlyContinue) { + Stop-Service $Service; + } +} diff --git a/lib/core/framework/Test-IcingaZipBinaryChecksum.psm1 b/lib/core/framework/Test-IcingaZipBinaryChecksum.psm1 new file mode 100644 index 0000000..94612fa --- /dev/null +++ b/lib/core/framework/Test-IcingaZipBinaryChecksum.psm1 @@ -0,0 +1,23 @@ +function Test-IcingaZipBinaryChecksum() +{ + param( + $Path + ); + + $MD5Path = [string]::Format('{0}.md5', $Path); + + if ((Test-Path $MD5Path) -eq $FALSE) { + return $TRUE; + } + + [string]$MD5Checksum = Get-Content $MD5Path; + $MD5Checksum = ($MD5Checksum.Split(' ')[0]).ToLower(); + + $FileHash = ((Get-FileHash $Path -Algorithm MD5).Hash).ToLower(); + + if ($MD5Checksum -ne $FileHash) { + return $FALSE; + } + + return $TRUE; +} diff --git a/lib/core/icingaagent/installer/Uninstall-IcingaAgent.psm1 b/lib/core/icingaagent/installer/Uninstall-IcingaAgent.psm1 index 4afea1b..cb99f8f 100644 --- a/lib/core/icingaagent/installer/Uninstall-IcingaAgent.psm1 +++ b/lib/core/icingaagent/installer/Uninstall-IcingaAgent.psm1 @@ -9,6 +9,8 @@ function Uninstall-IcingaAgent() Write-Host 'Removing current installed Icinga Agent'; + Stop-IcingaService 'icinga2'; + $Uninstaller = Start-IcingaProcess -Executable 'MsiExec.exe' -Arguments ([string]::Format('{0} /q', $IcingaData.Uninstaller)) -FlushNewLine; if ($Uninstaller.ExitCode -ne 0) { 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'; } } } diff --git a/lib/core/icingaagent/setters/Set-IcingaAgentServiceUser.psm1 b/lib/core/icingaagent/setters/Set-IcingaAgentServiceUser.psm1 index 6779bf8..d627efe 100644 --- a/lib/core/icingaagent/setters/Set-IcingaAgentServiceUser.psm1 +++ b/lib/core/icingaagent/setters/Set-IcingaAgentServiceUser.psm1 @@ -2,7 +2,8 @@ function Set-IcingaAgentServiceUser() { param( [string]$User, - [securestring]$Password + [securestring]$Password, + [string]$Service = 'icinga2' ); if ([string]::IsNullOrEmpty($User)) { @@ -14,14 +15,14 @@ function Set-IcingaAgentServiceUser() $User = [string]::Format('.\{0}', $User); } - $ArgString = 'config icinga2 obj= "{0}" password="{1}"'; + $ArgString = 'config {0} obj= "{1}" password="{2}"'; if($null -eq $Password) { - $ArgString = 'config icinga2 obj= "{0}"{1}'; + $ArgString = 'config {0} obj= "{1}"{2}'; } $Output = Start-IcingaProcess ` -Executable 'sc.exe' ` - -Arguments ([string]::Format($ArgString, $User, (ConvertFrom-IcingaSecureString $Password))) ` + -Arguments ([string]::Format($ArgString, $Service, $User, (ConvertFrom-IcingaSecureString $Password))) ` -FlushNewLines $TRUE; if ($Output.ExitCode -eq 0) { diff --git a/lib/daemon/Start-IcingaPowerShellDaemon.psm1 b/lib/daemon/Start-IcingaPowerShellDaemon.psm1 index 4fe858c..87bbc53 100644 --- a/lib/daemon/Start-IcingaPowerShellDaemon.psm1 +++ b/lib/daemon/Start-IcingaPowerShellDaemon.psm1 @@ -1,5 +1,9 @@ function Start-IcingaPowerShellDaemon() { + param( + [switch]$RunAsService + ); + $ScriptBlock = { param($IcingaDaemonData); @@ -32,4 +36,10 @@ function Start-IcingaPowerShellDaemon() $global:IcingaDaemonData.Add('Config', (Read-IcingaPowerShellConfig)); New-IcingaThreadInstance -Name "Icinga_PowerShell_Background_Daemon" -ThreadPool $IcingaDaemonData.IcingaThreadPool.BackgroundPool -ScriptBlock $ScriptBlock -Arguments @( $global:IcingaDaemonData ) -Start; + + if ($RunAsService) { + while ($TRUE) { + Start-Sleep -Seconds 100; + } + } }