class Hookit::Resource::Service

Public Class Methods

new(name) click to toggle source
# File lib/hookit/resource/service.rb, line 12
def initialize(name)
  service_name(name) unless service_name

  # if init scheme is not provided, try to set reasonable defaults
  if not init
    case platform.name
    when 'smartos'
      init(:smf)
    when 'ubuntu'
      init(:upstart)
    when 'docker'
      init(:runit)
    end
  end
end

Public Instance Methods

run(action) click to toggle source
# File lib/hookit/resource/service.rb, line 28
def run(action)
  case action
  when :enable
    enable!
  when :disable
    disable!
  when :start
    enable!
  when :stop
    disable!
  when :restart
    restart!
  when :reload
    reload!
  end
end

Protected Instance Methods

disable!() click to toggle source
# File lib/hookit/resource/service.rb, line 84
def disable!
  case init
  when :smf
    run_command! "svcadm disable -s #{service_name}"
  when :runit
    run_command! "sv stop #{service_name}"
  else
    raise Hookit::Error::UnsupportedOption "Unsupported init schema '#{init}'"
  end
end
enable!() click to toggle source
# File lib/hookit/resource/service.rb, line 47
def enable!
  case init
  when :smf
    run_command! "svcadm enable -s #{"-r" if recursive} #{service_name}"
  when :runit
    # register and potentially start the service
    run_command! "sv start #{service_name}", false

    # runit can take up to 5 seconds to register the service before the
    # service starts to run. We'll keep checking the status for up to
    # 6 seconds, after which time we'll raise an exception.
    registered = false

    6.times do
      # check the status
      `sv status #{service_name}`
      if $?.exitstatus == 0
        registered = true
        break
      end

      sleep 1
    end

    if registered
      # just in case the service is registered but not started, try
      # to start the service one more time
      run_command! "sv start #{service_name}"
    else
      raise Hookit::Error::UnexpectedExit "Service #{service_name} did not register within 6 seconds."
    end

  else
    raise Hookit::Error::UnsupportedOption "Unsupported init schema '#{init}'"
  end
end
reload!() click to toggle source
# File lib/hookit/resource/service.rb, line 106
def reload!
  case init
  when :smf
    run_command! "svcadm refresh #{service_name}"
  else
    raise Hookit::Error::UnsupportedOption "Unsupported init schema '#{init}'"
  end
end
restart!() click to toggle source
# File lib/hookit/resource/service.rb, line 95
def restart!
  case init
  when :smf
    run_command! "svcadm restart #{service_name}"
  when :runit
    disable!; enable!
  else
    raise Hookit::Error::UnsupportedOption "Unsupported init schema '#{init}'"
  end
end