diff options
| author | Mathieu Sauve-Frankel <msf@kisoku.net> | 2009-09-07 13:32:34 +0900 |
|---|---|---|
| committer | AJ Christensen <aj@junglist.gen.nz> | 2009-09-07 16:56:58 +1200 |
| commit | fadea36b29e59d04ad58884982f3caee0154b8c7 (patch) | |
| tree | 296916dd02c9f13d20860410d3bb6b6ab2f99f2c | |
| parent | 06d0628bc6ff056795834854f574a08011a95630 (diff) | |
| download | chef-fadea36b29e59d04ad58884982f3caee0154b8c7.tar.gz | |
refactor init to inherit from simple
| -rw-r--r-- | chef/lib/chef/provider/service/init.rb | 67 | ||||
| -rw-r--r-- | chef/lib/chef/provider/service/simple.rb | 58 |
2 files changed, 48 insertions, 77 deletions
diff --git a/chef/lib/chef/provider/service/init.rb b/chef/lib/chef/provider/service/init.rb index 2d3a03e637..39f3a63ee7 100644 --- a/chef/lib/chef/provider/service/init.rb +++ b/chef/lib/chef/provider/service/init.rb @@ -17,76 +17,22 @@ # require 'chef/provider/service' +require 'chef/provider/service/simple' require 'chef/mixin/command' class Chef class Provider class Service - class Init < Chef::Provider::Service + class Init < Chef::Provider::Service::Simple def initialize(node, new_resource) super(node, new_resource) @init_command = "/etc/init.d/#{@new_resource.service_name}" end - def load_current_resource - @current_resource = Chef::Resource::Service.new(@new_resource.name) - @current_resource.service_name(@new_resource.service_name) - if @new_resource.status_command - Chef::Log.debug("#{@new_resource} you have specified a status command, running..") - - begin - if run_command(:command => @new_resource.status_command) == 0 - @current_resource.running true - end - rescue Chef::Exceptions::Exec - @current_resource.running false - nil - end - - elsif @new_resource.supports[:status] - Chef::Log.debug("#{@new_resource} supports status, running") - - begin - if run_command(:command => "#{@init_command} status") == 0 - @current_resource.running true - end - rescue Chef::Exceptions::Exec - @current_resource.running false - nil - end - - else - Chef::Log.debug("#{@new_resource} does not support status and you have not specified a status command, falling back to process table inspection") - - if @node[:command][:ps].nil? or @node[:command][:ps].empty? - raise Chef::Exceptions::Service, "#{@new_resource}: could not determine how to inspect the process table, please set this nodes 'ps' attribute" - end - - status = popen4(@node[:command][:ps]) do |pid, stdin, stdout, stderr| - r = Regexp.new(@new_resource.pattern) - Chef::Log.debug("#{@new_resource}: attempting to match #{@new_resource.pattern} (#{r}) against process table") - stdout.each_line do |line| - if r.match(line) - @current_resource.running true - break - end - end - @current_resource.running false unless @current_resource.running - end - unless status.exitstatus == 0 - raise Chef::Exceptions::Service, "Command #{@node[:command][:ps]} failed" - else - Chef::Log.debug("#{@new_resource}: #{@node[:command][:ps]} exited and parsed successfully, process running: #{@current_resource.running}") - end - end - - @current_resource - end - def start_service if @new_resource.start_command - run_command(:command => @new_resource.start_command) + super else run_command(:command => "#{@init_command} start") end @@ -94,7 +40,7 @@ class Chef def stop_service if @new_resource.stop_command - run_command(:command => @new_resource.stop_command) + super else run_command(:command => "#{@init_command} stop") end @@ -102,7 +48,7 @@ class Chef def restart_service if @new_resource.restart_command - run_command(:command => @new_resource.restart_command) + super elsif @new_resource.supports[:restart] run_command(:command => "#{@init_command} restart") else @@ -114,12 +60,11 @@ class Chef def reload_service if @new_resource.reload_command - run_command(:command => @new_resource.reload_command) + super elsif @new_resource.supports[:reload] run_command(:command => "#{@init_command} reload") end end - end end end diff --git a/chef/lib/chef/provider/service/simple.rb b/chef/lib/chef/provider/service/simple.rb index ab443858ea..68d6ec7f7f 100644 --- a/chef/lib/chef/provider/service/simple.rb +++ b/chef/lib/chef/provider/service/simple.rb @@ -26,27 +26,53 @@ class Chef def load_current_resource @current_resource = Chef::Resource::Service.new(@new_resource.name) @current_resource.service_name(@new_resource.service_name) - - if @node[:command][:ps].nil? or @node[:command][:ps].empty? - raise Chef::Exceptions::Service, "#{@new_resource}: could not determine how to inspect the process table, please set this nodes 'ps' attribute" - end + if @new_resource.status_command + Chef::Log.debug("#{@new_resource} you have specified a status command, running..") - status = popen4(@node[:command][:ps]) do |pid, stdin, stdout, stderr| - stdin.close - r = Regexp.new(@new_resource.pattern) - Chef::Log.debug("#{@new_resource}: attempting to match #{@new_resource.pattern} (#{r}) against process table") - stdout.each_line do |line| - if r.match(line) + begin + if run_command(:command => @new_resource.status_command) == 0 @current_resource.running true - break end + rescue Chef::Exceptions::Exec + @current_resource.running false + nil end - @current_resource.running false unless @current_resource.running - end - unless status.exitstatus == 0 - raise Chef::Exceptions::Service, "Command #{@node[:command][:ps]} failed" + + elsif @new_resource.supports[:status] + Chef::Log.debug("#{@new_resource} supports status, running") + + begin + if run_command(:command => "#{@init_command} status") == 0 + @current_resource.running true + end + rescue Chef::Exceptions::Exec + @current_resource.running false + nil + end + else - Chef::Log.debug("#{@new_resource}: #{@node[:command][:ps]} exited and parsed successfully, process running: #{@current_resource.running}") + Chef::Log.debug("#{@new_resource} does not support status and you have not specified a status command, falling back to process table inspection") + if @node[:command][:ps].nil? or @node[:command][:ps].empty? + raise Chef::Exceptions::Service, "#{@new_resource}: could not determine how to inspect the process table, please set this nodes 'ps' attribute" + end + + status = popen4(@node[:command][:ps]) do |pid, stdin, stdout, stderr| + stdin.close + r = Regexp.new(@new_resource.pattern) + Chef::Log.debug("#{@new_resource}: attempting to match #{@new_resource.pattern} (#{r}) against process table") + stdout.each_line do |line| + if r.match(line) + @current_resource.running true + break + end + end + @current_resource.running false unless @current_resource.running + end + unless status.exitstatus == 0 + raise Chef::Exceptions::Service, "Command #{@node[:command][:ps]} failed" + else + Chef::Log.debug("#{@new_resource}: #{@node[:command][:ps]} exited and parsed successfully, process running: #{@current_resource.running}") + end end @current_resource |
