diff options
author | AJ Christensen <aj@junglist.gen.nz> | 2008-09-25 03:28:01 +1200 |
---|---|---|
committer | AJ Christensen <aj@junglist.gen.nz> | 2008-10-01 09:34:28 +1300 |
commit | 5c89c995cb0bbecfbd15badba571b60b46b75134 (patch) | |
tree | 8c7526c620c47ed21e6a081a5f03b5952ae11da8 /lib/chef | |
parent | 922bed81a0504a7fe08f8edafd3419a7906cd03b (diff) | |
download | chef-5c89c995cb0bbecfbd15badba571b60b46b75134.tar.gz |
Working Debian service provider for enabling/disabling services.
TODO: Base and Init providers that support status and start/stopping init scripts.
Diffstat (limited to 'lib/chef')
-rw-r--r-- | lib/chef/exceptions.rb | 3 | ||||
-rw-r--r-- | lib/chef/node.rb | 2 | ||||
-rw-r--r-- | lib/chef/platform.rb | 4 | ||||
-rw-r--r-- | lib/chef/provider/service.rb | 39 | ||||
-rw-r--r-- | lib/chef/provider/service/base.rb | 18 | ||||
-rw-r--r-- | lib/chef/provider/service/debian.rb | 56 | ||||
-rw-r--r-- | lib/chef/provider/service/init.rb | 26 | ||||
-rw-r--r-- | lib/chef/resource/service.rb | 95 |
8 files changed, 161 insertions, 82 deletions
diff --git a/lib/chef/exceptions.rb b/lib/chef/exceptions.rb index a2125e862f..a70692b001 100644 --- a/lib/chef/exceptions.rb +++ b/lib/chef/exceptions.rb @@ -20,8 +20,9 @@ class Chef class Exec < RuntimeError; end class FileNotFound < RuntimeError; end class Package < RuntimeError; end + class Service < RuntimeError; end class SearchIndex < RuntimeError; end class Override < RuntimeError; end class UnsupportedAction < RuntimeError; end end -end
\ No newline at end of file +end diff --git a/lib/chef/node.rb b/lib/chef/node.rb index 9ad20cddc8..4eb72e1856 100644 --- a/lib/chef/node.rb +++ b/lib/chef/node.rb @@ -249,4 +249,4 @@ class Chef end end -end
\ No newline at end of file +end diff --git a/lib/chef/platform.rb b/lib/chef/platform.rb index 0345445fbe..c6c4a7580c 100644 --- a/lib/chef/platform.rb +++ b/lib/chef/platform.rb @@ -27,6 +27,7 @@ class Chef :ubuntu => { :default => { :package => Chef::Provider::Apt, + :service => Chef::Provider::Debian, } }, :centos => {}, @@ -43,6 +44,7 @@ class Chef :sysctl => Chef::Provider::Sysctl, :execute => Chef::Provider::Execute, :script => Chef::Provider::Script, + :service => Chef::Provider::Init, :perl => Chef::Provider::Script, :python => Chef::Provider::Script, :ruby => Chef::Provider::Script, @@ -190,4 +192,4 @@ class Chef end end -end
\ No newline at end of file +end diff --git a/lib/chef/provider/service.rb b/lib/chef/provider/service.rb index 79041727eb..de33984328 100644 --- a/lib/chef/provider/service.rb +++ b/lib/chef/provider/service.rb @@ -30,19 +30,11 @@ class Chef end def action_enable - do_service = false - if @current_resource.enabled == nil - do_service = true - elsif @new_resource.enabled != nil - if @new_resource.enabled != @current_resource.enabled - do_service = true - end - end - - if do_package + if @current_resource.enabled == false + Chef::Log.debug("Trying to enable #{@new_resource}") status = enable_service(@new_resource.service_name) if status - @new_resource.enabled = true + @new_resource.enabled == true Chef::Log.info("Enabled service #{@new_resource} successfully") end end @@ -50,25 +42,32 @@ class Chef def action_disable if @current_resource.enabled == true - disable_service(@new_resource.service_name) - @new_resource.enabled = false - Chef::Log.info("Disabled service #{@new_resource} succesfully") + Chef::Log.debug("Trying to disable #{@new_resource}") + status = disable_service(@new_resource.service_name) + if status + @new_resource.enabled == false + Chef::Log.info("Disabled service #{@new_resource} succesfully") + end end end def action_start if @current_resource.running == false - start_service(@new_resource.service_name) - @new_resource.running = true - Chef::Log.info("Started service #{@new_resource} succesfully") + status = start_service(@new_resource.service_name) + if status + @new_resource.running == true + Chef::Log.info("Started service #{@new_resource} succesfully") + end end end def action_stop if @current_resource.running == true - start_service(@new_resource.service_name) - @new_resource.running = false - Chef::Log.info("Stopped service #{@new_resource} succesfully") + status = stop_service(@new_resource.service_name) + if status + @new_resource.running == false + Chef::Log.info("Stopped service #{@new_resource} succesfully") + end end end diff --git a/lib/chef/provider/service/base.rb b/lib/chef/provider/service/base.rb index d194c33fc7..178878296e 100644 --- a/lib/chef/provider/service/base.rb +++ b/lib/chef/provider/service/base.rb @@ -21,22 +21,12 @@ require File.join(File.dirname(__FILE__), "..", "..", "mixin", "command") class Chef class Provider - class Service - class Base < Chef::Provider::Service + class Base < Chef::Provider::Service - def load_current_resource - @current_resource - end + def load_current_resource + true + end - def start_service - run_command(:command => "/etc/init.d/#{name} start") - end - - def stop_service - run_command(:command => "/etc/init.d/#{name} stop") - end - - end end end end diff --git a/lib/chef/provider/service/debian.rb b/lib/chef/provider/service/debian.rb index a8944163f6..2a4c385a55 100644 --- a/lib/chef/provider/service/debian.rb +++ b/lib/chef/provider/service/debian.rb @@ -21,38 +21,36 @@ require File.join(File.dirname(__FILE__), "..", "..", "mixin", "command") class Chef class Provider - class Service - class Debian < Chef::Provider::Service::Init - - def load_current_resource - @current_resource = Chef::Resource::Service.new(@new_resource.name) - @current_resource.service_name(@new_resource.service_name) - - status = popen4("update-rc.d -n -f #{@new_resource.service_name}") do |pid, stdin, stdout, stderr| - stdin.close - if stdout.gets(nil) =~ /etc\/rc[\dS].d\/S|not installed/ - @current_resource.enabled(true) - else - @current_resource.enabled(false) - end - end - - unless status.exitstatus = 0 - raise Chef::Exception::Service, "update-rc.d -n -f #{@new_resource.service_name} failed - #{status.inspect}" + class Debian < Chef::Provider::Service + def load_current_resource + @current_resource = Chef::Resource::Service.new(@new_resource.name) + @current_resource.service_name(@new_resource.service_name) + + status = popen4("update-rc.d -n -f #{@current_resource.service_name} remove") do |pid, stdin, stdout, stderr| + stdin.close + if stdout.gets(nil) =~ /etc\/rc[\dS].d\/S|not installed/ + Chef::Log.debug("#{@current_resource} is currently enabled") + @current_resource.enabled(true) + else + Chef::Log.debug("#{@current_resource} is currently disabled") + @current_resource.enabled(false) end + end - @current_resource - end - - def enable_service(name) - run_command(:command => "update-rc.d #{name} defaults") + unless status.exitstatus == 0 + raise Chef::Exception::Service, "update-rc.d -n -f #{@current_resource.service_name} failed - #{status.inspect}" end - - def disable_service(name) - run_command(:command => "update-rc.d -f #{name} remove") - run_command(:command => "update-rc.d #{name} stop 00 1 2 3 4 5 6 .") - end - + + @current_resource + end + + def enable_service(name) + run_command(:command => "update-rc.d #{name} defaults") + end + + def disable_service(name) + run_command(:command => "update-rc.d -f #{name} remove") + #run_command(:command => "update-rc.d #{name} stop 00 1 2 3 4 5 6 .") end end end diff --git a/lib/chef/provider/service/init.rb b/lib/chef/provider/service/init.rb index aa673e15e8..95275499f8 100644 --- a/lib/chef/provider/service/init.rb +++ b/lib/chef/provider/service/init.rb @@ -21,22 +21,20 @@ require File.join(File.dirname(__FILE__), "..", "..", "mixin", "command") class Chef class Provider - class Service - class Init < Chef::Provider::Service::Base - - def load_current_resource - @current_resource - end - - def start_service - run_command(:command => "/etc/init.d/#{name} start") - end - - def stop_service - run_command(:command => "/etc/init.d/#{name} stop") - end + class Init < Chef::Provider::Service + def load_current_resource + true end + + def start_service + run_command(:command => "/etc/init.d/#{name} start") + end + + def stop_service + run_command(:command => "/etc/init.d/#{name} stop") + end + end end end diff --git a/lib/chef/resource/service.rb b/lib/chef/resource/service.rb index a94b1f4fb5..5d7ef03f4b 100644 --- a/lib/chef/resource/service.rb +++ b/lib/chef/resource/service.rb @@ -31,10 +31,101 @@ class Chef @start = nil @stop = nil @status = nil - @action = "enable" + @restart = nil + @action = "none" @allowed_actions.push(:enable, :disable, :start, :stop) end - + + def service_name(arg=nil) + set_or_return( + :service_name, + arg, + :kind_of => [ String ] + ) + end + + # bool, init script has a status action + def hasstatus(arg=nil) + set_or_return( + :hasstatus, + arg, + :kind_of => [ String ] + ) + end + + # bool, init script has a restart action + def hasrestart(arg=nil) + set_or_return( + :hasrestart, + arg, + :kind_of => [ TrueClass, FalseClass ] + ) + end + + # regex for match against ps -ef when hasstatus => false and status = nil + def pattern(arg=nil) + set_or_return( + :pattern, + arg, + :kind_of => [ Regex ] + ) + end + + # command to call to start service + def start(arg=nil) + set_or_return( + :start, + arg, + :kind_of => [ String ] + ) + end + + # command to call to stop service + def stop(arg=nil) + set_or_return( + :stop, + arg, + :kind_of => [ String ] + ) + end + + # command to call to get status of service + def status(arg=nil) + set_or_return( + :status, + arg, + :kind_of => [ String ] + ) + end + + # command to call to restart service + def restart(arg=nil) + set_or_return( + :restart, + arg, + :kind_of => [ String ] + ) + end + + # if the service is enabled or not + def enabled(arg=nil) + set_or_return( + :enabled, + arg, + :kind_of => [ TrueClass, FalseClass ] + ) + end + + # if the service is running or not + def running(arg=nil) + set_or_return( + :running, + arg, + :kind_of => [ TrueClass, FalseClass ] + ) + end + + end end end |