|
| 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 |
0 commit comments