Class: Inspec::Resources::LaunchCtl

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

Overview

MacOS / Darwin new launctl on macos 10.10

Instance Attribute Summary

Attributes inherited from ServiceManager

#inspec, #service_ctl

Instance Method Summary collapse

Constructor Details

#initialize(service_name, service_ctl = nil) ⇒ LaunchCtl

Returns a new instance of LaunchCtl.



501
502
503
504
# File 'lib/resources/service.rb', line 501

def initialize(service_name, service_ctl = nil)
  @service_ctl = service_ctl || 'launchctl'
  super
end

Instance Method Details

#info(service_name) ⇒ Object



506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
# File 'lib/resources/service.rb', line 506

def info(service_name)
  # get the status of upstart service
  cmd = inspec.command("#{service_ctl} list")
  return nil if cmd.exit_status != 0

  # search for the service
  srv = /(^.*#{service_name}.*)/.match(cmd.stdout)
  return nil if srv.nil? || srv[0].nil?

  # extract values from service
  parsed_srv = /^(?<pid>[0-9-]+)\t(?<exit>[0-9]+)\t(?<name>\S*)$/.match(srv[0])
  enabled = !parsed_srv['name'].nil? # it's in the list

  # check if the service is running
  pid = parsed_srv['pid']
  running = pid != '-'

  # extract service label
  srv = parsed_srv['name'] || service_name

  {
    name: srv,
    description: nil,
    installed: true,
    running: running,
    enabled: enabled,
    type: 'darwin',
  }
end