Class: Y2Network::Config

Inherits:
Object
  • Object
show all
Includes:
CanBeCopied, Yast2::Equatable, Yast::Logger
Defined in:
src/lib/y2network/config.rb

Overview

This class represents the current network configuration including interfaces, routes, etc.

Examples:

Reading from wicked

config = Y2Network::Config.from(:wicked).config
config.interfaces.map(&:name) #=> ["lo", eth0", "wlan0"]

Adding a default route to the first routing table

config = Y2Network::Config.from(:wicked).config
route = Y2Network::Route.new(to: :default)
config.routing.tables.first << route
config.write

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from CanBeCopied

#copy

Constructor Details

#initialize(source:, **opts) ⇒ Config

Constructor

Parameters:

  • source (Symbol)

    Configuration source

  • opts (Hash)

    configuration options

Options Hash (**opts):



119
120
121
122
123
124
125
126
127
128
129
# File 'src/lib/y2network/config.rb', line 119

def initialize(source:, **opts)
  @backend = opts.fetch(:backend, nil)
  @interfaces = opts.fetch(:interfaces, InterfacesCollection.new)
  @connections = opts.fetch(:connections, ConnectionConfigsCollection.new)
  @s390_devices = opts.fetch(:s390_devices, S390GroupDevicesCollection.new)
  @drivers = opts.fetch(:drivers, [])
  @routing = opts.fetch(:routing, Routing.new)
  @dns = opts.fetch(:dns, DNS.new)
  @hostname = opts.fetch(:hostname, Hostname.new)
  @source = source
end

Instance Attribute Details

#backendBackend, ...

Returns:



51
52
53
# File 'src/lib/y2network/config.rb', line 51

def backend
  @backend
end

#connectionsConnectionConfigsCollection



55
56
57
# File 'src/lib/y2network/config.rb', line 55

def connections
  @connections
end

#dnsDNS

Returns DNS configuration.

Returns:

  • (DNS)

    DNS configuration



61
62
63
# File 'src/lib/y2network/config.rb', line 61

def dns
  @dns
end

#driversArray<Driver>

Returns Available drivers.

Returns:

  • (Array<Driver>)

    Available drivers



65
66
67
# File 'src/lib/y2network/config.rb', line 65

def drivers
  @drivers
end

#hostnameHostname

Returns Hostname configuration.

Returns:



63
64
65
# File 'src/lib/y2network/config.rb', line 63

def hostname
  @hostname
end

#interfacesInterfacesCollection



53
54
55
# File 'src/lib/y2network/config.rb', line 53

def interfaces
  @interfaces
end

#routingRouting

Returns Routing configuration.

Returns:

  • (Routing)

    Routing configuration



59
60
61
# File 'src/lib/y2network/config.rb', line 59

def routing
  @routing
end

#s390_devicesS390GroupDevicesCollection



57
58
59
# File 'src/lib/y2network/config.rb', line 57

def s390_devices
  @s390_devices
end

#sourceSymbol

Returns Information source (see Reader and Writer).

Returns:

  • (Symbol)

    Information source (see Reader and Writer)



67
68
69
# File 'src/lib/y2network/config.rb', line 67

def source
  @source
end

Class Method Details

.add(id, config) ⇒ Object

Adds the configuration to the register

Parameters:

  • id (Symbol)

    Configuration ID

  • config (Y2Network::Config)

    Network configuration



83
84
85
# File 'src/lib/y2network/config.rb', line 83

def add(id, config)
  configs[id] = config
end

.find(id) ⇒ Config?

Finds the configuration in the register

Parameters:

  • id (Symbol)

    Configuration ID

Returns:

  • (Config, nil)

    Configuration with the given ID or nil if not found



91
92
93
# File 'src/lib/y2network/config.rb', line 91

def find(id)
  configs[id]
end

.from(source, *opts) ⇒ IssuesResult

Returns Result of reading the network configuration.

Parameters:

  • source (Symbol)

    Source to read the configuration from

  • opts (Array<Object>)

    Reader options. Check readers documentation to find out supported options.

Returns:

  • (IssuesResult)

    Result of reading the network configuration



74
75
76
77
# File 'src/lib/y2network/config.rb', line 74

def from(source, *opts)
  reader = ConfigReader.for(source, *opts)
  reader.read
end

.resetObject

Resets the configuration register



96
97
98
# File 'src/lib/y2network/config.rb', line 96

def reset
  configs.clear
end

Instance Method Details

#add_or_update_connection_config(connection_config) ⇒ Object

Adds or update a connection config

If the interface which is associated to does not exist (because it is a virtual one or it is not present), it gets added.



189
190
191
192
193
194
195
196
197
# File 'src/lib/y2network/config.rb', line 189

def add_or_update_connection_config(connection_config)
  log.info "add_update connection config #{connection_config.inspect}"
  connections.add_or_update(connection_config)
  interface = interfaces.by_name(connection_config.interface)
  return if interface

  log.info "Creating new interface"
  interfaces << Interface.from_connection(connection_config)
end

#add_or_update_driver(new_driver) ⇒ Object

Adds or update a driver

Parameters:

  • new_driver (Driver)

    Driver to add or update



214
215
216
217
218
219
220
221
# File 'src/lib/y2network/config.rb', line 214

def add_or_update_driver(new_driver)
  idx = drivers.find_index { |d| d.name == new_driver.name }
  if idx
    drivers[idx] = new_driver
  else
    drivers << new_driver
  end
end

#backend?(name) ⇒ Boolean

Return whether the config backend is the one given or not

Parameters:

  • name (Symbol)

Returns:

  • (Boolean)


252
253
254
# File 'src/lib/y2network/config.rb', line 252

def backend?(name)
  backend&.id == name
end

#configured_interface?(iface_name) ⇒ Boolean

Determines whether a given interface is configured or not

An interface is considered as configured when it has an associated collection.

Parameters:

  • iface_name (String)

    Interface's name

Returns:

  • (Boolean)


229
230
231
232
233
# File 'src/lib/y2network/config.rb', line 229

def configured_interface?(iface_name)
  return false if iface_name.nil? || iface_name.empty?

  !connections.by_interface(iface_name).empty?
end

#connections_to_modify(connection_config) ⇒ ConnectionConfigsCollection

Note:

does not work recursively. So for delete it needs to be called for all modified vlans.

Returns collection of interfaces that needs to be modified or deleted if connection_config is deleted or renamed

Returns:

  • (ConnectionConfigsCollection)

    returns collection of interfaces that needs to be modified or deleted if connection_config is deleted or renamed



238
239
240
241
242
243
244
245
246
247
# File 'src/lib/y2network/config.rb', line 238

def connections_to_modify(connection_config)
  result = []
  bond_bridge = connection_config.find_parent(connections)
  result << bond_bridge if bond_bridge
  vlans = connections.to_a.select do |c|
    c.type.vlan? && c.parent_device == connection_config.name
  end
  result.concat(vlans)
  ConnectionConfigsCollection.new(result)
end

#delete_interface(name) ⇒ Object

deletes interface and all its config. If interface is physical, it is not removed as we cannot remove physical interface.

Parameters:

  • name (String)

    Interface's name



173
174
175
176
177
178
179
180
181
182
183
# File 'src/lib/y2network/config.rb', line 173

def delete_interface(name)
  delete_dependents(name)

  connections.reject! { |c| c.interface == name }
  # do not use no longer existing device name
  hostname.dhcp_hostname = :none if hostname.dhcp_hostname == name
  interface = interfaces.by_name(name)
  return if interface.is_a?(PhysicalInterface) && interface.present?

  interfaces.reject! { |i| i.name == name }
end

#drivers_for_interface(name) ⇒ Array<Driver>

Returns the candidate drivers for a given interface

Returns:



202
203
204
205
206
207
208
209
# File 'src/lib/y2network/config.rb', line 202

def drivers_for_interface(name)
  interface = interfaces.by_name(name)
  names = interface.drivers.map(&:name)
  if interface.custom_driver && !names.include?(interface.custom_driver)
    names << interface.custom_driver
  end
  drivers.select { |d| names.include?(d.name) }
end

#rename_interface(old_name, new_name, mechanism) ⇒ Object

Renames a given interface and the associated connections

Parameters:

  • old_name (String)

    Old interface's name

  • new_name (String)

    New interface's name

  • mechanism (Symbol)

    Property to base the rename on (:mac or :bus_id)



156
157
158
159
160
161
162
163
164
165
166
167
# File 'src/lib/y2network/config.rb', line 156

def rename_interface(old_name, new_name, mechanism)
  log.info "Renaming #{old_name.inspect} to #{new_name.inspect} using #{mechanism.inspect}"
  interface = interfaces.by_name(old_name || new_name)
  interface.rename(new_name, mechanism)
  return unless old_name # do not modify configurations if it is just renaming mechanism

  connections.by_interface(old_name).each do |connection|
    connection.interface = new_name
    rename_dependencies(old_name, new_name, connection)
  end
  hostname.dhcp_hostname = new_name if hostname.dhcp_hostname == old_name
end

#update(changes = {}) ⇒ Y2Network::Config

Updates configuration section

This method returns a new instance of Config, leaving the received as it is.

Examples:

Update interfaces list

config.update(interfaces: InterfacesCollection.new)

Parameters:

  • changes (Hash<Symbol,Object>) (defaults to: {})

    A hash where the keys are the sections to update and the values are the new values

Returns:



271
272
273
274
275
276
277
278
279
280
281
282
283
# File 'src/lib/y2network/config.rb', line 271

def update(changes = {})
  self.class.new(
    interfaces:   changes[:interfaces] || interfaces,
    connections:  changes[:connections] || connections,
    s390_devices: changes[:s390_devices] || s390_devices,
    drivers:      changes[:drivers] || drivers,
    routing:      changes[:routing] || routing,
    dns:          changes[:dns] || dns,
    hostname:     changes[:hostname] || hostname,
    source:       changes[:source] || source,
    backend:      changes[:backend] || backend
  )
end

#write(original: nil, target: nil, only: nil) ⇒ IssuesResult

Writes the configuration into the YaST modules

Writes only changes against original configuration if the original configuration is provided

Parameters:

  • original (Y2Network::Config) (defaults to: nil)

    configuration used for detecting changes

  • target (Symbol) (defaults to: nil)

    Target to write the configuration to (:wicked)

  • only (Array<symbol>, nil) (defaults to: nil)

    explicit sections to be written, by default if no parameter is given then all changes will be written.

Returns:

  • (IssuesResult)

    Result of writing the network configuration

See Also:



144
145
146
147
# File 'src/lib/y2network/config.rb', line 144

def write(original: nil, target: nil, only: nil)
  target = target || backend&.id || source
  Y2Network::ConfigWriter.for(target).write(self, original, only: only)
end