Class: CFA::InterfaceFile
- Inherits:
-
Object
- Object
- CFA::InterfaceFile
- 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'
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.
/(\.bak|\.orig|\.rpmnew|\.rpmorig|-range|~|\.old|\.scpmbackup)$/.freeze
- SYSCONFIG_NETWORK_PATH =
Pathname.new("/etc").join("sysconfig", "network").freeze
Instance Attribute Summary collapse
-
#bonding_master ⇒ String
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.
-
#bonding_module_opts ⇒ String
readonly
Options for the bonding module ('mode=active-backup miimon=100').
-
#bonding_slaves ⇒ Hash
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.
-
#bridge ⇒ String
readonly
Whether the interface is a bridge or not.
- #bridge_forwarddelay ⇒ Integer readonly
-
#bridge_ports ⇒ String
readonly
Interfaces members of the bridge.
-
#bridge_stp ⇒ String
readonly
Spanning Tree Protocol ("off" or "on").
-
#dhclient_set_hostname ⇒ String
readonly
Use hostname from dhcp.
-
#interface ⇒ String
readonly
Interface's name.
-
#ipoib_mode ⇒ String
readonly
IPOIB mode ("connected" or "datagram").
-
#tunnel ⇒ String
readonly
Tunnel protocol ("sit", "gre", "ipip", "tun", "tap").
-
#tunnel_set_group ⇒ String
readonly
Tunnel group.
-
#tunnel_set_owner ⇒ String
readonly
Tunnel owner.
-
#wireless_ap ⇒ String
readonly
AP MAC address.
-
#wireless_ap_scanmode ⇒ Integer
readonly
SSID scan mode (0, 1 or 2).
-
#wireless_ca_cert ⇒ String
readonly
CA certificate used to sign server certificate.
-
#wireless_channel ⇒ Integer?
readonly
Wireless channel or nil for auto selection.
-
#wireless_client_cert ⇒ String
readonly
CA certificate used to sign server certificate.
-
#wireless_client_key ⇒ String
readonly
Client private key used for encryption in TLS.
-
#wireless_client_key_password ⇒ String
readonly
Client private key password used for encryption in TLS.
-
#wireless_eap_auth ⇒ String
readonly
WPA-EAP inner authentication with TLS tunnel method.
-
#wireless_eap_mode ⇒ String
readonly
WPA-EAP outer authentication method.
-
#wireless_mode ⇒ String
readonly
Operating mode for the device (managed, ad-hoc or master).
-
#wireless_nwid ⇒ String
readonly
Network ID.
-
#wireless_rate ⇒ String
readonly
Wireless bit rate specification ( Mb/s).
-
#wireless_wpa_anonid ⇒ String
readonly
Anonymous identity used for initial tunnel (TTLS).
-
#wireless_wpa_driver ⇒ String
readonly
Driver to be used by the wpa_supplicant program.
-
#wireless_wpa_identity ⇒ String
readonly
WPA identify.
-
#wireless_wpa_password ⇒ String
readonly
Password as configured on the RADIUS server (for WPA-EAP).
-
#wireless_wpa_psk ⇒ String
readonly
WPA preshared key (for WPA-PSK).
Class Method Summary collapse
-
.all ⇒ Array<InterfaceFile>
Returns all configuration files.
-
.define_collection_variable(param_name, type = :string) ⇒ Object
Defines an array parameter.
-
.define_variable(param_name, type = :string) ⇒ Object
Defines a parameter.
-
.find(interface) ⇒ CFA::InterfaceFile?
Finds the ifcfg-* file for a given interface.
-
.variables ⇒ Array<Symbol>
Known configuration variables.
Instance Method Summary collapse
-
#clean ⇒ Object
Empties all known values.
-
#exist? ⇒ Boolean
Determines whether the ifcfg-* file exists.
-
#initialize(interface) ⇒ InterfaceFile
constructor
Constructor.
-
#load ⇒ Hash<String, Object>
Loads values from the configuration file.
-
#path ⇒ Pathname
Returns the file path.
-
#remove ⇒ Object
Removes the file.
-
#save ⇒ Object
Writes the changes to the file.
-
#type ⇒ Y2Network::InterfaceType
Determines the interface's type.
Constructor Details
#initialize(interface) ⇒ InterfaceFile
Constructor
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_master ⇒ String (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
321 |
# File 'src/lib/cfa/interface_file.rb', line 321 define_variable(:bonding_master) |
#bonding_module_opts ⇒ String (readonly)
Returns 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_slaves ⇒ Hash (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
327 |
# File 'src/lib/cfa/interface_file.rb', line 327 define_collection_variable(:bonding_slave) |
#bridge ⇒ String (readonly)
Returns whether the interface is a bridge or not.
338 |
# File 'src/lib/cfa/interface_file.rb', line 338 define_variable(:bridge) |
#bridge_forwarddelay ⇒ Integer (readonly)
350 |
# File 'src/lib/cfa/interface_file.rb', line 350 define_variable(:bridge_forwarddelay, :integer) |
#bridge_ports ⇒ String (readonly)
Returns interfaces members of the bridge.
342 |
# File 'src/lib/cfa/interface_file.rb', line 342 define_variable(:bridge_ports) |
#bridge_stp ⇒ String (readonly)
Returns Spanning Tree Protocol ("off" or "on").
346 |
# File 'src/lib/cfa/interface_file.rb', line 346 define_variable(:bridge_stp) |
#dhclient_set_hostname ⇒ String (readonly)
Returns use hostname from dhcp.
368 |
# File 'src/lib/cfa/interface_file.rb', line 368 define_variable(:dhclient_set_hostname) |
#interface ⇒ String (readonly)
Returns Interface's name.
143 144 145 |
# File 'src/lib/cfa/interface_file.rb', line 143 def interface @interface end |
#ipoib_mode ⇒ String (readonly)
Returns IPOIB mode ("connected" or "datagram").
303 |
# File 'src/lib/cfa/interface_file.rb', line 303 define_variable(:ipoib_mode) |
#tunnel ⇒ String (readonly)
Returns tunnel protocol ("sit", "gre", "ipip", "tun", "tap").
356 |
# File 'src/lib/cfa/interface_file.rb', line 356 define_variable(:tunnel) |
#tunnel_set_group ⇒ String (readonly)
Returns tunnel group.
364 |
# File 'src/lib/cfa/interface_file.rb', line 364 define_variable(:tunnel_set_group) |
#tunnel_set_owner ⇒ String (readonly)
Returns tunnel owner.
360 |
# File 'src/lib/cfa/interface_file.rb', line 360 define_variable(:tunnel_set_owner) |
#wireless_ap ⇒ String (readonly)
Returns AP MAC address.
285 |
# File 'src/lib/cfa/interface_file.rb', line 285 define_variable(:wireless_ap) |
#wireless_ap_scanmode ⇒ Integer (readonly)
Returns 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_cert ⇒ String (readonly)
Returns CA certificate used to sign server certificate.
257 |
# File 'src/lib/cfa/interface_file.rb', line 257 define_variable(:wireless_ca_cert) |
#wireless_channel ⇒ Integer? (readonly)
Returns Wireless channel or nil for auto selection.
289 |
# File 'src/lib/cfa/interface_file.rb', line 289 define_variable(:wireless_channel, :integer) |
#wireless_client_cert ⇒ String (readonly)
Returns CA certificate used to sign server certificate.
261 |
# File 'src/lib/cfa/interface_file.rb', line 261 define_variable(:wireless_client_cert) |
#wireless_client_key ⇒ String (readonly)
Returns 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_password ⇒ String (readonly)
Returns 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_auth ⇒ String (readonly)
Returns 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_mode ⇒ String (readonly)
Returns WPA-EAP outer authentication method.
273 |
# File 'src/lib/cfa/interface_file.rb', line 273 define_variable(:wireless_eap_mode) |
#wireless_mode ⇒ String (readonly)
Returns 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_nwid ⇒ String (readonly)
Returns Network ID.
293 |
# File 'src/lib/cfa/interface_file.rb', line 293 define_variable(:wireless_nwid) |
#wireless_rate ⇒ String (readonly)
Returns Wireless bit rate specification ( Mb/s).
297 |
# File 'src/lib/cfa/interface_file.rb', line 297 define_variable(:wireless_rate, :float) |
#wireless_wpa_anonid ⇒ String (readonly)
Returns anonymous identity used for initial tunnel (TTLS).
241 |
# File 'src/lib/cfa/interface_file.rb', line 241 define_variable(:wireless_wpa_anonid) |
#wireless_wpa_driver ⇒ String (readonly)
Returns 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_identity ⇒ String (readonly)
Returns WPA identify.
253 |
# File 'src/lib/cfa/interface_file.rb', line 253 define_variable(:wireless_wpa_identity) |
#wireless_wpa_password ⇒ String (readonly)
Returns 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_psk ⇒ String (readonly)
Returns WPA preshared key (for WPA-PSK).
249 |
# File 'src/lib/cfa/interface_file.rb', line 249 define_variable(:wireless_wpa_psk) |
Class Method Details
.all ⇒ Array<InterfaceFile>
Returns all configuration files
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.
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.
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
75 76 77 78 |
# File 'src/lib/cfa/interface_file.rb', line 75 def find(interface) file = new(interface) file.exist? ? file : nil end |
.variables ⇒ Array<Symbol>
Known configuration variables
A variable is defined by using define_variable or define_collection_variable methods.
127 128 129 |
# File 'src/lib/cfa/interface_file.rb', line 127 def variables @variables ||= {} end |
Instance Method Details
#clean ⇒ Object
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
452 453 454 |
# File 'src/lib/cfa/interface_file.rb', line 452 def exist? Yast::FileUtils.Exists(path.to_s) end |
#load ⇒ Hash<String, Object>
Loads values from the configuration 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 |
#path ⇒ Pathname
Returns the file path
385 386 387 |
# File 'src/lib/cfa/interface_file.rb', line 385 def path SYSCONFIG_NETWORK_PATH.join("ifcfg-#{interface}") end |
#remove ⇒ Object
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 |
#save ⇒ Object
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.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 |
#type ⇒ Y2Network::InterfaceType
Determines the interface's type
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 |