Class: Yast::SystemdUnit
- Inherits:
-
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
collapse
- 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 =
{
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
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
@name = id.to_s.split(".").first || unit_name
end
|
Instance Attribute Details
Returns 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
|
Returns 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
|
91
92
93
|
# File 'library/systemd/src/lib/yast2/systemd_unit.rb', line 91
def properties
@properties
end
|
86
87
88
|
# File 'library/systemd/src/lib/yast2/systemd_unit.rb', line 86
def propmap
@propmap
end
|
#unit_name ⇒ String
82
83
84
|
# File 'library/systemd/src/lib/yast2/systemd_unit.rb', line 82
def unit_name
@unit_name
end
|
#unit_type ⇒ String
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, ...
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
|
#disable ⇒ Object
141
142
143
|
# File 'library/systemd/src/lib/yast2/systemd_unit.rb', line 141
def disable
run_command! { command("disable") }
end
|
#enable ⇒ Object
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
|
#reload ⇒ Object
153
154
155
|
# File 'library/systemd/src/lib/yast2/systemd_unit.rb', line 153
def reload
run_command! { command("reload") }
end
|
#reload_or_restart ⇒ Object
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_restart ⇒ Object
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
|
#restart ⇒ Object
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
#start ⇒ Object
129
130
131
|
# File 'library/systemd/src/lib/yast2/systemd_unit.rb', line 129
def start
run_command! { command("start") }
end
|
#status ⇒ Object
125
126
127
|
# File 'library/systemd/src/lib/yast2/systemd_unit.rb', line 125
def status
command("status", options: "2>&1").stdout
end
|
#stop ⇒ Object
133
134
135
|
# File 'library/systemd/src/lib/yast2/systemd_unit.rb', line 133
def stop
run_command! { command("stop") }
end
|
#try_restart ⇒ Object
149
150
151
|
# File 'library/systemd/src/lib/yast2/systemd_unit.rb', line 149
def try_restart
run_command! { command("try-restart") }
end
|