Class: Y2Network::Config

Inherits:
Object
  • Object
show all
Includes:
CanBeCopied, 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(:sysconfig)
config.interfaces.map(&:name) #=> ["lo", eth0", "wlan0"]

Adding a default route to the first routing table

config = Y2Network::Config.from(:sysconfig)
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(interfaces: InterfacesCollection.new, connections: ConnectionConfigsCollection.new, routing: Routing.new, dns: DNS.new, drivers: [], source:) ⇒ Config

Constructor

Parameters:

  • interfaces (InterfacesCollection) (defaults to: InterfacesCollection.new)

    List of interfaces

  • connections (ConnectionConfigsCollection) (defaults to: ConnectionConfigsCollection.new)

    List of connection configurations

  • routing (Routing) (defaults to: Routing.new)

    Object with routing configuration

  • dns (DNS) (defaults to: DNS.new)

    Object with DNS configuration

  • source (Symbol)

    Configuration source

  • drivers (Array<Driver>) (defaults to: [])

    List of available drivers


104
105
106
107
108
109
110
111
112
# File 'src/lib/y2network/config.rb', line 104

def initialize(interfaces: InterfacesCollection.new, connections: ConnectionConfigsCollection.new,
  routing: Routing.new, dns: DNS.new, drivers: [], source:)
  @interfaces = interfaces
  @connections = connections
  @drivers = drivers
  @routing = routing
  @dns = dns
  @source = source
end

Instance Attribute Details

#connectionsConnectionConfigsCollection


48
49
50
# File 'src/lib/y2network/config.rb', line 48

def connections
  @connections
end

#dnsDNS

Returns DNS configuration.

Returns:

  • (DNS)

    DNS configuration


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

def dns
  @dns
end

#driversArray<Driver>

Returns Available drivers.

Returns:

  • (Array<Driver>)

    Available drivers


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

def drivers
  @drivers
end

#interfacesInterfacesCollection


46
47
48
# File 'src/lib/y2network/config.rb', line 46

def interfaces
  @interfaces
end

#routingRouting

Returns Routing configuration.

Returns:

  • (Routing)

    Routing configuration


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

def routing
  @routing
end

#sourceSymbol

Returns Information source (see Reader and Writer).

Returns:

  • (Symbol)

    Information source (see Reader and Writer)


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

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


71
72
73
# File 'src/lib/y2network/config.rb', line 71

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


79
80
81
# File 'src/lib/y2network/config.rb', line 79

def find(id)
  configs[id]
end

.from(source, *opts) ⇒ Object

Parameters:

  • source (Symbol)

    Source to read the configuration from

  • opts (Array<Object>)

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


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

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

.resetObject

Resets the configuration register


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

def reset
  configs.clear
end

Instance Method Details

#==(other) ⇒ Boolean Also known as: eql?

Determines whether two configurations are equal

Returns:

  • (Boolean)

    true if both configurations are equal; false otherwise


131
132
133
134
# File 'src/lib/y2network/config.rb', line 131

def ==(other)
  source == other.source && interfaces == other.interfaces &&
    routing == other.routing && dns == other.dns && connections == other.connections
end

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


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

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


195
196
197
198
199
200
201
202
# File 'src/lib/y2network/config.rb', line 195

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

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

210
211
212
213
# File 'src/lib/y2network/config.rb', line 210

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


218
219
220
221
222
223
224
225
# File 'src/lib/y2network/config.rb', line 218

def connections_to_modify(connection_config)
  result = []
  bond_bridge = connection_config.find_master(connections)
  result << bond_bridge if bond_bridge
  vlans = connections.to_a.select { |c| c.type.vlan? && c.parent_device == connection_config.name }
  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


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

def delete_interface(name)
  delete_dependents(name)

  connections.reject! { |c| c.interface == name }
  # do not use no longer existing device name
  dns.dhcp_hostname = :none if dns.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:


185
186
187
188
189
190
# File 'src/lib/y2network/config.rb', line 185

def drivers_for_interface(name)
  interface = interfaces.by_name(name)
  names = interface.drivers.map(&:name)
  names << interface.custom_driver if interface.custom_driver && !names.include?(interface.custom_driver)
  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)


141
142
143
144
145
146
147
148
149
150
151
# File 'src/lib/y2network/config.rb', line 141

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
  dns.dhcp_hostname = new_name if dns.dhcp_hostname == old_name
end

#write(original: nil, target: nil) ⇒ Object

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 (:sysconfig)

See Also:


123
124
125
126
# File 'src/lib/y2network/config.rb', line 123

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