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


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


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

def dns
  @dns
end

#driversArray<Driver>


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


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

def routing
  @routing
end

#sourceSymbol


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


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


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

def find(id)
  configs[id]
end

.from(source, *opts) ⇒ Object


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


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


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.


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


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.


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


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


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


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