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
77 78 79 80 81 82 83 84 85 86 87 88 89 90 |
# File 'src/lib/y2network/interface_config_builder.rb', line 77 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.
59 60 61 |
# File 'src/lib/y2network/interface_config_builder.rb', line 59 def connection_config @connection_config end |
#firewall_zone ⇒ Object
gets currently assigned firewall zone
197 198 199 200 201 202 203 |
# File 'src/lib/y2network/interface_config_builder.rb', line 197 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.
64 65 66 |
# File 'src/lib/y2network/interface_config_builder.rb', line 64 def interface @interface end |
#name ⇒ String
Returns Device name (eth0, wlan0, etc.).
54 55 56 |
# File 'src/lib/y2network/interface_config_builder.rb', line 54 def name @name end |
#newly_added=(value) ⇒ Boolean (writeonly)
Returns True when it is a new connection.
66 67 68 |
# File 'src/lib/y2network/interface_config_builder.rb', line 66 def newly_added=(value) @newly_added = value end |
#renaming_mechanism ⇒ Symbol?
Returns the current renaming mechanism
157 158 159 |
# File 'src/lib/y2network/interface_config_builder.rb', line 157 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.
57 58 59 |
# File 'src/lib/y2network/interface_config_builder.rb', line 57 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 |
# 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
274 275 276 277 278 279 280 281 |
# File 'src/lib/y2network/interface_config_builder.rb', line 274 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
262 263 264 265 266 |
# File 'src/lib/y2network/interface_config_builder.rb', line 262 def aliases return @aliases if @aliases @aliases = @connection_config.ip_aliases.map { |d| alias_for(d) } end |
#aliases=(value) ⇒ Object
sets aliases for interface
285 286 287 |
# File 'src/lib/y2network/interface_config_builder.rb', line 285 def aliases=(value) @aliases = value end |
#boot_protocol ⇒ Y2Network::BootProtocol
209 210 211 |
# File 'src/lib/y2network/interface_config_builder.rb', line 209 def boot_protocol @connection_config.bootproto end |
#boot_protocol=(value) ⇒ Object
@param[String, Y2Network::BootProtocol]
214 215 216 217 |
# File 'src/lib/y2network/interface_config_builder.rb', line 214 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
376 377 378 379 380 381 382 |
# File 'src/lib/y2network/interface_config_builder.rb', line 376 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
244 245 246 247 248 249 250 251 |
# File 'src/lib/y2network/interface_config_builder.rb', line 244 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
255 256 257 |
# File 'src/lib/y2network/interface_config_builder.rb', line 255 def driver=(value) @driver = value end |
#drivers ⇒ Object
gets a list of available kernel modules for the interface
190 191 192 193 194 |
# File 'src/lib/y2network/interface_config_builder.rb', line 190 def drivers return [] unless interface yast_config.drivers_for_interface(interface.name) end |
#hostname ⇒ String
331 332 333 |
# File 'src/lib/y2network/interface_config_builder.rb', line 331 def hostname @connection_config.hostname || "" end |
#hostname=(value) ⇒ Object
336 337 338 |
# File 'src/lib/y2network/interface_config_builder.rb', line 336 def hostname=(value) @connection_config.hostname = value end |
#hwinfo ⇒ Hwinfo
391 392 393 |
# File 'src/lib/y2network/interface_config_builder.rb', line 391 def hwinfo @hwinfo ||= Hwinfo.for(name) end |
#hwinfo_from(info) ⇒ Hwinfo
386 387 388 |
# File 'src/lib/y2network/interface_config_builder.rb', line 386 def hwinfo_from(info) @hwinfo = Hwinfo.new(info) end |
#ifplugd_priority ⇒ Integer
239 240 241 |
# File 'src/lib/y2network/interface_config_builder.rb', line 239 def ifplugd_priority (startmode.name == "ifplugd") ? startmode.priority : 0 end |
#ifplugd_priority=(value) ⇒ Object
230 231 232 233 234 235 236 |
# File 'src/lib/y2network/interface_config_builder.rb', line 230 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
290 291 292 293 294 295 296 297 |
# File 'src/lib/y2network/interface_config_builder.rb', line 290 def ip_address default = @connection_config.ip if default default.address.address.to_s else "" end end |
#ip_address=(value) ⇒ Object
300 301 302 303 304 305 306 |
# File 'src/lib/y2network/interface_config_builder.rb', line 300 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
366 367 368 |
# File 'src/lib/y2network/interface_config_builder.rb', line 366 def mtu @connection_config.mtu.to_s end |
#mtu=(value) ⇒ Object
Sets Maximum Transition Unit
372 373 374 |
# File 'src/lib/y2network/interface_config_builder.rb', line 372 def mtu=(value) @connection_config.mtu = value.to_i end |
#name_exists?(name) ⇒ Boolean
checks if interface name already exists
177 178 179 |
# File 'src/lib/y2network/interface_config_builder.rb', line 177 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
183 184 185 186 187 |
# File 'src/lib/y2network/interface_config_builder.rb', line 183 def name_valid_characters Yast.import "NetworkInterfaces" Yast::NetworkInterfaces.ValidCharsIfcfg end |
#newly_added? ⇒ Boolean
103 104 105 |
# File 'src/lib/y2network/interface_config_builder.rb', line 103 def newly_added? @newly_added end |
#proposed_names ⇒ Array<String>
Proposes bunch of possible names for interface do not modify anything
166 167 168 |
# File 'src/lib/y2network/interface_config_builder.rb', line 166 def proposed_names interfaces.free_names(type.short_name, NEW_DEVICES_COUNT) end |
#remote_ip ⇒ String
341 342 343 344 345 346 347 348 |
# File 'src/lib/y2network/interface_config_builder.rb', line 341 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
354 355 356 357 358 359 360 361 362 |
# File 'src/lib/y2network/interface_config_builder.rb', line 354 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
149 150 151 152 |
# File 'src/lib/y2network/interface_config_builder.rb', line 149 def rename_interface(new_name) @old_name ||= name @name = new_name end |
#renamed_interface? ⇒ Boolean
Determines whether the interface has been renamed
140 141 142 143 144 |
# File 'src/lib/y2network/interface_config_builder.rb', line 140 def renamed_interface? return false unless interface name != interface.name || @renaming_mechanism != interface.renaming_mechanism end |
#save ⇒ Object
saves builder content to backend
108 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 135 |
# File 'src/lib/y2network/interface_config_builder.rb', line 108 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? # TODO: should change only if different, but maybe firewall_interface responsibility? if !firewall_interface.zone || firewall_zone != firewall_interface.zone.name firewall_interface.zone = firewall_zone end 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
221 222 223 224 225 226 227 |
# File 'src/lib/y2network/interface_config_builder.rb', line 221 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 "/
309 310 311 312 313 314 315 |
# File 'src/lib/y2network/interface_config_builder.rb', line 309 def subnet_prefix if @connection_config.ip "/" + @connection_config.ip.address.prefix.to_s else "" end end |
#subnet_prefix=(value) ⇒ Object
318 319 320 321 322 323 324 325 326 327 328 |
# File 'src/lib/y2network/interface_config_builder.rb', line 318 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
172 173 174 |
# File 'src/lib/y2network/interface_config_builder.rb', line 172 def valid_name?(name) !!(name =~ /^[[:alnum:]._:-]{1,15}\z/) end |