Class: Y2Network::Sysconfig::InterfaceFile

Inherits:
Object
  • Object
show all
Defined in:
src/lib/y2network/sysconfig/interface_file.rb

Overview

This class represents a sysconfig file containing an interface configuration

The configuration is defined by a set of variables that are included in the file. Check ifcfg(5) for further information.

Multivalued variables

When dealing with multivalued variables, values are returned in a hash which indexes are the suffixes. For instance:

IPADDR='192.168.122.1/24' IPADDR_EXTRA='192.168.123.1/24' IPADDR_ALT='10.0.0.1/8'

Examples:

Finding the file for a given interface

file = Y2Network::Sysconfig::InterfaceFile.find("wlan0")
file.wireless_essid #=> "dummy"

Reading multivalued variables

file = Y2Network::Sysconfig::InterfaceFile.find("wlan0")
file.ipaddrs #=> { default: #<IPAddr: ...>, "_EXTRA" => #<IPAddr: ...>, "_ALT" => #<IPAddr: ...> }

Defined Under Namespace

Classes: Variable

Constant Summary collapse

SYSCONFIG_NETWORK_DIR =
Pathname.new("/etc/sysconfig/network").freeze
IGNORE_IFCFG_REGEX =
/(\.bak|\.orig|\.rpmnew|\.rpmorig|-range|~|\.old|\.scpmbackup)$/
SYSCONFIG_NETWORK_PATH =
Pathname.new("/etc").join("sysconfig", "network").freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(interface) ⇒ InterfaceFile

Constructor


360
361
362
363
364
365
# File 'src/lib/y2network/sysconfig/interface_file.rb', line 360

def initialize(interface)
  @interface = interface
  @values = collection_variables.each_with_object({}) do |variable, hash|
    hash[variable.name] = {}
  end
end

Instance Attribute Details

#bonding_masterString (readonly)


310
# File 'src/lib/y2network/sysconfig/interface_file.rb', line 310

define_variable(:bonding_master)

#bonding_module_optsString (readonly)


319
# File 'src/lib/y2network/sysconfig/interface_file.rb', line 319

define_variable(:bonding_module_opts)

#bonding_slavesHash (readonly)


314
# File 'src/lib/y2network/sysconfig/interface_file.rb', line 314

define_collection_variable(:bonding_slave)

#bridgeString (readonly)


325
# File 'src/lib/y2network/sysconfig/interface_file.rb', line 325

define_variable(:bridge)

#bridge_forwarddelayInteger (readonly)


337
# File 'src/lib/y2network/sysconfig/interface_file.rb', line 337

define_variable(:bridge_forwarddelay, :integer)

#bridge_portsString (readonly)


329
# File 'src/lib/y2network/sysconfig/interface_file.rb', line 329

define_variable(:bridge_ports)

#bridge_stpString (readonly)


333
# File 'src/lib/y2network/sysconfig/interface_file.rb', line 333

define_variable(:bridge_stp)

#dhclient_set_hostnameString (readonly)


355
# File 'src/lib/y2network/sysconfig/interface_file.rb', line 355

define_variable(:dhclient_set_hostname)

#interfaceString (readonly)


143
144
145
# File 'src/lib/y2network/sysconfig/interface_file.rb', line 143

def interface
  @interface
end

#ipoib_modeString (readonly)


294
# File 'src/lib/y2network/sysconfig/interface_file.rb', line 294

define_variable(:ipoib_mode)

#tunnelString (readonly)


343
# File 'src/lib/y2network/sysconfig/interface_file.rb', line 343

define_variable(:tunnel)

#tunnel_set_groupString (readonly)


351
# File 'src/lib/y2network/sysconfig/interface_file.rb', line 351

define_variable(:tunnel_set_group)

#tunnel_set_ownerString (readonly)


347
# File 'src/lib/y2network/sysconfig/interface_file.rb', line 347

define_variable(:tunnel_set_owner)

#wireless_apString (readonly)


276
# File 'src/lib/y2network/sysconfig/interface_file.rb', line 276

define_variable(:wireless_ap)

#wireless_ap_scanmodeString (readonly)


272
# File 'src/lib/y2network/sysconfig/interface_file.rb', line 272

define_variable(:wireless_ap_scanmode)

#wireless_ca_certString (readonly)


252
# File 'src/lib/y2network/sysconfig/interface_file.rb', line 252

define_variable(:wireless_ca_cert)

#wireless_channelInteger? (readonly)


280
# File 'src/lib/y2network/sysconfig/interface_file.rb', line 280

define_variable(:wireless_channel, :integer)

#wireless_client_certString (readonly)


256
# File 'src/lib/y2network/sysconfig/interface_file.rb', line 256

define_variable(:wireless_client_cert)

#wireless_client_keyString (readonly)


260
# File 'src/lib/y2network/sysconfig/interface_file.rb', line 260

define_variable(:wireless_client_key)

#wireless_eap_authString (readonly)


268
# File 'src/lib/y2network/sysconfig/interface_file.rb', line 268

define_variable(:wireless_eap_auth)

#wireless_eap_modeString (readonly)


264
# File 'src/lib/y2network/sysconfig/interface_file.rb', line 264

define_variable(:wireless_eap_mode)

#wireless_modeSymbol (readonly)


228
# File 'src/lib/y2network/sysconfig/interface_file.rb', line 228

define_variable(:wireless_mode, :symbol)

#wireless_nwidString (readonly)


284
# File 'src/lib/y2network/sysconfig/interface_file.rb', line 284

define_variable(:wireless_nwid)

#wireless_rateString (readonly)


288
# File 'src/lib/y2network/sysconfig/interface_file.rb', line 288

define_variable(:wireless_rate, :float)

#wireless_wpa_anonidString (readonly)


236
# File 'src/lib/y2network/sysconfig/interface_file.rb', line 236

define_variable(:wireless_wpa_anonid)

#wireless_wpa_driverString (readonly)


240
# File 'src/lib/y2network/sysconfig/interface_file.rb', line 240

define_variable(:wireless_wpa_driver)

#wireless_wpa_identityString (readonly)


248
# File 'src/lib/y2network/sysconfig/interface_file.rb', line 248

define_variable(:wireless_wpa_identity)

#wireless_wpa_passwordString (readonly)


232
# File 'src/lib/y2network/sysconfig/interface_file.rb', line 232

define_variable(:wireless_wpa_password)

#wireless_wpa_pskString (readonly)


244
# File 'src/lib/y2network/sysconfig/interface_file.rb', line 244

define_variable(:wireless_wpa_psk)

Class Method Details

.allArray<InterfaceFile>

Returns all configuration files


64
65
66
67
68
69
# File 'src/lib/y2network/sysconfig/interface_file.rb', line 64

def all
  Yast::SCR.Dir(Yast::Path.new(".network.section"))
           .reject { |f| IGNORE_IFCFG_REGEX =~ f || f == "lo" }
           .map { |f| find(f) }
           .compact
end

.define_collection_variable(param_name, type = :string) ⇒ Object

Defines an array parameter

This method registers the parameter and adds a pair of methods to get and set its value. In this case, the parameter is an array.


109
110
111
112
113
114
115
116
117
118
119
120
# File 'src/lib/y2network/sysconfig/interface_file.rb', line 109

def define_collection_variable(param_name, type = :string)
  name = variable_name(param_name)
  variables[name] = Variable.new(name, type, true)

  define_method "#{param_name}s" do
    @values[name]
  end

  define_method "#{param_name}s=" do |value|
    @values[name] = value
  end
end

.define_variable(param_name, type = :string) ⇒ Object

Defines a parameter

This method registers the parameter and adds a pair of methods to get and set its value.


87
88
89
90
91
92
93
94
95
96
97
98
99
100
# File 'src/lib/y2network/sysconfig/interface_file.rb', line 87

def define_variable(param_name, type = :string)
  name = variable_name(param_name)
  variables[name] = Variable.new(name, type, false)

  define_method param_name do
    @values[name]
  end

  define_method "#{param_name}=" do |value|
    # The `value` should be an object which responds to #to_s so its value can be written to
    # the ifcfg file.
    @values[name] = value
  end
end

.find(interface) ⇒ Sysconfig::InterfaceFile?

Finds the ifcfg-* file for a given interface


75
76
77
78
# File 'src/lib/y2network/sysconfig/interface_file.rb', line 75

def find(interface)
  return nil unless Yast::FileUtils.Exists(SYSCONFIG_NETWORK_DIR.join("ifcfg-#{interface}").to_s)
  new(interface)
end

.variablesArray<Symbol>

Known configuration variables

A variable is defined by using define_variable or define_collection_variable methods.


127
128
129
# File 'src/lib/y2network/sysconfig/interface_file.rb', line 127

def variables
  @variables ||= {}
end

Instance Method Details

#cleanObject

Empties all known values

This method clears all values from the file. The idea is to use this method to do some clean-up before writing the final values.


415
416
417
418
419
420
# File 'src/lib/y2network/sysconfig/interface_file.rb', line 415

def clean
  @values = self.class.variables.values.each_with_object({}) do |variable, hash|
    hash[variable.name] = variable.collection? ? {} : nil
  end
  @defined_variables = nil
end

#loadHash<String, Object>

Loads values from the configuration file


379
380
381
382
383
384
# File 'src/lib/y2network/sysconfig/interface_file.rb', line 379

def load
  @values = self.class.variables.values.each_with_object({}) do |variable, hash|
    meth = variable.collection? ? :fetch_collection : :fetch_scalar
    hash[variable.name] = send(meth, variable.name, variable.type)
  end
end

#pathPathname

Returns the file path


372
373
374
# File 'src/lib/y2network/sysconfig/interface_file.rb', line 372

def path
  SYSCONFIG_NETWORK_PATH.join("ifcfg-#{interface}")
end

#removeObject

Removes the file


423
424
425
426
# File 'src/lib/y2network/sysconfig/interface_file.rb', line 423

def remove
  return unless Yast::FileUtils.Exists(path.to_s)
  Yast::SCR.Execute(Yast::Path.new(".target.remove"), path.to_s)
end

#saveObject

Note:

Writes only changed values, keeping the rest as they are.

Writes the changes to the file


389
390
391
392
393
394
395
396
# File 'src/lib/y2network/sysconfig/interface_file.rb', line 389

def save
  self.class.variables.keys.each do |name|
    value = @values[name]
    meth = value.is_a?(Hash) ? :write_collection : :write_scalar
    send(meth, name, value)
  end
  Yast::SCR.Write(Yast::Path.new(".network"), nil)
end

#typeY2Network::InterfaceType

Determines the interface's type


403
404
405
406
407
408
409
# File 'src/lib/y2network/sysconfig/interface_file.rb', line 403

def type
  type_by_key_value ||
    type_by_key_existence ||
    type_from_interfacetype ||
    type_by_name ||
    InterfaceType::ETHERNET
end