Class: Yast::SystemdUnit

Inherits:
Object
  • Object
show all
Includes:
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 < Yast::SystemdUnit
     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']

Defined Under Namespace

Classes: InstallationProperties, PropMap, Properties

Constant Summary

SUPPORTED_TYPES =
%w(service socket target).freeze
SUPPORTED_STATES =
%w(enabled disabled).freeze
ACTIVE_STATES =

Values of #active_state fow which we consider a unit “active”.

systemctl.c:check_unit_active uses (active, reloading) For bsc#884756 we also consider “activating” to be active. (The remaining states are “deactivating”, “inactive”, “failed”.)

Yes, depending on systemd states that are NOT covered by their interface stability promise is fragile. But: 10 to 50ms per call of systemctl is-active, times 100 to 300 services (depending on hardware and software installed, VM or not) is a 1 to 15 second delay (bsc#1045658). That is why we try hard to avoid many systemctl calls.

["active", "activating", "reloading"].freeze
DEFAULT_PROPMAP =

Returns:

{
  id:              "Id",
  pid:             "MainPID",
  description:     "Description",
  load_state:      "LoadState",
  active_state:    "ActiveState",
  sub_state:       "SubState",
  unit_file_state: "UnitFileState",
  path:            "FragmentPath"
}.freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(full_unit_name, propmap = {}, property_text = nil) ⇒ SystemdUnit

Returns a new instance of SystemdUnit

Parameters:

  • full_unit_name (String)

    eg “foo.service”

  • propmap (PropMap) (defaults to: {})
  • property_text (String, nil) (defaults to: nil)

    if provided, use it instead of calling `systemctl show`



97
98
99
100
101
102
103
104
105
106
107
108
# File 'library/systemd/src/lib/yast2/systemd_unit.rb', line 97

def initialize(full_unit_name, propmap = {}, 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!(DEFAULT_PROPMAP)

  @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 apache2@.service is not valid.”

Returns:

  • (String)

    eg “Failed to get properties: Unit name apache2@.service is not valid.”



89
90
91
# File 'library/systemd/src/lib/yast2/systemd_unit.rb', line 89

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)



80
81
82
# File 'library/systemd/src/lib/yast2/systemd_unit.rb', line 80

def name
  @name
end

#propertiesProperties (readonly)

Returns:



91
92
93
# File 'library/systemd/src/lib/yast2/systemd_unit.rb', line 91

def properties
  @properties
end

#propmapPropMap (readonly)

Returns:



86
87
88
# File 'library/systemd/src/lib/yast2/systemd_unit.rb', line 86

def propmap
  @propmap
end

#unit_nameString (readonly)

Returns eg. “apache2”

Returns:

  • (String)

    eg. “apache2”



82
83
84
# File 'library/systemd/src/lib/yast2/systemd_unit.rb', line 82

def unit_name
  @unit_name
end

#unit_typeString (readonly)

Returns eg. “service”

Returns:

  • (String)

    eg. “service”



84
85
86
# File 'library/systemd/src/lib/yast2/systemd_unit.rb', line 84

def unit_type
  @unit_type
end

Instance Method Details

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

Parameters:

Returns:

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


167
168
169
170
# File 'library/systemd/src/lib/yast2/systemd_unit.rb', line 167

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

#disableObject



141
142
143
# File 'library/systemd/src/lib/yast2/systemd_unit.rb', line 141

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

#enableObject



137
138
139
# File 'library/systemd/src/lib/yast2/systemd_unit.rb', line 137

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

#refresh!Object



110
111
112
113
114
# File 'library/systemd/src/lib/yast2/systemd_unit.rb', line 110

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

#reloadObject



153
154
155
# File 'library/systemd/src/lib/yast2/systemd_unit.rb', line 153

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

#reload_or_restartObject



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

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

#reload_or_try_restartObject



161
162
163
# File 'library/systemd/src/lib/yast2/systemd_unit.rb', line 161

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

#restartObject



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

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

#show(property_text = nil) ⇒ Properties

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:



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

def show(property_text = nil)
  # Using different handler during first stage (installation, update, ...)
  Stage.initial ? InstallationProperties.new(self) : Properties.new(self, property_text)
end

#startObject



129
130
131
# File 'library/systemd/src/lib/yast2/systemd_unit.rb', line 129

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

#statusObject



125
126
127
# File 'library/systemd/src/lib/yast2/systemd_unit.rb', line 125

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

#stopObject



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

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

#try_restartObject



149
150
151
# File 'library/systemd/src/lib/yast2/systemd_unit.rb', line 149

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