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:


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

Returns connection config on which builder operates.

Returns:


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

def connection_config
  @connection_config
end

#firewall_zoneObject

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

#interfaceY2Network::Interface?

Returns Underlying interface if it exists.

Returns:


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

def interface
  @interface
end

#nameString

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

Returns:

  • (String)

    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.

Returns:

  • (Boolean)

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

Returns the current renaming mechanism

Returns:

  • (Symbol, nil)

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


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

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:


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.

Parameters:


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

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.


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

#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


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

Parameters:

  • value (Array<Hash>)

    see #alias_for for hash values


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

def aliases=(value)
  @aliases = value
end

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


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

#driverObject

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

Parameters:


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

def driver=(value)
  @driver = value
end

#driversObject

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

#hostnameString

Returns:

  • (String)

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

def hostname
  @connection_config.hostname || ""
end

#hostname=(value) ⇒ Object

Parameters:

  • value (String)

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

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

#hwinfoHwinfo

Returns:


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

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

#hwinfo_from(info) ⇒ Hwinfo

Parameters:

  • info (Hash<String,Object>)

    Hardware information

Returns:


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

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

#ifplugd_priorityInteger

Returns:

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

Parameters:

  • value (Integer)

    priority value


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_addressString

Returns:

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

Parameters:

  • value (String)

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

#mtuString

Gets Maximum Transition Unit

Returns:

  • (String)

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

Parameters:

  • value (String)

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

Returns:

  • (Boolean)

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_charactersObject

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

Returns:

  • (Boolean)

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

def newly_added?
  @newly_added
end

#proposed_namesArray<String>

Proposes bunch of possible names for interface do not modify anything

Returns:

  • (Array<String>)

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_ipString

Returns:

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

Parameters:

  • value (String, nil)

Returns:


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

Parameters:

  • new_name (String)

    New interface's name


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

Returns:

  • (Boolean)

    true if it was renamed; false otherwise


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

#saveObject

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

Parameters:

  • name (String, Y2Network::Startmode)

    startmode name used to create Startmode object or object itself


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_prefixString

Returns prefix or netmask. prefix in format "/"

Returns:

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

Parameters:

  • value (String)

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


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

Returns:

  • (Boolean)

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

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