Skip to content

Commit 82164e2

Browse files
committed
Fix ubuntu 16.04 mixed systemd/sysvinit style configuration
1 parent 7551a86 commit 82164e2

File tree

3 files changed

+101
-66
lines changed

3 files changed

+101
-66
lines changed

manifests/init.pp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -358,7 +358,7 @@
358358
}
359359
}
360360

361-
if $facts['os']['family'] == 'Debian' and $facts['service_provider'] == 'systemd' {
361+
if $facts['service_provider'] == 'systemd' and $snmp::sysconfig == undef {
362362
systemd::dropin_file { 'snmpd.conf':
363363
unit => 'snmpd.service',
364364
content => epp($template_snmpd_systemd_dropin, { 'snmpd_options' => $snmpd_options, }),

manifests/params.pp

Lines changed: 29 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -115,18 +115,38 @@
115115
$varnetsnmp_group = 'snmp'
116116
}
117117

118-
if $::facts['service_provider'] == 'systemd' {
119-
$sysconfig = undef
120-
$trap_sysconfig = undef
121-
$snmpd_options = "-Lsd -Lf /dev/null -u ${varnetsnmp_owner} -g ${varnetsnmp_group} -I -smux -p /var/run/snmpd.pid -f"
122-
$snmptrapd_options = '-Lsd -p /var/run/snmptrapd.pid -f'
118+
# Config / Services on debian/ubuntu are a mess:
119+
# - Debian * + sysvinit -> config in /etc/default + uses init scripts
120+
# - Debian == 8 + systemd -> config in /etc/default + systemctl uses init scripts
121+
# - Ubuntu == 16.04 + systemd -> config in /etc/default + systemctl uses init scripts and needs -p as /run/snmpd.pid
122+
# - Debian >=9 + Systemd -> config in /etc/systemd/*dropin* + pure systemctl (need -f)
123+
# - Ubuntu >= 18.04 + systemd -> config in /etc/systemd/*dropin* + pure systemctl (need -f)
124+
125+
if $facts['service_provider'] == 'systemd' {
126+
if $facts['os']['name'] == 'Ubuntu' and versioncmp($majordistrelease, '18.04') < 0 {
127+
$sysconfig = '/etc/default/snmpd'
128+
$trap_sysconfig = '/etc/default/snmptrapd'
129+
} elsif $facts['os']['name'] == 'Debian' and versioncmp($majordistrelease, '9') < 0 {
130+
$sysconfig = '/etc/default/snmpd'
131+
$trap_sysconfig = '/etc/default/snmptrapd'
132+
} else {
133+
$sysconfig = undef
134+
$trap_sysconfig = undef
135+
}
123136
} else {
124-
$sysconfig = '/etc/default/snmpd'
125-
$trap_sysconfig = '/etc/default/snmptrapd'
126-
$snmpd_options = "-Lsd -Lf /dev/null -u ${varnetsnmp_owner} -g ${varnetsnmp_group} -I -smux -p /var/run/snmpd.pid"
127-
$snmptrapd_options = '-Lsd -p /var/run/snmptrapd.pid'
137+
$sysconfig = '/etc/default/snmpd'
138+
$trap_sysconfig = '/etc/default/snmptrapd'
128139
}
129140

141+
if $facts['os']['name'] == 'Ubuntu' {
142+
$snmpd_options = "-Lsd -Lf /dev/null -u ${varnetsnmp_owner} -g ${varnetsnmp_group} -I -smux -p /run/snmpd.pid"
143+
$snmptrapd_options = '-Lsd -p /run/snmptrapd.pid'
144+
} else {
145+
$snmpd_options = "-Lsd -Lf /dev/null -u ${varnetsnmp_owner} -g ${varnetsnmp_group} -I -smux -p /var/run/snmpd.pid"
146+
$snmptrapd_options = '-Lsd -p /var/run/snmptrapd.pid'
147+
}
148+
149+
130150
$package_name = 'snmpd'
131151
$snmptrapd_package_name = 'snmptrapd'
132152
$service_config = '/etc/snmp/snmpd.conf'

spec/classes/snmp_init_spec.rb

Lines changed: 71 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -541,6 +541,15 @@
541541
end
542542
end
543543

544+
let(:pid_path) do
545+
case facts[:os]['name']
546+
when 'Ubuntu'
547+
'/run'
548+
else
549+
'/var/run'
550+
end
551+
end
552+
544553
it {
545554
is_expected.to contain_package('snmpd').with(
546555
ensure: 'present',
@@ -630,13 +639,13 @@
630639
it 'contains File[snmpd.sysconfig] with contents "SNMPDOPTS=\'-Lsd -Lf /dev/null -u snmp -g snmp -I -smux -p /var/run/snmpd.pid\'"' do
631640
verify_contents(catalogue, 'snmpd.sysconfig', [
632641
'SNMPDRUN=yes',
633-
"SNMPDOPTS='-Lsd -Lf /dev/null -u #{snmp_user} -g #{snmp_user} -I -smux -p /var/run/snmpd.pid'"
642+
"SNMPDOPTS='-Lsd -Lf /dev/null -u #{snmp_user} -g #{snmp_user} -I -smux -p #{pid_path}/snmpd.pid'"
634643
])
635644
end
636645
it 'contains File[snmptrapd.sysconfig] with contents "TRAPDOPTS=\'-Lsd -p /var/run/snmptrapd.pid\'"' do
637646
verify_contents(catalogue, 'snmptrapd.sysconfig', [
638647
'TRAPDRUN=yes',
639-
'TRAPDOPTS=\'-Lsd -p /var/run/snmptrapd.pid\''
648+
"TRAPDOPTS='-Lsd -p #{pid_path}/snmptrapd.pid'"
640649
])
641650
end
642651
end
@@ -670,71 +679,77 @@
670679
end
671680
end
672681

673-
context 'with systemd' do
674-
let(:facts) do
675-
facts.merge(service_provider: 'systemd')
676-
end
682+
# Deal with the weird part where ubuntu mixes systemd and sysvinit configuration.
683+
mixed_systemd_sysvinit = (facts[:os]['name'] == 'Ubuntu' && facts[:os]['release']['major'] == '16.04') \
684+
|| (facts[:os]['name'] == 'Debian' && facts[:os]['release']['major'] == '8')
677685

678-
it { is_expected.not_to contain_file('snmptrapd.sysconfig') }
679-
it { is_expected.not_to contain_file('snmpd.sysconfig') }
680-
681-
it {
682-
is_expected.to contain_service('snmpd').with(
683-
ensure: 'running',
684-
name: 'snmpd',
685-
enable: true,
686-
hasstatus: true,
687-
hasrestart: true
688-
).that_requires(['Package[snmpd]', 'File[var-net-snmp]'])
689-
}
686+
unless mixed_systemd_sysvinit
687+
context 'with systemd (only!)' do
688+
let(:facts) do
689+
facts.merge(service_provider: 'systemd')
690+
end
690691

691-
describe 'default params' do
692-
let(:params) { {} }
692+
it { is_expected.not_to contain_file('snmptrapd.sysconfig') }
693+
it { is_expected.not_to contain_file('snmpd.sysconfig') }
693694

694-
it 'contains systemd dropin file for snmpd with execstart' do
695-
is_expected.to contain_systemd__dropin_file('snmpd.conf').with(
696-
unit: 'snmpd.service'
697-
).that_requires('Package[snmpd]').that_notifies('Service[snmpd]')
695+
it {
696+
is_expected.to contain_service('snmpd').with(
697+
ensure: 'running',
698+
name: 'snmpd',
699+
enable: true,
700+
hasstatus: true,
701+
hasrestart: true
702+
).that_requires(['Package[snmpd]', 'File[var-net-snmp]'])
703+
}
698704

699-
expected_lines = [
700-
'[Service]',
701-
'ExecStart=',
702-
"ExecStart=/usr/sbin/snmpd -Lsd -Lf /dev/null -u #{snmp_user} -g #{snmp_user} -I -smux -p /var/run/snmpd.pid -f"
703-
]
704-
verify_contents(catalogue, '/etc/systemd/system/snmpd.service.d/snmpd.conf', expected_lines)
705+
describe 'default params' do
706+
let(:params) { {} }
707+
708+
it 'contains systemd dropin file for snmpd with execstart' do
709+
is_expected.to contain_systemd__dropin_file('snmpd.conf').with(
710+
unit: 'snmpd.service'
711+
).that_requires('Package[snmpd]').that_notifies('Service[snmpd]')
712+
713+
expected_lines = [
714+
'[Service]',
715+
'ExecStart=',
716+
"ExecStart=/usr/sbin/snmpd -Lsd -Lf /dev/null -u #{snmp_user} -g #{snmp_user} -I -smux -p #{pid_path}/snmpd.pid -f"
717+
]
718+
verify_contents(catalogue, '/etc/systemd/system/snmpd.service.d/snmpd.conf', expected_lines)
719+
end
705720
end
706-
end
707-
describe 'ensure_service => stopped, trap_service_ensure => running' do
708-
let :params do
709-
{
710-
service_ensure: 'stopped',
711-
trap_service_ensure: 'running'
712-
}
721+
describe 'ensure_service => stopped, trap_service_ensure => running' do
722+
let :params do
723+
{
724+
service_ensure: 'stopped',
725+
trap_service_ensure: 'running'
726+
}
727+
end
728+
729+
it { is_expected.to contain_service('snmpd').with_ensure('stopped') }
730+
it { is_expected.to contain_service('snmptrapd').with_ensure('running') }
713731
end
714732

715-
it { is_expected.to contain_service('snmpd').with_ensure('stopped') }
716-
it { is_expected.to contain_service('snmptrapd').with_ensure('running') }
717-
end
733+
describe 'snmpd_options => blah' do
734+
let(:params) { { snmpd_options: 'blah' } }
718735

719-
describe 'snmpd_options => blah' do
720-
let(:params) { { snmpd_options: 'blah' } }
721-
722-
it { is_expected.to contain_systemd__dropin_file('snmpd.conf') }
723-
it 'contains systemd drop-in file with contents ExecStart' do
724-
verify_contents(catalogue, '/etc/systemd/system/snmpd.service.d/snmpd.conf', [
725-
'ExecStart=/usr/sbin/snmpd blah -f'
726-
])
736+
it { is_expected.to contain_systemd__dropin_file('snmpd.conf') }
737+
it 'contains systemd drop-in file with contents ExecStart' do
738+
verify_contents(catalogue, '/etc/systemd/system/snmpd.service.d/snmpd.conf', [
739+
'ExecStart=/usr/sbin/snmpd blah -f'
740+
])
741+
end
727742
end
728-
end
729743

730-
describe 'snmptrapd_options => bleh' do
731-
let(:params) { { snmptrapd_options: 'bleh' } }
744+
describe 'snmptrapd_options => bleh' do
745+
let(:params) { { snmptrapd_options: 'bleh' } }
732746

733-
it { is_expected.to contain_systemd__dropin_file('snmptrapd.conf') }
734-
it 'contains systemd drop-in file with contents ExecStart' do
735-
verify_contents(catalogue, '/etc/systemd/system/snmptrapd.service.d/snmptrapd.conf', [
736-
'ExecStart=/usr/sbin/snmptrapd bleh -f'
737-
])
747+
it { is_expected.to contain_systemd__dropin_file('snmptrapd.conf') }
748+
it 'contains systemd drop-in file with contents ExecStart' do
749+
verify_contents(catalogue, '/etc/systemd/system/snmptrapd.service.d/snmptrapd.conf', [
750+
'ExecStart=/usr/sbin/snmptrapd bleh -f'
751+
])
752+
end
738753
end
739754
end
740755
end

0 commit comments

Comments
 (0)