def load_current_resource
@current_resource = Chef::Resource::Service.new(@new_resource.name)
@current_resource.service_name(@new_resource.service_name)
if ::File.exists?("/etc/rc.d/#{current_resource.service_name}")
@init_command = "/etc/rc.d/#{current_resource.service_name}"
elsif ::File.exists?("/usr/local/etc/rc.d/#{current_resource.service_name}")
@init_command = "/usr/local/etc/rc.d/#{current_resource.service_name}"
else
raise Chef::Exceptions::Service, "#{@new_resource}: unable to locate the rc.d script"
end
Chef::Log.debug("#{@current_resource.name} found at #{@init_command}")
if @new_resource.supports[:status]
Chef::Log.debug("#{@new_resource} supports status, checking state")
begin
if run_command(:command => "#{@init_command} status") == 0
@current_resource.running true
end
rescue Chef::Exceptions::Exec
@current_resource.running false
nil
end
elsif @new_resource.status_command
Chef::Log.debug("#{@new_resource} doesn't support status but you have specified a status command, running..")
begin
if run_command(:command => @new_resource.status_command) == 0
@current_resource.running true
end
rescue Chef::Exceptions::Exec
@current_resource.running false
nil
end
else
Chef::Log.debug("#{@new_resource} does not support status and you have not specified a status command, falling back to process table inspection")
if node[:command][:ps].nil? or node[:command][:ps].empty?
raise Chef::Exceptions::Service, "#{@new_resource}: could not determine how to inspect the process table, please set this nodes 'ps' attribute"
end
status = popen4(node[:command][:ps]) do |pid, stdin, stdout, stderr|
r = Regexp.new(@new_resource.pattern)
Chef::Log.debug("#{@new_resource}: attempting to match #{@new_resource.pattern} (#{r}) against process table")
stdout.each_line do |line|
if r.match(line)
@current_resource.running true
break
end
end
@current_resource.running false unless @current_resource.running
end
unless status.exitstatus == 0
raise Chef::Exceptions::Service, "Command #{node[:command][:ps]} failed"
else
Chef::Log.debug("#{@new_resource}: #{node[:command][:ps]} exited and parsed successfully, process running: #{@current_resource.running}")
end
end
if ::File.exists?("/etc/rc.conf")
read_rc_conf.each do |line|
case line
when /#{Regexp.escape(service_enable_variable_name)}="(\w+)"/
if $1 =~ /[Yy][Ee][Ss]/
@current_resource.enabled true
elsif $1 =~ /[Nn][Oo][Nn]?[Oo]?[Nn]?[Ee]?/
@current_resource.enabled false
end
end
end
end
unless @current_resource.enabled
Chef::Log.debug("#{@new_resource.name} enable/disable state unknown")
end
@current_resource
end