2019-09-29 18:35:29 -04:00
function Start-IcingaAgentInstallWizard ( )
{
param (
[ string ] $Hostname ,
[ switch ] $AutoUseFQDN = $FALSE ,
[ switch ] $AutoUseHostname = $FALSE ,
[ switch ] $LowerCase = $FALSE ,
[ switch ] $UpperCase = $FALSE ,
2019-10-17 02:44:48 -04:00
$AddDirectorGlobal = $null ,
$AddGlobalTemplates = $null ,
2019-09-29 18:35:29 -04:00
[ string ] $PackageSource ,
[ string ] $AgentVersion ,
[ switch ] $AllowVersionChanges = $FALSE ,
$UpdateAgent = $null ,
$AcceptConnections = $null ,
[ array ] $Endpoints = @ ( ) ,
[ array ] $EndpointConnections = @ ( ) ,
[ string ] $ParentZone ,
[ array ] $GlobalZones = $null ,
[ string ] $CAEndpoint ,
$CAPort = $null ,
[ string ] $Ticket ,
[ string ] $CAFile ,
[ switch ] $RunInstaller ,
2019-10-28 11:59:11 -04:00
[ switch ] $Reconfigure ,
[ string ] $ServiceUser ,
2019-10-31 09:33:56 -04:00
[ securestring ] $ServicePass = $null ,
$InstallFrameworkService = $null ,
$FrameworkServiceUrl = $null ,
$ServiceDirectory = $null ,
2019-11-02 12:42:39 -04:00
$ServiceBin = $null ,
$UseDirectorSelfService = $null ,
[ bool ] $SkipDirectorQuestion = $FALSE ,
[ string ] $DirectorUrl ,
[ string ] $SelfServiceAPIKey ,
$OverrideDirectorVars = $null ,
[ hashtable ] $ProvidedArgs = @ { }
2019-09-29 18:35:29 -04:00
) ;
[ array ] $InstallerArguments = @ ( ) ;
2019-10-17 02:44:48 -04:00
[ array ] $GlobalZoneConfig = @ ( ) ;
2019-09-29 18:35:29 -04:00
2019-11-02 12:42:39 -04:00
if ( $SkipDirectorQuestion -eq $FALSE ) {
if ( $null -eq $UseDirectorSelfService ) {
if ( ( Get-IcingaAgentInstallerAnswerInput -Prompt 'Do you want to use the Icinga Director Self-Service API?' -Default 'y' ) . result -eq 1 ) {
$UseDirectorSelfService = $TRUE ;
} else {
$UseDirectorSelfService = $FALSE ;
$InstallerArguments + = '-UseDirectorSelfService 0' ;
}
}
if ( $UseDirectorSelfService ) {
$InstallerArguments + = '-UseDirectorSelfService 1' ;
Start-IcingaAgentDirectorWizard `
-DirectorUrl $DirectorUrl `
-SelfServiceAPIKey $SelfServiceAPIKey `
-OverrideDirectorVars $OverrideDirectorVars `
-InstallFrameworkService $InstallFrameworkService `
-ServiceDirectory $ServiceDirectory `
-ServiceBin $ServiceBin `
-RunInstaller $RunInstaller ;
return ;
}
}
2019-09-29 18:35:29 -04:00
if ( [ string ] :: IsNullOrEmpty ( $Hostname ) -And $AutoUseFQDN -eq $FALSE -And $AutoUseHostname -eq $FALSE ) {
if ( ( Get-IcingaAgentInstallerAnswerInput -Prompt 'Do you want to manually specify a hostname?' -Default 'n' ) . result -eq 1 ) {
if ( ( Get-IcingaAgentInstallerAnswerInput -Prompt 'Do you want to automatically fetch the hostname with its FQDN?' -Default 'y' ) . result -eq 1 ) {
$InstallerArguments + = '-AutoUseFQDN' ;
$AutoUseFQDN = $TRUE ;
} else {
$InstallerArguments + = '-AutoUseHostname' ;
$AutoUseHostname = $TRUE ;
}
if ( ( Get-IcingaAgentInstallerAnswerInput -Prompt 'Do you want to modify the hostname to only include lower case characters?' -Default 'y' ) . result -eq 1 ) {
$InstallerArguments + = '-LowerCase' ;
$LowerCase = $TRUE ;
} else {
if ( ( Get-IcingaAgentInstallerAnswerInput -Prompt 'Do you want to modify the hostname to only include upper case characters?' -Default 'n' ) . result -eq 0 ) {
$InstallerArguments + = '-UpperCase' ;
$UpperCase = $TRUE ;
}
}
$Hostname = Get-IcingaHostname -AutoUseFQDN $AutoUseFQDN -AutoUseHostname $AutoUseHostname -LowerCase $LowerCase -UpperCase $UpperCase ;
} else {
$Hostname = ( Get-IcingaAgentInstallerAnswerInput -Prompt 'Please specify the hostname to use' -Default 'v' ) . answer ;
}
} else {
if ( $AutoUseFQDN -Or $AutoUseHostname ) {
$Hostname = Get-IcingaHostname -AutoUseFQDN $AutoUseFQDN -AutoUseHostname $AutoUseHostname -LowerCase $LowerCase -UpperCase $UpperCase ;
}
}
Write-Host ( [ string ] :: Format ( 'Using hostname "{0}" for the Icinga 2 Agent configuration' , $Hostname ) ) ;
$IcingaAgent = Get-IcingaAgentInstallation ;
if ( $IcingaAgent . Installed -eq $FALSE ) {
if ( [ string ] :: IsNullOrEmpty ( $PackageSource ) ) {
if ( ( Get-IcingaAgentInstallerAnswerInput -Prompt 'Do you want to install the Icinga Agent now?' -Default 'y' ) . result -eq 1 ) {
2019-10-31 11:35:50 -04:00
if ( ( Get-IcingaAgentInstallerAnswerInput -Prompt 'Do you want to use a different package source then "https://packages.icinga.com/windows/"?' -Default 'n' ) . result -eq 0 ) {
2019-09-29 18:35:29 -04:00
$PackageSource = ( Get-IcingaAgentInstallerAnswerInput -Prompt 'Please specify your package source' -Default 'v' ) . answer ;
$InstallerArguments + = " -PackageSource ' $PackageSource ' " ;
} else {
$PackageSource = 'https://packages.icinga.com/windows/'
$InstallerArguments + = " -PackageSource ' $PackageSource ' " ;
}
Write-Host ( [ string ] :: Format ( 'Using package source "{0}" for the Icinga 2 Agent package' , $PackageSource ) ) ;
}
if ( [ string ] :: IsNullOrEmpty ( $AgentVersion ) ) {
2019-10-31 11:35:50 -04:00
$AgentVersion = ( Get-IcingaAgentInstallerAnswerInput -Prompt 'Please specify the version you wish to install ("latest", "snapshot", or a version like "2.11.0")' -Default 'v' -DefaultInput 'latest' ) . answer ;
2019-09-29 18:35:29 -04:00
$InstallerArguments + = " -AgentVersion ' $AgentVersion ' " ;
Write-Host ( [ string ] :: Format ( 'Installing Icinga Version: "{0}"' , $AgentVersion ) ) ;
}
}
} else {
if ( $null -eq $UpdateAgent ) {
if ( ( Get-IcingaAgentInstallerAnswerInput -Prompt 'The Icinga 2 Agent is already installed. Would you like to update it?' -Default 'y' ) . result -eq 1 ) {
$UpdateAgent = 1 ;
} else {
$UpdateAgent = 0 ;
}
$InstallerArguments + = " -UpdateAgent $UpdateAgent " ;
}
if ( $UpdateAgent -eq 1 ) {
if ( [ string ] :: IsNullOrEmpty ( $AgentVersion ) ) {
$AgentVersion = ( Get-IcingaAgentInstallerAnswerInput -Prompt 'Please specify the version you wish to install ("latest", "snapshot", or a version like "2.11.0")' -Default 'v' ) . answer ;
$AllowVersionChanges = $TRUE ;
$InstallerArguments + = " -AgentVersion ' $AgentVersion ' " ;
$InstallerArguments + = '-AllowVersionChanges' ;
Write-Host ( [ string ] :: Format ( 'Updating/Downgrading Icinga 2 Agent to version: "{0}"' , $AgentVersion ) ) ;
}
if ( [ string ] :: IsNullOrEmpty ( $PackageSource ) ) {
if ( ( Get-IcingaAgentInstallerAnswerInput -Prompt 'Do you want to use a different package source then "https://packages.icinga.com/windows/" ?' -Default 'n' ) . result -eq 0 ) {
$PackageSource = ( Get-IcingaAgentInstallerAnswerInput -Prompt 'Please specify your package source' -Default 'v' ) . answer ;
$InstallerArguments + = " -PackageSource ' $PackageSource ' " ;
} else {
$PackageSource = 'https://packages.icinga.com/windows/'
$InstallerArguments + = " -PackageSource ' $PackageSource ' " ;
}
}
}
}
if ( $null -eq $AcceptConnections ) {
if ( ( Get-IcingaAgentInstallerAnswerInput -Prompt 'Will this Agent connect to its parent endpoint(s)?' -Default 'y' ) . result -eq 1 ) {
$InstallerArguments + = " -AcceptConnections 1 " ;
$AcceptConnections = 1 ;
} else {
$InstallerArguments + = " -AcceptConnections 0 " ;
$AcceptConnections = 0 ;
}
}
if ( $Endpoints . Count -eq 0 ) {
$ArrayString = ( Get-IcingaAgentInstallerAnswerInput -Prompt 'Please specify all endpoints this Agent will report to (separated by ",")' -Default 'v' ) . answer ;
$Endpoints = ( $ArrayString . Replace ( ' ' , '' ) ) . Split ( ',' ) ;
$InstallerArguments + = ( " -Endpoints " + ( [ string ] :: Join ( ',' , $Endpoints ) ) ) ;
}
2019-10-31 05:12:25 -04:00
if ( $null -eq $CAPort ) {
if ( ( Get-IcingaAgentInstallerAnswerInput -Prompt 'Are you using a different port than 5665 for Icinga communications?' -Default 'n' ) . result -eq 0 ) {
$CAPort = ( Get-IcingaAgentInstallerAnswerInput -Prompt 'Please enter the port for Icinga 2 communication' -Default 'v' ) . answer ;
$InstallerArguments + = " -CAPort $CAPort " ;
} else {
$InstallerArguments + = " -CAPort 5665 " ;
$CAPort = 5665 ;
}
}
2019-09-29 18:35:29 -04:00
if ( $EndpointConnections . Count -eq 0 -And $AcceptConnections -eq 1 ) {
2019-10-31 05:12:25 -04:00
$NetworkDefault = '' ;
foreach ( $Endpoint in $Endpoints ) {
$NetworkDefault + = [ string ] :: Format ( '[{0}]:{1},' , $Endpoint , $CAPort ) ;
}
if ( [ string ] :: IsNullOrEmpty ( $NetworkDefault ) -eq $FALSE ) {
$NetworkDefault = $NetworkDefault . Substring ( 0 , $NetworkDefault . Length - 1 ) ;
}
$ArrayString = ( Get-IcingaAgentInstallerAnswerInput -Prompt 'Please specify the network destinations this agent will connect to, separated by ","' -Default 'v' -DefaultInput $NetworkDefault ) . answer ;
2019-09-29 18:35:29 -04:00
$EndpointConnections = ( $ArrayString . Replace ( ' ' , '' ) ) . Split ( ',' ) ;
$InstallerArguments + = ( " -EndpointConnections " + ( [ string ] :: Join ( ',' , $EndpointConnections ) ) ) ;
}
if ( [ string ] :: IsNullOrEmpty ( $ParentZone ) ) {
2019-10-31 05:12:25 -04:00
$ParentZone = ( Get-IcingaAgentInstallerAnswerInput -Prompt 'Please specify the parent zone this agent will connect to' -Default 'v' -DefaultInput 'master' ) . answer ;
2019-09-29 18:35:29 -04:00
$InstallerArguments + = " -ParentZone $ParentZone " ;
}
2019-10-17 02:44:48 -04:00
if ( $null -eq $AddDirectorGlobal ) {
if ( ( Get-IcingaAgentInstallerAnswerInput -Prompt 'Do you want to add the global zone "director-global"?' -Default 'y' ) . result -eq 1 ) {
$AddDirectorGlobal = $TRUE ;
} else {
$AddDirectorGlobal = $FALSE ;
}
}
$InstallerArguments + = ( " -AddDirectorGlobal $AddDirectorGlobal " ) ;
if ( $AddDirectorGlobal ) {
$GlobalZoneConfig + = 'director-global' ;
}
if ( $null -eq $AddGlobalTemplates ) {
if ( ( Get-IcingaAgentInstallerAnswerInput -Prompt 'Do you want to add the global zone "global-templates"?' -Default 'y' ) . result -eq 1 ) {
$AddGlobalTemplates = $TRUE ;
} else {
$AddGlobalTemplates = $FALSE ;
}
}
$InstallerArguments + = ( " -AddGlobalTemplates $AddGlobalTemplates " ) ;
if ( $AddGlobalTemplates ) {
$GlobalZoneConfig + = 'global-templates' ;
}
2019-09-29 18:35:29 -04:00
if ( $null -eq $GlobalZones ) {
2019-10-17 02:44:48 -04:00
if ( ( Get-IcingaAgentInstallerAnswerInput -Prompt 'Do you want to add custom global zones?' -Default 'n' ) . result -eq 0 ) {
2019-09-29 18:35:29 -04:00
$ArrayString = ( Get-IcingaAgentInstallerAnswerInput -Prompt 'Please specify your additional zones seperated by ","' -Default 'v' ) . answer ;
2019-10-17 02:44:48 -04:00
$GlobalZones = ( $ArrayString . Replace ( ' ' , '' ) ) . Split ( ',' )
$GlobalZoneConfig + = $GlobalZones ;
2019-09-29 18:35:29 -04:00
$InstallerArguments + = ( " -GlobalZones " + ( [ string ] :: Join ( ',' , $GlobalZones ) ) ) ;
} else {
$GlobalZones = @ ( ) ;
2019-10-17 02:44:48 -04:00
$InstallerArguments + = ( " -GlobalZones @() " ) ;
2019-09-29 18:35:29 -04:00
}
2019-10-17 02:44:48 -04:00
} else {
$GlobalZoneConfig + = $GlobalZones ;
2019-09-29 18:35:29 -04:00
}
[ bool ] $CanConnectToParent = $FALSE ;
if ( $AcceptConnections -eq 0 ) {
if ( ( Get-IcingaAgentInstallerAnswerInput -Prompt 'Is this Agent able to connect to its parent node for certificate generation?' -Default 'y' ) . result -eq 1 ) {
$CanConnectToParent = $TRUE ;
}
} else {
$CanConnectToParent = $TRUE ;
}
if ( $CanConnectToParent ) {
if ( [ string ] :: IsNullOrEmpty ( $CAEndpoint ) ) {
2019-11-02 07:33:01 -04:00
$CAEndpoint = ( Get-IcingaAgentInstallerAnswerInput -Prompt 'Please enter the FQDN for either ONE of your Icinga parent node/nodes or your Icinga 2 CA master' -Default 'v' -DefaultInput ( Get-IPConfigFromString $EndpointConnections [ 0 ] ) . address ) . answer ;
2019-09-29 18:35:29 -04:00
$InstallerArguments + = " -CAEndpoint $CAEndpoint " ;
}
2019-10-17 02:44:48 -04:00
if ( $null -eq $Ticket ) {
2019-09-29 18:35:29 -04:00
if ( ( Get-IcingaAgentInstallerAnswerInput -Prompt 'Do you have a Icinga Ticket available to sign your certificate?' -Default 'y' ) . result -eq 1 ) {
$Ticket = ( Get-IcingaAgentInstallerAnswerInput -Prompt 'Please enter your Icinga Ticket' -Default 'v' ) . answer ;
$InstallerArguments + = " -Ticket $Ticket " ;
2019-10-17 02:44:48 -04:00
} else {
$InstallerArguments + = " -Ticket '' " ;
2019-09-29 18:35:29 -04:00
}
}
} else {
if ( [ string ] :: IsNullOrEmpty ( $CAFile ) ) {
if ( ( Get-IcingaAgentInstallerAnswerInput -Prompt 'Is your public Icinga 2 CA (ca.crt) available on a local, network or web share?' -Default 'y' ) . result -eq 1 ) {
$CAFile = ( Get-IcingaAgentInstallerAnswerInput -Prompt 'Please provide the full path to your ca.crt file' -Default 'v' ) . answer ;
$InstallerArguments + = " -CAFile $CAFile " ;
}
}
}
2019-10-28 11:59:11 -04:00
if ( [ string ] :: IsNullOrEmpty ( $ServiceUser ) ) {
if ( ( Get-IcingaAgentInstallerAnswerInput -Prompt 'Do you want to change the user the Icinga Agent service is running with (Default: "NT Authority\NetworkService")?' -Default 'n' ) . result -eq 0 ) {
$ServiceUser = ( Get-IcingaAgentInstallerAnswerInput -Prompt 'Please enter the user you wish the Icinga Agent service to run with' -Default 'v' ) . answer ;
$InstallerArguments + = " -ServiceUser $ServiceUser " ;
if ( $null -eq $ServicePass ) {
if ( ( Get-IcingaAgentInstallerAnswerInput -Prompt 'Does your Icinga Service user require a password to login (not required for System users)?' -Default 'y' ) . result -eq 1 ) {
$ServicePass = ( Get-IcingaAgentInstallerAnswerInput -Prompt 'Please enter the password for your service user' -Secure -Default 'v' ) . answer ;
$InstallerArguments + = " -ServicePass $ServicePass " ;
} else {
$ServicePass = '' ;
$InstallerArguments + = " -ServicePass '' " ;
}
}
} else {
$InstallerArguments + = " -ServiceUser 'NT Authority\NetworkService' " ;
$ServiceUser = 'NT Authority\NetworkService' ;
}
}
2019-10-31 09:33:56 -04:00
if ( $null -eq $InstallFrameworkService ) {
2019-10-31 11:35:50 -04:00
if ( ( Get-IcingaAgentInstallerAnswerInput -Prompt 'Do you want to install the PowerShell Framework as a Service?' -Default 'y' ) . result -eq 1 ) {
2019-10-31 09:33:56 -04:00
$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 ;
2019-11-02 12:42:39 -04:00
} else {
$InstallerArguments + = " -InstallFrameworkService 0 " ;
2019-10-31 09:33:56 -04:00
}
} elseif ( $InstallFrameworkService -eq $TRUE ) {
$result = Get-IcingaFrameworkServiceBinary -FrameworkServiceUrl $FrameworkServiceUrl -ServiceDirectory $ServiceDirectory ;
$ServiceBin = $result . ServiceBin ;
2019-11-02 12:42:39 -04:00
} else {
$InstallerArguments + = " -InstallFrameworkService 0 " ;
2019-10-31 09:33:56 -04:00
}
2019-09-29 18:35:29 -04:00
if ( $InstallerArguments . Count -ne 0 ) {
$InstallerArguments + = " -RunInstaller " ;
Write-Host 'The wizard is complete. These are the configured settings:' ;
2019-11-02 12:42:39 -04:00
foreach ( $entry in $ProvidedArgs . Keys ) {
if ( $entry -eq 'ProvidedArgs' -Or $entry -eq 'SkipDirectorQuestion' ) {
continue ;
}
[ bool ] $SkipArgument = $FALSE ;
if ( $OverrideDirectorVars -eq $FALSE ) {
switch ( $entry ) {
'InstallFrameworkService' { break ; } ;
'FrameworkServiceUrl' { break ; } ;
'ServiceDirectory' { break ; } ;
'ServiceBin' { break ; } ;
'UseDirectorSelfService' { break ; } ;
'DirectorUrl' { break ; } ;
'SelfServiceAPIKey' { break ; } ;
'OverrideDirectorVars' { break ; } ;
Default {
$SkipArgument = $TRUE ;
break ;
}
}
if ( $SkipArgument ) {
continue ;
}
}
[ bool ] $KnownArgument = $FALSE ;
foreach ( $item in $InstallerArguments ) {
if ( $item -like " - $entry * " -Or $item -eq " - $entry " ) {
$KnownArgument = $TRUE ;
break ;
}
}
if ( $KnownArgument ) {
continue ;
}
$Value = $ProvidedArgs [ $entry ] ;
if ( $Value -is [ System.Object ] ) {
$Value = [ string ] :: Join ( ',' , $Value ) ;
}
if ( $OverrideDirectorVars -eq $FALSE ) {
#$ClearedArguments += [string]::Format("-{0} '{1}'", $entry, $Value);
#continue;
}
$InstallerArguments + = [ string ] :: Format ( " -{0} '{1}' " , $entry , $Value ) ;
}
2019-09-29 18:35:29 -04:00
Write-Host ( $InstallerArguments | Out-String ) ;
2019-11-02 12:42:39 -04:00
if ( -Not $RunInstaller ) {
if ( ( Get-IcingaAgentInstallerAnswerInput -Prompt 'Is this configuration correct?' -Default 'y' ) . result -eq 1 ) {
if ( ( Get-IcingaAgentInstallerAnswerInput -Prompt 'Do you want to run the installer now? (Otherwise only the configration command will be printed)' -Default 'y' ) . result -eq 1 ) {
Write-Host 'To execute your Icinga Agent installation based on your answers again on this or another machine, simply run this command:'
2019-09-29 18:35:29 -04:00
2019-11-02 12:42:39 -04:00
$RunInstaller = $TRUE ;
} else {
Write-Host 'To execute your Icinga Agent installation based on your answers, simply run this command:'
}
2019-09-29 18:35:29 -04:00
} else {
2019-11-02 12:42:39 -04:00
Write-Host 'Please run the wizard again to modify your answers or modify the command below:'
2019-09-29 18:35:29 -04:00
}
}
Get-IcingaAgentInstallCommand -InstallerArguments $InstallerArguments -PrintConsole ;
}
if ( $RunInstaller ) {
if ( ( Install-IcingaAgent -Version $AgentVersion -Source $PackageSource -AllowUpdates $AllowVersionChanges ) -Or $Reconfigure ) {
Move-IcingaAgentDefaultConfig ;
2019-11-02 12:42:39 -04:00
Set-IcingaAgentServiceUser -User $ServiceUser -Password $ServicePass | Out-Null ;
Set-IcingaAgentServicePermission | Out-Null ;
2019-10-28 11:59:11 -04:00
Set-IcingaAcl " $Env:ProgramData \icinga2\etc " ;
Set-IcingaAcl " $Env:ProgramData \icinga2\var " ;
Set-IcingaAcl ( Get-IcingaCacheDir ) ;
2019-10-31 09:33:56 -04:00
Install-IcingaFrameworkService -Path $ServiceBin -User $ServiceUser -Password $ServicePass | Out-Null ;
Register-IcingaBackgroundDaemon -Command 'Start-IcingaServiceCheckDaemon' ;
2019-09-29 18:35:29 -04:00
Install-IcingaAgentBaseFeatures ;
Install-IcingaAgentCertificates -Hostname $Hostname -Endpoint $CAEndpoint -Port $CAPort -CACert $CAFile -Ticket $Ticket | Out-Null ;
Write-IcingaAgentApiConfig -Port $CAPort ;
2019-10-17 02:44:48 -04:00
Write-IcingaAgentZonesConfig -Endpoints $Endpoints -EndpointConnections $EndpointConnections -ParentZone $ParentZone -GlobalZones $GlobalZoneConfig -Hostname $Hostname ;
2019-09-29 18:35:29 -04:00
Test-IcingaAgent ;
2019-10-31 09:33:56 -04:00
Restart-IcingaService 'icingapowershell' ;
Restart-IcingaService 'icinga2' ;
2019-09-29 18:35:29 -04:00
}
}
}
function Get-IcingaAgentInstallCommand ( )
{
param (
$InstallerArguments ,
[ switch ] $PrintConsole
) ;
[ string ] $Installer = (
[ string ] :: Format (
'Start-IcingaAgentInstallWizard {0}' ,
( [ string ] :: Join ( ' ' , $InstallerArguments ) )
)
) ;
if ( $PrintConsole ) {
Write-Host '####'
Write-Host $Installer -ForegroundColor ( [ System.ConsoleColor ] :: Cyan ) ;
Write-Host '####'
} else {
return $Installer ;
}
}