Class: Yast2::SystemService

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Defined in:
library/systemd/src/lib/yast2/system_service.rb

Overview

Note:

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.

Examples:

Enabling a service

cups = SystemService.find("cups")
cups.start_mode = :on_boot
cups.save

Activating a service

cups = SystemService.find("cups")

cups.currently_active? #=> false
cups.active?           #=> false

cups.start

cups.currently_active? #=> false
cups.active?           #=> true

cups.save

cups.currently_active? #=> true
cups.active?           #=> true

Changing start mode

cups = SystemService.find("cups")
cups.start_mode = :on_demand

cups.current_start_mode #=> :on_boot
cups.start_mode         #=> :on_demand

cups.save

cups.current_start_mode #=> :on_demand
cups.start_mode         #=> :on_demand

Ignoring status changes (useful when changing the service on 1st stage)

cups = SystemService.find("cups")
cups.start

cups.currently_active? #=> false
cups.active?           #=> true

cups.save(keep_state: true)

cups.currently_active? #=> false

Defined Under Namespace

Classes: NotFoundError

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(service) ⇒ SystemService

Constructor

Parameters:


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

#actionSymbol?

Returns :start, :stop, :restart or :reload. It returns nil if no action has been requested yet.

Returns:

  • (Symbol, nil)

    :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

#errorsHash<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.

Returns:

  • (Hash<Symbol, Object>)

    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

#serviceYast2::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

Parameters:

  • name (String)

    Service name

Returns:

See Also:


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

Parameters:

  • name (String)

    service name with or without extension (e.g., "cups" or "cups.service")

Returns:


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

Parameters:

  • name (String)

    service name

Returns:

Raises:


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

Parameters:

  • names (Array<String>)

    service names to find

Returns:

  • (Array<SystemService>)

    when not found then the SystemService.servie is nil for the respective service in the list


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

Note:

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

Returns:

  • (Boolean)

    true if the service must be active; false otherwise


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.

Examples:

service.changed?(:start_mode)

Returns:

  • (Boolean)

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_modeSymbol

Note:

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

Returns:

  • (Symbol)

    :on_boot, :on_demand, :manual


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

Returns:

  • (Boolean)

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_modeSymbol

Determines the default start mode for this service

Returns:

  • (Symbol)

    :on_boot, :on_demand, :manual


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

Returns:

  • (Boolean)

See Also:


266
267
268
# File 'library/systemd/src/lib/yast2/system_service.rb', line 266

def default_start_mode?
  default_start_mode == start_mode
end

#descriptionString

Returns:

  • (String)

See Also:

  • Yast2::Systemd::Service#description

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

Returns:

  • (Boolean)

    true if it exists; false otherwise.


172
173
174
# File 'library/systemd/src/lib/yast2/system_service.rb', line 172

def found?
  !service.not_found?
end

#keywordsArray<String>

Keywords to search for this service

In case the service has an associated socket, the socket name is included as keyword.

Returns:

  • (Array<String>)

    e.g., #=> ["tftp.service", "tftp.socket"]


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

#nameString

Returns:

  • (String)

See Also:

  • Yast2::Systemd::Service#name

113
# File 'library/systemd/src/lib/yast2/system_service.rb', line 113

def_delegators :@service, :name, :static?, :running?, :description

#refreshBoolean

Refreshes the underlying service

Returns:

  • (Boolean)

    true if the service was refreshed correctly; false otherwise.


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

Returns:

  • (Boolean)

    true if the service was refreshed correctly

Raises:


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

#reloadvoid

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

#resetBoolean

Reverts cached changes

The underlying service is not refreshed. For that, see #refresh.

Returns:

  • (Boolean)

    true if the service was reset correctly. Actually, the service always can be reset.


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

#restartvoid

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

Returns:

  • (Boolean)

See Also:


113
# File 'library/systemd/src/lib/yast2/system_service.rb', line 113

def_delegators :@service, :name, :static?, :running?, :description

#save(keep_state: false) ⇒ Boolean

Note:

All cached changes are reset and the underlying service is refreshed when the changes are correctly applied.

Saves changes into the underlying system

Parameters:

  • keep_state (Boolean) (defaults to: false)

    Do not change service status. Useful when running on 1st stage.

Returns:

  • (Boolean)

    true if the service was saved correctly; false otherwise.

Raises:


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

#startvoid

This method returns an undefined value.

Sets the service to be started after calling #save

See Also:

  • #active=

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_modeSymbol

Note:

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).

Returns:

  • (Symbol)

    :on_boot, :on_demand, :manual


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.

Raises:

  • ArgumentError when mode is not valid

See Also:


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_modesArray<Symbol>

Note:

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.

Returns:

  • (Array<Symbol>)

    List of supported modes.


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

#stateString

State of the service

In case the service is not active but socket is, the socket state is considered

Returns:

  • (String)

    all possible active_state values of systemd


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

Returns:

  • (Boolean)

See Also:


113
# File 'library/systemd/src/lib/yast2/system_service.rb', line 113

def_delegators :@service, :name, :static?, :running?, :description

#stopvoid

This method returns an undefined value.

Sets the service to be stopped after calling #save

See Also:

  • #active=

336
337
338
339
# File 'library/systemd/src/lib/yast2/system_service.rb', line 336

def stop
  self.active = false
  self.action = :stop
end

#substateString

Substate of the service

In case the service is not active but socket is, the socket substate is considered

Returns:

  • (String)

    all possible sub_state values of systemd


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

Returns:

  • (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

Returns:

  • (Boolean)

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

Returns:

  • (Boolean)

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