2021-07-16 15:38:08 -04:00
function Update-Icinga ( )
{
param (
[ string ] $Name = $null ,
2023-11-03 12:05:24 -04:00
[ string ] $Version = 'release' ,
2021-07-16 15:38:08 -04:00
[ switch ] $Release = $FALSE ,
[ switch ] $Snapshot = $FALSE ,
[ switch ] $Confirm = $FALSE ,
[ switch ] $Force = $FALSE
) ;
if ( $Release -eq $FALSE -And $Snapshot -eq $FALSE ) {
$Release = $TRUE ;
}
2024-04-01 10:03:37 -04:00
$CurrentInstallation = Get-IcingaInstallation -Release: $Release -Snapshot: $Snapshot ;
[ bool ] $UpdateJEA = $FALSE ;
[ array ] $ComponentsList = @ ( ) ;
2024-06-06 08:39:45 -04:00
[ bool ] $IsInstalled = $FALSE ;
2021-07-16 15:38:08 -04:00
2024-04-01 10:03:37 -04:00
# We need to make sure that the framework is always installed first as component
# to prevent possible race-conditions during update, in case we update plugins
# before the framework. For plugins this applies as well, as other components
# could use them as depdency
if ( $CurrentInstallation . ContainsKey ( 'framework' ) ) {
$ComponentsList + = 'framework' ;
}
if ( $CurrentInstallation . ContainsKey ( 'plugins' ) ) {
$ComponentsList + = 'plugins' ;
}
# Add all other components, but skip the framework in this case
2021-07-16 15:38:08 -04:00
foreach ( $entry in $CurrentInstallation . Keys ) {
2024-04-01 10:03:37 -04:00
if ( $entry -eq 'framework' -Or $entry -eq 'plugins' ) {
continue ;
}
$ComponentsList + = $entry ;
}
# Now process with your installation
foreach ( $entry in $ComponentsList ) {
2021-07-16 15:38:08 -04:00
$Component = $CurrentInstallation [ $entry ] ;
if ( [ string ] :: IsNullOrEmpty ( $Name ) -eq $FALSE -And $Name -ne $entry ) {
continue ;
}
2024-06-06 08:39:45 -04:00
$IsInstalled = $TRUE ;
2023-08-11 10:20:39 -04:00
$NewVersion = $Component . LatestVersion ;
if ( [ string ] :: IsNullOrEmpty ( $Version ) -eq $FALSE ) {
2023-11-03 12:05:24 -04:00
# Ensure we are backwards compatible with Icinga for Windows v1.11.0 which broke the version update feature
if ( $Version . ToLower ( ) -ne 'release' -And $Version . ToLower ( ) -ne 'latest' ) {
$NewVersion = $Version ;
}
2023-07-25 09:32:31 -04:00
}
2021-07-16 15:38:08 -04:00
if ( [ string ] :: IsNullOrEmpty ( $NewVersion ) ) {
Write-IcingaConsoleNotice 'No update package found for component "{0}"' -Objects $entry ;
continue ;
}
$LockedVersion = Get-IcingaComponentLock -Name $entry ;
if ( $null -ne $LockedVersion ) {
$NewVersion = $LockedVersion ;
}
if ( [ Version ] $NewVersion -le [ Version ] $Component . CurrentVersion -And $Force -eq $FALSE ) {
Write-IcingaConsoleNotice 'The installed version "{0}" of component "{1}" is identical or lower than the new version "{2}". Use "-Force" to install anyway' -Objects $Component . CurrentVersion , $entry , $NewVersion ;
continue ;
}
2023-08-25 08:30:26 -04:00
if ( $entry . ToLower ( ) -ne 'agent' -And $entry . ToLower ( ) -ne 'service' ) {
$UpdateJEA = $TRUE ;
}
2024-04-01 12:31:34 -04:00
Install-IcingaComponent -Name $entry -Version $NewVersion -Release: $Release -Snapshot: $Snapshot -Confirm: $Confirm -Force: $Force -KeepRepoErrors ;
2022-01-28 09:22:12 -05:00
}
2024-06-06 08:39:45 -04:00
if ( $IsInstalled -eq $FALSE ) {
Write-IcingaConsoleError 'Failed to update the component "{0}", as it is not installed' -Objects $Name ;
}
2022-01-28 09:22:12 -05:00
# Update JEA profile if JEA is enabled once the update is complete
2023-08-25 08:30:26 -04:00
if ( [ string ] :: IsNullOrEmpty ( ( Get-IcingaJEAContext ) ) -eq $FALSE -And $UpdateJEA ) {
2022-01-28 09:22:12 -05:00
Update-IcingaJEAProfile ;
2024-03-13 11:50:36 -04:00
Restart-IcingaForWindows ;
2021-07-16 15:38:08 -04:00
}
}