Class: CFA::InterfaceFile

Inherits:
Object
  • Object
show all
Defined in:
src/lib/cfa/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 = CFA::InterfaceFile.find("wlan0")
file.wireless_essid #=> "dummy"

Reading multivalued variables

file = CFA::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 =

Returns expression to filter out invalid ifcfg-* files.

Returns:

  • (Regex)

    expression to filter out invalid ifcfg-* files

/(\.bak|\.orig|\.rpmnew|\.rpmorig|-range|~|\.old|\.scpmbackup)$/.freeze
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

Parameters:

  • interface (String)

    Interface interface



373
374
375
376
377
378
# File 'src/lib/cfa/interface_file.rb', line 373

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)

TODO: this name doesn't correspond to inclusive naming, however as long as the name is used in syscfg file, the name is kept even here to avoid confusion

Returns:

  • (String)

    whether the interface is a bond device or not



321
# File 'src/lib/cfa/interface_file.rb', line 321

define_variable(:bonding_master)

#bonding_module_optsString (readonly)

Returns options for the bonding module ('mode=active-backup miimon=100').

Returns:

  • (String)

    options for the bonding module ('mode=active-backup miimon=100')



332
# File 'src/lib/cfa/interface_file.rb', line 332

define_variable(:bonding_module_opts)

#bonding_slavesHash (readonly)

TODO: this name doesn't correspond to inclusive naming, however as long as the name is used in syscfg file, the name is kept even here to avoid confusion

Returns:

  • (Hash)

    Bonding slaves



327
# File 'src/lib/cfa/interface_file.rb', line 327

define_collection_variable(:bonding_slave)

#bridgeString (readonly)

Returns whether the interface is a bridge or not.

Returns:

  • (String)

    whether the interface is a bridge or not



338
# File 'src/lib/cfa/interface_file.rb', line 338

define_variable(:bridge)

#bridge_forwarddelayInteger (readonly)

Returns:

  • (Integer)


350
# File 'src/lib/cfa/interface_file.rb', line 350

define_variable(:bridge_forwarddelay, :integer)

#bridge_portsString (readonly)

Returns interfaces members of the bridge.

Returns:

  • (String)

    interfaces members of the bridge



342
# File 'src/lib/cfa/interface_file.rb', line 342

define_variable(:bridge_ports)

#bridge_stpString (readonly)

Returns Spanning Tree Protocol ("off" or "on").

Returns:

  • (String)

    Spanning Tree Protocol ("off" or "on")



346
# File 'src/lib/cfa/interface_file.rb', line 346

define_variable(:bridge_stp)

#dhclient_set_hostnameString (readonly)

Returns use hostname from dhcp.

Returns:

  • (String)

    use hostname from dhcp



368
# File 'src/lib/cfa/interface_file.rb', line 368

define_variable(:dhclient_set_hostname)

#interfaceString (readonly)

Returns Interface's name.

Returns:

  • (String)

    Interface's name



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

def interface
  @interface
end

#ipoib_modeString (readonly)

Returns IPOIB mode ("connected" or "datagram").

Returns:

  • (String)

    IPOIB mode ("connected" or "datagram")



303
# File 'src/lib/cfa/interface_file.rb', line 303

define_variable(:ipoib_mode)

#tunnelString (readonly)

Returns tunnel protocol ("sit", "gre", "ipip", "tun", "tap").

Returns:

  • (String)

    tunnel protocol ("sit", "gre", "ipip", "tun", "tap")



356
# File 'src/lib/cfa/interface_file.rb', line 356

define_variable(:tunnel)

#tunnel_set_groupString (readonly)

Returns tunnel group.

Returns:

  • (String)

    tunnel group



364
# File 'src/lib/cfa/interface_file.rb', line 364

define_variable(:tunnel_set_group)

#tunnel_set_ownerString (readonly)

Returns tunnel owner.

Returns:

  • (String)

    tunnel owner



360
# File 'src/lib/cfa/interface_file.rb', line 360

define_variable(:tunnel_set_owner)

#wireless_apString (readonly)

Returns AP MAC address.

Returns:

  • (String)

    AP MAC address



285
# File 'src/lib/cfa/interface_file.rb', line 285

define_variable(:wireless_ap)

#wireless_ap_scanmodeInteger (readonly)

Returns SSID scan mode (0, 1 or 2).

Returns:

  • (Integer)

    SSID scan mode (0, 1 or 2)



281
# File 'src/lib/cfa/interface_file.rb', line 281

define_variable(:wireless_ap_scanmode, :integer)

#wireless_ca_certString (readonly)

Returns CA certificate used to sign server certificate.

Returns:

  • (String)

    CA certificate used to sign server certificate



257
# File 'src/lib/cfa/interface_file.rb', line 257

define_variable(:wireless_ca_cert)

#wireless_channelInteger? (readonly)

Returns Wireless channel or nil for auto selection.

Returns:

  • (Integer, nil)

    Wireless channel or nil for auto selection



289
# File 'src/lib/cfa/interface_file.rb', line 289

define_variable(:wireless_channel, :integer)

#wireless_client_certString (readonly)

Returns CA certificate used to sign server certificate.

Returns:

  • (String)

    CA certificate used to sign server certificate



261
# File 'src/lib/cfa/interface_file.rb', line 261

define_variable(:wireless_client_cert)

#wireless_client_keyString (readonly)

Returns client private key used for encryption in TLS.

Returns:

  • (String)

    client private key used for encryption in TLS



265
# File 'src/lib/cfa/interface_file.rb', line 265

define_variable(:wireless_client_key)

#wireless_client_key_passwordString (readonly)

Returns client private key password used for encryption in TLS.

Returns:

  • (String)

    client private key password used for encryption in TLS



269
# File 'src/lib/cfa/interface_file.rb', line 269

define_variable(:wireless_client_key_password)

#wireless_eap_authString (readonly)

Returns WPA-EAP inner authentication with TLS tunnel method.

Returns:

  • (String)

    WPA-EAP inner authentication with TLS tunnel method



277
# File 'src/lib/cfa/interface_file.rb', line 277

define_variable(:wireless_eap_auth)

#wireless_eap_modeString (readonly)

Returns WPA-EAP outer authentication method.

Returns:

  • (String)

    WPA-EAP outer authentication method



273
# File 'src/lib/cfa/interface_file.rb', line 273

define_variable(:wireless_eap_mode)

#wireless_modeString (readonly)

Returns Operating mode for the device (managed, ad-hoc or master).

Returns:

  • (String)

    Operating mode for the device (managed, ad-hoc or master)



233
# File 'src/lib/cfa/interface_file.rb', line 233

define_variable(:wireless_mode, :string)

#wireless_nwidString (readonly)

Returns Network ID.

Returns:

  • (String)

    Network ID



293
# File 'src/lib/cfa/interface_file.rb', line 293

define_variable(:wireless_nwid)

#wireless_rateString (readonly)

Returns Wireless bit rate specification ( Mb/s).

Returns:

  • (String)

    Wireless bit rate specification ( Mb/s)



297
# File 'src/lib/cfa/interface_file.rb', line 297

define_variable(:wireless_rate, :float)

#wireless_wpa_anonidString (readonly)

Returns anonymous identity used for initial tunnel (TTLS).

Returns:

  • (String)

    anonymous identity used for initial tunnel (TTLS)



241
# File 'src/lib/cfa/interface_file.rb', line 241

define_variable(:wireless_wpa_anonid)

#wireless_wpa_driverString (readonly)

Returns Driver to be used by the wpa_supplicant program.

Returns:

  • (String)

    Driver to be used by the wpa_supplicant program



245
# File 'src/lib/cfa/interface_file.rb', line 245

define_variable(:wireless_wpa_driver)

#wireless_wpa_identityString (readonly)

Returns WPA identify.

Returns:

  • (String)

    WPA identify



253
# File 'src/lib/cfa/interface_file.rb', line 253

define_variable(:wireless_wpa_identity)

#wireless_wpa_passwordString (readonly)

Returns Password as configured on the RADIUS server (for WPA-EAP).

Returns:

  • (String)

    Password as configured on the RADIUS server (for WPA-EAP)



237
# File 'src/lib/cfa/interface_file.rb', line 237

define_variable(:wireless_wpa_password)

#wireless_wpa_pskString (readonly)

Returns WPA preshared key (for WPA-PSK).

Returns:

  • (String)

    WPA preshared key (for WPA-PSK)



249
# File 'src/lib/cfa/interface_file.rb', line 249

define_variable(:wireless_wpa_psk)

Class Method Details

.allArray<InterfaceFile>

Returns all configuration files

Returns:



64
65
66
67
68
69
# File 'src/lib/cfa/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.

Parameters:

  • param_name (Symbol)

    Parameter name

  • type (Symbol) (defaults to: :string)

    Array elements type (:string, :integer, :symbol, :ipaddr)



109
110
111
112
113
114
115
116
117
118
119
120
# File 'src/lib/cfa/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.

Parameters:

  • param_name (Symbol)

    Parameter name

  • type (Symbol) (defaults to: :string)

    Parameter type (:string, :integer, :symbol, :ipaddr)



87
88
89
90
91
92
93
94
95
96
97
98
99
100
# File 'src/lib/cfa/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) ⇒ CFA::InterfaceFile?

Finds the ifcfg-* file for a given interface

Parameters:

  • interface (String)

    Interface name

Returns:



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

def find(interface)
  file = new(interface)
  file.exist? ? file : nil
end

.variablesArray<Symbol>

Known configuration variables

A variable is defined by using define_variable or define_collection_variable methods.

Returns:

  • (Array<Symbol>)


127
128
129
# File 'src/lib/cfa/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.



428
429
430
431
432
433
434
435
436
437
438
439
440
# File 'src/lib/cfa/interface_file.rb', line 428

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

  @defined_variables = nil
end

#exist?Boolean

Determines whether the ifcfg-* file exists

Returns:

  • (Boolean)

    true if the file exists; false otherwise



452
453
454
# File 'src/lib/cfa/interface_file.rb', line 452

def exist?
  Yast::FileUtils.Exists(path.to_s)
end

#loadHash<String, Object>

Loads values from the configuration file

Returns:

  • (Hash<String, Object>)

    All values from the file



392
393
394
395
396
397
# File 'src/lib/cfa/interface_file.rb', line 392

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

Returns:

  • (Pathname)


385
386
387
# File 'src/lib/cfa/interface_file.rb', line 385

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

#removeObject

Removes the file



443
444
445
446
447
# File 'src/lib/cfa/interface_file.rb', line 443

def remove
  return unless exist?

  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



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

def save
  self.class.variables.each_key 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

Returns:

  • (Y2Network::InterfaceType)

    Interface's type depending on the configuration If particular type cannot be recognized, then ETHERNET is returned (same default as in wicked)



416
417
418
419
420
421
422
# File 'src/lib/cfa/interface_file.rb', line 416

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