Class: Yast2::CompoundService

Inherits:
Object
  • Object
show all
Includes:
Yast::Logger
Defined in:
library/systemd/src/lib/yast2/compound_service.rb

Overview

Class that represents several related services that need to be synchronized. It mimics behavior of single SystemService, but it adds possible states.

Constant Summary collapse

AUTOSTART_OPTIONS =
[:on_boot, :on_demand, :manual, :inconsistent].freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*services) ⇒ CompoundService

Creates a new service composed by several services

Examples:

iscsi = Yast2::SystemService.find("iscsi")
iscsid = Yast2::SystemService.find("iscsid")
service = Yast2::CompoundService.new(iscsi, iscsid)

Parameters:

Raises:

  • (ArgumentError)


42
43
44
45
46
# File 'library/systemd/src/lib/yast2/compound_service.rb', line 42

def initialize(*services)
  raise ArgumentError, "Services can be only System Service - #{services.inspect}" if services.any? { |s| !s.is_a?(Yast2::SystemService) }

  @services = services
end

Instance Attribute Details

#servicesArray<Yast2::SystemService> (readonly)

Managed services

Returns:



32
33
34
# File 'library/systemd/src/lib/yast2/compound_service.rb', line 32

def services
  @services
end

Instance Method Details

#action:start, ...

Action to perform over all services

Returns:

  • (:start, :stop, :restart, :reload, nil)
    • :start starts all services. If a service is already active, does nothing. if a service has socket it starts socket instead of the service.

    • :stop stops all services. If a service is inactive, does nothing.

    • :restart restarts all services. If a service is inactive, it is started.

    • :reload reloads all services that support it and restarts that does not support it. If service is inactive, it is started.

    • nil no action has been indicated. Does nothing.

See Also:



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

def action
  # TODO: check for inconsistencies?
  services.first.action
end

#current_start_mode:on_boot, ...

Note:

It offers an additional state :inconsistent that is not in SystemService

Current start mode in the system

Returns:

  • (:on_boot, :on_demand, :manual, :inconsistent)
    • :on_boot all services start during boot.

    • :on_demand all sockets associated with services are enabled and for services that do not have socket, they start on boot.

    • :manual all services and all their associated sockets are disabled.

    • :inconsistent mixture of start modes

See Also:



146
147
148
# File 'library/systemd/src/lib/yast2/compound_service.rb', line 146

def current_start_mode
  @current_start_mode ||= services_mode(method(:current_mode?))
end

#currently_active?true, ...

Note:

It offers an additional value :inconsistent that is not in SystemService

Whether the services are currently active

Returns:

  • (true, false, :inconsistent)
    • true when all services are active

    • false when no services are active

    • :inconsistent when part of services are active and part not.



70
71
72
73
74
75
# File 'library/systemd/src/lib/yast2/compound_service.rb', line 70

def currently_active?
  return true if services.all?(&:currently_active?)
  return false if services.none?(&:currently_active?)

  :inconsistent
end

#errorsHash<Symbol, Object>

Errors when trying to write changes to the underlying system

Returns:

  • (Hash<Symbol, Object>)

See Also:



214
215
216
217
218
# File 'library/systemd/src/lib/yast2/compound_service.rb', line 214

def errors
  services.each_with_object({}) do |s, result|
    result.merge!(s.errors)
  end
end

#keywordsArray<String>

Keywords that can be used to search for involved underlying units

Returns:

  • (Array<String>)

See Also:



104
105
106
# File 'library/systemd/src/lib/yast2/compound_service.rb', line 104

def keywords
  services.map(&:keywords).reduce(:+).uniq
end

#reloadObject

Sets all services to be reloaded after calling #save



259
260
261
# File 'library/systemd/src/lib/yast2/compound_service.rb', line 259

def reload
  services.each(&:reload)
end

#reset(exclude: []) ⇒ Object

Resets changes

Parameters:

  • exclude (Array) (defaults to: [])

    to exclude from reset some parts. Now supported: :action and :start_mode

See Also:



226
227
228
229
230
231
232
233
# File 'library/systemd/src/lib/yast2/compound_service.rb', line 226

def reset(exclude: [])
  old_action = action
  old_start_mode = start_mode
  services.each(&:reset)
  @current_start_mode = nil
  public_send(old_action) if !old_action.nil? && exclude.include?(:action)
  self.start_mode = old_start_mode if old_start_mode != :inconsistent && exclude.include?(:start_mode)
end

#restartObject

Sets all services to be restarted after calling #save



252
253
254
# File 'library/systemd/src/lib/yast2/compound_service.rb', line 252

def restart
  services.each(&:restart)
end

#save(keep_state: false) ⇒ Boolean

Saves all services

Returns:

  • (Boolean)

    true if all services were correctly saved; false otherwise

See Also:



53
54
55
56
57
# File 'library/systemd/src/lib/yast2/compound_service.rb', line 53

def save(keep_state: false)
  services.each { |s| s.save(keep_state: keep_state) }

  errors.empty?
end

#startObject

Sets all services to be started after calling #save



238
239
240
# File 'library/systemd/src/lib/yast2/compound_service.rb', line 238

def start
  services.each(&:start)
end

#start_mode:on_boot, ...

Note:

It offers and additional start mode :inconsistent that is not in SystemService

Target start mode

Returns:

  • (:on_boot, :on_demand, :manual, :inconsistent)
    • :on_boot when start mode is set to :on_boot for all services.

    • :on_demand when start mode is set to :on_demand for services that support it and :on_boot for the rest.

    • :manual when start mode is set to :manual for all services.

    • :inconsistent when services have mixture of start modes

See Also:



166
167
168
# File 'library/systemd/src/lib/yast2/compound_service.rb', line 166

def start_mode
  services_mode(method(:mode?))
end

#start_mode=(configuration) ⇒ Object

Note:

It offers and additional start mode :inconsistent that is not in SystemService

Sets the target start mode

Parameters:

  • configuration (Symbol)

    new start mode (e.g., :on_boot, :on_demand, :manual, :inconsistent)

Raises:

  • (ArgumentError)


177
178
179
180
181
182
183
184
185
186
187
188
189
# File 'library/systemd/src/lib/yast2/compound_service.rb', line 177

def start_mode=(configuration)
  raise ArgumentError, "Invalid parameter #{configuration.inspect}" if !AUTOSTART_OPTIONS.include?(configuration)

  case configuration
  when :inconsistent
    reset(exclude: [:action])
  when :on_demand
    services_with_socket.each { |s| s.start_mode = :on_demand }
    services_without_socket.each { |s| s.start_mode = :on_boot }
  else
    services.each { |s| s.start_mode = configuration }
  end
end

#start_modesArray<Symbol>

Possible start modes taking into account all services

If a service supports :on_boot and :manual start modes, but another service supports :on_demand too, the possible start modes will the all of them: :on_boot, on_demand and :manual.

Returns:

  • (Array<Symbol>)

    start modes (:on_boot, :on_demand, :manual)

See Also:



95
96
97
# File 'library/systemd/src/lib/yast2/compound_service.rb', line 95

def start_modes
  @start_modes ||= services.map(&:start_modes).reduce(:+).uniq
end

#stopObject

Sets all services to be stopped after calling #save

See Also:



245
246
247
# File 'library/systemd/src/lib/yast2/compound_service.rb', line 245

def stop
  services.each(&:stop)
end

#support_reload?Boolean

Whether any service supports reload

Returns:

  • (Boolean)

See Also:



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

def support_reload?
  services.any?(&:support_reload?)
end

#support_start_on_boot?Boolean

Whether the service supports :on_boot start mode

Returns:

  • (Boolean)

See Also:



205
206
207
# File 'library/systemd/src/lib/yast2/compound_service.rb', line 205

def support_start_on_boot?
  services.any?(&:support_start_on_boot?)
end

#support_start_on_demand?Boolean

Whether any service allows to start on demand

Returns:

  • (Boolean)

See Also:



196
197
198
# File 'library/systemd/src/lib/yast2/compound_service.rb', line 196

def support_start_on_demand?
  services.any?(&:support_start_on_demand?)
end