class Chef::Provider::Service::Openbsd

Constants

RC_CONF_LOCAL_PATH
RC_CONF_PATH

Attributes

enabled_state_found[R]
init_command[R]
rc_conf[R]
rc_conf_local[R]

Public Class Methods

new(new_resource, run_context) click to toggle source
Calls superclass method Chef::Provider::Service::Init::new
# File lib/chef/provider/service/openbsd.rb, line 34
def initialize(new_resource, run_context)
  super
  @rc_conf = ::File.read(RC_CONF_PATH) rescue ""
  @rc_conf_local = ::File.read(RC_CONF_LOCAL_PATH) rescue ""
  @init_command = ::File.exist?(rcd_script_path) ? rcd_script_path : nil
  new_resource.status_command("#{default_init_command} check")
end

Public Instance Methods

define_resource_requirements() click to toggle source
# File lib/chef/provider/service/openbsd.rb, line 55
def define_resource_requirements
  shared_resource_requirements

  requirements.assert(:start, :enable, :reload, :restart) do |a|
    a.assertion { init_command }
    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 consistency 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 { init_command && !builtin_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_service() click to toggle source
# File lib/chef/provider/service/openbsd.rb, line 101
def disable_service
  if is_enabled?
    if is_builtin?
      if is_enabled_by_default?
        # add line to disable
        update_rcl rc_conf_local + "\n" + "#{builtin_service_enable_variable_name}=\"NO\"\n"
      else
        # remove line to disable
        update_rcl rc_conf_local.sub(/^#{Regexp.escape(builtin_service_enable_variable_name)}=.*/, "")
      end
    else
      # remove from pkg_scripts
      old_list = rc_conf_local.match(/^pkg_scripts="(.*)"/)
      old_list = old_list ? old_list[1].split(" ") : []
      new_list = old_list - [new_resource.service_name]
      update_rcl rc_conf_local.sub(/^pkg_scripts="(.*)"/, pkg_scripts = (new_list.join(" ")).to_s)
    end
  end
end
enable_service() click to toggle source
# File lib/chef/provider/service/openbsd.rb, line 77
def enable_service
  unless is_enabled?
    if is_builtin?
      if is_enabled_by_default?
        update_rcl rc_conf_local.sub(/^#{Regexp.escape(builtin_service_enable_variable_name)}=.*/, "")
      else
        # add line with blank string, which means enable
        update_rcl rc_conf_local + "\n" + "#{builtin_service_enable_variable_name}=\"\"\n"
      end
    else
      # add to pkg_scripts, most recent addition goes last
      old_services_list = rc_conf_local.match(/^pkg_scripts="(.*)"/)
      old_services_list = old_services_list ? old_services_list[1].split(" ") : []
      new_services_list = old_services_list + [new_resource.service_name]
      if /^pkg_scripts="(.*)"/.match?(rc_conf_local)
        new_rcl = rc_conf_local.sub(/^pkg_scripts="(.*)"/, "pkg_scripts=\"#{new_services_list.join(" ")}\"")
      else
        new_rcl = rc_conf_local + "\n" + "pkg_scripts=\"#{new_services_list.join(" ")}\"\n"
      end
      update_rcl new_rcl
    end
  end
end
load_current_resource() click to toggle source
# File lib/chef/provider/service/openbsd.rb, line 42
def load_current_resource
  supports[:status] = true if supports[:status].nil?

  @current_resource = Chef::Resource::Service.new(new_resource.name)
  current_resource.service_name(new_resource.service_name)

  logger.trace("#{current_resource} found at #{init_command}")

  determine_current_status!
  determine_enabled_status!
  current_resource
end

Private Instance Methods

builtin_service_enable_variable_name() click to toggle source

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

# File lib/chef/provider/service/openbsd.rb, line 138
def builtin_service_enable_variable_name
  @bsevn ||= begin
    result = nil
    if rcd_script_found?
      ::File.open(init_command) do |rcscript|
        if m = rcscript.read.match(/^# \$OpenBSD: (\w+)[(.rc),]?/)
          result = m[1] + "_flags"
        end
      end
    end
    # Fallback allows us to keep running in whyrun mode when
    # the script does not exist.
    result || new_resource.service_name
  end
end
determine_enabled_status!() click to toggle source
# File lib/chef/provider/service/openbsd.rb, line 178
def determine_enabled_status!
  result = false # Default to disabled if the service doesn't currently exist at all
  @enabled_state_found = false
  if is_builtin?
    var_name = builtin_service_enable_variable_name
    if var_name
      if m = rc_conf_local.match(/^#{Regexp.escape(var_name)}=(.*)/)
        @enabled_state_found = true
        unless /"?[Nn][Oo]"?/.match?(m[1]) # e.g. looking for httpd_flags=NO
          result = true
        end
      end
    end
    unless @enabled_state_found
      result = is_enabled_by_default?
    end
  else
    var_name = @new_resource.service_name
    if var_name
      if m = rc_conf_local.match(/^pkg_scripts="(.*)"/)
        @enabled_state_found = true
        if m[1].include?(var_name) # e.g. looking for 'gdm' in pkg_scripts="gdm unbound"
          result = true
        end
      end
    end
  end

  current_resource.enabled result
end
Also aliased as: is_enabled?
is_builtin?() click to toggle source
# File lib/chef/provider/service/openbsd.rb, line 154
def is_builtin?
  result = false
  var_name = builtin_service_enable_variable_name
  if var_name
    if /^#{Regexp.escape(var_name)}=(.*)/.match?(rc_conf)
      result = true
    end
  end
  result
end
is_enabled?()
is_enabled_by_default?() click to toggle source
# File lib/chef/provider/service/openbsd.rb, line 165
def is_enabled_by_default?
  result = false
  var_name = builtin_service_enable_variable_name
  if var_name
    if m = rc_conf.match(/^#{Regexp.escape(var_name)}=(.*)/)
      unless /"?[Nn][Oo]"?/.match?(m[1])
        result = true
      end
    end
  end
  result
end
rcd_script_found?() click to toggle source
# File lib/chef/provider/service/openbsd.rb, line 123
def rcd_script_found?
  !init_command.nil?
end
rcd_script_path() click to toggle source
# File lib/chef/provider/service/openbsd.rb, line 127
def rcd_script_path
  "/etc/rc.d/#{new_resource.service_name}"
end
update_rcl(value) click to toggle source
# File lib/chef/provider/service/openbsd.rb, line 131
def update_rcl(value)
  FileUtils.touch RC_CONF_LOCAL_PATH unless ::File.exist? RC_CONF_LOCAL_PATH
  ::File.write(RC_CONF_LOCAL_PATH, value)
  @rc_conf_local = value
end