class PoiseService::ServiceProviders::Base

Public Class Methods

inversion_options(node, resource) click to toggle source

Extend the default options to check for service_name too.

@api private

Calls superclass method
# File lib/poise_service/service_providers/base.rb, line 37
def self.inversion_options(node, resource)
  super.tap do |opts|
    attrs = resolve_inversion_attribute(node)
    opts.update(attrs[resource.service_name]) if attrs[resource.service_name]
    run_state = Mash.new(node.run_state.fetch('poise_inversion', {}).fetch(inversion_resource, {}))[resource.service_name] || {}
    opts.update(run_state['*']) if run_state['*']
    opts.update(run_state[provides]) if run_state[provides]
  end
end
resolve_inversion_provider(node, resource) click to toggle source

Extend the default lookup behavior to check for service_name too.

@api private

Calls superclass method
# File lib/poise_service/service_providers/base.rb, line 29
def self.resolve_inversion_provider(node, resource)
  attrs = resolve_inversion_attribute(node)
  (attrs[resource.service_name] && attrs[resource.service_name]['provider']) || super
end
service_resource_hints() click to toggle source

Cache the service hints to improve performance. This is called from the provides_auto? on most service providers and hits the filesystem a lot.

@return [Array<Symbol>]

# File lib/poise_service/service_providers/base.rb, line 51
def self.service_resource_hints
  @@service_resource_hints ||= Chef::Platform::ServiceHelpers.service_resource_providers
end

Public Instance Methods

action_disable() click to toggle source
# File lib/poise_service/service_providers/base.rb, line 64
def action_disable
  action_stop
  disable_service
  notifying_block do
    destroy_service
  end
end
action_enable() click to toggle source
# File lib/poise_service/service_providers/base.rb, line 55
def action_enable
  include_recipe(*Array(recipes)) if recipes
  notifying_block do
    create_service
  end
  enable_service
  action_start
end
action_reload() click to toggle source
# File lib/poise_service/service_providers/base.rb, line 93
def action_reload
  return if options['never_reload']
  notify_if_service do
    service_resource.run_action(:reload)
  end
end
action_restart() click to toggle source
# File lib/poise_service/service_providers/base.rb, line 86
def action_restart
  return if options['never_restart']
  notify_if_service do
    service_resource.run_action(:restart)
  end
end
action_start() click to toggle source
# File lib/poise_service/service_providers/base.rb, line 72
def action_start
  return if options['never_start']
  notify_if_service do
    service_resource.run_action(:start)
  end
end
action_stop() click to toggle source
# File lib/poise_service/service_providers/base.rb, line 79
def action_stop
  return if options['never_stop']
  notify_if_service do
    service_resource.run_action(:stop)
  end
end
pid() click to toggle source
# File lib/poise_service/service_providers/base.rb, line 100
def pid
  raise NotImplementedError
end

Private Instance Methods

create_service() click to toggle source

Subclass hook to create the required files et al for the service.

# File lib/poise_service/service_providers/base.rb, line 113
def create_service
  raise NotImplementedError
end
destroy_service() click to toggle source

Subclass hook to remove the required files et al for the service.

# File lib/poise_service/service_providers/base.rb, line 118
def destroy_service
  raise NotImplementedError
end
disable_service() click to toggle source
# File lib/poise_service/service_providers/base.rb, line 128
def disable_service
  notify_if_service do
    service_resource.run_action(:disable)
  end
end
enable_service() click to toggle source
# File lib/poise_service/service_providers/base.rb, line 122
def enable_service
  notify_if_service do
    service_resource.run_action(:enable)
  end
end
notify_if_service(&block) click to toggle source
# File lib/poise_service/service_providers/base.rb, line 134
def notify_if_service(&block)
  service_resource.updated_by_last_action(false)
  block.call
  new_resource.updated_by_last_action(true) if service_resource.updated_by_last_action?
end
recipes() click to toggle source

Recipes to include for this provider to work. Subclasses can override.

@return [String, Array]

# File lib/poise_service/service_providers/base.rb, line 109
def recipes
end
service_resource() click to toggle source

Subclass hook to create the resource used to delegate start, stop, and restart actions.

# File lib/poise_service/service_providers/base.rb, line 142
def service_resource
  @service_resource ||= Chef::Resource::Service.new(new_resource.service_name, run_context).tap do |r|
    r.declared_type = :service
    r.enclosing_provider = self
    r.source_line = new_resource.source_line
    r.supports(status: true, restart: true, reload: true)
  end
end
service_template(path, default_source, &block) click to toggle source
# File lib/poise_service/service_providers/base.rb, line 151
def service_template(path, default_source, &block)
  # Sigh scoping.
  template path do
    owner 'root'
    group node['root_group']
    mode '644'
    if options['template']
      # If we have a template override, allow specifying a cookbook via
      # "cookbook:template".
      parts = options['template'].split(/:/, 2)
      if parts.length == 2
        source parts[1]
        cookbook parts[0]
      else
        source parts.first
        cookbook new_resource.cookbook_name.to_s
      end
    else
      source default_source
      cookbook self.poise_defined_in_cookbook
    end
    variables(
      command: options['command'] || new_resource.command,
      directory: options['directory'] || new_resource.directory,
      environment: options['environment'] || new_resource.environment,
      name: new_resource.service_name,
      new_resource: new_resource,
      options: options,
      reload_signal: options['reload_signal'] || new_resource.reload_signal,
      stop_signal: options['stop_signal'] || new_resource.stop_signal,
      user: options['user'] || new_resource.user,
    )
    # Don't trigger a restart if the template doesn't already exist, this
    # prevents restarting on the run that first creates the service.
    restart_on_update = options.fetch('restart_on_update', new_resource.restart_on_update)
    if restart_on_update && ::File.exist?(path)
      mode = restart_on_update.to_s == 'immediately' ? :immediately : :delayed
      notifies :restart, new_resource, mode
    end
    instance_exec(&block) if block
  end
end