diff options
author | Robby Dyer <rdyer@tropo.com> | 2014-06-17 14:53:46 -0400 |
---|---|---|
committer | Lamont Granquist <lamont@scriptkiddie.org> | 2015-08-18 13:40:45 -0700 |
commit | b5e9c70e43b8d37eee1fb9f5aaae391125c4cc22 (patch) | |
tree | 3d60b52c13c5ce007c00ba9cf3409beca15af1d7 | |
parent | 1bdf739b844489424283852af56f5a8f2b8f362a (diff) | |
download | chef-b5e9c70e43b8d37eee1fb9f5aaae391125c4cc22.tar.gz |
CHEF-5372: Support specific run_levels for RedHat service
-rw-r--r-- | lib/chef/provider/service/redhat.rb | 24 | ||||
-rw-r--r-- | lib/chef/resource/service.rb | 11 | ||||
-rw-r--r-- | spec/unit/provider/service/redhat_spec.rb | 34 |
3 files changed, 64 insertions, 5 deletions
diff --git a/lib/chef/provider/service/redhat.rb b/lib/chef/provider/service/redhat.rb index 19cd2aa485..a471d4bbe4 100644 --- a/lib/chef/provider/service/redhat.rb +++ b/lib/chef/provider/service/redhat.rb @@ -1,6 +1,6 @@ # # Author:: AJ Christensen (<aj@hjksolutions.com>) -# Copyright:: Copyright (c) 2008 Opscode, Inc. +# Copyright:: Copyright (c) 2008-2015 Chef Software, Inc. # License:: Apache License, Version 2.0 # # Licensed under the Apache License, Version 2.0 (the "License"); @@ -39,6 +39,7 @@ class Chef @init_command = "/sbin/service #{@new_resource.service_name}" @new_resource.supports[:status] = true @service_missing = false + @run_levels = @new_resource.run_levels end def define_resource_requirements @@ -62,19 +63,34 @@ class Chef if ::File.exists?("/sbin/chkconfig") chkconfig = shell_out!("/sbin/chkconfig --list #{@current_resource.service_name}", :returns => [0,1]) - @current_resource.enabled(!!(chkconfig.stdout =~ CHKCONFIG_ON)) + unless @run_levels.nil? or @run_levels.empty? + chkconfig.split(/\s+/)[1..-1].each do |level| + index = level.split(':').first + status = level.split(':').last + is_enabled = false + is_enabled = true if @run_levels.include?(index) and status =~ CHKCONFIG_ON + @current_resource.enabled(is_enabled) + end + else + @current_resource.enabled(!!(chkconfig.stdout =~ CHKCONFIG_ON)) + end @service_missing = !!(chkconfig.stderr =~ CHKCONFIG_MISSING) end @current_resource end + def levels + (@run_levels.nil? or @run_levels.empty?) ? levels = "" : levels = "--level #{@run_levels.join('')} " + levels + end + def enable_service() - shell_out! "/sbin/chkconfig #{@new_resource.service_name} on" + shell_out! "/sbin/chkconfig #{levels}#{@new_resource.service_name} on" end def disable_service() - shell_out! "/sbin/chkconfig #{@new_resource.service_name} off" + shell_out! "/sbin/chkconfig #{levels}#{@new_resource.service_name} off" end end end diff --git a/lib/chef/resource/service.rb b/lib/chef/resource/service.rb index aa59b543be..e65f1bf724 100644 --- a/lib/chef/resource/service.rb +++ b/lib/chef/resource/service.rb @@ -1,7 +1,7 @@ # # Author:: AJ Christensen (<aj@hjksolutions.com>) # Author:: Tyler Cloke (<tyler@opscode.com>) -# Copyright:: Copyright (c) 2008 Opscode, Inc. +# Copyright:: Copyright (c) 2008-2015 Chef Software, Inc. # License:: Apache License, Version 2.0 # # Licensed under the Apache License, Version 2.0 (the "License"); @@ -44,6 +44,8 @@ class Chef @init_command = nil @priority = nil @timeout = nil + @run_levels = nil + @action = "nothing" @supports = { :restart => false, :reload => false, :status => false } end @@ -174,6 +176,13 @@ class Chef ) end + def run_levels(arg=nil) + set_or_return( + :run_levels, + arg, + :kind_of => [ Array ] ) + end + def supports(args={}) if args.is_a? Array args.each { |arg| @supports[arg] = true } diff --git a/spec/unit/provider/service/redhat_spec.rb b/spec/unit/provider/service/redhat_spec.rb index 73cfec8a6f..78be98f533 100644 --- a/spec/unit/provider/service/redhat_spec.rb +++ b/spec/unit/provider/service/redhat_spec.rb @@ -83,6 +83,28 @@ describe "Chef::Provider::Service::Redhat" do expect(@provider.load_current_resource).to eql(@current_resource) expect(@current_resource.enabled).to be_falsey end + + it "sets the current enabled status to true if the service is enabled at specified run levels" do + status = double("Status", :exitstatus => 0, :stdout => "" , :stderr => "") + @current_resource.instance_variable_set(:@run_levels, [ 1, 2 ]) + @provider.should_receive(:shell_out).with("/sbin/service chef status").and_return(status) + chkconfig = double("Chkconfig", :exitstatus => 0, :stdout => "chef 0:off 1:on 2:on 3:off 4:off 5:off 6:off", :stderr => "") + @provider.should_receive(:shell_out!).with("/sbin/chkconfig --list chef", :returns => [0,1]).and_return(chkconfig) + @provider.instance_variable_get("@service_missing").should be_false + @provider.load_current_resource.should + @current_resource.enabled.should be_true + end + + it "sets the current enabled status to false if the service is not enabled at specified run levels" do + status = double("Status", :exitstatus => 0, :stdout => "" , :stderr => "") + @current_resource.instance_variable_set(:@run_levels, [ 2 ]) + @provider.should_receive(:shell_out).with("/sbin/service chef status").and_return(status) + chkconfig = double("Chkconfig", :exitstatus => 0, :stdout => "chef 0:off 1:on 2:off 3:off 4:off 5:off 6:off", :stderr => "") + @provider.should_receive(:shell_out!).with("/sbin/chkconfig --list chef", :returns => [0,1]).and_return(chkconfig) + @provider.instance_variable_get("@service_missing").should be_false + @provider.load_current_resource.should + @current_resource.enabled.should be_true + end end describe "define resource requirements" do @@ -144,6 +166,12 @@ describe "Chef::Provider::Service::Redhat" do expect(@provider).to receive(:shell_out!).with("/sbin/chkconfig #{@new_resource.service_name} on") @provider.enable_service end + + it "should call chkconfig to add 'service_name' at specified run_levels" do + @provider.instance_variable_set(:@run_levels, [ 1,2 ]) + @provider.should_receive(:shell_out!).with("/sbin/chkconfig --level 12 #{@new_resource.service_name} on") + @provider.enable_service + end end describe "disable_service" do @@ -151,6 +179,12 @@ describe "Chef::Provider::Service::Redhat" do expect(@provider).to receive(:shell_out!).with("/sbin/chkconfig #{@new_resource.service_name} off") @provider.disable_service end + + it "should call chkconfig to del 'service_name' at specified run_levels" do + @provider.instance_variable_set(:@run_levels, [ 1,2 ]) + @provider.should_receive(:shell_out!).with("/sbin/chkconfig --level 12 #{@new_resource.service_name} off") + @provider.disable_service + end end end |