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"

Constants included from Common

Common::BIN_DIRS

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

Methods included from Common

#cmd, #cmd?, #run, #run!

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
16
# File 'lib/linux_admin/network_interface/network_interface_rh.rb', line 12

def initialize(interface)
  super
  @interface_file = Pathname.new(IFACE_DIR).join("ifcfg-#{@interface}")
  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



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

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



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

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



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

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



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

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



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

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



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

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

#parse_confObject

Parses the interface configuration file into the @interface_config hash



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

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



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

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



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

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