Class: Y2Network::InterfaceConfigBuilder

Inherits:
Object
  • Object
show all
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.

Constant Summary collapse

NEW_DEVICES_COUNT =

how many device names is proposed

10

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(type:, config: nil) ⇒ InterfaceConfigBuilder

Constructor

Load with reasonable defaults

Parameters:



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_configY2Network::ConnectionConfig (readonly)

Returns connection config on which builder operates.

Returns:



60
61
62
# File 'src/lib/y2network/interface_config_builder.rb', line 60

def connection_config
  @connection_config
end

#firewall_zoneObject

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

#interfaceY2Network::Interface?

Returns Underlying interface if it exists.

Returns:



65
66
67
# File 'src/lib/y2network/interface_config_builder.rb', line 65

def interface
  @interface
end

#nameString

Returns Device name (eth0, wlan0, etc.).

Returns:

  • (String)

    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.

Returns:

  • (Boolean)

    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_mechanismSymbol?

Returns the current renaming mechanism

Returns:

  • (Symbol, nil)

    Mechanism to rename the interface (nil -no rename-, :mac or :bus_id)



156
157
158
# File 'src/lib/y2network/interface_config_builder.rb', line 156

def renaming_mechanism
  @renaming_mechanism || interface.renaming_mechanism
end

#typeY2Network::InterfaceType (readonly)

Returns type of @see Y2Network::Interface which is intended to be build.

Returns:



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.

Parameters:



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

Parameters:

  • data (IPConfig, nil)

    Additional IP address configuration

Returns:

  • (Hash<String>)

    hash values are :label for alias label, :ip_address for ip address, :mask for netmask and :subnet_prefix for prefix.



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

#aliasesArray<Hash>

gets aliases for interface

Returns:

  • (Array<Hash>)

    array of the connection additional IP address in hash format see #alias_for for the hash values



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

Parameters:

  • value (Array<Hash>)

    see #alias_for for hash values



284
285
286
# File 'src/lib/y2network/interface_config_builder.rb', line 284

def aliases=(value)
  @aliases = value
end

#boot_protocolY2Network::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_portObject



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

#driverObject

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

Parameters:



254
255
256
# File 'src/lib/y2network/interface_config_builder.rb', line 254

def driver=(value)
  @driver = value
end

#driversObject

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

#hostnameString

Returns:

  • (String)


330
331
332
# File 'src/lib/y2network/interface_config_builder.rb', line 330

def hostname
  @connection_config.hostname || ""
end

#hostname=(value) ⇒ Object

Parameters:

  • value (String)


335
336
337
# File 'src/lib/y2network/interface_config_builder.rb', line 335

def hostname=(value)
  @connection_config.hostname = value
end

#hwinfoHwinfo

Returns:



390
391
392
# File 'src/lib/y2network/interface_config_builder.rb', line 390

def hwinfo
  @hwinfo ||= Hwinfo.for(name)
end

#hwinfo_from(info) ⇒ Hwinfo

Parameters:

  • info (Hash<String,Object>)

    Hardware information

Returns:



385
386
387
# File 'src/lib/y2network/interface_config_builder.rb', line 385

def hwinfo_from(info)
  @hwinfo = Hwinfo.new(info)
end

#ifplugd_priorityInteger

Returns:

  • (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

Parameters:

  • value (Integer)

    priority value



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_addressString

Returns:

  • (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

Parameters:

  • value (String)


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

#mtuString

Gets Maximum Transition Unit

Returns:

  • (String)


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

Parameters:

  • value (String)


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

Returns:

  • (Boolean)


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_charactersObject

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

Returns:

  • (Boolean)


104
105
106
# File 'src/lib/y2network/interface_config_builder.rb', line 104

def newly_added?
  @newly_added
end

#proposed_namesArray<String>

Proposes bunch of possible names for interface do not modify anything

Returns:

  • (Array<String>)


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_ipString

Returns:

  • (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

Parameters:

  • value (String, nil)

Returns:



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

Parameters:

  • new_name (String)

    New interface's name



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

Returns:

  • (Boolean)

    true if it was renamed; false otherwise



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

#saveObject

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

Parameters:

  • name (String, Y2Network::Startmode)

    startmode name used to create Startmode object or object itself



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_prefixString

Returns prefix or netmask. prefix in format "/"

Returns:

  • (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

Parameters:

  • value (String)

    prefix or netmask is accepted. prefix in format "/"



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

Returns:

  • (Boolean)


171
172
173
# File 'src/lib/y2network/interface_config_builder.rb', line 171

def valid_name?(name)
  !!(name =~ /^[[:alnum:]._:-]{1,15}\z/)
end