class PoiseService::Resources::PoiseService::Resource

`poise_service` resource. Provides a unified service interface with a dependency injection framework.

@since 1.0.0 @provides poise_service @action enable @action disable @action start @action stop @action restart @action reload @example

poise_service 'myapp' do
  command 'myapp --serve'
  user 'myuser'
  directory '/home/myapp'
end

Public Instance Methods

after_created() click to toggle source

Resource DSL callback.

@api private

# File lib/poise_service/resources/poise_service.rb, line 95
def after_created
  # Set signals to clean values.
  stop_signal(clean_signal(stop_signal))
  reload_signal(clean_signal(reload_signal))
end
pid() click to toggle source

Return the PID of the main process for this service or nil if the service isn't running or the PID cannot be found.

@return [Integer, nil] @example

execute "kill -WINCH #{resources('poise_test[myapp]').pid}"
# File lib/poise_service/resources/poise_service.rb, line 107
def pid
  # :pid isn't a real action, but this should still work.
  provider_for_action(:pid).pid
end

Private Instance Methods

clean_signal(signal) click to toggle source

Clean up a signal string/integer. Ints are mapped to the signal name, and strings are reformatted to upper case and without the SIG.

@see stop_signal @param signal [String, Symbol, Integer] Signal value to clean. @return [String]

# File lib/poise_service/resources/poise_service.rb, line 149
def clean_signal(signal)
  if signal.is_a?(Integer)
    raise Error.new("Unknown signal #{signal}") unless (0..31).include?(signal)
    Signal.signame(signal)
  else
    short_sig = signal.to_s.upcase
    short_sig = short_sig[3..-1] if short_sig.start_with?('SIG')
    raise Error.new("Unknown signal #{signal}") unless Signal.list.include?(short_sig)
    short_sig
  end
end
default_directory() click to toggle source

Try to find the home diretory for the configured user. This will fail if nsswitch.conf was changed during this run such as with LDAP. Defaults to the system root directory.

@see directory @return [String]

# File lib/poise_service/resources/poise_service.rb, line 120
def default_directory
  # Default fallback.
  sysroot = case node['platform_family']
  when 'windows'
    ENV.fetch('SystemRoot', 'C:\\')
  else
    '/'
  end
  # For root we always want the system root path.
  return sysroot if user == 'root'
  # Force a reload in case any users were created earlier in the run.
  Etc.endpwent
  # ArgumentError means we can't find the user, possibly nsswitch caching?
  home = begin
    Dir.home(user)
  rescue ArgumentError
    sysroot
  end
  # If the home doesn't exist or is empty, use sysroot.
  home = sysroot if home.empty? || !::File.directory?(home)
  home
end