Skip to content

Commit

Permalink
Implement priority support
Browse files Browse the repository at this point in the history
Allow setting the priority of a service using one of two syntaxes:

service "foo" do
    priority 20
end

service "foo" do
    priority { 2 => [ :start, 20 ], 3 => [ :stop, 80 ] }
end

The former will enable the service with priority 20 in the default
runlevel.  It will be ignored if the system does not have a priority
or runlevel concept.  The second syntax will set the start priority to
20 in runlevel 2 and stop priority to 80 in runlevel 3.
  • Loading branch information
tfheen authored and danielsdeleo committed Jul 19, 2010
1 parent 264b7cb commit ad90e98
Show file tree
Hide file tree
Showing 3 changed files with 80 additions and 7 deletions.
41 changes: 38 additions & 3 deletions chef/lib/chef/provider/service/debian.rb
Expand Up @@ -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
Expand Down
17 changes: 17 additions & 0 deletions chef/lib/chef/resource/service.rb
Expand Up @@ -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 }
Expand Down Expand Up @@ -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 }
Expand Down
29 changes: 25 additions & 4 deletions chef/spec/unit/provider/service/debian_service_spec.rb
Expand Up @@ -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
Expand Down

0 comments on commit ad90e98

Please sign in to comment.