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, #link_type

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from NetworkInterface

#address, #address6, dist_class, #gateway, #gateway6, list, #loopback?, #mac_address, #netmask, #netmask6, new, #prefix, #prefix6, #start, #stop

Constructor Details

#initialize(interface) ⇒ NetworkInterfaceRH

Returns a new instance of NetworkInterfaceRH.

Parameters:

  • interface (String)

    Name of the network interface to manage



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

def initialize(interface)
  @interface_file = self.class.path_to_interface_config_file(interface)
  super
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

Class Method Details

.path_to_interface_config_file(interface) ⇒ Object



194
195
196
# File 'lib/linux_admin/network_interface/network_interface_rh.rb', line 194

def self.path_to_interface_config_file(interface)
  Pathname.new(IFACE_DIR).join("ifcfg-#{interface}")
end

Instance Method Details

#address6=(address) ⇒ Object

Set the IPv6 address for this interface

Parameters:

  • address (String)

    IPv6 address including the prefix length (i.e. ‘::1/127’)

Raises:

  • ArgumentError if the address is not formatted properly



56
57
58
59
60
61
# File 'lib/linux_admin/network_interface/network_interface_rh.rb', line 56

def address6=(address)
  validate_ip(address)
  @interface_config['IPV6INIT']  = 'yes'
  @interface_config['DHCPV6C']   = 'no'
  @interface_config['IPV6ADDR']  = address
end

#address=(address) ⇒ Object

Set the IPv4 address for this interface

Parameters:

  • address (String)

Raises:

  • ArgumentError if the address is not formatted properly



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

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



140
141
142
143
144
145
146
147
# File 'lib/linux_admin/network_interface/network_interface_rh.rb', line 140

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

#apply_static6(ip, prefix, gw, dns, search = nil) ⇒ Boolean

Applies the given static IPv6 network configuration to the interface

Parameters:

  • ip (String)

    IPv6 address

  • prefix (Number)

    prefix length for IPv6 address

  • 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 or interface does not start



158
159
160
161
162
163
164
# File 'lib/linux_admin/network_interface/network_interface_rh.rb', line 158

def apply_static6(ip, prefix, gw, dns, search = nil)
  self.address6     = "#{ip}/#{prefix}"
  self.gateway6     = 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



93
94
95
96
97
# File 'lib/linux_admin/network_interface/network_interface_rh.rb', line 93

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 IPv4 networking information



108
109
110
111
112
113
114
115
116
117
# File 'lib/linux_admin/network_interface/network_interface_rh.rb', line 108

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

#enable_dhcp6Object

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



121
122
123
124
125
126
127
128
129
# File 'lib/linux_admin/network_interface/network_interface_rh.rb', line 121

def enable_dhcp6
  @interface_config['IPV6INIT'] = 'yes'
  @interface_config['DHCPV6C'] = 'yes'
  @interface_config.delete('IPV6ADDR')
  @interface_config.delete('IPV6_DEFAULTGW')
  @interface_config.delete("DNS1")
  @interface_config.delete("DNS2")
  @interface_config.delete("DOMAIN")
end

#gateway6=(address) ⇒ Object

Set the IPv6 gateway address for this interface

Parameters:

  • address (String)

    IPv6 address optionally including the prefix length

Raises:

  • ArgumentError if the address is not formatted properly



76
77
78
79
# File 'lib/linux_admin/network_interface/network_interface_rh.rb', line 76

def gateway6=(address)
  validate_ip(address)
  @interface_config['IPV6_DEFAULTGW'] = address
end

#gateway=(address) ⇒ Object

Set the IPv4 gateway address for this interface

Parameters:

  • address (String)

Raises:

  • ArgumentError if the address is not formatted properly



67
68
69
70
# File 'lib/linux_admin/network_interface/network_interface_rh.rb', line 67

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



85
86
87
88
# File 'lib/linux_admin/network_interface/network_interface_rh.rb', line 85

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

#parse_confObject

Parses the interface configuration file into the @interface_config hash



27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/linux_admin/network_interface/network_interface_rh.rb', line 27

def parse_conf
  @interface_config = {}

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

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

  @interface_config["NM_CONTROLLED"] = "no"
end

#reloadBoolean

Gathers current network information for this interface

Returns:

  • (Boolean)

    true if network information was gathered successfully



20
21
22
23
24
# File 'lib/linux_admin/network_interface/network_interface_rh.rb', line 20

def reload
  super
  parse_conf
  true
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



171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
# File 'lib/linux_admin/network_interface/network_interface_rh.rb', line 171

def save
  old_contents = @interface_file.file? ? File.read(@interface_file) : ""

  stop_success = stop
  # Stop twice because when configure both ipv4 and ipv6 as dhcp, ipv6 dhcp client will
  # exit and leave a /var/run/dhclient6-eth0.pid file. Then stop (ifdown eth0) will try
  # to kill this exited process so it returns 1. In the second call, this `.pid' file
  # has been deleted and ifdown returns 0.
  # See: https://bugzilla.redhat.com/show_bug.cgi?id=1472396
  stop_success = stop unless stop_success
  return false unless stop_success

  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

  reload
end

#search_order=(*domains) ⇒ Object

Sets the search domain list for this network interface

Parameters:

  • domains (Array<String>)

    the list of search domains



102
103
104
# File 'lib/linux_admin/network_interface/network_interface_rh.rb', line 102

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