class PoiseMonit::Resources::MonitService::Provider

The provider for `monit_service`.

@see Resource @provides monit_service

Public Instance Methods

load_current_resource() click to toggle source
Calls superclass method
# File lib/poise_monit/resources/monit_service.rb, line 82
def load_current_resource
  # Only call super if it won't ruin things. Chef 12.4 and earlier didn't
  # declare Provider::Service#load_current_resource.
  super if self.class.superclass.instance_method(:load_current_resource).owner != Chef::Provider
  @current_resource = MonitService::Resource.new(new_resource.name).tap do |r|
    r.service_name(new_resource.service_name)
    if defined?(new_resource.monit_config_path) && new_resource.monit_config_path && !::File.exist?(new_resource.monit_config_path)
      Chef::Log.debug("[#{new_resource}] Config file #{new_resource.monit_config_path} does not exist, not checking status")
      r.enabled(false)
      r.running(false)
    else
      Chef::Log.debug("[#{new_resource}] Checking status for #{new_resource.service_name}")
      status = find_monit_status
      Chef::Log.debug("[#{new_resource}] Status is #{status.inspect}")
      case status
      when nil, false
        # Unable to find a status at all.
        r.enabled(false)
        r.running(false)
      when /^Does not exist/
        # It is monitored but we don't know the status yet, assume the
        # worst (run start and stop always).
        r.enabled(true)
        r.running(self.action != :start)
      when DISABLED_STATUSES
        r.enabled(false)
        # It could be running, but we don't know.
        r.running(false)
      when RUNNING_STATUSES
        r.enabled(true)
        r.running(true)
      else
        r.enabled(true)
        r.running(false)
      end
    end
  end
end

Private Instance Methods

disable_service() click to toggle source
# File lib/poise_monit/resources/monit_service.rb, line 127
def disable_service
  if defined?(new_resource.monit_config_path) && new_resource.monit_config_path && !::File.exist?(new_resource.monit_config_path)
    Chef::Log.debug("[#{new_resource}] Config file #{new_resource.monit_config_path} does not exist, not trying to unmonitor")
    return
  end
  monit_shell_out!('unmonitor') do |cmd|
    # Command fails if it has an error and does not include the service
    # error message.
    cmd.error? && cmd.stdout !~ NO_SERVICE_ERROR && cmd.stderr !~ NO_SERVICE_ERROR
  end
end
enable_service() click to toggle source
# File lib/poise_monit/resources/monit_service.rb, line 123
def enable_service
  monit_shell_out!('monitor')
end
find_monit_status() click to toggle source
# File lib/poise_monit/resources/monit_service.rb, line 163
def find_monit_status
  re = /^Process '#{new_resource.service_name}'\s+status\s+(\w.+)$/
  status_cmd = monit_shell_out!('status') do |cmd|
    # Command fails if it has an error, does't have Process line, or
    # does have Initializing.
    cmd.error? || cmd.stdout !~ re || cmd.stdout =~ /Initializing/
  end
  status_cmd.stdout =~ re && $1
end
monit_shell_out!(monit_cmd, timeout: node['poise-monit']['monit_service_timeout'], wait: node['poise-monit']['monit_service_wait'], &block) click to toggle source
# File lib/poise_monit/resources/monit_service.rb, line 173
def monit_shell_out!(monit_cmd, timeout: node['poise-monit']['monit_service_timeout'], wait: node['poise-monit']['monit_service_wait'], &block)
  while true
    cmd_args = [new_resource.parent.monit_binary]
    cmd_args << '-v' if (defined?(new_resource.monit_verbose) && new_resource.monit_verbose) || Chef::Log.level == :debug
    cmd_args.concat(['-c', new_resource.parent.config_path, monit_cmd, new_resource.service_name])
    Chef::Log.debug("[#{new_resource}] Running #{cmd_args.join(' ')}")
    cmd = poise_shell_out(cmd_args, user: new_resource.parent.owner, group: new_resource.parent.group)
    error = block ? block.call(cmd) : cmd.error?
    # If there was an error (or error-like output), sleep and try again.
    if error
      # We fell off the end of the timeout, doneburger.
      if timeout <= 0
        Chef::Log.debug("[#{new_resource}] Timeout while running `monit #{monit_cmd}`")
        # If there was a run error, raise that first.
        cmd.error!
        # Otherwise we just didn't have the requested output, which is fine.
        break
      end
      # Wait and try again.
      Chef::Log.debug("[#{new_resource}] Failure running `monit #{monit_cmd}`, retrying in #{wait}")
      timeout -= Kernel.sleep(wait)
    else
      # All's quiet on the western front.
      break
    end
  end
  cmd
end
restart_service() click to toggle source
# File lib/poise_monit/resources/monit_service.rb, line 159
def restart_service
  monit_shell_out!('restart')
end
start_service() click to toggle source
# File lib/poise_monit/resources/monit_service.rb, line 139
def start_service
  monit_shell_out!('start')
  # No this isn't a typo, it's a ragefix to make older Monit (like
  # Ubuntu' packages) always start things even right after an enable
  # action. I am really unclear why running start twice fixes this :-(
  monit_shell_out!('start')
end
stop_service() click to toggle source
# File lib/poise_monit/resources/monit_service.rb, line 147
def stop_service
  if defined?(new_resource.monit_config_path) && new_resource.monit_config_path && !::File.exist?(new_resource.monit_config_path)
    Chef::Log.debug("[#{new_resource}] Config file #{new_resource.monit_config_path} does not exist, not trying to stop")
    return
  end
  monit_shell_out!('stop') do |cmd|
    # Command fails if it has an error and does not include the service
    # error message. Then check that it is really stopped.
    cmd.error? && cmd.stdout !~ NO_SERVICE_ERROR && cmd.stderr !~ NO_SERVICE_ERROR
  end
end