Skip to content

Commit

Permalink
feat: allow uninstalling the Zabbix agent
Browse files Browse the repository at this point in the history
feat: add tests for new "ensure" parameter in zabbix::agent
  • Loading branch information
coreone committed Jun 25, 2024
1 parent 051f24c commit 43679ad
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 9 deletions.
31 changes: 22 additions & 9 deletions manifests/agent.pp
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
# @summary This will install and configure the zabbix-agent deamon
# @param ensure Ensure that the agent is either present or absent
# @param zabbix_version This is the zabbix version.
# @param zabbix_package_state The state of the package that needs to be installed: present or latest.
# @param zabbix_package_agent The name of the agent package that we manage
Expand Down Expand Up @@ -147,6 +148,7 @@
#
# @author Werner Dijkerman [email protected]
class zabbix::agent (
Enum['absent', 'present'] $ensure = 'present',
$zabbix_version = $zabbix::params::zabbix_version,
$zabbix_package_state = $zabbix::params::zabbix_package_state,
$zabbix_package_agent = $zabbix::params::zabbix_package_agent,
Expand Down Expand Up @@ -232,6 +234,16 @@
) inherits zabbix::params {
$agent2 = $zabbix_package_agent == 'zabbix-agent2'

# Set up overrides if the ensure parameter is set to 'absent'
$dir_ensure = $ensure ? { 'absent' => $ensure, default => 'directory' }
$file_ensure = $ensure ? { 'absent' => $ensure, default => 'file' }
$include_dir_force = $ensure ? { 'absent' => true, default => undef }
$_include_dir_purge = $ensure ? { 'absent' => true, default => $include_dir_purge }
$_manage_repo = $ensure ? { 'absent' => false, default => $manage_repo }
$_service_enable = $ensure ? { 'absent' => false, default => $service_enable }
$_service_ensure = $ensure ? { 'absent' => 'stopped', default => $service_ensure }
$_zabbix_package_state = $ensure ? { 'absent' => $ensure, default => $zabbix_package_state }

# Find if listenip is set. If not, we can set to specific ip or
# to network name. If more than 1 interfaces are available, we
# can find the ipaddress of this specific interface if listenip
Expand Down Expand Up @@ -276,7 +288,7 @@
# Only include the repo class if it has not yet been included
unless defined(Class['Zabbix::Repo']) {
class { 'zabbix::repo':
manage_repo => $manage_repo,
manage_repo => $_manage_repo,
zabbix_version => $zabbix_version,
}
}
Expand All @@ -291,7 +303,7 @@
} else {
assert_type(Stdlib::Windowspath, $zabbix_package_source)
package { $zabbix_package_agent:
ensure => $zabbix_package_state,
ensure => $_zabbix_package_state,
tag => 'zabbix',
provider => $zabbix_package_provider,
source => $zabbix_package_source,
Expand All @@ -302,7 +314,7 @@
else {
# Installing the package
package { $zabbix_package_agent:
ensure => $zabbix_package_state,
ensure => $_zabbix_package_state,
require => Class['zabbix::repo'],
tag => 'zabbix',
provider => $zabbix_package_provider,
Expand Down Expand Up @@ -336,8 +348,8 @@

# Controlling the 'zabbix-agent' service
service { $servicename:
ensure => $service_ensure,
enable => $service_enable,
ensure => $_service_ensure,
enable => $_service_enable,
require => $service_require,
}

Expand All @@ -352,7 +364,7 @@

# Configuring the zabbix-agent configuration file
file { $agent_configfile_path:
ensure => file,
ensure => $file_ensure,
owner => $agent_config_owner,
group => $agent_config_group,
mode => '0644',
Expand All @@ -364,11 +376,12 @@

# Include dir for specific zabbix-agent checks.
file { $include_dir:
ensure => directory,
ensure => $dir_ensure,
owner => $agent_config_owner,
group => $agent_config_group,
force => $include_dir_force,
recurse => true,
purge => $include_dir_purge,
purge => $_include_dir_purge,
notify => Service[$servicename],
require => File[$agent_configfile_path],
}
Expand All @@ -394,7 +407,7 @@
# https://support.zabbix.com/browse/ZBX-11631
if fact('os.selinux.enabled') == true and $manage_selinux {
selinux::module { 'zabbix-agent':
ensure => 'present',
ensure => $ensure,
content_te => template('zabbix/selinux/zabbix-agent.te.erb'),
before => Service[$servicename],
}
Expand Down
26 changes: 26 additions & 0 deletions spec/classes/agent_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -264,6 +264,32 @@
it { is_expected.to contain_file(config_path).with_content %r{^Alias=name2$} }
end

context 'when declaring ensure is absent' do
let :params do
{
ensure: 'absent'
}
end

if facts[:kernel] == 'Linux'
it { is_expected.to contain_package(package_name).with_ensure('absent') }
end

it { is_expected.to contain_class('zabbix::repo').with_manage_repo(false) }
it do
is_expected.to contain_service(service_name).
with_ensure('stopped').
with_enable(false).
that_requires("Package[#{package_name}]")
end
it do
is_expected.to contain_file(include_dir).
with_ensure('absent').
with_force(true).
with_purge(true)
end
end

context 'configuration file with full options' do
if facts[:kernel] == 'Linux'
let :params do
Expand Down

0 comments on commit 43679ad

Please sign in to comment.