Class: Chef::Provider::Service::Freebsd

Inherits:
Init show all
Includes:
Mixin::ShellOut
Defined in:
lib/chef/provider/service/freebsd.rb

Constant Summary

Constants included from Mixin::ShellOut

Mixin::ShellOut::DEPRECATED_OPTIONS

Instance Attribute Summary

Attributes inherited from Chef::Provider

#action, #current_resource, #new_resource, #run_context

Instance Method Summary collapse

Methods included from Mixin::ShellOut

#run_command_compatible_options, #shell_out, #shell_out!

Methods inherited from Init

#initialize, #reload_service

Methods inherited from Simple

#reload_service, #shared_resource_requirements, #whyrun_supported?

Methods inherited from Chef::Provider::Service

#action_disable, #action_enable, #action_reload, #action_restart, #action_start, #action_stop, #initialize, #load_new_resource_state, #reload_service, #shared_resource_requirements, #whyrun_supported?

Methods included from Mixin::Command

#chdir_or_tmpdir, #handle_command_failures, #output_of_command, #run_command, #run_command_with_systems_locale

Methods included from Mixin::Command::Windows

#popen4

Methods included from Mixin::Command::Unix

#popen4

Methods inherited from Chef::Provider

#action_nothing, #cleanup_after_converge, #cookbook_name, #events, #initialize, #node, #process_resource_requirements, #requirements, #resource_collection, #run_action, #set_updated_status, #whyrun_mode?, #whyrun_supported?

Methods included from DSL::Recipe

#method_missing

Methods included from Mixin::ConvertToClassName

#convert_to_class_name, #convert_to_snake_case, #filename_to_qualified_string, #snake_case_basename

Constructor Details

This class inherits a constructor from Chef::Provider::Service::Init

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class Chef::DSL::Recipe

Instance Method Details

#define_resource_requirementsObject



70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/chef/provider/service/freebsd.rb', line 70

def define_resource_requirements
  shared_resource_requirements
  requirements.assert(:start, :enable, :reload, :restart) 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(:start, :enable, :reload, :restart) 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

#disable_serviceObject



168
169
170
# File 'lib/chef/provider/service/freebsd.rb', line 168

def disable_service()
  set_service_enable("NO") if @current_resource.enabled
end

#enable_serviceObject



164
165
166
# File 'lib/chef/provider/service/freebsd.rb', line 164

def enable_service()
  set_service_enable("YES") unless @current_resource.enabled
end

#load_current_resourceObject



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/chef/provider/service/freebsd.rb', line 30

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
    @rcd_script_found = false 
    return
  end
  Chef::Log.debug("#{@current_resource} found at #{@init_command}")
  determine_current_status!
  # 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(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

#read_rc_confObject



120
121
122
# File 'lib/chef/provider/service/freebsd.rb', line 120

def read_rc_conf
  ::File.open("/etc/rc.conf", 'r') { |file| file.readlines }
end

#restart_serviceObject



107
108
109
110
111
112
113
114
115
116
117
118
# File 'lib/chef/provider/service/freebsd.rb', line 107

def restart_service
  if @new_resource.restart_command

    super
  elsif @new_resource.supports[:restart]
    shell_out!("#{@init_command} fastrestart")
  else
    stop_service
    sleep 1
    start_service
  end
end

#service_enable_variable_nameObject

The variable name used in /etc/rc.conf for enabling this service



131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
# File 'lib/chef/provider/service/freebsd.rb', line 131

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$
  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
  # Fallback allows us to keep running in whyrun mode when
  # the script does not exist.
  @new_resource.service_name
end

#set_service_enable(value) ⇒ Object



155
156
157
158
159
160
161
162
# File 'lib/chef/provider/service/freebsd.rb', line 155

def set_service_enable(value)
  lines = read_rc_conf
  # Remove line that set the old value
  lines.delete_if { |line| line =~ /#{Regexp.escape(service_enable_variable_name)}/ }
  # And append the line that sets the new value at the end
  lines << "#{service_enable_variable_name}=\"#{value}\""
  write_rc_conf(lines)
end

#start_serviceObject



91
92
93
94
95
96
97
# File 'lib/chef/provider/service/freebsd.rb', line 91

def start_service
  if @new_resource.start_command
    super
  else
    shell_out!("#{@init_command} faststart")
  end
end

#stop_serviceObject



99
100
101
102
103
104
105
# File 'lib/chef/provider/service/freebsd.rb', line 99

def stop_service
  if @new_resource.stop_command
    super
  else
    shell_out!("#{@init_command} faststop")
  end
end

#write_rc_conf(lines) ⇒ Object



124
125
126
127
128
# File 'lib/chef/provider/service/freebsd.rb', line 124

def write_rc_conf(lines)
  ::File.open("/etc/rc.conf", 'w') do |file|
    lines.each { |line| file.puts(line) }
  end
end