Class: Inspec::Resources::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.



245
246
247
248
# File 'lib/resources/service.rb', line 245

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

Instance Method Details

#info(service_name) ⇒ Object



258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
# File 'lib/resources/service.rb', line 258

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'

  {
    name: params['Id'],
    description: params['Description'],
    installed: installed,
    running: is_active?(service_name),
    enabled: is_enabled?(service_name),
    type: 'systemd',
    params: params,
  }
end

#is_active?(service_name) ⇒ Boolean

Returns:

  • (Boolean)


254
255
256
# File 'lib/resources/service.rb', line 254

def is_active?(service_name)
  inspec.command("#{service_ctl} is-active #{service_name} --quiet").exit_status == 0
end

#is_enabled?(service_name) ⇒ Boolean

Returns:

  • (Boolean)


250
251
252
# File 'lib/resources/service.rb', line 250

def is_enabled?(service_name)
  inspec.command("#{service_ctl} is-enabled #{service_name} --quiet").exit_status == 0
end