Class: Yast2::Systemd::Unit

Inherits:
Object
  • Object
show all
Includes:
Yast::Logger
Defined in:
library/systemd/src/lib/yast2/systemd/unit.rb

Overview

Use this class always as a parent class for implementing various systemd units. Do not use it directly for ad-hoc implementation of systemd units.

@example Create a systemd service unit

class Service < Yast2::Systemd::Unit
  SUFFIX = ".service"
  PROPMAP = {
    before: "Before"
  }

  def initialize service_name, propmap={}
    service_name += SUFFIX unless service_name.end_with?(SUFFIX)
    super(service_name, PROPMAP.merge(propmap))
  end

  def before
    properties.before.split
  end
end

service = Service.new('sshd')

service.before # ['shutdown.target', 'multi-user.target']

Direct Known Subclasses

Service, Socket, Target

Constant Summary collapse

SUPPORTED_TYPES =
%w[service socket target].freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(full_unit_name, propmap = UnitPropMap.new, property_text = nil) ⇒ Unit

Returns a new instance of Unit.

Parameters:

  • full_unit_name (String)

    eg "foo.service"

  • propmap (Yast2::Systemd::UnitPropMap) (defaults to: UnitPropMap.new)
  • property_text (String, nil) (defaults to: nil)

    if provided, use it instead of calling systemctl show



82
83
84
85
86
87
88
89
90
91
92
93
# File 'library/systemd/src/lib/yast2/systemd/unit.rb', line 82

def initialize(full_unit_name, propmap = UnitPropMap.new, property_text = nil)
  @unit_name, dot, @unit_type = full_unit_name.rpartition(".")
  raise "Missing unit type suffix" if dot.empty?

  log.warn "Unsupported unit type '#{unit_type}'" unless SUPPORTED_TYPES.include?(unit_type)
  @propmap = propmap.merge!(UnitPropMap::DEFAULT)

  @properties = show(property_text)
  @error = properties.error
  # Id is not present when the unit name is not valid
  @name = id.to_s.split(".").first || unit_name
end

Instance Attribute Details

#errorString (readonly)

Returns eg "Failed to get properties: Unit name [email protected] is not valid.".

Returns:



74
75
76
# File 'library/systemd/src/lib/yast2/systemd/unit.rb', line 74

def error
  @error
end

#nameString (readonly)

Returns eg. "apache2" (the canonical one, may be different from unit_name).

Returns:

  • (String)

    eg. "apache2" (the canonical one, may be different from unit_name)



65
66
67
# File 'library/systemd/src/lib/yast2/systemd/unit.rb', line 65

def name
  @name
end

#propertiesYast2::Systemd::UnitProperties (readonly)



76
77
78
# File 'library/systemd/src/lib/yast2/systemd/unit.rb', line 76

def properties
  @properties
end

#propmapYast2::Systemd::UnitPropMap (readonly)



71
72
73
# File 'library/systemd/src/lib/yast2/systemd/unit.rb', line 71

def propmap
  @propmap
end

#unit_nameString (readonly)

Returns eg. "apache2".

Returns:

  • (String)

    eg. "apache2"



67
68
69
# File 'library/systemd/src/lib/yast2/systemd/unit.rb', line 67

def unit_name
  @unit_name
end

#unit_typeString (readonly)

Returns eg. "service".

Returns:

  • (String)

    eg. "service"



69
70
71
# File 'library/systemd/src/lib/yast2/systemd/unit.rb', line 69

def unit_type
  @unit_type
end

Instance Method Details

#command(command_name, options = {}) ⇒ #command, ...

Runs a command in the underlying system

Parameters:

  • command_name (String)

Returns:

  • (#command, #stdout, #stderr, #exit)

Raises:

  • (Yast::SystemctlError)

    if the command cannot be executed



185
186
187
188
# File 'library/systemd/src/lib/yast2/systemd/unit.rb', line 185

def command(command_name, options = {})
  command = "#{command_name} #{unit_name}.#{unit_type} #{options[:options]}"
  Systemctl.execute(command)
end

#disableBoolean

Returns true if the unit was correctly disabled.

Returns:

  • (Boolean)

    true if the unit was correctly disabled

Raises:

  • (Yast::SystemctlError)

    if the command cannot be executed



145
146
147
# File 'library/systemd/src/lib/yast2/systemd/unit.rb', line 145

def disable
  run_command! { command("disable") }
end

#enableBoolean

Returns true if the unit was correctly enabled.

Returns:

  • (Boolean)

    true if the unit was correctly enabled

Raises:

  • (Yast::SystemctlError)

    if the command cannot be executed



139
140
141
# File 'library/systemd/src/lib/yast2/systemd/unit.rb', line 139

def enable
  run_command! { command("enable") }
end

#refresh!Object

Raises:

  • (Yast::SystemctlError)

    if 'systemctl show' cannot be executed



96
97
98
99
100
# File 'library/systemd/src/lib/yast2/systemd/unit.rb', line 96

def refresh!
  @properties = show
  @error = properties.error
  properties
end

#reloadBoolean

Returns true if the unit was correctly reloaded.

Returns:

  • (Boolean)

    true if the unit was correctly reloaded

Raises:

  • (Yast::SystemctlError)

    if the command cannot be executed



163
164
165
# File 'library/systemd/src/lib/yast2/systemd/unit.rb', line 163

def reload
  run_command! { command("reload") }
end

#reload_or_restartBoolean

Returns true if the unit was correctly reloaded or restarted.

Returns:

  • (Boolean)

    true if the unit was correctly reloaded or restarted

Raises:

  • (Yast::SystemctlError)

    if the command cannot be executed



169
170
171
# File 'library/systemd/src/lib/yast2/systemd/unit.rb', line 169

def reload_or_restart
  run_command! { command("reload-or-restart") }
end

#reload_or_try_restartBoolean

Returns true if the unit was correctly reloaded or restarted.

Returns:

  • (Boolean)

    true if the unit was correctly reloaded or restarted

Raises:

  • (Yast::SystemctlError)

    if the command cannot be executed



175
176
177
# File 'library/systemd/src/lib/yast2/systemd/unit.rb', line 175

def reload_or_try_restart
  run_command! { command("reload-or-try-restart") }
end

#restartBoolean

Returns true if the unit was correctly restarted.

Returns:

  • (Boolean)

    true if the unit was correctly restarted

Raises:

  • (Yast::SystemctlError)

    if the command cannot be executed



151
152
153
# File 'library/systemd/src/lib/yast2/systemd/unit.rb', line 151

def restart
  run_command! { command("restart") }
end

#show(property_text = nil) ⇒ Yast2::Systemd::UnitProperties

Run 'systemctl show' and parse the unit properties

Parameters:

  • property_text (String, nil) (defaults to: nil)

    if provided, use it instead of calling systemctl show

Returns:

Raises:

  • (Yast::SystemctlError)

    if 'systemctl show' cannot be executed



108
109
110
111
112
113
114
115
116
117
# File 'library/systemd/src/lib/yast2/systemd/unit.rb', line 108

def show(property_text = nil)
  # Using different handler during first stage (installation, update, ...)
  # Avoid to call systemctl when running inside a chroot (bsc#1168849) as
  # it reports an error by default.
  if Yast::WFM.scr_chrooted? || (Yast::Stage.initial && !Yast::Systemd.Running)
    UnitInstallationProperties.new(self)
  else
    UnitProperties.new(self, property_text)
  end
end

#startBoolean

Returns true if the unit was correctly started.

Returns:

  • (Boolean)

    true if the unit was correctly started

Raises:

  • (Yast::SystemctlError)

    if the command cannot be executed



127
128
129
# File 'library/systemd/src/lib/yast2/systemd/unit.rb', line 127

def start
  run_command! { command("start") }
end

#statusString

Returns:

  • (String)

Raises:

  • (Yast::SystemctlError)

    if the command cannot be executed



121
122
123
# File 'library/systemd/src/lib/yast2/systemd/unit.rb', line 121

def status
  command("status", options: "2>&1").stdout
end

#stopBoolean

Returns true if the unit was correctly stopped.

Returns:

  • (Boolean)

    true if the unit was correctly stopped

Raises:

  • (Yast::SystemctlError)

    if the command cannot be executed



133
134
135
# File 'library/systemd/src/lib/yast2/systemd/unit.rb', line 133

def stop
  run_command! { command("stop") }
end

#try_restartBoolean

Returns true if the service was correctly restarted.

Returns:

  • (Boolean)

    true if the service was correctly restarted

Raises:

  • (Yast::SystemctlError)

    if the command cannot be executed



157
158
159
# File 'library/systemd/src/lib/yast2/systemd/unit.rb', line 157

def try_restart
  run_command! { command("try-restart") }
end