lib/chef/provider/service/freebsd.rb in chef-10.12.0 vs lib/chef/provider/service/freebsd.rb in chef-10.14.0.beta.1
- old
+ new
@@ -28,42 +28,69 @@
include Chef::Mixin::ShellOut
def load_current_resource
@current_resource = Chef::Resource::Service.new(@new_resource.name)
@current_resource.service_name(@new_resource.service_name)
-
+ @rcd_script_found = true
+ @enabled_state_found = false
# Determine if we're talking about /etc/rc.d or /usr/local/etc/rc.d
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"
+ @rcd_script_found = false
+ return
end
Chef::Log.debug("#{@current_resource} found at #{@init_command}")
-
determine_current_status!
-
- if ::File.exists?("/etc/rc.conf")
+ # Default to disabled if the service doesn't currently exist
+ # at all
+ var_name = service_enable_variable_name
+ if ::File.exists?("/etc/rc.conf") && var_name
read_rc_conf.each do |line|
case line
- when /#{Regexp.escape(service_enable_variable_name)}="(\w+)"/
+ when /#{Regexp.escape(var_name)}="(\w+)"/
+ @enabled_state_found = true
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")
+ @current_resource.enabled false
end
@current_resource
end
+ def define_resource_requirements
+ super
+
+ requirements.assert(:all_actions) do |a|
+ a.assertion { @rcd_script_found }
+ a.failure_message Chef::Exceptions::Service, "#{@new_resource}: unable to locate the rc.d script"
+ end
+
+ requirements.assert(:all_actions) do |a|
+ a.assertion { @enabled_state_found }
+ # for consistentcy with original behavior, this will not fail in non-whyrun mode;
+ # rather it will silently set enabled state=>false
+ a.whyrun "Unable to determine enabled/disabled state, assuming this will be correct for an actual run. Assuming disabled."
+ end
+
+ requirements.assert(:all_actions) do |a|
+ a.assertion { @rcd_script_found && service_enable_variable_name != nil }
+ a.failure_message Chef::Exceptions::Service, "Could not find the service name in #{@init_command} and rcvar"
+ # No recovery in whyrun mode - the init file is present but not correct.
+ end
+ end
+
def start_service
if @new_resource.start_command
super
else
shell_out!("#{@init_command} faststart")
@@ -78,10 +105,11 @@
end
end
def restart_service
if @new_resource.restart_command
+
super
elsif @new_resource.supports[:restart]
shell_out!("#{@init_command} fastrestart")
else
stop_service
@@ -98,24 +126,32 @@
::File.open("/etc/rc.conf", 'w') do |file|
lines.each { |line| file.puts(line) }
end
end
-
# The variable name used in /etc/rc.conf for enabling this service
def service_enable_variable_name
# Look for name="foo" in the shell script @init_command. Use this for determining the variable name in /etc/rc.conf
# corresponding to this service
# For example: to enable the service mysql-server with the init command /usr/local/etc/rc.d/mysql-server, you need
- # to set mysql_enable="YES" in /etc/rc.conf
- makefile = ::File.open(@init_command)
- makefile.each do |line|
- case line
- when /^name="?(\w+)"?/
- return $1 + "_enable"
+ # to set mysql_enable="YES" in /etc/rc.conf$
+ if @rcd_script_found
+ ::File.open(@init_command) do |rcscript|
+ rcscript.each_line do |line|
+ if line =~ /^name="?(\w+)"?/
+ return $1 + "_enable"
+ end
+ end
end
+ # some scripts support multiple instances through symlinks such as openvpn.
+ # We should get the service name from rcvar.
+ Chef::Log.debug("name=\"service\" not found at #{@init_command}. falling back to rcvar")
+ sn = shell_out!("#{@init_command} rcvar").stdout[/(\w+_enable)=/, 1]
+ return sn
end
- raise Chef::Exceptions::Service, "Could not find name=\"service\" line in #{@init_command}"
+ # Fallback allows us to keep running in whyrun mode when
+ # the script does not exist.
+ @new_resource.service_name
end
def set_service_enable(value)
lines = read_rc_conf
# Remove line that set the old value