Class: Y2Network::InterfaceConfigBuilder
- Inherits:
-
Object
- Object
- Y2Network::InterfaceConfigBuilder
- Extended by:
- Forwardable
- Includes:
- Yast::Logger
- Defined in:
- src/lib/y2network/interface_config_builder.rb
Overview
Collects data from the UI until we have enough of it to create a ConnectionConfig::Base object.
Direct Known Subclasses
Y2Network::InterfaceConfigBuilders::Bonding, Y2Network::InterfaceConfigBuilders::Bridge, Y2Network::InterfaceConfigBuilders::Ctc, Y2Network::InterfaceConfigBuilders::Dummy, Y2Network::InterfaceConfigBuilders::Infiniband, Y2Network::InterfaceConfigBuilders::Lcs, Y2Network::InterfaceConfigBuilders::Qeth, Y2Network::InterfaceConfigBuilders::Tap, Y2Network::InterfaceConfigBuilders::Tun, Y2Network::InterfaceConfigBuilders::Vlan, Y2Network::InterfaceConfigBuilders::Wireless
Constant Summary collapse
- NEW_DEVICES_COUNT =
how many device names is proposed
10
Instance Attribute Summary collapse
-
#connection_config ⇒ Y2Network::ConnectionConfig
readonly
Connection config on which builder operates.
-
#firewall_zone ⇒ Object
gets currently assigned firewall zone.
-
#interface ⇒ Y2Network::Interface?
readonly
Underlying interface if it exists.
-
#name ⇒ String
Device name (eth0, wlan0, etc.).
-
#newly_added ⇒ Boolean
writeonly
True when it is a new connection.
-
#renaming_mechanism ⇒ Symbol?
Returns the current renaming mechanism.
-
#type ⇒ Y2Network::InterfaceType
readonly
Type of @see Y2Network::Interface which is intended to be build.
Class Method Summary collapse
-
.for(type, config: nil) ⇒ Object
Load fresh instance of interface config builder for given type.
Instance Method Summary collapse
-
#alias_for(data) ⇒ Hash<String>
Convenience method to obtain a new hash from the IP additional address data.
-
#aliases ⇒ Array<Hash>
gets aliases for interface.
-
#aliases=(value) ⇒ Object
sets aliases for interface.
- #boot_protocol ⇒ Y2Network::BootProtocol
-
#boot_protocol=(value) ⇒ Object
@param[String, Y2Network::BootProtocol].
- #configure_as_port ⇒ Object
-
#driver ⇒ Object
gets currently assigned kernel module.
-
#driver=(value) ⇒ Object
sets kernel module for interface.
-
#drivers ⇒ Object
gets a list of available kernel modules for the interface.
- #hostname ⇒ String
- #hostname=(value) ⇒ Object
- #hwinfo ⇒ Hwinfo
- #hwinfo_from(info) ⇒ Hwinfo
- #ifplugd_priority ⇒ Integer
- #ifplugd_priority=(value) ⇒ Object
-
#initialize(type:, config: nil) ⇒ InterfaceConfigBuilder
constructor
Constructor.
- #ip_address ⇒ String
- #ip_address=(value) ⇒ Object
-
#mtu ⇒ String
Gets Maximum Transition Unit.
-
#mtu=(value) ⇒ Object
Sets Maximum Transition Unit.
-
#name_exists?(name) ⇒ Boolean
checks if interface name already exists.
-
#name_valid_characters ⇒ Object
gets valid characters that can be used in interface name TODO: looks sysconfig specific.
- #newly_added? ⇒ Boolean
-
#proposed_names ⇒ Array<String>
Proposes bunch of possible names for interface do not modify anything.
- #remote_ip ⇒ String
-
#remote_ip=(value) ⇒ IPAddress?
Sets remote ip for ptp connections.
-
#rename_interface(new_name) ⇒ Object
Renames the interface.
-
#renamed_interface? ⇒ Boolean
Determines whether the interface has been renamed.
-
#save ⇒ Object
saves builder content to backend.
- #startmode=(name) ⇒ Object
-
#subnet_prefix ⇒ String
Returns prefix or netmask.
- #subnet_prefix=(value) ⇒ Object
-
#valid_name?(name) ⇒ Boolean
checks if passed name is valid as interface name TODO: looks sysconfig specific.
Constructor Details
#initialize(type:, config: nil) ⇒ InterfaceConfigBuilder
Constructor
Load with reasonable defaults
78 79 80 81 82 83 84 85 86 87 88 89 90 91 |
# File 'src/lib/y2network/interface_config_builder.rb', line 78 def initialize(type:, config: nil) @type = type # TODO: also config need to store it, as newly added can be later # edited with option for not yet created interface @newly_added = config.nil? if config self.name = config.name else config = connection_config_klass(type).new config.propose end @connection_config = config @original_ip_config = ip_config_default.copy end |
Instance Attribute Details
#connection_config ⇒ Y2Network::ConnectionConfig (readonly)
Returns connection config on which builder operates.
60 61 62 |
# File 'src/lib/y2network/interface_config_builder.rb', line 60 def connection_config @connection_config end |
#firewall_zone ⇒ Object
gets currently assigned firewall zone
196 197 198 199 200 201 202 |
# File 'src/lib/y2network/interface_config_builder.rb', line 196 def firewall_zone return @firewall_zone if @firewall_zone # TODO: handle renaming firewall_interface = Y2Firewall::Firewalld::Interface.new(name) @firewall_zone = firewall_interface.zone&.name || @connection_config.firewall_zone end |
#interface ⇒ Y2Network::Interface?
Returns Underlying interface if it exists.
65 66 67 |
# File 'src/lib/y2network/interface_config_builder.rb', line 65 def interface @interface end |
#name ⇒ String
Returns Device name (eth0, wlan0, etc.).
55 56 57 |
# File 'src/lib/y2network/interface_config_builder.rb', line 55 def name @name end |
#newly_added=(value) ⇒ Boolean (writeonly)
Returns True when it is a new connection.
67 68 69 |
# File 'src/lib/y2network/interface_config_builder.rb', line 67 def newly_added=(value) @newly_added = value end |
#renaming_mechanism ⇒ Symbol?
Returns the current renaming mechanism
156 157 158 |
# File 'src/lib/y2network/interface_config_builder.rb', line 156 def renaming_mechanism @renaming_mechanism || interface.renaming_mechanism end |
#type ⇒ Y2Network::InterfaceType (readonly)
Returns type of @see Y2Network::Interface which is intended to be build.
58 59 60 |
# File 'src/lib/y2network/interface_config_builder.rb', line 58 def type @type end |
Class Method Details
.for(type, config: nil) ⇒ Object
Load fresh instance of interface config builder for given type. It can be specialized type or generic, depending if specialized is needed.
42 43 44 45 46 47 48 49 50 51 52 |
# File 'src/lib/y2network/interface_config_builder.rb', line 42 def self.for(type, config: nil) if !type.is_a?(InterfaceType) type = InterfaceType.from_short_name(type) or raise "Unknown type #{type.inspect}" end require "y2network/interface_config_builders/#{type.file_name}" InterfaceConfigBuilders.const_get(type.class_name).new(config: config) rescue LoadError => e log.info "Specialized builder for #{type} not found. Falling back to default. #{e.inspect}" new(type: type, config: config) end |
Instance Method Details
#alias_for(data) ⇒ Hash<String>
Convenience method to obtain a new hash from the IP additional address data
273 274 275 276 277 278 279 280 |
# File 'src/lib/y2network/interface_config_builder.rb', line 273 def alias_for(data) { label: data&.label.to_s, ip_address: data&.address&.address.to_s, subnet_prefix: data&.address&.prefix ? "/#{data.address.prefix}" : "", id: data&.id.to_s } end |
#aliases ⇒ Array<Hash>
gets aliases for interface
261 262 263 264 265 |
# File 'src/lib/y2network/interface_config_builder.rb', line 261 def aliases return @aliases if @aliases @aliases = @connection_config.ip_aliases.map { |d| alias_for(d) } end |
#aliases=(value) ⇒ Object
sets aliases for interface
284 285 286 |
# File 'src/lib/y2network/interface_config_builder.rb', line 284 def aliases=(value) @aliases = value end |
#boot_protocol ⇒ Y2Network::BootProtocol
208 209 210 |
# File 'src/lib/y2network/interface_config_builder.rb', line 208 def boot_protocol @connection_config.bootproto end |
#boot_protocol=(value) ⇒ Object
@param[String, Y2Network::BootProtocol]
213 214 215 216 |
# File 'src/lib/y2network/interface_config_builder.rb', line 213 def boot_protocol=(value) value = value.name if value.is_a?(Y2Network::BootProtocol) @connection_config.bootproto = Y2Network::BootProtocol.from_name(value) end |
#configure_as_port ⇒ Object
375 376 377 378 379 380 381 |
# File 'src/lib/y2network/interface_config_builder.rb', line 375 def configure_as_port self.boot_protocol = "none" self.aliases = [] self.ip_address = nil self.subnet_prefix = "" self.remote_ip = "" end |
#driver ⇒ Object
gets currently assigned kernel module
243 244 245 246 247 248 249 250 |
# File 'src/lib/y2network/interface_config_builder.rb', line 243 def driver return @driver if @driver if @interface&.custom_driver @driver = yast_config.drivers.find { |d| d.name == @interface.custom_driver } end @driver ||= :auto end |
#driver=(value) ⇒ Object
sets kernel module for interface
254 255 256 |
# File 'src/lib/y2network/interface_config_builder.rb', line 254 def driver=(value) @driver = value end |
#drivers ⇒ Object
gets a list of available kernel modules for the interface
189 190 191 192 193 |
# File 'src/lib/y2network/interface_config_builder.rb', line 189 def drivers return [] unless interface yast_config.drivers_for_interface(interface.name) end |
#hostname ⇒ String
330 331 332 |
# File 'src/lib/y2network/interface_config_builder.rb', line 330 def hostname @connection_config.hostname || "" end |
#hostname=(value) ⇒ Object
335 336 337 |
# File 'src/lib/y2network/interface_config_builder.rb', line 335 def hostname=(value) @connection_config.hostname = value end |
#hwinfo ⇒ Hwinfo
390 391 392 |
# File 'src/lib/y2network/interface_config_builder.rb', line 390 def hwinfo @hwinfo ||= Hwinfo.for(name) end |
#hwinfo_from(info) ⇒ Hwinfo
385 386 387 |
# File 'src/lib/y2network/interface_config_builder.rb', line 385 def hwinfo_from(info) @hwinfo = Hwinfo.new(info) end |
#ifplugd_priority ⇒ Integer
238 239 240 |
# File 'src/lib/y2network/interface_config_builder.rb', line 238 def ifplugd_priority (startmode.name == "ifplugd") ? startmode.priority : 0 end |
#ifplugd_priority=(value) ⇒ Object
229 230 231 232 233 234 235 |
# File 'src/lib/y2network/interface_config_builder.rb', line 229 def ifplugd_priority=(value) if !@connection_config.startmode || @connection_config.startmode.name != "ifplugd" log.info "priority set and startmode is not ifplugd. Adapting..." @connection_config.startmode = Startmode.create("ifplugd") end @connection_config.startmode.priority = value.to_i end |
#ip_address ⇒ String
289 290 291 292 293 294 295 296 |
# File 'src/lib/y2network/interface_config_builder.rb', line 289 def ip_address default = @connection_config.ip if default default.address.address.to_s else "" end end |
#ip_address=(value) ⇒ Object
299 300 301 302 303 304 305 |
# File 'src/lib/y2network/interface_config_builder.rb', line 299 def ip_address=(value) if value.nil? || value.empty? @connection_config.ip = nil else ip_config_default.address.address = value end end |
#mtu ⇒ String
Gets Maximum Transition Unit
365 366 367 |
# File 'src/lib/y2network/interface_config_builder.rb', line 365 def mtu @connection_config.mtu.to_s end |
#mtu=(value) ⇒ Object
Sets Maximum Transition Unit
371 372 373 |
# File 'src/lib/y2network/interface_config_builder.rb', line 371 def mtu=(value) @connection_config.mtu = value.to_i end |
#name_exists?(name) ⇒ Boolean
checks if interface name already exists
176 177 178 |
# File 'src/lib/y2network/interface_config_builder.rb', line 176 def name_exists?(name) interfaces.known_names.include?(name) end |
#name_valid_characters ⇒ Object
gets valid characters that can be used in interface name TODO: looks sysconfig specific
182 183 184 185 186 |
# File 'src/lib/y2network/interface_config_builder.rb', line 182 def name_valid_characters Yast.import "NetworkInterfaces" Yast::NetworkInterfaces.ValidCharsIfcfg end |
#newly_added? ⇒ Boolean
104 105 106 |
# File 'src/lib/y2network/interface_config_builder.rb', line 104 def newly_added? @newly_added end |
#proposed_names ⇒ Array<String>
Proposes bunch of possible names for interface do not modify anything
165 166 167 |
# File 'src/lib/y2network/interface_config_builder.rb', line 165 def proposed_names interfaces.free_names(type.short_name, NEW_DEVICES_COUNT) end |
#remote_ip ⇒ String
340 341 342 343 344 345 346 347 |
# File 'src/lib/y2network/interface_config_builder.rb', line 340 def remote_ip default = @connection_config.ip if default default.remote_address.to_s else "" end end |
#remote_ip=(value) ⇒ IPAddress?
Sets remote ip for ptp connections
353 354 355 356 357 358 359 360 361 |
# File 'src/lib/y2network/interface_config_builder.rb', line 353 def remote_ip=(value) return unless ip_config_default ip_config_default.remote_address = if value.nil? || value.empty? nil else IPAddress.from_string(value) end end |
#rename_interface(new_name) ⇒ Object
Renames the interface
148 149 150 151 |
# File 'src/lib/y2network/interface_config_builder.rb', line 148 def rename_interface(new_name) @old_name ||= name @name = new_name end |
#renamed_interface? ⇒ Boolean
Determines whether the interface has been renamed
139 140 141 142 143 |
# File 'src/lib/y2network/interface_config_builder.rb', line 139 def renamed_interface? return false unless interface name != interface.name || @renaming_mechanism != interface.renaming_mechanism end |
#save ⇒ Object
saves builder content to backend
109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 |
# File 'src/lib/y2network/interface_config_builder.rb', line 109 def save @connection_config.name = name @connection_config.interface = name @connection_config.ip_aliases = aliases_to_ip_configs @connection_config.firewall_zone = firewall_zone # create new instance as name can change firewall_interface = Y2Firewall::Firewalld::Interface.new(name) if Y2Firewall::Firewalld.instance.installed? && (!firewall_interface.zone || firewall_zone != firewall_interface.zone.name) firewall_interface.zone = firewall_zone end yast_config.rename_interface(@old_name, name, renaming_mechanism) if renamed_interface? yast_config.add_or_update_connection_config(@connection_config) # Assign the newly added interface in case of a new connection for an # unplugged one (bsc#1162679) self.interface = find_interface unless interface if interface.respond_to?(:custom_driver) interface.custom_driver = driver_auto? ? nil : driver.name yast_config.add_or_update_driver(driver) unless driver_auto? end nil end |
#startmode=(name) ⇒ Object
220 221 222 223 224 225 226 |
# File 'src/lib/y2network/interface_config_builder.rb', line 220 def startmode=(name) mode = name.is_a?(Startmode) ? name : Startmode.create(name) # assign only if it is not already this value. This helps with ordering of ifplugd_priority return if @connection_config.startmode && @connection_config.startmode.name == mode.name @connection_config.startmode = mode end |
#subnet_prefix ⇒ String
Returns prefix or netmask. prefix in format "/
308 309 310 311 312 313 314 |
# File 'src/lib/y2network/interface_config_builder.rb', line 308 def subnet_prefix if @connection_config.ip "/" + @connection_config.ip.address.prefix.to_s else "" end end |
#subnet_prefix=(value) ⇒ Object
317 318 319 320 321 322 323 324 325 326 327 |
# File 'src/lib/y2network/interface_config_builder.rb', line 317 def subnet_prefix=(value) if value.empty? ip_config_default.address.prefix = nil elsif value.start_with?("/") ip_config_default.address.prefix = value[1..-1].to_i elsif value =~ /^\d{1,3}$/ ip_config_default.address.prefix = value.to_i else ip_config_default.address.netmask = value end end |
#valid_name?(name) ⇒ Boolean
checks if passed name is valid as interface name TODO: looks sysconfig specific
171 172 173 |
# File 'src/lib/y2network/interface_config_builder.rb', line 171 def valid_name?(name) !!(name =~ /^[[:alnum:]._:-]{1,15}\z/) end |