Skip to content

Commit 45f451d

Browse files
committed
Adding Redhat init provider, fixing CHEF-56 - also refactoring service providers in general to be a bit more flexible
1 parent 54c22b4 commit 45f451d

11 files changed

Lines changed: 271 additions & 54 deletions

File tree

chef/lib/chef/platform.rb

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,8 +47,18 @@ class Platform
4747
:cron => Chef::Provider::Cron,
4848
}
4949
},
50-
:centos => {},
51-
:redhat => {},
50+
:centos => {
51+
:default => {
52+
:service => Chef::Provider::Service::Redhat,
53+
:cron => Chef::Provider::Cron
54+
}
55+
},
56+
:redhat => {
57+
:default => {
58+
:service => Chef::Provider::Service::Redhat,
59+
:cron => Chef::Provider::Cron
60+
}
61+
},
5262
:gentoo => {
5363
:default => {
5464
:package => Chef::Provider::Package::Portage,

chef/lib/chef/provider/service.rb

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ def initialize(node, new_resource)
3333
def action_enable
3434
unless @current_resource.enabled
3535
Chef::Log.debug("#{@new_resource}: attempting to enable")
36-
status = enable_service(@new_resource.service_name)
36+
status = enable_service()
3737
if status
3838
Chef::Log.info("#{@new_resource}: enabled succesfully")
3939
end
@@ -45,7 +45,7 @@ def action_enable
4545
def action_disable
4646
if @current_resource.enabled
4747
Chef::Log.debug("#{@new_resource}: attempting to disable")
48-
status = disable_service(@new_resource.service_name)
48+
status = disable_service()
4949
if status
5050
Chef::Log.info("#{@new_resource}: disabled succesfully")
5151
end
@@ -57,7 +57,7 @@ def action_disable
5757
def action_start
5858
unless @current_resource.running
5959
Chef::Log.debug("#{@new_resource}: attempting to start")
60-
status = start_service(@new_resource.service_name)
60+
status = start_service()
6161
if status
6262
Chef::Log.info("Started service #{@new_resource} succesfully")
6363
end
@@ -69,7 +69,7 @@ def action_start
6969
def action_stop
7070
if @current_resource.running
7171
Chef::Log.debug("#{@new_resource}: attempting to stop")
72-
status = stop_service(@new_resource.service_name)
72+
status = stop_service()
7373
if status
7474
Chef::Log.info("#{@new_resource}: stopped succesfully")
7575
end
@@ -81,7 +81,7 @@ def action_stop
8181
def action_restart
8282
if @current_resource.running
8383
Chef::Log.debug("#{@new_resource}: attempting to restart")
84-
status = restart_service(@new_resource.service_name)
84+
status = restart_service()
8585
if status
8686
Chef::Log.info("#{@new_resource}: restarted succesfully")
8787
end
@@ -94,7 +94,7 @@ def action_reload
9494
else
9595
if @current_resource.running
9696
Chef::Log.debug("#{@new_resource}: attempting to reload")
97-
status = reload_service(@new_resource.service_name)
97+
status = reload_service()
9898
if status
9999
Chef::Log.info("#{@new_resource}: reloaded succesfully")
100100
end

chef/lib/chef/provider/service/debian.rb

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -51,12 +51,12 @@ def load_current_resource
5151
@current_resource
5252
end
5353

54-
def enable_service(name)
55-
run_command(:command => "/usr/sbin/update-rc.d #{name} defaults")
54+
def enable_service()
55+
run_command(:command => "/usr/sbin/update-rc.d #{@new_resource.service_name} defaults")
5656
end
5757

58-
def disable_service(name)
59-
run_command(:command => "/usr/sbin/update-rc.d -f #{name} remove")
58+
def disable_service()
59+
run_command(:command => "/usr/sbin/update-rc.d -f #{@new_resource.service_name} remove")
6060
end
6161

6262
end

chef/lib/chef/provider/service/gentoo.rb

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -45,12 +45,12 @@ def load_current_resource
4545
@current_resource
4646
end
4747

48-
def enable_service(name)
49-
run_command(:command => "/sbin/rc-update add #{name} default")
48+
def enable_service()
49+
run_command(:command => "/sbin/rc-update add #{@new_resource.service_name} default")
5050
end
5151

52-
def disable_service(name)
53-
run_command(:command => "/sbin/rc-update del #{name} default")
52+
def disable_service()
53+
run_command(:command => "/sbin/rc-update del #{@new_resource.service_name} default")
5454
end
5555
end
5656
end

chef/lib/chef/provider/service/init.rb

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,11 @@ class Chef
2323
class Provider
2424
class Service
2525
class Init < Chef::Provider::Service
26+
27+
def initialize(node, new_resource)
28+
super(node, new_resource)
29+
@init_command ||= "/etc/init.d/#{@new_resource.service_name}"
30+
end
2631

2732
def load_current_resource
2833
@current_resource = Chef::Resource::Service.new(@new_resource.name)
@@ -31,7 +36,7 @@ def load_current_resource
3136
Chef::Log.debug("#{@new_resource} supports status, running")
3237

3338
begin
34-
if run_command(:command => "/etc/init.d/#{@current_resource.service_name} status") == 0
39+
if run_command(:command => "#{@init_command} status") == 0
3540
@current_resource.running true
3641
end
3742
rescue Chef::Exception::Exec
@@ -80,37 +85,37 @@ def load_current_resource
8085
@current_resource
8186
end
8287

83-
def start_service(name)
88+
def start_service
8489
if @new_resource.start_command
8590
run_command(:command => @new_resource.start_command)
8691
else
87-
run_command(:command => "/etc/init.d/#{name} start")
92+
run_command(:command => "#{@init_command} start")
8893
end
8994
end
9095

91-
def stop_service(name)
96+
def stop_service
9297
if @new_resource.stop_command
9398
run_command(:command => @new_resource.stop_command)
9499
else
95-
run_command(:command => "/etc/init.d/#{name} stop")
100+
run_command(:command => "#{@init_command} stop")
96101
end
97102
end
98103

99-
def restart_service(name)
104+
def restart_service
100105
if @new_resource.supports[:restart]
101-
run_command(:command => "/etc/init.d/#{name} restart")
106+
run_command(:command => "#{@init_command} restart")
102107
elsif @new_resource.restart_command
103108
run_command(:command => @new_resource.restart_command)
104109
else
105-
stop_service(name)
110+
stop_service
106111
sleep 1
107-
start_service(name)
112+
start_service
108113
end
109114
end
110115

111-
def reload_service(name)
116+
def reload_service
112117
if @new_resource.supports[:reload]
113-
run_command(:command => "/etc/init.d/#{name} reload")
118+
run_command(:command => "#{@init_command} reload")
114119
elsif @new_resource.reload_command
115120
run_command(:command => @new_resource.reload_command)
116121
end
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
#
2+
# Author:: AJ Christensen (<aj@hjksolutions.com>)
3+
# Copyright:: Copyright (c) 2008 Opscode, Inc.
4+
# License:: Apache License, Version 2.0
5+
#
6+
# Licensed under the Apache License, Version 2.0 (the "License");
7+
# you may not use this file except in compliance with the License.
8+
# You may obtain a copy of the License at
9+
#
10+
# http://www.apache.org/licenses/LICENSE-2.0
11+
#
12+
# Unless required by applicable law or agreed to in writing, software
13+
# distributed under the License is distributed on an "AS IS" BASIS,
14+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
# See the License for the specific language governing permissions and
16+
# limitations under the License.
17+
#
18+
19+
require 'chef/provider/service'
20+
require 'chef/provider/service/init'
21+
require 'chef/mixin/command'
22+
23+
class Chef
24+
class Provider
25+
class Service
26+
class Redhat < Chef::Provider::Service::Init
27+
28+
def initialize(node, new_resource)
29+
super(node, new_resource)
30+
@init_command ||= "/sbin/service #{@new_resource.service_name}"
31+
end
32+
33+
def load_current_resource
34+
super
35+
36+
unless ::File.exists? "/sbin/chkconfig"
37+
raise Chef::Exception::Service, "/sbin/chkconfig does not exist!"
38+
end
39+
40+
status = popen4("/sbin/chkconfig --list #{@current_resource.service_name}") do |pid, stdin, stdout, stderr|
41+
stdin.close
42+
43+
if stdout.gets =~ /\d:on/
44+
@current_resource.enabled true
45+
else
46+
@current_resource.enabled false
47+
end
48+
end
49+
50+
unless status.exitstatus == 0
51+
raise Chef::Exception::Service, "/sbin/chkconfig --list #{@current_resource.service_name} failed - #{status.inspect}"
52+
end
53+
54+
@current_resource
55+
end
56+
57+
def enable_service()
58+
run_command(:command => "/sbin/chkconfig --add #{@new_resource.service_name}")
59+
end
60+
61+
def disable_service()
62+
run_command(:command => "/sbin/chkconfig --del #{@new_resource.service_name}")
63+
end
64+
65+
end
66+
end
67+
end
68+
end

chef/spec/unit/provider/service/debian_service_spec.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@
119119

120120
it "should call update-rc.d 'service_name' defaults" do
121121
@provider.should_receive(:run_command).with({:command => "/usr/sbin/update-rc.d #{@new_resource.service_name} defaults"})
122-
@provider.enable_service(@new_resource.service_name)
122+
@provider.enable_service()
123123
end
124124
end
125125

@@ -138,6 +138,6 @@
138138

139139
it "should call update-rc.d -f 'service_name' remove" do
140140
@provider.should_receive(:run_command).with({:command => "/usr/sbin/update-rc.d -f #{@new_resource.service_name} remove"})
141-
@provider.disable_service(@new_resource.service_name)
141+
@provider.disable_service()
142142
end
143143
end

chef/spec/unit/provider/service/gentoo_service_spec.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -78,14 +78,14 @@
7878
describe Chef::Provider::Service::Gentoo, "enable_service" do
7979
it "should call rc-update add *service* default" do
8080
@provider.should_receive(:run_command).with({:command => "/sbin/rc-update add chef default"})
81-
@provider.enable_service('chef')
81+
@provider.enable_service()
8282
end
8383
end
8484

8585
describe Chef::Provider::Service::Gentoo, "disable_service" do
8686
it "should call rc-update del *service* default" do
8787
@provider.should_receive(:run_command).with({:command => "/sbin/rc-update del chef default"})
88-
@provider.disable_service('chef')
88+
@provider.disable_service()
8989
end
9090
end
9191
end

chef/spec/unit/provider/service/init_service_spec.rb

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -174,12 +174,12 @@
174174
it "should call the start command if one is specified" do
175175
@new_resource.stub!(:start_command).and_return("/etc/init.d/chef startyousillysally")
176176
@provider.should_receive(:run_command).with({:command => "/etc/init.d/chef startyousillysally"}).and_return(0)
177-
@provider.start_service(@new_resource.service_name)
177+
@provider.start_service()
178178
end
179179

180180
it "should call '/etc/init.d/service_name start' if no start command is specified" do
181181
@provider.should_receive(:run_command).with({:command => "/etc/init.d/#{@new_resource.service_name} start"}).and_return(0)
182-
@provider.start_service(@new_resource.service_name)
182+
@provider.start_service()
183183
end
184184
end
185185

@@ -200,12 +200,12 @@
200200
it "should call the stop command if one is specified" do
201201
@new_resource.stub!(:stop_command).and_return("/etc/init.d/chef itoldyoutostop")
202202
@provider.should_receive(:run_command).with({:command => "/etc/init.d/chef itoldyoutostop"}).and_return(0)
203-
@provider.stop_service(@new_resource.service_name)
203+
@provider.stop_service()
204204
end
205205

206206
it "should call '/etc/init.d/service_name stop' if no stop command is specified" do
207207
@provider.should_receive(:run_command).with({:command => "/etc/init.d/#{@new_resource.service_name} stop"}).and_return(0)
208-
@provider.stop_service(@new_resource.service_name)
208+
@provider.stop_service()
209209
end
210210
end
211211

@@ -227,20 +227,20 @@
227227
it "should call 'restart' on the service_name if the resource supports it" do
228228
@new_resource.stub!(:supports).and_return({:restart => true})
229229
@provider.should_receive(:run_command).with({:command => "/etc/init.d/#{@new_resource.service_name} restart"}).and_return(0)
230-
@provider.restart_service(@new_resource.service_name)
230+
@provider.restart_service()
231231
end
232232

233233
it "should call the restart_command if one has been specified" do
234234
@new_resource.stub!(:restart_command).and_return("/etc/init.d/chef restartinafire")
235235
@provider.should_receive(:run_command).with({:command => "/etc/init.d/#{@new_resource.service_name} restartinafire"}).and_return(0)
236-
@provider.restart_service(@new_resource.service_name)
236+
@provider.restart_service()
237237
end
238238

239239
it "should just call stop, then start when the resource doesn't support restart and no restart_command is specified" do
240-
@provider.should_receive(:stop_service).with(@new_resource.service_name)
240+
@provider.should_receive(:stop_service)
241241
@provider.should_receive(:sleep).with(1)
242-
@provider.should_receive(:start_service).with(@new_resource.service_name)
243-
@provider.restart_service(@new_resource.service_name)
242+
@provider.should_receive(:start_service)
243+
@provider.restart_service()
244244
end
245245
end
246246

@@ -262,12 +262,12 @@
262262
it "should call 'reload' on the service if it supports it" do
263263
@new_resource.stub!(:supports).and_return({:reload => true})
264264
@provider.should_receive(:run_command).with({:command => "/etc/init.d/#{@new_resource.service_name} reload"}).and_return(0)
265-
@provider.reload_service(@new_resource.service_name)
265+
@provider.reload_service()
266266
end
267267

268268
it "should should run the user specified reload command if one is specified and the service doesn't support reload" do
269269
@new_resource.stub!(:reload_command).and_return("/etc/init.d/chef lollerpants")
270270
@provider.should_receive(:run_command).with({:command => "/etc/init.d/#{@new_resource.service_name} lollerpants"}).and_return(0)
271-
@provider.reload_service(@new_resource.service_name)
271+
@provider.reload_service()
272272
end
273273
end

0 commit comments

Comments
 (0)