Class: Systemd

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

Overview

Instance Attribute Summary

Attributes inherited from ServiceManager

#inspec, #service_ctl

Instance Method Summary collapse

Constructor Details

#initialize(inspec, service_ctl = nil) ⇒ Systemd

Returns a new instance of Systemd.



135
136
137
138
# File 'lib/resources/service.rb', line 135

def initialize(inspec, service_ctl = nil)
  @service_ctl ||= 'systemctl'
  super
end

Instance Method Details

#info(service_name) ⇒ Object



140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
# File 'lib/resources/service.rb', line 140

def info(service_name)
  cmd = inspec.command("#{service_ctl} show --all #{service_name}")
  return nil if cmd.exit_status.to_i != 0

  # parse data
  params = SimpleConfig.new(
    cmd.stdout.chomp,
    assignment_re: /^\s*([^=]*?)\s*=\s*(.*?)\s*$/,
    multiple_values: false,
  ).params

  # LoadState values eg. loaded, not-found
  installed = params['LoadState'] == 'loaded'
  # test via 'systemctl is-active service'
  # SubState values running
  running = params['SubState'] == 'running'
  # test via systemctl --quiet is-enabled
  # ActiveState values eg.g inactive, active
  enabled = params['UnitFileState'] == 'enabled'

  {
    name: params['Id'],
    description: params['Description'],
    installed: installed,
    running: running,
    enabled: enabled,
    type: 'systemd',
  }
end