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

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

Instance Attribute Summary

Attributes inherited from Chef::Provider

#current_resource, #new_resource, #node

Instance Method Summary collapse

Methods inherited from Init

#initialize, #reload_service, #restart_service, #start_service, #stop_service

Methods inherited from Simple

#reload_service, #restart_service, #start_service, #stop_service

Methods inherited from Chef::Provider::Service

#action_disable, #action_enable, #action_reload, #action_restart, #action_start, #action_stop, #initialize, #reload_service, #restart_service, #start_service, #stop_service

Methods included from Mixin::Command

handle_command_failures, not_if, only_if, output_of_command, popen4, run_command, run_command_with_systems_locale

Methods inherited from Chef::Provider

#action_nothing, build_from_file, #initialize

Methods included from Mixin::ConvertToClassName

#convert_to_class_name, #filename_to_qualified_string

Methods included from Mixin::RecipeDefinitionDSLCore

#method_missing

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



150
151
152
# File 'lib/chef/provider/service/freebsd.rb', line 150

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

#enable_serviceObject



146
147
148
# File 'lib/chef/provider/service/freebsd.rb', line 146

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

#load_current_resourceObject



27
28
29
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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
# File 'lib/chef/provider/service/freebsd.rb', line 27

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.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|
      stdin.close rescue nil
      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 /#{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



110
111
112
# File 'lib/chef/provider/service/freebsd.rb', line 110

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

#service_enable_variable_nameObject

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



122
123
124
125
126
127
128
129
130
131
132
133
134
135
# File 'lib/chef/provider/service/freebsd.rb', line 122

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



137
138
139
140
141
142
143
144
# File 'lib/chef/provider/service/freebsd.rb', line 137

def set_service_enable(value)
  lines = read_rc_conf
  # Remove line that set the old value
  lines.delete_if { |line| line =~ /#{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

#write_rc_conf(lines) ⇒ Object



114
115
116
117
118
# File 'lib/chef/provider/service/freebsd.rb', line 114

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