2019-09-29 12:25:40 -04:00
function Install-IcingaAgent ( )
{
param (
[ string ] $Version ,
2019-09-29 18:32:45 -04:00
[ string ] $Source = 'https://packages.icinga.com/windows/' ,
[ string ] $InstallDir = '' ,
[ bool ] $AllowUpdates = $FALSE
2019-09-29 12:25:40 -04:00
) ;
2019-11-05 03:21:49 -05:00
if ( [ string ] :: IsNullOrEmpty ( $Version ) ) {
2020-05-13 10:53:15 -04:00
Write-IcingaConsoleError 'No Icinga Agent version specified. Skipping installation.' ;
2019-11-05 03:21:49 -05:00
return $FALSE ;
}
2019-09-29 12:25:40 -04:00
if ( $IcingaData . Installed -eq $TRUE -and $AllowUpdates -eq $FALSE ) {
2020-05-13 10:53:15 -04:00
Write-IcingaConsoleWarning 'The Icinga Agent is already installed on this system. To perform updates or downgrades, please add the "-AllowUpdates" argument' ;
2019-09-29 12:25:40 -04:00
return $FALSE ;
}
2019-11-05 03:35:10 -05:00
$IcingaData = Get-IcingaAgentInstallation ;
$InstalledVersion = Get-IcingaAgentVersion ;
$IcingaInstaller = Get-IcingaAgentMSIPackage -Source $Source -Version $Version -SkipDownload ;
$InstallTarget = $IcingaData . RootDir ;
2019-09-29 12:25:40 -04:00
if ( $Version -eq 'snapshot' ) {
if ( $IcingaData . InstallDate -ge $IcingaInstaller . LastUpdate -And [ string ] :: IsNullOrEmpty ( $InstalledVersion . Snapshot ) -eq $FALSE ) {
2020-05-13 10:53:15 -04:00
Write-IcingaConsoleNotice 'There is no new snapshot package available which requires to be installed.'
2019-09-29 12:25:40 -04:00
return $FALSE ;
}
$IcingaInstaller . Version = 'snapshot' ;
} elseif ( $IcingaInstaller . Version -eq $InstalledVersion . Full ) {
2020-05-13 10:53:15 -04:00
Write-IcingaConsoleNotice ( [ string ] :: Format (
2019-09-29 12:25:40 -04:00
'No installation required. Your installed version [{0}] is matching the online version [{1}]' ,
$InstalledVersion . Full ,
$IcingaInstaller . Version
) ) ;
return $FALSE ;
}
$IcingaInstaller = Get-IcingaAgentMSIPackage -Source $Source -Version $IcingaInstaller . Version ;
if ( ( Test-Path $IcingaInstaller . InstallerPath ) -eq $FALSE ) {
throw 'Failed to locate Icinga Agent installer file' ;
}
if ( [ string ] :: IsNullOrEmpty ( $InstallDir ) -eq $FALSE ) {
if ( ( Test-Path $InstallDir ) -eq $FALSE ) {
New-Item -Path $InstallDir -Force | Out-Null ;
$InstallTarget = $InstallDir ;
}
}
[ string ] $InstallFolderMsg = $InstallTarget ;
if ( [ string ] :: IsNullOrEmpty ( $InstallTarget ) -eq $FALSE ) {
$InstallTarget = [ string ] :: Format ( ' INSTALL_ROOT="{0}"' , $InstallTarget ) ;
} else {
$InstallTarget = '' ;
if ( $IcingaData . Architecture -eq 'x86' ) {
$InstallFolderMsg = Join-Path -Path $ { env : ProgramFiles ( x86 ) } -ChildPath 'ICINGA2' ;
} else {
$InstallFolderMsg = Join-Path -Path $env:ProgramFiles -ChildPath 'ICINGA2' ;
}
}
2020-05-13 10:53:15 -04:00
Write-IcingaConsoleNotice ( [ string ] :: Format ( 'Installing new Icinga Agent version into "{0}"' , $InstallFolderMsg ) ) ;
2019-09-29 12:25:40 -04:00
if ( $IcingaData . Installed ) {
if ( ( Uninstall-IcingaAgent ) -eq $FALSE ) {
return $FALSE ;
}
}
$InstallProcess = Start-IcingaProcess -Executable 'MsiExec.exe' -Arguments ( [ string ] :: Format ( '/quiet /i "{0}" {1}' , $IcingaInstaller . InstallerPath , $InstallTarget ) ) -FlushNewLines ;
if ( $InstallProcess . ExitCode -ne 0 ) {
2020-05-13 10:53:15 -04:00
Write-IcingaConsoleError -Message 'Failed to install Icinga 2 Agent: {0}{1}' -Objects $InstallProcess . Message , $InstallProcess . Error ;
2019-09-29 12:25:40 -04:00
return $FALSE ;
}
2020-05-13 10:53:15 -04:00
Write-IcingaConsoleNotice 'Icinga Agent was successfully installed' ;
2019-09-29 12:25:40 -04:00
return $TRUE ;
}