Class: SysV

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

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.



266
267
268
269
# File 'lib/resources/service.rb', line 266

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

Instance Method Details

#info(service_name) ⇒ Object



271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
# File 'lib/resources/service.rb', line 271

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 -name S*')
  enabled_services = enabled_services_cmd.stdout.split("\n").select { |line|
    /(^.*#{service_name}.*)/.match(line)
  }
  enabled = !enabled_services.empty?

  # 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,
    type: 'sysv',
  }
end