diff --git a/chef/lib/chef/provider/service/debian.rb b/chef/lib/chef/provider/service/debian.rb index f7235b5c994..c806882bbe5 100644 --- a/chef/lib/chef/provider/service/debian.rb +++ b/chef/lib/chef/provider/service/debian.rb @@ -51,16 +51,51 @@ def service_currently_enabled? assert_update_rcd_available status = popen4("/usr/sbin/update-rc.d -n -f #{@current_resource.service_name} remove") do |pid, stdin, stdout, stderr| + priority = {} + enabled = false + status_re = /etc\/rc([\dS]).d\/([SK])(\d\d)/i stdout.each_line do |line| - return true if line =~ UPDATE_RC_D_ENABLED_MATCHES + if status_re.match(line) + priority[$1] = [($2 == "S" ? :start : :stop), $3] + end + if line =~ UPDATE_RC_D_ENABLED_MATCHES + enabled = true + end end + @current_resource.enabled enabled + @current_resource.priority priority end unless status.exitstatus == 0 raise Chef::Exceptions::Service, "/usr/sbin/update-rc.d -n -f #{@current_resource.service_name} failed - #{status.inspect}" end - - false + @current_resource.enabled + end + + def enable_service() + # If we have a priority which is just a number, we have to + # construct the actual priority object + + if @new_resource.priority.is_a? Integer + run_command(:command => "/usr/sbin/update-rc.d #{@new_resource.service_name} defaults #{@new_resource.priority} #{100 - @new_resource.priority}") + elsif @new_resource.priority.is_a? Hash + args = "" + @new_resource.priority.each do |level, o| + action = o[0] + priority = o[1] + args += "#{action} #{priority} #{level} . " + end + run_command(:command => "/usr/sbin/update-rc.d #{@new_resource.service_name} #{args}") + else # No priority, go with update-rc.d defaults + run_command(:command => "/usr/sbin/update-rc.d #{@new_resource.service_name} defaults") + end + + end + + def disable_service() + run_command(:command => "/usr/sbin/update-rc.d #{@new_resource.service_name} disable") +# @new_resource.priority({2 => [:stop, 80]}) +# enable_service end end diff --git a/chef/lib/chef/resource/service.rb b/chef/lib/chef/resource/service.rb index d118f09c804..6720cc4882c 100644 --- a/chef/lib/chef/resource/service.rb +++ b/chef/lib/chef/resource/service.rb @@ -34,6 +34,7 @@ def initialize(name, run_context=nil) @status_command = nil @restart_command = nil @reload_command = nil + @priority = nil @action = "nothing" @startup_type = :automatic @supports = { :restart => false, :reload => false, :status => false } @@ -119,6 +120,22 @@ def running(arg=nil) ) end + # Priority arguments can have two forms: + # + # - a simple number, in which the default start runlevels get + # that as the start value and stop runlevels get 100 - value. + # + # - a hash like { 2 => [:start, 20], 3 => [:stop, 55] }, where + # the service will be marked as started with priority 20 in + # runlevel 2, stopped in 3 with priority 55 and no symlinks or + # similar for other runlevels + # + def priority(arg=nil) + set_or_return(:priority, + arg, + :kind_of => [ Integer, String, Hash ]) + end + def supports(args={}) if args.is_a? Array args.each { |arg| @supports[arg] = true } diff --git a/chef/spec/unit/provider/service/debian_service_spec.rb b/chef/spec/unit/provider/service/debian_service_spec.rb index 94c87ac4cee..bcb9918d4df 100644 --- a/chef/spec/unit/provider/service/debian_service_spec.rb +++ b/chef/spec/unit/provider/service/debian_service_spec.rb @@ -102,18 +102,39 @@ end - describe "when enabling a service" do - + describe "when enabling a service without priority" do it "should call update-rc.d 'service_name' defaults" do @provider.should_receive(:run_command).with({:command => "/usr/sbin/update-rc.d #{@new_resource.service_name} defaults"}) @provider.enable_service() end end + + describe "when enabling a service with simple priority" do + before do + @new_resource.stub!(:priority).and_return(75) + end + it "should call update-rc.d 'service_name' defaults" do + @provider.should_receive(:run_command).with({:command => "/usr/sbin/update-rc.d #{@new_resource.service_name} defaults 75 25"}) + @provider.enable_service() + end + end + + describe "when enabling a service with complex priorities" do + before do + @new_resource.stub!(:priority).and_return({ 2 => [:start, 20], 3 => [:stop, 55] }) + end + + it "should call update-rc.d 'service_name' defaults" do + @provider.should_receive(:run_command).with({:command => "/usr/sbin/update-rc.d #{@new_resource.service_name} start 20 2 . stop 55 3 . "}) + @provider.enable_service() + end + end + describe "when disabling a service" do - it "should call update-rc.d -f 'service_name' remove" do - @provider.should_receive(:run_command).with({:command => "/usr/sbin/update-rc.d -f #{@new_resource.service_name} remove"}) + it "should call update-rc.d 'service_name' disable" do + @provider.should_receive(:run_command).with({:command => "/usr/sbin/update-rc.d #{@new_resource.service_name} disable"}) @provider.disable_service() end end