Class: Yast2::SystemService
- Inherits:
-
Object
- Object
- Yast2::SystemService
- Extended by:
- Forwardable
- Defined in:
- library/systemd/src/lib/yast2/system_service.rb
Overview
All changes performed over an object of this class are not applied into the underlying system until the #save method is called.
This class represents a service from a high level point of view
When talking about systemd, it might happen that a service could have an associated socket (or path or timer). This class is able to group those units and offer an API to handle them together.
See also services_and_sockets.
Defined Under Namespace
Classes: NotFoundError
Instance Attribute Summary collapse
-
#action ⇒ Symbol?
readonly
:start, :stop, :restart or :reload.
-
#errors ⇒ Hash<Symbol, Object>
readonly
Errors when trying to write changes to the underlying system.
- #service ⇒ Yast2::Systemd::Service readonly
Class Method Summary collapse
-
.build(name) ⇒ SystemService
Builds a service instance based on the given name.
-
.find(name) ⇒ SystemService?
Finds a service by its name.
-
.find!(name) ⇒ SystemService
Finds a service by its name.
-
.find_many(names) ⇒ Array<SystemService>
Finds a set of services by their names.
Instance Method Summary collapse
-
#active? ⇒ Boolean
Whether the service will be active after calling #save.
-
#changed?(key = nil) ⇒ Boolean
Whether there is any cached change that will be applied by calling #save.
-
#current_start_mode ⇒ Symbol
Gets the current start_mode.
-
#currently_active? ⇒ Boolean
Whether the service is currently active in the system.
-
#default_start_mode ⇒ Symbol
Determines the default start mode for this service.
-
#default_start_mode? ⇒ Boolean
Determines whether the start mode has been changed from system's default.
- #description ⇒ String
-
#found? ⇒ Boolean
Determines whether the service exists in the underlying system.
-
#initialize(service) ⇒ SystemService
constructor
Constructor.
-
#keywords ⇒ Array<String>
Keywords to search for this service.
- #name ⇒ String
-
#refresh ⇒ Boolean
Refreshes the underlying service.
-
#refresh! ⇒ Boolean
Refreshes the underlying service.
-
#reload ⇒ void
Sets the service to be reloaded after calling #save.
-
#reset ⇒ Boolean
Reverts cached changes.
-
#restart ⇒ void
Sets the service to be restarted after calling #save.
- #running? ⇒ Boolean
-
#save(keep_state: false) ⇒ Boolean
Saves changes into the underlying system.
-
#start ⇒ void
Sets the service to be started after calling #save.
-
#start_mode ⇒ Symbol
Returns the start mode.
-
#start_mode=(mode) ⇒ Object
Sets the service start mode.
-
#start_modes ⇒ Array<Symbol>
Returns the list of supported start modes for this service (if a socket unit is available, :on_demand is supported, otherwise not).
-
#state ⇒ String
State of the service.
- #static? ⇒ Boolean
-
#stop ⇒ void
Sets the service to be stopped after calling #save.
-
#substate ⇒ String
Substate of the service.
- #support_reload? ⇒ Boolean
-
#support_start_on_boot? ⇒ Boolean
Whether the service supports :on_boot start mode.
-
#support_start_on_demand? ⇒ Boolean
Whether the service supports :on_demand start mode.
Constructor Details
#initialize(service) ⇒ SystemService
Constructor
163 164 165 166 167 |
# File 'library/systemd/src/lib/yast2/system_service.rb', line 163 def initialize(service) @service = service @changes = {} @errors = {} end |
Instance Attribute Details
#action ⇒ Symbol?
Returns :start, :stop, :restart or :reload. It returns nil if no action has been requested yet.
94 95 96 |
# File 'library/systemd/src/lib/yast2/system_service.rb', line 94 def action @action end |
#errors ⇒ Hash<Symbol, Object> (readonly)
Returns Errors when trying to write changes to the underlying system.
- :active [Boolean] whether the service should be active after saving
- :start_mode [Symbol] start mode the service should have after saving.
90 91 92 |
# File 'library/systemd/src/lib/yast2/system_service.rb', line 90 def errors @errors end |
#service ⇒ Yast2::Systemd::Service (readonly)
85 86 87 |
# File 'library/systemd/src/lib/yast2/system_service.rb', line 85 def service @service end |
Class Method Details
.build(name) ⇒ SystemService
Builds a service instance based on the given name
146 147 148 |
# File 'library/systemd/src/lib/yast2/system_service.rb', line 146 def build(name) new(Yast2::Systemd::Service.build(name)) end |
.find(name) ⇒ SystemService?
Finds a service by its name
120 121 122 123 124 125 |
# File 'library/systemd/src/lib/yast2/system_service.rb', line 120 def find(name) systemd_service = Yast2::Systemd::Service.find(name) return nil unless systemd_service new(systemd_service) end |
.find!(name) ⇒ SystemService
Finds a service by its name
133 134 135 136 137 138 |
# File 'library/systemd/src/lib/yast2/system_service.rb', line 133 def find!(name) system_service = find(name) raise(NotFoundError, name) unless system_service system_service end |
.find_many(names) ⇒ Array<SystemService>
Finds a set of services by their names
155 156 157 |
# File 'library/systemd/src/lib/yast2/system_service.rb', line 155 def find_many(names) Yast2::Systemd::Service.find_many(names).map { |s| new(s) } end |
Instance Method Details
#active? ⇒ Boolean
This is a temporary value (not saved yet). Use #currently_active? to get the actual active value of the service in the system.
Whether the service will be active after calling #save
304 305 306 307 |
# File 'library/systemd/src/lib/yast2/system_service.rb', line 304 def active? new_value = new_value_for(:active) new_value.nil? ? currently_active? : new_value end |
#changed?(key = nil) ⇒ Boolean
Whether there is any cached change that will be applied by calling #save.
Some specific change can be checked by using the key parameter.
417 418 419 |
# File 'library/systemd/src/lib/yast2/system_service.rb', line 417 def changed?(key = nil) key ? changed_value?(key) : any_change? end |
#current_start_mode ⇒ Symbol
This is the start mode that the service currently has in the system. Method #start_mode returns the last start mode that has been set to the service, but that value has not been applied yet (only changed in memory).
Gets the current start_mode
205 206 207 208 209 |
# File 'library/systemd/src/lib/yast2/system_service.rb', line 205 def current_start_mode return @current_start_mode unless @current_start_mode.nil? @current_start_mode = start_mode_from(service, socket, :enabled?) end |
#currently_active? ⇒ Boolean
Whether the service is currently active in the system
223 224 225 |
# File 'library/systemd/src/lib/yast2/system_service.rb', line 223 def currently_active? service.active? || (socket? && socket.active?) end |
#default_start_mode ⇒ Symbol
Determines the default start mode for this service
214 215 216 217 218 |
# File 'library/systemd/src/lib/yast2/system_service.rb', line 214 def default_start_mode return @default_start_mode unless @default_start_mode.nil? @default_start_mode = start_mode_from(service, socket, :preset_enabled?) end |
#default_start_mode? ⇒ Boolean
Determines whether the start mode has been changed from system's default
266 267 268 |
# File 'library/systemd/src/lib/yast2/system_service.rb', line 266 def default_start_mode? default_start_mode == start_mode end |
#description ⇒ String
113 |
# File 'library/systemd/src/lib/yast2/system_service.rb', line 113 def_delegators :@service, :name, :static?, :running?, :description |
#found? ⇒ Boolean
Determines whether the service exists in the underlying system
172 173 174 |
# File 'library/systemd/src/lib/yast2/system_service.rb', line 172 def found? !service.not_found? end |
#keywords ⇒ Array<String>
Keywords to search for this service
In case the service has an associated socket, the socket name is included as keyword.
315 316 317 318 319 |
# File 'library/systemd/src/lib/yast2/system_service.rb', line 315 def keywords keywords = [service.id] keywords << socket.id if socket keywords end |
#name ⇒ String
113 |
# File 'library/systemd/src/lib/yast2/system_service.rb', line 113 def_delegators :@service, :name, :static?, :running?, :description |
#refresh ⇒ Boolean
Refreshes the underlying service
390 391 392 393 394 |
# File 'library/systemd/src/lib/yast2/system_service.rb', line 390 def refresh refresh! rescue Yast2::Systemctl::Error false end |
#refresh! ⇒ Boolean
Refreshes the underlying service
401 402 403 404 405 406 407 |
# File 'library/systemd/src/lib/yast2/system_service.rb', line 401 def refresh! service.refresh! @start_modes = nil @current_start_mode = nil true end |
#reload ⇒ void
This method returns an undefined value.
Sets the service to be reloaded after calling #save
352 353 354 355 |
# File 'library/systemd/src/lib/yast2/system_service.rb', line 352 def reload register_change(:active, true) self.action = :reload end |
#reset ⇒ Boolean
Reverts cached changes
The underlying service is not refreshed. For that, see #refresh.
380 381 382 383 384 385 |
# File 'library/systemd/src/lib/yast2/system_service.rb', line 380 def reset clear_changes @action = nil true end |
#restart ⇒ void
This method returns an undefined value.
Sets the service to be restarted after calling #save
344 345 346 347 |
# File 'library/systemd/src/lib/yast2/system_service.rb', line 344 def restart register_change(:active, true) self.action = :restart end |
#running? ⇒ Boolean
113 |
# File 'library/systemd/src/lib/yast2/system_service.rb', line 113 def_delegators :@service, :name, :static?, :running?, :description |
#save(keep_state: false) ⇒ Boolean
All cached changes are reset and the underlying service is refreshed when the changes are correctly applied.
Saves changes into the underlying system
366 367 368 369 370 371 372 |
# File 'library/systemd/src/lib/yast2/system_service.rb', line 366 def save(keep_state: false) clear_errors save_start_mode perform_action unless keep_state errors.none? && reset && refresh! end |
#start ⇒ void
This method returns an undefined value.
Sets the service to be started after calling #save
326 327 328 329 |
# File 'library/systemd/src/lib/yast2/system_service.rb', line 326 def start self.active = true self.action = :start end |
#start_mode ⇒ Symbol
This is a temporary value (not saved yet). Use #current_start_mode to get the actual start mode of the service in the system.
Returns the start mode
See #start_modes to find out the supported modes for a given service (usually :on_boot, :manual and, in some cases, :on_demand).
258 259 260 |
# File 'library/systemd/src/lib/yast2/system_service.rb', line 258 def start_mode new_value_for(:start_mode) || current_start_mode end |
#start_mode=(mode) ⇒ Object
Sets the service start mode
See #start_modes to find out the supported modes for a given service (usually :on_boot, :manual and, in some cases, :on_demand). The given value will be applied after calling #save.
278 279 280 281 282 |
# File 'library/systemd/src/lib/yast2/system_service.rb', line 278 def start_mode=(mode) raise ArgumentError, "Invalid start mode: '#{mode}' for service '#{service.name}'" if !start_modes.include?(mode) register_change(:start_mode, mode) end |
#start_modes ⇒ Array<Symbol>
When the service does not exist in the underlying system (for instance,
Returns the list of supported start modes for this service (if a socket unit is available, :on_demand is supported, otherwise not)
- :on_boot: The service will be started when the system boots.
- :on_demand: The service will be started on demand.
- :manual: The service is disabled and it will be started manually.
during 1st stage) all possible start modes are returned, as there is no way to find out which of them are supported.
239 240 241 242 243 244 245 246 247 |
# File 'library/systemd/src/lib/yast2/system_service.rb', line 239 def start_modes @start_modes = [:on_boot, :manual, :on_demand] unless found? return @start_modes if @start_modes @start_modes = [:manual] @start_modes << :on_boot unless service.static? @start_modes << :on_demand if socket @start_modes end |
#state ⇒ String
State of the service
In case the service is not active but socket is, the socket state is considered
181 182 183 184 185 |
# File 'library/systemd/src/lib/yast2/system_service.rb', line 181 def state return socket.active_state if socket? && socket.active? && !service.active? service.active_state end |
#static? ⇒ Boolean
113 |
# File 'library/systemd/src/lib/yast2/system_service.rb', line 113 def_delegators :@service, :name, :static?, :running?, :description |
#stop ⇒ void
This method returns an undefined value.
Sets the service to be stopped after calling #save
336 337 338 339 |
# File 'library/systemd/src/lib/yast2/system_service.rb', line 336 def stop self.active = false self.action = :stop end |
#substate ⇒ String
Substate of the service
In case the service is not active but socket is, the socket substate is considered
192 193 194 195 196 |
# File 'library/systemd/src/lib/yast2/system_service.rb', line 192 def substate return socket.sub_state if socket? && socket.active? && !service.active? service.sub_state end |
#support_reload? ⇒ Boolean
99 |
# File 'library/systemd/src/lib/yast2/system_service.rb', line 99 def_delegator :@service, :can_reload?, :support_reload? |
#support_start_on_boot? ⇒ Boolean
Whether the service supports :on_boot start mode
294 295 296 |
# File 'library/systemd/src/lib/yast2/system_service.rb', line 294 def support_start_on_boot? start_modes.include?(:on_boot) end |
#support_start_on_demand? ⇒ Boolean
Whether the service supports :on_demand start mode
287 288 289 |
# File 'library/systemd/src/lib/yast2/system_service.rb', line 287 def support_start_on_demand? start_modes.include?(:on_demand) end |