Class: ForemanMaintain::Utils::Service::Systemd

Inherits:
Abstract
  • Object
show all
Defined in:
lib/foreman_maintain/utils/service/systemd.rb

Instance Attribute Summary collapse

Attributes inherited from Abstract

#name, #priority

Instance Method Summary collapse

Methods inherited from Abstract

#<=>, #inspect, #matches?, #socket, #to_s

Constructor Details

#initialize(name, priority, options = {}) ⇒ Systemd

Returns a new instance of Systemd.



5
6
7
8
9
# File 'lib/foreman_maintain/utils/service/systemd.rb', line 5

def initialize(name, priority, options = {})
  super
  @sys = SystemHelpers.new
  @instance_parent_unit = options.fetch(:instance_parent_unit, nil)
end

Instance Attribute Details

#instance_parent_unitObject (readonly)

Returns the value of attribute instance_parent_unit.



4
5
6
# File 'lib/foreman_maintain/utils/service/systemd.rb', line 4

def instance_parent_unit
  @instance_parent_unit
end

Instance Method Details

#command(action, options = {}) ⇒ Object



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/foreman_maintain/utils/service/systemd.rb', line 11

def command(action, options = {})
  do_wait = options.fetch(:wait, true) # wait for service to start
  all = @options.fetch(:all, false)
  skip_enablement = @options.fetch(:skip_enablement, false)
  if skip_enablement && %w[enable disable].include?(action)
    return skip_enablement_message(action, @name)
  end

  if do_wait && File.exist?('/usr/sbin/service-wait')
    "service-wait #{@name} #{action}"
  else
    cmd = "systemctl #{action} #{@name}"
    cmd += ' --all' if all
    cmd
  end
end

#disableObject



44
45
46
# File 'lib/foreman_maintain/utils/service/systemd.rb', line 44

def disable
  execute('disable', :wait => false)
end

#enableObject



40
41
42
# File 'lib/foreman_maintain/utils/service/systemd.rb', line 40

def enable
  execute('enable', :wait => false)
end

#enabled?Boolean

Returns:

  • (Boolean)


61
62
63
64
65
# File 'lib/foreman_maintain/utils/service/systemd.rb', line 61

def enabled?
  if @sys.systemd_installed?
    service_enabled_status == 'enabled'
  end
end

#exist?Boolean

Returns:

  • (Boolean)


52
53
54
55
56
57
58
59
# File 'lib/foreman_maintain/utils/service/systemd.rb', line 52

def exist?
  if @sys.systemd_installed?
    systemd = service_enabled_status
    systemd == 'enabled' || systemd == 'disabled'
  else
    File.exist?("/etc/init.d/#{@name}")
  end
end

#running?Boolean

Returns:

  • (Boolean)


48
49
50
# File 'lib/foreman_maintain/utils/service/systemd.rb', line 48

def running?
  status.first == 0
end

#startObject



32
33
34
# File 'lib/foreman_maintain/utils/service/systemd.rb', line 32

def start
  execute('start')
end

#statusObject



28
29
30
# File 'lib/foreman_maintain/utils/service/systemd.rb', line 28

def status
  execute('status')
end

#stopObject



36
37
38
# File 'lib/foreman_maintain/utils/service/systemd.rb', line 36

def stop
  execute('stop')
end