Icinga2AgentInstaller.ps1: get rid of tabs

This commit is contained in:
Thomas Gelf 2016-05-25 21:05:18 +02:00
parent f6bcc310de
commit 4a21b8eb86
1 changed files with 318 additions and 318 deletions

View File

@ -1,9 +1,9 @@
function Icinga2AgentInstaller { function Icinga2AgentInstaller {
# #
# Setup parameters which can be accessed # Setup parameters which can be accessed
# with -<ParamName> # with -<ParamName>
# #
param( param(
# Agent setup # Agent setup
[string]$AgentName, [string]$AgentName,
@ -23,33 +23,33 @@ function Icinga2AgentInstaller {
# Agent signing # Agent signing
[string]$CAServer, [string]$CAServer,
[int]$CAPort = 5665, [int]$CAPort = 5665,
[bool]$ForceCertificateGeneration = $FALSE, [bool]$ForceCertificateGeneration = $FALSE,
#Internal handling #Internal handling
[bool]$DebugMode = $FALSE [bool]$DebugMode = $FALSE
) )
# #
# Initialise our installer object # Initialise our installer object
# and generate our config objects # and generate our config objects
# #
$installer = New-Object -TypeName PSObject; $installer = New-Object -TypeName PSObject;
$installer | Add-Member -membertype NoteProperty -name 'properties' -value @{} $installer | Add-Member -membertype NoteProperty -name 'properties' -value @{}
$installer | Add-Member -membertype NoteProperty -name 'cfg' -value @{ $installer | Add-Member -membertype NoteProperty -name 'cfg' -value @{
agent_name = $AgentName; agent_name = $AgentName;
ticket = $Ticket; ticket = $Ticket;
agent_version = $InstallAgentVersion; agent_version = $InstallAgentVersion;
parent_zone = $ParentZone; parent_zone = $ParentZone;
accept_config = $AcceptConfig; accept_config = $AcceptConfig;
endpoints = $Endpoints; endpoints = $Endpoints;
download_url = $DownloadUrl; download_url = $DownloadUrl;
allow_updates = $AllowUpdates; allow_updates = $AllowUpdates;
installer_hashes = $InstallerHashes; installer_hashes = $InstallerHashes;
ca_server = $CAServer; ca_server = $CAServer;
ca_port = $CAPort; ca_port = $CAPort;
force_cert = $ForceCertificateGeneration; force_cert = $ForceCertificateGeneration;
debug_mode = $DebugMode; debug_mode = $DebugMode;
} }
# #
# Access default script config parameters # Access default script config parameters
@ -62,10 +62,10 @@ function Icinga2AgentInstaller {
return $this.cfg[$key] return $this.cfg[$key]
} }
# #
# Convert a boolean value $TRUE $FALSE # Convert a boolean value $TRUE $FALSE
# to a string value # to a string value
# #
$installer | Add-Member -membertype ScriptMethod -name 'convertBoolToString' -value { $installer | Add-Member -membertype ScriptMethod -name 'convertBoolToString' -value {
param([bool]$key) param([bool]$key)
if ($key) { if ($key) {
@ -82,11 +82,11 @@ function Icinga2AgentInstaller {
$installer | Add-Member -membertype ScriptMethod -name 'getProperty' -value { $installer | Add-Member -membertype ScriptMethod -name 'getProperty' -value {
param([string] $key) param([string] $key)
# Initialse some variables first # Initialse some variables first
# will only be called once # will only be called once
if (-Not $this.properties.Get_Item('initialized')) { if (-Not $this.properties.Get_Item('initialized')) {
$this.init(); $this.init();
} }
return $this.properties.Get_Item($key); return $this.properties.Get_Item($key);
} }
@ -121,16 +121,16 @@ function Icinga2AgentInstaller {
# #
$installer | Add-Member -membertype ScriptMethod -name 'exception' -value { $installer | Add-Member -membertype ScriptMethod -name 'exception' -value {
param([string]$message, [string[]]$args) param([string]$message, [string[]]$args)
$Error.clear(); $Error.clear();
throw 'Exception: ' + $message; throw 'Exception: ' + $message;
} }
# #
# Print the relevant exception # Print the relevant exception
# By reading the relevant info # By reading the relevant info
# from the stack # from the stack
# #
$installer | Add-Member -membertype ScriptMethod -name 'printLastException' -value { $installer | Add-Member -membertype ScriptMethod -name 'printLastException' -value {
Write-Host $this.error($error[$error.count - 1].FullyQualifiedErrorId) -ForegroundColor red; Write-Host $this.error($error[$error.count - 1].FullyQualifiedErrorId) -ForegroundColor red;
} }
@ -158,28 +158,28 @@ function Icinga2AgentInstaller {
Write-Host 'Notice:' $message -ForegroundColor green; Write-Host 'Notice:' $message -ForegroundColor green;
} }
# #
# Return a debug message with blue text # Return a debug message with blue text
# in case debug mode is enabled # in case debug mode is enabled
# #
$installer | Add-Member -membertype ScriptMethod -name 'debug' -value { $installer | Add-Member -membertype ScriptMethod -name 'debug' -value {
param([string] $message, [array] $args) param([string] $message, [array] $args)
if ($this.config('debug_mode')) { if ($this.config('debug_mode')) {
Write-Host 'Debug:' $message -ForegroundColor blue; Write-Host 'Debug:' $message -ForegroundColor blue;
} }
} }
# #
# Initialise certain parts of the # Initialise certain parts of the
# script first # script first
# #
$installer | Add-Member -membertype ScriptMethod -name 'init' -value { $installer | Add-Member -membertype ScriptMethod -name 'init' -value {
$this.setProperty('initialized', $TRUE); $this.setProperty('initialized', $TRUE);
# Set the default config dir # Set the default config dir
$this.setProperty('config_dir', $Env:ProgramData + '\icinga2\etc\icinga2\'); $this.setProperty('config_dir', $Env:ProgramData + '\icinga2\etc\icinga2\');
# Generate endpoint nodes based on iput # Generate endpoint nodes based on iput
# parameters # parameters
$this.generateEndpointNodes(); $this.generateEndpointNodes();
} }
# #
@ -204,9 +204,9 @@ function Icinga2AgentInstaller {
# #
$installer | Add-Member -membertype ScriptMethod -name 'generateEndpointNodes' -value { $installer | Add-Member -membertype ScriptMethod -name 'generateEndpointNodes' -value {
if (-Not $this.config('endpoints')) { if (-Not $this.config('endpoints')) {
$this.exception('You require to specify atleast one endpoint with parameter -Endpoints <nodes>'); $this.exception('You require to specify atleast one endpoint with parameter -Endpoints <nodes>');
} }
$endpoint_objects = ''; $endpoint_objects = '';
$endpoint_nodes = ''; $endpoint_nodes = '';
@ -247,13 +247,13 @@ function Icinga2AgentInstaller {
# Download the Icinga 2 Agent Installer from out defined source # Download the Icinga 2 Agent Installer from out defined source
# #
$installer | Add-Member -membertype ScriptMethod -name 'downloadInstaller' -value { $installer | Add-Member -membertype ScriptMethod -name 'downloadInstaller' -value {
if (-Not $this.config('agent_version')) { if (-Not $this.config('agent_version')) {
return; return;
} }
$WebStatusCode = 0; $WebStatusCode = 0;
$url = $this.config('download_url') + $this.getProperty('install_msi_package'); $url = $this.config('download_url') + $this.getProperty('install_msi_package');
$this.info('Downloading Icinga 2 Agent Binary from ' + $url + ' ...'); $this.info('Downloading Icinga 2 Agent Binary from ' + $url + ' ...');
$execptionMsg = ''; $execptionMsg = '';
Try { Try {
$WebStatusCode = Invoke-WebRequest -Method Head -Uri "$url" $WebStatusCode = Invoke-WebRequest -Method Head -Uri "$url"
@ -319,8 +319,8 @@ function Icinga2AgentInstaller {
$this.verifyInstallerChecksumAndThrowException(); $this.verifyInstallerChecksumAndThrowException();
$this.info('Installing Icinga 2 Agent'); $this.info('Installing Icinga 2 Agent');
Start-Process $this.getInstallerPath() -ArgumentList "/quiet" -wait; Start-Process $this.getInstallerPath() -ArgumentList "/quiet" -wait;
$this.info('Icinga 2 Agent installed.'); $this.info('Icinga 2 Agent installed.');
$this.setProperty('require_restart', 'true'); $this.setProperty('require_restart', 'true');
} }
# #
@ -332,17 +332,17 @@ function Icinga2AgentInstaller {
if (-Not $this.installerExists()) { if (-Not $this.installerExists()) {
$this.exception('Failed to update Icinga 2 Agent. Installer package not found.'); $this.exception('Failed to update Icinga 2 Agent. Installer package not found.');
} }
$this.verifyInstallerChecksumAndThrowException() $this.verifyInstallerChecksumAndThrowException()
if (-Not $this.getProperty('uninstall_id')) { if (-Not $this.getProperty('uninstall_id')) {
$this.exception('Failed to update Icinga 2 Agent. Uninstaller is not specified.'); $this.exception('Failed to update Icinga 2 Agent. Uninstaller is not specified.');
} }
$this.info('Removing previous Icinga 2 Agent version...'); $this.info('Removing previous Icinga 2 Agent version...');
Start-Process "MsiExec.exe" -ArgumentList ($this.getProperty('uninstall_id') +' /q') -wait; Start-Process "MsiExec.exe" -ArgumentList ($this.getProperty('uninstall_id') +' /q') -wait;
$this.info('Installing new Icinga 2 Agent version...'); $this.info('Installing new Icinga 2 Agent version...');
Start-Process $this.getInstallerPath() -ArgumentList "/quiet" -wait; Start-Process $this.getInstallerPath() -ArgumentList "/quiet" -wait;
$this.info('Agent successfully updated.'); $this.info('Agent successfully updated.');
$this.setProperty('require_restart', 'true'); $this.setProperty('require_restart', 'true');
} }
# #
@ -445,32 +445,32 @@ function Icinga2AgentInstaller {
# #
# Restart the Icinga 2 service and get the # Restart the Icinga 2 service and get the
# result if the restart failed or everything # result if the restart failed or everything
# worked as expected # worked as expected
# #
$installer | Add-Member -membertype ScriptMethod -name 'restartAgent' -value { $installer | Add-Member -membertype ScriptMethod -name 'restartAgent' -value {
$this.info("Restarting Icinga 2 service..."); $this.info("Restarting Icinga 2 service...");
Restart-Service icinga2; Restart-Service icinga2;
Start-Sleep -Seconds 2; Start-Sleep -Seconds 2;
$service = Get-WmiObject -Class Win32_Service -Filter "Name='icinga2'" $service = Get-WmiObject -Class Win32_Service -Filter "Name='icinga2'"
if (-Not ($service.State -eq 'Running')) { if (-Not ($service.State -eq 'Running')) {
$this.exception('Failed to restart Icinga 2 service.'); $this.exception('Failed to restart Icinga 2 service.');
} else { } else {
$this.info('Icinga 2 Agent successfully restarted.'); $this.info('Icinga 2 Agent successfully restarted.');
$this.setProperty('require_restart', ''); $this.setProperty('require_restart', '');
} }
} }
$installer | Add-Member -membertype ScriptMethod -name 'generateIcingaConfiguration' -value { $installer | Add-Member -membertype ScriptMethod -name 'generateIcingaConfiguration' -value {
$this.checkConfigInputParametersAndThrowException(); $this.checkConfigInputParametersAndThrowException();
$icingaCurrentConfig = ''; $icingaCurrentConfig = '';
if (Test-Path $this.getIcingaConfigFile()) { if (Test-Path $this.getIcingaConfigFile()) {
$icingaCurrentConfig = Get-Content $this.getIcingaConfigFile() -Raw; $icingaCurrentConfig = Get-Content $this.getIcingaConfigFile() -Raw;
} }
$icingaNewConfig = $icingaNewConfig =
'/** Icinga 2 Config - proposed by Icinga Director */ '/** Icinga 2 Config - proposed by Icinga Director */
include "constants.conf" include "constants.conf"
include <itl> include <itl>
@ -508,94 +508,94 @@ object ApiListener "api" {
accept_config = ' + $this.convertBoolToString($this.config('accept_config')) + ' accept_config = ' + $this.convertBoolToString($this.config('accept_config')) + '
}' }'
$this.setProperty('new_icinga_config', $icingaNewConfig); $this.setProperty('new_icinga_config', $icingaNewConfig);
$this.setProperty('old_icinga_config', $icingaCurrentConfig); $this.setProperty('old_icinga_config', $icingaCurrentConfig);
} }
# #
# Generate a hash for old and new config # Generate a hash for old and new config
# and determine if the configuration has changed # and determine if the configuration has changed
# #
$installer | Add-Member -membertype ScriptMethod -name 'hasConfigChanged' -value { $installer | Add-Member -membertype ScriptMethod -name 'hasConfigChanged' -value {
if (-Not $this.getProperty('new_icinga_config')) { if (-Not $this.getProperty('new_icinga_config')) {
$this.exception('New Icinga 2 configuration not generated. Please call "generateIcingaConfiguration" before.'); $this.exception('New Icinga 2 configuration not generated. Please call "generateIcingaConfiguration" before.');
} }
$oldConfigHash = $this.getHashFromString($this.getProperty('old_icinga_config')); $oldConfigHash = $this.getHashFromString($this.getProperty('old_icinga_config'));
$newConfigHash = $this.getHashFromString($this.getProperty('new_icinga_config')); $newConfigHash = $this.getHashFromString($this.getProperty('new_icinga_config'));
$this.debug('Old Config Hash: "' + $oldConfigHash + '" New Hash: "' + $newConfigHash + '"'); $this.debug('Old Config Hash: "' + $oldConfigHash + '" New Hash: "' + $newConfigHash + '"');
if ($oldConfigHash -eq $newConfigHash) { if ($oldConfigHash -eq $newConfigHash) {
return $FALSE; return $FALSE;
} }
return $TRUE; return $TRUE;
} }
# #
# Generate a SHA1 Hash from a provided string # Generate a SHA1 Hash from a provided string
# #
$installer | Add-Member -membertype ScriptMethod -name 'getHashFromString' -value { $installer | Add-Member -membertype ScriptMethod -name 'getHashFromString' -value {
param([string]$text) param([string]$text)
$algorithm = new-object System.Security.Cryptography.SHA1Managed $algorithm = new-object System.Security.Cryptography.SHA1Managed
$hash = [System.Text.Encoding]::UTF8.GetBytes($text) $hash = [System.Text.Encoding]::UTF8.GetBytes($text)
$hashInBytes = $algorithm.ComputeHash($hash) $hashInBytes = $algorithm.ComputeHash($hash)
foreach($byte in $hashInBytes) { foreach($byte in $hashInBytes) {
$result += $byte.ToString() $result += $byte.ToString()
} }
return $result; return $result;
} }
# #
# Return the path to the Icinga 2 config file # Return the path to the Icinga 2 config file
# #
$installer | Add-Member -membertype ScriptMethod -name 'getIcingaConfigFile' -value { $installer | Add-Member -membertype ScriptMethod -name 'getIcingaConfigFile' -value {
return ($this.getProperty('config_dir') + 'icinga2.conf'); return ($this.getProperty('config_dir') + 'icinga2.conf');
} }
# #
# Create Icinga 2 configuration file based # Create Icinga 2 configuration file based
# on Director settings # on Director settings
# #
$installer | Add-Member -membertype ScriptMethod -name 'writeConfig' -value { $installer | Add-Member -membertype ScriptMethod -name 'writeConfig' -value {
# Write new configuration to file # Write new configuration to file
$this.info('Writing icinga2.conf to ' + $this.getProperty('config_dir')); $this.info('Writing icinga2.conf to ' + $this.getProperty('config_dir'));
[System.IO.File]::WriteAllText($this.getIcingaConfigFile(), $this.getProperty('new_icinga_config')); [System.IO.File]::WriteAllText($this.getIcingaConfigFile(), $this.getProperty('new_icinga_config'));
$this.setProperty('require_restart', 'true'); $this.setProperty('require_restart', 'true');
} }
# #
# Write old coniguration again # Write old coniguration again
# just in case we received errors # just in case we received errors
# #
$installer | Add-Member -membertype ScriptMethod -name 'rollbackConfig' -value { $installer | Add-Member -membertype ScriptMethod -name 'rollbackConfig' -value {
# Write new configuration to file # Write new configuration to file
$this.info('Rolling back previous icinga2.conf to ' + $this.getProperty('config_dir')); $this.info('Rolling back previous icinga2.conf to ' + $this.getProperty('config_dir'));
[System.IO.File]::WriteAllText($this.getIcingaConfigFile(), $this.getProperty('old_icinga_config')); [System.IO.File]::WriteAllText($this.getIcingaConfigFile(), $this.getProperty('old_icinga_config'));
$this.setProperty('require_restart', 'true'); $this.setProperty('require_restart', 'true');
} }
# #
# Provide a result of an operation (string) and # Provide a result of an operation (string) and
# the intended match value. In case every was # the intended match value. In case every was
# ok, the function will return an info message # ok, the function will return an info message
# with the result. Otherwise it will thrown an # with the result. Otherwise it will thrown an
# exception # exception
# #
$installer | Add-Member -membertype ScriptMethod -name 'printResultOkOrException' -value { $installer | Add-Member -membertype ScriptMethod -name 'printResultOkOrException' -value {
param([string]$result, [string]$expected) param([string]$result, [string]$expected)
if ($result -And $expected) { if ($result -And $expected) {
if (-Not ($result -Like $expected)) { if (-Not ($result -Like $expected)) {
$this.exception($result); $this.exception($result);
} else { } else {
$this.info($result); $this.info($result);
} }
} elseif ($result) { } elseif ($result) {
$this.info($result); $this.info($result);
} }
} }
# #
# Generate the Icinga 2 SSL certificate to ensure the communication between the # Generate the Icinga 2 SSL certificate to ensure the communication between the
@ -603,9 +603,9 @@ object ApiListener "api" {
# #
$installer | Add-Member -membertype ScriptMethod -name 'generateCertificates' -value { $installer | Add-Member -membertype ScriptMethod -name 'generateCertificates' -value {
if (-Not $this.config('agent_name') -Or -Not $this.config('ca_server') -Or -Not $this.config('ticket')) { if (-Not $this.config('agent_name') -Or -Not $this.config('ca_server') -Or -Not $this.config('ticket')) {
$this.exception('One or more of the following arguments is missing: -AgentName <name> -CAServer <server> -Ticket <ticket>'); $this.exception('One or more of the following arguments is missing: -AgentName <name> -CAServer <server> -Ticket <ticket>');
} }
$icingaPkiDir = $this.getProperty('config_dir') + 'pki\'; $icingaPkiDir = $this.getProperty('config_dir') + 'pki\';
$icingaBinary = $this.getInstallPath() + '\sbin\icinga2.exe'; $icingaBinary = $this.getInstallPath() + '\sbin\icinga2.exe';
@ -614,137 +614,137 @@ object ApiListener "api" {
# Generate the certificate # Generate the certificate
$this.info("Generating Icinga 2 certificates"); $this.info("Generating Icinga 2 certificates");
$result = &$icingaBinary @('pki', 'new-cert', '--cn', $this.config('agent_name'), '--key', ($icingaPkiDir + $agentName + '.key'), '--cert', ($icingaPkiDir + $agentName + '.crt')); $result = &$icingaBinary @('pki', 'new-cert', '--cn', $this.config('agent_name'), '--key', ($icingaPkiDir + $agentName + '.key'), '--cert', ($icingaPkiDir + $agentName + '.crt'));
if ($LASTEXITCODE -ne 0) { if ($LASTEXITCODE -ne 0) {
$this.exception($result); $this.exception($result);
} else { } else {
$this.info($result); $this.info($result);
} }
# Save Certificate # Save Certificate
$this.info("Storing Icinga 2 certificates"); $this.info("Storing Icinga 2 certificates");
$result = &$icingaBinary @('pki', 'save-cert', '--key', ($icingaPkiDir + $agentName + '.key'), '--trustedcert', ($icingaPkiDir + 'trusted-master.crt'), '--host', $this.config('ca_server')); $result = &$icingaBinary @('pki', 'save-cert', '--key', ($icingaPkiDir + $agentName + '.key'), '--trustedcert', ($icingaPkiDir + 'trusted-master.crt'), '--host', $this.config('ca_server'));
if ($LASTEXITCODE -ne 0) { if ($LASTEXITCODE -ne 0) {
$this.exception($result); $this.exception($result);
} else { } else {
$this.info($result); $this.info($result);
} }
# Request certificate # Request certificate
$this.info("Requesting Icinga 2 certificates"); $this.info("Requesting Icinga 2 certificates");
$result = &$icingaBinary @('pki', 'request', '--host', $this.config('ca_server'), '--port', $this.config('ca_port'), '--ticket', $this.config('ticket'), '--key', ($icingaPkiDir + $agentName + '.key'), '--cert', ($icingaPkiDir + $agentName + '.crt'), '--trustedcert', ($icingaPkiDir + 'trusted-master.crt'), '--ca', ($icingaPkiDir + 'ca.crt')); $result = &$icingaBinary @('pki', 'request', '--host', $this.config('ca_server'), '--port', $this.config('ca_port'), '--ticket', $this.config('ticket'), '--key', ($icingaPkiDir + $agentName + '.key'), '--cert', ($icingaPkiDir + $agentName + '.crt'), '--trustedcert', ($icingaPkiDir + 'trusted-master.crt'), '--ca', ($icingaPkiDir + 'ca.crt'));
if ($LASTEXITCODE -ne 0) { if ($LASTEXITCODE -ne 0) {
$this.exception($result); $this.exception($result);
} else { } else {
$this.info($result); $this.info($result);
} }
$this.setProperty('require_restart', 'true'); $this.setProperty('require_restart', 'true');
} }
# #
# Check the Icinga install directory and determine # Check the Icinga install directory and determine
# if the certificates are both available for the # if the certificates are both available for the
# Agent. If not, return FALSE # Agent. If not, return FALSE
# #
$installer | Add-Member -membertype ScriptMethod -name 'hasCertificates' -value { $installer | Add-Member -membertype ScriptMethod -name 'hasCertificates' -value {
$icingaPkiDir = $this.getProperty('config_dir') + 'pki\'; $icingaPkiDir = $this.getProperty('config_dir') + 'pki\';
$agentName = $this.config('agent_name'); $agentName = $this.config('agent_name');
if ( if (
((Test-Path ($icingaPkiDir + $agentName + '.key')) ` ((Test-Path ($icingaPkiDir + $agentName + '.key')) `
-And (Test-Path ($icingaPkiDir + $agentName + '.crt'))) -And (Test-Path ($icingaPkiDir + $agentName + '.crt')))
) { ) {
return $TRUE; return $TRUE;
} }
return $FALSE; return $FALSE;
} }
# #
# Have we passed an argument to force # Have we passed an argument to force
# the creation of the certificates? # the creation of the certificates?
# #
$installer | Add-Member -membertype ScriptMethod -name 'forceCertificateGeneration' -value { $installer | Add-Member -membertype ScriptMethod -name 'forceCertificateGeneration' -value {
return $this.config('force_cert'); return $this.config('force_cert');
} }
# #
# Is the current Agent the version # Is the current Agent the version
# we would like to install? # we would like to install?
# #
$installer | Add-Member -membertype ScriptMethod -name 'isAgentUpToDate' -value { $installer | Add-Member -membertype ScriptMethod -name 'isAgentUpToDate' -value {
if ($this.canInstallAgent() -And $this.getProperty('agent_version') -eq $this.config('agent_version')) { if ($this.canInstallAgent() -And $this.getProperty('agent_version') -eq $this.config('agent_version')) {
return $TRUE; return $TRUE;
} }
return $FALSE return $FALSE
} }
# #
# Print a message telling us the installed # Print a message telling us the installed
# and intended version of the Agent # and intended version of the Agent
# #
$installer | Add-Member -membertype ScriptMethod -name 'printAgentUpdateMessage' -value { $installer | Add-Member -membertype ScriptMethod -name 'printAgentUpdateMessage' -value {
$this.info('Current Icinga 2 Agent Version (' + $this.getProperty('agent_version') + ') is not matching intended version (' + $this.config('agent_version') + '). Downloading new version...'); $this.info('Current Icinga 2 Agent Version (' + $this.getProperty('agent_version') + ') is not matching intended version (' + $this.config('agent_version') + '). Downloading new version...');
} }
# #
# Do we allow Agent updates / downgrades? # Do we allow Agent updates / downgrades?
# #
$installer | Add-Member -membertype ScriptMethod -name 'allowAgentUpdates' -value { $installer | Add-Member -membertype ScriptMethod -name 'allowAgentUpdates' -value {
return $this.config('allow_updates'); return $this.config('allow_updates');
} }
# #
# Have we specified a version to install the Agent? # Have we specified a version to install the Agent?
# #
$installer | Add-Member -membertype ScriptMethod -name 'canInstallAgent' -value { $installer | Add-Member -membertype ScriptMethod -name 'canInstallAgent' -value {
if (-Not $this.config('agent_version')) { if (-Not $this.config('agent_version')) {
return $FALSE; return $FALSE;
} }
return $TRUE; return $TRUE;
} }
# #
# Check if all required arguments for writing a valid # Check if all required arguments for writing a valid
# configuration are set # configuration are set
# #
$installer | Add-Member -membertype ScriptMethod -name 'checkConfigInputParametersAndThrowException' -value { $installer | Add-Member -membertype ScriptMethod -name 'checkConfigInputParametersAndThrowException' -value {
if (-Not $this.config('agent_name')) { if (-Not $this.config('agent_name')) {
$this.exception('Argument -AgentName <name> required for config generation.'); $this.exception('Argument -AgentName <name> required for config generation.');
} }
if (-Not $this.config('parent_zone')) { if (-Not $this.config('parent_zone')) {
$this.exception('Argument -ParentZone <name> required for config generation.'); $this.exception('Argument -ParentZone <name> required for config generation.');
} }
if (-Not $this.getProperty('endpoint_nodes') -Or -Not $this.getProperty('endpoint_objects')) { if (-Not $this.getProperty('endpoint_nodes') -Or -Not $this.getProperty('endpoint_objects')) {
$this.exception('Argument -Endpoints <name> requires atleast one defined endpoint.'); $this.exception('Argument -Endpoints <name> requires atleast one defined endpoint.');
} }
} }
# #
# Execute a check with Icinga2 daemon -C # Execute a check with Icinga2 daemon -C
# to ensure the configuration is valid # to ensure the configuration is valid
# #
$installer | Add-Member -membertype ScriptMethod -name 'isIcingaConfigValid' -value { $installer | Add-Member -membertype ScriptMethod -name 'isIcingaConfigValid' -value {
param([bool] $checkInternal = $TRUE) param([bool] $checkInternal = $TRUE)
if (-Not $this.config('parent_zone') -And $checkInternal) { if (-Not $this.config('parent_zone') -And $checkInternal) {
$this.exception('Parent Zone not defined. Please specify it with -ParentZone <name>'); $this.exception('Parent Zone not defined. Please specify it with -ParentZone <name>');
} }
$icingaBinary = $this.getInstallPath() + '\sbin\icinga2.exe'; $icingaBinary = $this.getInstallPath() + '\sbin\icinga2.exe';
$output = &$icingaBinary @('daemon', '-C'); $output = &$icingaBinary @('daemon', '-C');
if ($LASTEXITCODE -ne 0) { if ($LASTEXITCODE -ne 0) {
return $FALSE; return $FALSE;
} }
return $TRUE; return $TRUE;
} }
# #
# Returns true or false, depending # Returns true or false, depending
# if any changes were made requiring # if any changes were made requiring
# the Icinga 2 Agent to become restarted # the Icinga 2 Agent to become restarted
# #
$installer | Add-Member -membertype ScriptMethod -name 'madeChanges' -value { $installer | Add-Member -membertype ScriptMethod -name 'madeChanges' -value {
return $this.getProperty('require_restart'); return $this.getProperty('require_restart');
} }
# #
# This function will try to load all # This function will try to load all
@ -754,68 +754,68 @@ object ApiListener "api" {
# specified # specified
# #
$installer | Add-Member -membertype ScriptMethod -name 'unattendedInstall' -value { $installer | Add-Member -membertype ScriptMethod -name 'unattendedInstall' -value {
try { try {
if (-Not $this.isAdmin()) { if (-Not $this.isAdmin()) {
return $FALSE; return $FALSE;
} }
# Try to locate the current # Try to locate the current
# Installation data from the Agent # Installation data from the Agent
if ($this.isAgentInstalled()) { if ($this.isAgentInstalled()) {
if (-Not $this.isAgentUpToDate()) { if (-Not $this.isAgentUpToDate()) {
if ($this.allowAgentUpdates()) { if ($this.allowAgentUpdates()) {
$this.printAgentUpdateMessage(); $this.printAgentUpdateMessage();
$this.updateAgent(); $this.updateAgent();
$this.cleanupAgentInstaller(); $this.cleanupAgentInstaller();
} }
} else { } else {
$this.info('Icinga 2 Agent is up-to-date. Nothing to do.'); $this.info('Icinga 2 Agent is up-to-date. Nothing to do.');
} }
} else { } else {
if ($this.canInstallAgent()){ if ($this.canInstallAgent()){
$this.installAgent(); $this.installAgent();
$this.cleanupAgentInstaller(); $this.cleanupAgentInstaller();
} else { } else {
$this.exception('Icinga 2 Agent is not installed and not allowed of beeing installed. Nothing to do.'); $this.exception('Icinga 2 Agent is not installed and not allowed of beeing installed. Nothing to do.');
} }
} }
if (-Not $this.hasCertificates() -Or $this.forceCertificateGeneration()) { if (-Not $this.hasCertificates() -Or $this.forceCertificateGeneration()) {
$this.generateCertificates(); $this.generateCertificates();
} else { } else {
$this.info('Icinga 2 certificates already exist. Nothing to do.'); $this.info('Icinga 2 certificates already exist. Nothing to do.');
} }
$this.generateIcingaConfiguration(); $this.generateIcingaConfiguration();
if ($this.hasConfigChanged()) { if ($this.hasConfigChanged()) {
$this.backupDefaultConfig(); $this.backupDefaultConfig();
$this.writeConfig(); $this.writeConfig();
# Check if the config is valid and rollback otherwise # Check if the config is valid and rollback otherwise
if (-Not $this.isIcingaConfigValid()) { if (-Not $this.isIcingaConfigValid()) {
$this.error('Icinga 2 config validation failed. Rolling back to previous version.'); $this.error('Icinga 2 config validation failed. Rolling back to previous version.');
$this.rollbackConfig(); $this.rollbackConfig();
if ($this.isIcingaConfigValid($FALSE)) { if ($this.isIcingaConfigValid($FALSE)) {
$this.info('Rollback of Icinga 2 configuration successfull.'); $this.info('Rollback of Icinga 2 configuration successfull.');
} else { } else {
$this.exception('Icinga 2 config rollback failed. Please check the icinga2.log'); $this.exception('Icinga 2 config rollback failed. Please check the icinga2.log');
} }
} else { } else {
$this.info('Icinga 2 configuration check successfull.'); $this.info('Icinga 2 configuration check successfull.');
} }
} else { } else {
$this.info('icinga2.conf did not change. Nothing to do'); $this.info('icinga2.conf did not change. Nothing to do');
} }
if ($this.madeChanges()) { if ($this.madeChanges()) {
$this.restartAgent(); $this.restartAgent();
} else { } else {
$this.info('No changes detected.'); $this.info('No changes detected.');
} }
} catch { } catch {
$this.printLastException(); $this.printLastException();
return $FALSE; return $FALSE;
} }
return $TRUE; return $TRUE;
} }