Class: Y2Network::Sysconfig::InterfaceFile
- Inherits:
-
Object
- Object
- Y2Network::Sysconfig::InterfaceFile
- 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'
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)$/
- SYSCONFIG_NETWORK_PATH =
Pathname.new("/etc").join("sysconfig", "network").freeze
Instance Attribute Summary collapse
-
#bonding_master ⇒ String
readonly
Whether the interface is a bond device or not.
-
#bonding_module_opts ⇒ String
readonly
Options for the bonding module ('mode=active-backup miimon=100').
-
#bonding_slaves ⇒ Hash
readonly
Bonding slaves.
-
#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 ⇒ String
readonly
SSID scan mode (“0”, “1” and “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_eap_auth ⇒ String
readonly
WPA-EAP inner authentication with TLS tunnel method.
-
#wireless_eap_mode ⇒ String
readonly
WPA-EAP outer authentication method.
-
#wireless_mode ⇒ Symbol
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) ⇒ Sysconfig::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.
-
#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
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_master ⇒ String (readonly)
Returns whether the interface is a bond device or not.
310 |
# File 'src/lib/y2network/sysconfig/interface_file.rb', line 310 define_variable(:bonding_master) |
#bonding_module_opts ⇒ String (readonly)
Returns options for the bonding module ('mode=active-backup miimon=100').
319 |
# File 'src/lib/y2network/sysconfig/interface_file.rb', line 319 define_variable(:bonding_module_opts) |
#bonding_slaves ⇒ Hash (readonly)
Returns Bonding slaves.
314 |
# File 'src/lib/y2network/sysconfig/interface_file.rb', line 314 define_collection_variable(:bonding_slave) |
#bridge ⇒ String (readonly)
Returns whether the interface is a bridge or not.
325 |
# File 'src/lib/y2network/sysconfig/interface_file.rb', line 325 define_variable(:bridge) |
#bridge_forwarddelay ⇒ Integer (readonly)
337 |
# File 'src/lib/y2network/sysconfig/interface_file.rb', line 337 define_variable(:bridge_forwarddelay, :integer) |
#bridge_ports ⇒ String (readonly)
Returns interfaces members of the bridge.
329 |
# File 'src/lib/y2network/sysconfig/interface_file.rb', line 329 define_variable(:bridge_ports) |
#bridge_stp ⇒ String (readonly)
Returns Spanning Tree Protocol (“off” or “on”).
333 |
# File 'src/lib/y2network/sysconfig/interface_file.rb', line 333 define_variable(:bridge_stp) |
#dhclient_set_hostname ⇒ String (readonly)
Returns use hostname from dhcp.
355 |
# File 'src/lib/y2network/sysconfig/interface_file.rb', line 355 define_variable(:dhclient_set_hostname) |
#interface ⇒ String (readonly)
Returns Interface's name.
143 144 145 |
# File 'src/lib/y2network/sysconfig/interface_file.rb', line 143 def interface @interface end |
#ipoib_mode ⇒ String (readonly)
Returns IPOIB mode (“connected” or “datagram”).
294 |
# File 'src/lib/y2network/sysconfig/interface_file.rb', line 294 define_variable(:ipoib_mode) |
#tunnel ⇒ String (readonly)
Returns tunnel protocol (“sit”, “gre”, “ipip”, “tun”, “tap”).
343 |
# File 'src/lib/y2network/sysconfig/interface_file.rb', line 343 define_variable(:tunnel) |
#tunnel_set_group ⇒ String (readonly)
Returns tunnel group.
351 |
# File 'src/lib/y2network/sysconfig/interface_file.rb', line 351 define_variable(:tunnel_set_group) |
#tunnel_set_owner ⇒ String (readonly)
Returns tunnel owner.
347 |
# File 'src/lib/y2network/sysconfig/interface_file.rb', line 347 define_variable(:tunnel_set_owner) |
#wireless_ap ⇒ String (readonly)
Returns AP MAC address.
276 |
# File 'src/lib/y2network/sysconfig/interface_file.rb', line 276 define_variable(:wireless_ap) |
#wireless_ap_scanmode ⇒ String (readonly)
Returns SSID scan mode (“0”, “1” and “2”).
272 |
# File 'src/lib/y2network/sysconfig/interface_file.rb', line 272 define_variable(:wireless_ap_scanmode) |
#wireless_ca_cert ⇒ String (readonly)
Returns CA certificate used to sign server certificate.
252 |
# File 'src/lib/y2network/sysconfig/interface_file.rb', line 252 define_variable(:wireless_ca_cert) |
#wireless_channel ⇒ Integer? (readonly)
Returns Wireless channel or nil for auto selection.
280 |
# File 'src/lib/y2network/sysconfig/interface_file.rb', line 280 define_variable(:wireless_channel, :integer) |
#wireless_client_cert ⇒ String (readonly)
Returns CA certificate used to sign server certificate.
256 |
# File 'src/lib/y2network/sysconfig/interface_file.rb', line 256 define_variable(:wireless_client_cert) |
#wireless_client_key ⇒ String (readonly)
Returns client private key used for encryption in TLS.
260 |
# File 'src/lib/y2network/sysconfig/interface_file.rb', line 260 define_variable(:wireless_client_key) |
#wireless_eap_auth ⇒ String (readonly)
Returns WPA-EAP inner authentication with TLS tunnel method.
268 |
# File 'src/lib/y2network/sysconfig/interface_file.rb', line 268 define_variable(:wireless_eap_auth) |
#wireless_eap_mode ⇒ String (readonly)
Returns WPA-EAP outer authentication method.
264 |
# File 'src/lib/y2network/sysconfig/interface_file.rb', line 264 define_variable(:wireless_eap_mode) |
#wireless_mode ⇒ Symbol (readonly)
Returns Operating mode for the device (:managed, :ad_hoc or :master).
228 |
# File 'src/lib/y2network/sysconfig/interface_file.rb', line 228 define_variable(:wireless_mode, :symbol) |
#wireless_nwid ⇒ String (readonly)
Returns Network ID.
284 |
# File 'src/lib/y2network/sysconfig/interface_file.rb', line 284 define_variable(:wireless_nwid) |
#wireless_rate ⇒ String (readonly)
Returns Wireless bit rate specification ( Mb/s).
288 |
# File 'src/lib/y2network/sysconfig/interface_file.rb', line 288 define_variable(:wireless_rate, :float) |
#wireless_wpa_anonid ⇒ String (readonly)
Returns anonymous identity used for initial tunnel (TTLS).
236 |
# File 'src/lib/y2network/sysconfig/interface_file.rb', line 236 define_variable(:wireless_wpa_anonid) |
#wireless_wpa_driver ⇒ String (readonly)
Returns Driver to be used by the wpa_supplicant program.
240 |
# File 'src/lib/y2network/sysconfig/interface_file.rb', line 240 define_variable(:wireless_wpa_driver) |
#wireless_wpa_identity ⇒ String (readonly)
Returns WPA identify.
248 |
# File 'src/lib/y2network/sysconfig/interface_file.rb', line 248 define_variable(:wireless_wpa_identity) |
#wireless_wpa_password ⇒ String (readonly)
Returns Password as configured on the RADIUS server (for WPA-EAP).
232 |
# File 'src/lib/y2network/sysconfig/interface_file.rb', line 232 define_variable(:wireless_wpa_password) |
#wireless_wpa_psk ⇒ String (readonly)
Returns WPA preshared key (for WPA-PSK).
244 |
# File 'src/lib/y2network/sysconfig/interface_file.rb', line 244 define_variable(:wireless_wpa_psk) |
Class Method Details
.all ⇒ Array<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 |
.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/y2network/sysconfig/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.
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 |
#load ⇒ Hash<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 |
#path ⇒ Pathname
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 |
#remove ⇒ Object
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 |
#save ⇒ Object
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 |
#type ⇒ Y2Network::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 |