Skip to content

Commit 49dd593

Browse files
author
Soeren
committed
Merge pull request #53 from Jimdo/puppet_provider
Implement starting/stopping/getting status of supervisor programs as a puppet provider
2 parents cd8243a + e7ec321 commit 49dd593

5 files changed

Lines changed: 425 additions & 9 deletions

File tree

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,5 +5,6 @@
55
spec/fixtures/
66
Gemfile.lock
77
.rspec-local
8+
.rbenv*
89
.idea
910
atlassian-ide-plugin.xml
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
# Manage services using Supervisor. Start/stop uses /sbin/service and enable/disable uses chkconfig
2+
3+
Puppet::Type.type(:service).provide :supervisor, :parent => :base do
4+
5+
desc "Supervisor: A daemontools-like service monitor written in python"
6+
7+
commands :supervisorctl => "/usr/bin/supervisorctl"
8+
9+
def name_without_prefix
10+
@resource[:name].gsub(/^supervisor::/, '')
11+
end
12+
13+
def group_or_process_name
14+
name_without_prefix
15+
end
16+
17+
def supervisorctl_arg
18+
name_without_prefix + ':*'
19+
end
20+
21+
def status
22+
begin
23+
output = supervisorctl(:status)
24+
rescue Puppet::ExecutionFailure
25+
return :stopped
26+
end
27+
28+
filtered_output = output.lines.grep /#{self.group_or_process_name}[ :]/
29+
if filtered_output.empty?
30+
return :stopped
31+
end
32+
33+
status_is_starting = filtered_output.grep(/STARTING/)
34+
unless status_is_starting.empty?
35+
Puppet.warning "Could not reliably determine status: #{self.group_or_process_name} is still starting"
36+
end
37+
38+
status_not_running = filtered_output.reject {|item| item =~ /RUNNING|STARTING/}
39+
if status_not_running.empty?
40+
return :running
41+
end
42+
43+
:stopped
44+
end
45+
46+
def restart
47+
output = supervisorctl(:restart, self.supervisorctl_arg)
48+
49+
if output.include? 'ERROR (no such process)' or output.include? 'ERROR (abnormal termination)'
50+
raise Puppet::Error, "Could not restart #{self.group_or_process_name}: #{output}"
51+
end
52+
end
53+
54+
def start
55+
output = supervisorctl(:start, self.supervisorctl_arg)
56+
57+
if output.include? 'ERROR (no such process)' or output.include? 'ERROR (abnormal termination)'
58+
raise Puppet::Error, "Could not start #{self.group_or_process_name}: #{output}"
59+
end
60+
61+
filtered_output = output.lines.reject {|item| item.include? "ERROR (already started)"}
62+
63+
status_not_started = filtered_output.reject {|item| item =~ /started$/}
64+
unless status_not_started.empty?
65+
raise Puppet::Error, "Could not start #{self.group_or_process_name}: #{output}"
66+
end
67+
end
68+
69+
def stop
70+
output = supervisorctl(:stop, self.supervisorctl_arg)
71+
72+
if output.include? 'ERROR (no such process)'
73+
raise Puppet::Error, "Could not stop #{self.group_or_process_name}: #{output}"
74+
end
75+
76+
if output =~ /^error/
77+
raise Puppet::Error, "Could not stop #{self.group_or_process_name}: #{output}"
78+
end
79+
end
80+
81+
end

manifests/service.pp

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -77,20 +77,24 @@
7777
require => Class['supervisor'],
7878
}
7979

80-
file { "${supervisor::conf_dir}/${name}${supervisor::conf_ext}":
80+
$conf_file = "${supervisor::conf_dir}/${name}${supervisor::conf_ext}"
81+
82+
file { $conf_file:
8183
ensure => $config_ensure,
8284
content => template('supervisor/service.ini.erb'),
83-
require => File["/var/log/supervisor/${name}"],
84-
notify => Class['supervisor::update'],
8585
}
8686

8787
service { "supervisor::${name}":
8888
ensure => $service_ensure,
89-
provider => base,
90-
restart => "/usr/bin/supervisorctl restart ${process_name} | awk '/^${name}[: ]/{print \$2}' | grep -Pzo '^stopped\\nstarted$'",
91-
start => "/usr/bin/supervisorctl start ${process_name} | awk '/^${name}[: ]/{print \$2}' | grep '^started$'",
92-
status => "/usr/bin/supervisorctl status | awk '/^${name}[: ]/{print \$2}' | grep '^RUNNING$'",
93-
stop => "/usr/bin/supervisorctl stop ${process_name} | awk '/^${name}[: ]/{print \$2}' | grep '^stopped$'",
94-
require => [Class['supervisor::update'], File["${supervisor::conf_dir}/${name}${supervisor::conf_ext}"]],
89+
provider => supervisor,
90+
}
91+
92+
if $ensure == 'present' {
93+
File["/var/log/supervisor/${name}"] -> File[$conf_file] ~>
94+
Class['supervisor::update'] -> Service["supervisor::${name}"]
95+
} else { # $ensure == 'absent'
96+
# First stop the service, delete the .ini, reload the config, delete the log dir
97+
Service["supervisor::${name}"] -> File[$conf_file] ~>
98+
Class['supervisor::update'] -> File["/var/log/supervisor/${name}"]
9599
}
96100
}

spec/defines/service_spec.rb

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,18 @@ class { 'supervisor':
4242
should create_file('/etc/supervisor/sometitle.ini') \
4343
.with_content(Regexp.new Regexp.escape 'command=somecommand')
4444
end
45+
46+
context "with ensure => absent" do
47+
let (:params) { {
48+
:command => 'somecommand',
49+
:ensure => 'absent',
50+
} }
51+
52+
it "should delete config and log dir" do
53+
should contain_file('/etc/supervisor/sometitle.ini').with_ensure('absent')
54+
should contain_file('/var/log/supervisor/sometitle').with_ensure('absent')
55+
end
56+
end
4557
end
4658

4759
end

0 commit comments

Comments
 (0)