Class: Inspec::Resources::SysV

Inherits:
ServiceManager show all
Defined in:
lib/inspec/resources/service.rb

Constant Summary collapse

RUNLEVELS =
{ 0 => false, 1 => false, 2 => false, 3 => false, 4 => false, 5 => false, 6 => false }.freeze

Instance Attribute Summary

Attributes inherited from ServiceManager

#inspec, #service_ctl

Instance Method Summary collapse

Constructor Details

#initialize(service_name, service_ctl = nil) ⇒ SysV

Returns a new instance of SysV.



424
425
426
427
# File 'lib/inspec/resources/service.rb', line 424

def initialize(service_name, service_ctl = nil)
  @service_ctl = service_ctl || "service"
  super
end

Instance Method Details

#info(service_name) ⇒ Object



429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
# File 'lib/inspec/resources/service.rb', line 429

def info(service_name)
  # check if service is installed
  # read all available services via ls /etc/init.d/
  srvlist = inspec.command("ls -1 /etc/init.d/")
  return nil if srvlist.exit_status != 0

  # check if the service is in list
  service = srvlist.stdout.split("\n").select { |srv| srv == service_name }

  # abort if we could not find any service
  return nil if service.empty?

  # read all enabled services from runlevel
  # on rhel via: 'chkconfig --list', is not installed by default
  # bash: for i in `find /etc/rc*.d -name S*`; do basename $i | sed -r 's/^S[0-9]+//'; done | sort | uniq
  enabled_services_cmd = inspec.command('find /etc/rc*.d /etc/init.d/rc*.d -name "S*"').stdout
  service_line = %r{rc(?<runlevel>[0-6])\.d/S[^/]*?#{Regexp.escape service_name}$}
  all_services = enabled_services_cmd.split("\n").map do |line|
    service_line.match(line)
  end.compact
  enabled = !all_services.empty?

  # Determine a list of runlevels which this service is activated for
  runlevels = RUNLEVELS.dup
  all_services.each { |x| runlevels[x[:runlevel].to_i] = true }

  # check if service is really running
  # service throws an exit code if the service is not installed or
  # not enabled

  cmd = inspec.command("#{service_ctl} #{service_name} status")
  running = cmd.exit_status == 0
  {
    name: service_name,
    description: nil,
    installed: true,
    running: running,
    enabled: enabled,
    runlevels: runlevels,
    type: "sysv",
  }
end