Class: LinuxAdmin::NetworkInterfaceRH

Inherits:
NetworkInterface show all
Defined in:
lib/linux_admin/network_interface/network_interface_rh.rb

Constant Summary collapse

IFACE_DIR =
"/etc/sysconfig/network-scripts"

Instance Attribute Summary collapse

Attributes inherited from NetworkInterface

#interface

Instance Method Summary collapse

Methods inherited from NetworkInterface

#address, #address6, dist_class, #gateway, #mac_address, #netmask, #netmask6, new, #reload, #start, #stop

Constructor Details

#initialize(interface) ⇒ NetworkInterfaceRH

Returns a new instance of NetworkInterfaceRH.

Parameters:

  • interface (String)

    Name of the network interface to manage

Raises:



12
13
14
15
16
17
# File 'lib/linux_admin/network_interface/network_interface_rh.rb', line 12

def initialize(interface)
  @interface_file = Pathname.new(IFACE_DIR).join("ifcfg-#{interface}")
  raise MissingConfigurationFileError unless File.exist?(@interface_file)
  super
  parse_conf
end

Instance Attribute Details

#interface_configHash<String, String> (readonly)

Returns Key value mappings in the interface file.

Returns:

  • (Hash<String, String>)

    Key value mappings in the interface file



9
10
11
# File 'lib/linux_admin/network_interface/network_interface_rh.rb', line 9

def interface_config
  @interface_config
end

Instance Method Details

#address=(address) ⇒ Object

Set the IPv4 address for this interface

Parameters:

  • address (String)

Raises:

  • ArgumentError if the address is not formatted properly



36
37
38
39
40
# File 'lib/linux_admin/network_interface/network_interface_rh.rb', line 36

def address=(address)
  validate_ip(address)
  @interface_config["BOOTPROTO"] = "static"
  @interface_config["IPADDR"]    = address
end

#apply_static(ip, mask, gw, dns, search = nil) ⇒ Boolean

Applies the given static network configuration to the interface

Parameters:

  • ip (String)

    IPv4 address

  • mask (String)

    subnet mask

  • gw (String)

    gateway address

  • dns (Array<String>)

    list of dns servers

  • search (Array<String>) (defaults to: nil)

    list of search domains

Returns:

  • (Boolean)

    true on success, false otherwise

Raises:

  • ArgumentError if an IP is not formatted properly



98
99
100
101
102
103
104
105
# File 'lib/linux_admin/network_interface/network_interface_rh.rb', line 98

def apply_static(ip, mask, gw, dns, search = nil)
  self.address      = ip
  self.netmask      = mask
  self.gateway      = gw
  self.dns          = dns
  self.search_order = search if search
  save
end

#dns=(*servers) ⇒ Object

Sets one or both DNS servers for this network interface

Parameters:

  • servers (Array<String>)

    The DNS servers



63
64
65
66
67
# File 'lib/linux_admin/network_interface/network_interface_rh.rb', line 63

def dns=(*servers)
  server1, server2 = servers.flatten
  @interface_config["DNS1"] = server1
  @interface_config["DNS2"] = server2 if server2
end

#enable_dhcpObject

Set up the interface to use DHCP Removes any previously set static networking information



78
79
80
81
82
83
84
85
86
87
# File 'lib/linux_admin/network_interface/network_interface_rh.rb', line 78

def enable_dhcp
  @interface_config["BOOTPROTO"] = "dhcp"
  @interface_config.delete("IPADDR")
  @interface_config.delete("NETMASK")
  @interface_config.delete("GATEWAY")
  @interface_config.delete("PREFIX")
  @interface_config.delete("DNS1")
  @interface_config.delete("DNS2")
  @interface_config.delete("DOMAIN")
end

#gateway=(address) ⇒ Object

Set the IPv4 gateway address for this interface

Parameters:

  • address (String)

Raises:

  • ArgumentError if the address is not formatted properly



46
47
48
49
# File 'lib/linux_admin/network_interface/network_interface_rh.rb', line 46

def gateway=(address)
  validate_ip(address)
  @interface_config["GATEWAY"] = address
end

#netmask=(mask) ⇒ Object

Set the IPv4 sub-net mask for this interface

Parameters:

  • mask (String)

Raises:

  • ArgumentError if the mask is not formatted properly



55
56
57
58
# File 'lib/linux_admin/network_interface/network_interface_rh.rb', line 55

def netmask=(mask)
  validate_ip(mask)
  @interface_config["NETMASK"] = mask
end

#parse_confObject

Parses the interface configuration file into the @interface_config hash



20
21
22
23
24
25
26
27
28
29
30
# File 'lib/linux_admin/network_interface/network_interface_rh.rb', line 20

def parse_conf
  @interface_config = {}

  File.foreach(@interface_file) do |line|
    next if line =~ /^\s*#/

    key, value = line.split('=').collect(&:strip)
    @interface_config[key] = value
  end
  @interface_config["NM_CONTROLLED"] = "no"
end

#saveBoolean

Writes the contents of @interface_config to @interface_file as ‘key`=`value` pairs and resets the interface

Returns:

  • (Boolean)

    true if the interface was successfully brought up with the new configuration, false otherwise



112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
# File 'lib/linux_admin/network_interface/network_interface_rh.rb', line 112

def save
  old_contents = File.read(@interface_file)

  return false unless stop

  File.write(@interface_file, @interface_config.delete_blanks.collect { |k, v| "#{k}=#{v}" }.join("\n"))

  unless start
    File.write(@interface_file, old_contents)
    start
    return false
  end

  true
end

#search_order=(*domains) ⇒ Object

Sets the search domain list for this network interface

Parameters:

  • domains (Array<String>)

    the list of search domains



72
73
74
# File 'lib/linux_admin/network_interface/network_interface_rh.rb', line 72

def search_order=(*domains)
  @interface_config["DOMAIN"] = "\"#{domains.flatten.join(' ')}\""
end