Class: Inspec::Resources::BSDInit

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

Overview

Instance Attribute Summary

Attributes inherited from ServiceManager

#inspec, #service_ctl

Instance Method Summary collapse

Constructor Details

#initialize(service_name, service_ctl = nil) ⇒ BSDInit

Returns a new instance of BSDInit.



492
493
494
495
# File 'lib/inspec/resources/service.rb', line 492

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

Instance Method Details

#info(service_name) ⇒ Object



497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
# File 'lib/inspec/resources/service.rb', line 497

def info(service_name)
  # `service -e` lists all enabled services. Output format:
  # % service -e
  # /etc/rc.d/hostid
  # /etc/rc.d/hostid_save
  # /etc/rc.d/cleanvar
  # /etc/rc.d/ip6addrctl
  # /etc/rc.d/devd

  cmd = inspec.command("#{service_ctl} -e")
  return nil if cmd.exit_status != 0

  # search for the service

  srv = %r{^.*/(#{service_name}$)}.match(cmd.stdout)
  return nil if srv.nil? || srv[0].nil?

  enabled = true

  # check if the service is running
  # if the service is not available or not running, we always get an error code
  cmd = inspec.command("#{service_ctl} #{service_name} onestatus")
  running = cmd.exit_status == 0

  {
    name: service_name,
    description: nil,
    installed: true,
    running: running,
    enabled: enabled,
    type: "bsd-init",
  }
end