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.

Yast::LanItems.Commit(builder) use it.

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


75
76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'src/lib/y2network/interface_config_builder.rb', line 75

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)


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


190
191
192
193
194
195
196
# File 'src/lib/y2network/interface_config_builder.rb', line 190

def firewall_zone
  return @firewall_zone if @firewall_zone

  # TODO: handle renaming
  firewall_interface = Y2Firewall::Firewalld::Interface.new(name)
  @firewall_zone = (firewall_interface.zone && firewall_interface.zone.name) || @connection_config.firewall_zone
end

#interfaceY2Network::Interface?


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

def interface
  @interface
end

#nameString


56
57
58
# File 'src/lib/y2network/interface_config_builder.rb', line 56

def name
  @name
end

#renaming_mechanismSymbol?

Returns the current renaming mechanism


151
152
153
# File 'src/lib/y2network/interface_config_builder.rb', line 151

def renaming_mechanism
  @renaming_mechanism || interface.renaming_mechanism
end

#typeY2Network::InterfaceType (readonly)


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.


44
45
46
47
48
49
50
51
52
53
# File 'src/lib/y2network/interface_config_builder.rb', line 44

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

#aliasesArray<Hash>

gets aliases for interface


253
254
255
256
257
258
259
260
261
262
263
264
265
266
# File 'src/lib/y2network/interface_config_builder.rb', line 253

def aliases
  return @aliases if @aliases

  aliases = @connection_config.ip_aliases.map do |data|
    {
      label:     data.label.to_s,
      ip:        data.address.address.to_s,
      prefixlen: data.address.prefix.to_s,
      id:        data.id.to_s
      # NOTE: new API does not have netmask at all, we need to adapt UI to clearly mention only prefix
    }
  end
  @aliases = aliases
end

#aliases=(value) ⇒ Object

sets aliases for interface


270
271
272
# File 'src/lib/y2network/interface_config_builder.rb', line 270

def aliases=(value)
  @aliases = value
end

#boot_protocolY2Network::BootProtocol


202
203
204
# File 'src/lib/y2network/interface_config_builder.rb', line 202

def boot_protocol
  @connection_config.bootproto
end

#boot_protocol=(value) ⇒ Object

@param[String, Y2Network::BootProtocol]


207
208
209
210
# File 'src/lib/y2network/interface_config_builder.rb', line 207

def boot_protocol=(value)
  value = value.name if value.is_a?(Y2Network::BootProtocol)
  @connection_config.bootproto = Y2Network::BootProtocol.from_name(value)
end

#configure_as_slaveObject


355
356
357
358
# File 'src/lib/y2network/interface_config_builder.rb', line 355

def configure_as_slave
  self.boot_protocol = "none"
  self.aliases = []
end

#driverObject

gets currently assigned kernel module


237
238
239
240
241
# File 'src/lib/y2network/interface_config_builder.rb', line 237

def driver
  return @driver if @driver
  @driver = yast_config.drivers.find { |d| d.name == @interface.custom_driver } if @interface.custom_driver
  @driver ||= :auto
end

#driver=(value) ⇒ Object

sets kernel module for interface


245
246
247
# File 'src/lib/y2network/interface_config_builder.rb', line 245

def driver=(value)
  @driver = value
end

#driversObject

gets a list of available kernel modules for the interface


184
185
186
187
# File 'src/lib/y2network/interface_config_builder.rb', line 184

def drivers
  return [] unless interface
  yast_config.drivers_for_interface(interface.name)
end

#hostnameString


318
319
320
# File 'src/lib/y2network/interface_config_builder.rb', line 318

def hostname
  @connection_config.hostname || ""
end

#hostname=(value) ⇒ Object


323
324
325
# File 'src/lib/y2network/interface_config_builder.rb', line 323

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

#hwinfoHwinfo


367
368
369
# File 'src/lib/y2network/interface_config_builder.rb', line 367

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

#hwinfo_from(info) ⇒ Hwinfo


362
363
364
# File 'src/lib/y2network/interface_config_builder.rb', line 362

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

#ifplugd_priorityInteger


232
233
234
# File 'src/lib/y2network/interface_config_builder.rb', line 232

def ifplugd_priority
  startmode.name == "ifplugd" ? startmode.priority : 0
end

#ifplugd_priority=(value) ⇒ Object


223
224
225
226
227
228
229
# File 'src/lib/y2network/interface_config_builder.rb', line 223

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


275
276
277
278
279
280
281
282
# File 'src/lib/y2network/interface_config_builder.rb', line 275

def ip_address
  default = @connection_config.ip
  if default
    default.address.address.to_s
  else
    ""
  end
end

#ip_address=(value) ⇒ Object


285
286
287
288
289
290
291
# File 'src/lib/y2network/interface_config_builder.rb', line 285

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


345
346
347
# File 'src/lib/y2network/interface_config_builder.rb', line 345

def mtu
  @connection_config.mtu.to_s
end

#mtu=(value) ⇒ Object

Sets Maximum Transition Unit


351
352
353
# File 'src/lib/y2network/interface_config_builder.rb', line 351

def mtu=(value)
  @connection_config.mtu = value.to_i
end

#name_exists?(name) ⇒ Boolean

checks if interface name already exists


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

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


177
178
179
180
181
# File 'src/lib/y2network/interface_config_builder.rb', line 177

def name_valid_characters
  Yast.import "NetworkInterfaces"

  Yast::NetworkInterfaces.ValidCharsIfcfg
end

#newly_added?Boolean


101
102
103
# File 'src/lib/y2network/interface_config_builder.rb', line 101

def newly_added?
  @newly_added
end

#proposed_namesArray<String>

Proposes bunch of possible names for interface do not modify anything


160
161
162
# File 'src/lib/y2network/interface_config_builder.rb', line 160

def proposed_names
  interfaces.free_names(type.short_name, NEW_DEVICES_COUNT)
end

#remote_ipString

sets remote ip for ptp connections


329
330
331
332
333
334
335
336
# File 'src/lib/y2network/interface_config_builder.rb', line 329

def remote_ip
  default = @connection_config.ip
  if default
    default.remote_address.to_s
  else
    ""
  end
end

#remote_ip=(value) ⇒ Object


339
340
341
# File 'src/lib/y2network/interface_config_builder.rb', line 339

def remote_ip=(value)
  ip_config_default.remote_address = IPAddress.from_string(value)
end

#rename_interface(new_name) ⇒ Object

Renames the interface


143
144
145
146
# File 'src/lib/y2network/interface_config_builder.rb', line 143

def rename_interface(new_name)
  @old_name ||= name
  @name = new_name
end

#renamed_interface?Boolean

Determines whether the interface has been renamed


135
136
137
138
# File 'src/lib/y2network/interface_config_builder.rb', line 135

def renamed_interface?
  return false unless interface
  name != interface.name || @renaming_mechanism != interface.renaming_mechanism
end

#saveObject

saves builder content to backend @ TODO now still LanItems actively query config attribute and write it down, so here mainly workarounds, but ideally this save should change completely backend


109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
# 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?
    # TODO: should change only if different, but maybe firewall_interface responsibility?
    firewall_interface.zone = firewall_zone if !firewall_interface.zone || firewall_zone != firewall_interface.zone.name
  end

  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
  yast_config.rename_interface(@old_name, name, renaming_mechanism) if renamed_interface?
  yast_config.add_or_update_connection_config(@connection_config)

  nil
end

#startmode=(name) ⇒ Object


214
215
216
217
218
219
220
# File 'src/lib/y2network/interface_config_builder.rb', line 214

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


294
295
296
297
298
299
300
# File 'src/lib/y2network/interface_config_builder.rb', line 294

def subnet_prefix
  if @connection_config.ip
    "/" + @connection_config.ip.address.prefix.to_s
  else
    ""
  end
end

#subnet_prefix=(value) ⇒ Object


303
304
305
306
307
308
309
310
311
312
313
314
315
# File 'src/lib/y2network/interface_config_builder.rb', line 303

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.size < 3 # one or two digits can be only prefixlen
    ip_config_default.address.prefix = value.to_i
  elsif value =~ /^\d{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


166
167
168
# File 'src/lib/y2network/interface_config_builder.rb', line 166

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