Class: Yast2::Systemd::Unit
- Inherits:
-
Object
- Object
- Yast2::Systemd::Unit
- 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']
Constant Summary collapse
- SUPPORTED_TYPES =
%w[service socket target].freeze
Instance Attribute Summary collapse
-
#error ⇒ String
readonly
Eg "Failed to get properties: Unit name [email protected] is not valid.".
-
#name ⇒ String
readonly
Eg.
- #properties ⇒ Yast2::Systemd::UnitProperties readonly
- #propmap ⇒ Yast2::Systemd::UnitPropMap readonly
-
#unit_name ⇒ String
readonly
Eg.
-
#unit_type ⇒ String
readonly
Eg.
Instance Method Summary collapse
-
#command(command_name, options = {}) ⇒ #command, ...
Runs a command in the underlying system.
-
#disable ⇒ Boolean
True if the unit was correctly disabled.
-
#enable ⇒ Boolean
True if the unit was correctly enabled.
-
#initialize(full_unit_name, propmap = UnitPropMap.new, property_text = nil) ⇒ Unit
constructor
A new instance of Unit.
- #refresh! ⇒ Object
-
#reload ⇒ Boolean
True if the unit was correctly reloaded.
-
#reload_or_restart ⇒ Boolean
True if the unit was correctly reloaded or restarted.
-
#reload_or_try_restart ⇒ Boolean
True if the unit was correctly reloaded or restarted.
-
#restart ⇒ Boolean
True if the unit was correctly restarted.
-
#show(property_text = nil) ⇒ Yast2::Systemd::UnitProperties
Run 'systemctl show' and parse the unit properties.
-
#start ⇒ Boolean
True if the unit was correctly started.
- #status ⇒ String
-
#stop ⇒ Boolean
True if the unit was correctly stopped.
-
#try_restart ⇒ Boolean
True if the service was correctly restarted.
Constructor Details
#initialize(full_unit_name, propmap = UnitPropMap.new, property_text = nil) ⇒ Unit
Returns a new instance of Unit.
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
#error ⇒ String (readonly)
Returns eg "Failed to get properties: Unit name [email protected] is not valid.".
74 75 76 |
# File 'library/systemd/src/lib/yast2/systemd/unit.rb', line 74 def error @error end |
#name ⇒ String (readonly)
Returns 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 |
#properties ⇒ Yast2::Systemd::UnitProperties (readonly)
76 77 78 |
# File 'library/systemd/src/lib/yast2/systemd/unit.rb', line 76 def properties @properties end |
#propmap ⇒ Yast2::Systemd::UnitPropMap (readonly)
71 72 73 |
# File 'library/systemd/src/lib/yast2/systemd/unit.rb', line 71 def propmap @propmap end |
#unit_name ⇒ String (readonly)
Returns eg. "apache2".
67 68 69 |
# File 'library/systemd/src/lib/yast2/systemd/unit.rb', line 67 def unit_name @unit_name end |
#unit_type ⇒ String (readonly)
Returns 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
185 186 187 188 |
# File 'library/systemd/src/lib/yast2/systemd/unit.rb', line 185 def command(command_name, = {}) command = "#{command_name} #{unit_name}.#{unit_type} #{[:options]}" Systemctl.execute(command) end |
#disable ⇒ Boolean
Returns true if the unit was correctly disabled.
145 146 147 |
# File 'library/systemd/src/lib/yast2/systemd/unit.rb', line 145 def disable run_command! { command("disable") } end |
#enable ⇒ Boolean
Returns true if the unit was correctly enabled.
139 140 141 |
# File 'library/systemd/src/lib/yast2/systemd/unit.rb', line 139 def enable run_command! { command("enable") } end |
#refresh! ⇒ Object
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 |
#reload ⇒ Boolean
Returns true if the unit was correctly reloaded.
163 164 165 |
# File 'library/systemd/src/lib/yast2/systemd/unit.rb', line 163 def reload run_command! { command("reload") } end |
#reload_or_restart ⇒ Boolean
Returns true if the unit was correctly reloaded or restarted.
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_restart ⇒ Boolean
Returns true if the unit was correctly reloaded or restarted.
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 |
#restart ⇒ Boolean
Returns true if the unit was correctly restarted.
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
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 |
#start ⇒ Boolean
Returns true if the unit was correctly started.
127 128 129 |
# File 'library/systemd/src/lib/yast2/systemd/unit.rb', line 127 def start run_command! { command("start") } end |
#status ⇒ String
121 122 123 |
# File 'library/systemd/src/lib/yast2/systemd/unit.rb', line 121 def status command("status", options: "2>&1").stdout end |
#stop ⇒ Boolean
Returns true if the unit was correctly stopped.
133 134 135 |
# File 'library/systemd/src/lib/yast2/systemd/unit.rb', line 133 def stop run_command! { command("stop") } end |
#try_restart ⇒ Boolean
Returns true if the service was correctly restarted.
157 158 159 |
# File 'library/systemd/src/lib/yast2/systemd/unit.rb', line 157 def try_restart run_command! { command("try-restart") } end |