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, #run_context

Instance Method Summary collapse

Methods inherited from Init

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

Methods inherited from Simple

#ps_cmd, #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

#chdir_or_tmpdir, #handle_command_failures, #not_if, #only_if, #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::RecipeDefinitionDSLCore

#method_missing

Methods included from Mixin::Language

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

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



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

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

#enable_serviceObject



143
144
145
# File 'lib/chef/provider/service/freebsd.rb', line 143

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
# 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} found at #{@init_command}")

  if @new_resource.supports[:status]
    begin
      if run_command(:command => "#{@init_command} status") == 0
        @current_resource.running true
        Chef::Log.debug("#{@new_resource} is running")
      end
    rescue Chef::Exceptions::Exec
      @current_resource.running false
      nil
    end

  elsif @new_resource.status_command
    begin
      if run_command(:command => @new_resource.status_command) == 0
        @current_resource.running true
        Chef::Log.debug("#{@new_resource} is running")
      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

#read_rc_confObject



107
108
109
# File 'lib/chef/provider/service/freebsd.rb', line 107

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



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

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



134
135
136
137
138
139
140
141
# File 'lib/chef/provider/service/freebsd.rb', line 134

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



111
112
113
114
115
# File 'lib/chef/provider/service/freebsd.rb', line 111

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