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

#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

Methods inherited from Chef::Provider::Service

#action_disable, #action_enable, #action_reload, #action_restart, #action_start, #action_stop, #initialize, #reload_service

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, build_from_file, #cookbook_name, #initialize, #node, #resource_collection

Methods included from Mixin::ConvertToClassName

#convert_to_class_name, #convert_to_snake_case, #filename_to_qualified_string, #snake_case_basename

Methods included from Mixin::EnforceOwnershipAndPermissions

#enforce_ownership_and_permissions

Methods included from Mixin::RecipeDefinitionDSLCore

#method_missing

Methods included from Mixin::Language

#data_bag, #data_bag_item, #platform?, #platform_family?, #search, #value_for_platform, #value_for_platform_family

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::Mixin::RecipeDefinitionDSLCore

Instance Method Details

#disable_serviceObject



133
134
135
# File 'lib/chef/provider/service/freebsd.rb', line 133

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

#enable_serviceObject



129
130
131
# File 'lib/chef/provider/service/freebsd.rb', line 129

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
# 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)

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

  determine_current_status!

  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

#read_rc_confObject



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

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

#restart_serviceObject



81
82
83
84
85
86
87
88
89
90
91
# File 'lib/chef/provider/service/freebsd.rb', line 81

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



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

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"
    end
  end
  raise Chef::Exceptions::Service, "Could not find name=\"service\" line in #{@init_command}"
end

#set_service_enable(value) ⇒ Object



120
121
122
123
124
125
126
127
# File 'lib/chef/provider/service/freebsd.rb', line 120

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



65
66
67
68
69
70
71
# File 'lib/chef/provider/service/freebsd.rb', line 65

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

#stop_serviceObject



73
74
75
76
77
78
79
# File 'lib/chef/provider/service/freebsd.rb', line 73

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

#write_rc_conf(lines) ⇒ Object



97
98
99
100
101
# File 'lib/chef/provider/service/freebsd.rb', line 97

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