Class: Y2Network::Config
- Inherits:
-
Object
- Object
- Y2Network::Config
- 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.
Instance Attribute Summary collapse
- #backend ⇒ Backend, ...
- #connections ⇒ ConnectionConfigsCollection
-
#dns ⇒ DNS
DNS configuration.
-
#drivers ⇒ Array<Driver>
Available drivers.
-
#hostname ⇒ Hostname
Hostname configuration.
- #interfaces ⇒ InterfacesCollection
-
#routing ⇒ Routing
Routing configuration.
- #s390_devices ⇒ S390GroupDevicesCollection
-
#source ⇒ Symbol
Information source (see Reader and Writer).
Class Method Summary collapse
-
.add(id, config) ⇒ Object
Adds the configuration to the register.
-
.find(id) ⇒ Config?
Finds the configuration in the register.
-
.from(source, *opts) ⇒ IssuesResult
Result of reading the network configuration.
-
.reset ⇒ Object
Resets the configuration register.
Instance Method Summary collapse
-
#add_or_update_connection_config(connection_config) ⇒ Object
Adds or update a connection config.
-
#add_or_update_driver(new_driver) ⇒ Object
Adds or update a driver.
-
#backend?(name) ⇒ Boolean
Return whether the config backend is the one given or not.
-
#configured_interface?(iface_name) ⇒ Boolean
Determines whether a given interface is configured or not.
-
#connections_to_modify(connection_config) ⇒ ConnectionConfigsCollection
Returns collection of interfaces that needs to be modified or deleted if
connection_config
is deleted or renamed. -
#delete_interface(name) ⇒ Object
deletes interface and all its config.
-
#drivers_for_interface(name) ⇒ Array<Driver>
Returns the candidate drivers for a given interface.
-
#initialize(source:, **opts) ⇒ Config
constructor
Constructor.
-
#rename_interface(old_name, new_name, mechanism) ⇒ Object
Renames a given interface and the associated connections.
-
#update(changes = {}) ⇒ Y2Network::Config
Updates configuration section.
-
#write(original: nil, target: nil, only: nil) ⇒ IssuesResult
Writes the configuration into the YaST modules.
Methods included from CanBeCopied
Constructor Details
#initialize(source:, **opts) ⇒ Config
Constructor
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
#backend ⇒ Backend, ...
51 52 53 |
# File 'src/lib/y2network/config.rb', line 51 def backend @backend end |
#connections ⇒ ConnectionConfigsCollection
55 56 57 |
# File 'src/lib/y2network/config.rb', line 55 def connections @connections end |
#dns ⇒ DNS
Returns DNS configuration.
61 62 63 |
# File 'src/lib/y2network/config.rb', line 61 def dns @dns end |
#drivers ⇒ Array<Driver>
Returns Available drivers.
65 66 67 |
# File 'src/lib/y2network/config.rb', line 65 def drivers @drivers end |
#hostname ⇒ Hostname
Returns Hostname configuration.
63 64 65 |
# File 'src/lib/y2network/config.rb', line 63 def hostname @hostname end |
#interfaces ⇒ InterfacesCollection
53 54 55 |
# File 'src/lib/y2network/config.rb', line 53 def interfaces @interfaces end |
#routing ⇒ Routing
Returns Routing configuration.
59 60 61 |
# File 'src/lib/y2network/config.rb', line 59 def routing @routing end |
#s390_devices ⇒ S390GroupDevicesCollection
57 58 59 |
# File 'src/lib/y2network/config.rb', line 57 def s390_devices @s390_devices end |
#source ⇒ Symbol
Returns 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
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
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.
74 75 76 77 |
# File 'src/lib/y2network/config.rb', line 74 def from(source, *opts) reader = ConfigReader.for(source, *opts) reader.read end |
.reset ⇒ Object
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
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
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.
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
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
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.
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
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
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.
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
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 |