Module: OmfRc::Util::Ip

Includes:
Cocaine, ResourceProxyDSL
Included in:
ResourceProxy::Net, Iw
Defined in:
lib/omf_rc/util/ip.rb

Constant Summary

Constants included from ResourceProxyDSL

ResourceProxyDSL::DEFAULT_PROP_ACCESS, ResourceProxyDSL::PROXY_DIR, ResourceProxyDSL::UTIL_DIR

Instance Method Summary collapse

Methods included from ResourceProxyDSL

#call_hook, #hook_defined?, included

Instance Method Details

#configure_ip_addrString

Configure IP address

Parameters:

  • value

    value of IP address, it should have netmask. (e.g. 0.0.0.0/24)

Returns:

  • (String)

    IP address

Raises:

  • (ArgumentError)

    if provided no IP address or incorrect format



55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/omf_rc/util/ip.rb', line 55

configure :ip_addr do |resource, value|
  if value.nil? || value.split('/')[1].nil?
    raise ArgumentError, "You need to provide a netmask with the IP address, e.g. #{value}/24. Got #{value}."
  end
  # Remove all ip addrs associated with the device
  resource.flush_ip_addrs
  c=CommandLine.new("ip",  "addr add :ip_address dev :device")
  c.run({ :ip_address => value,
          :device => resource.property.if_name })

  resource.interface_up
  resource.request_ip_addr
end

#flush_ip_addrsObject

Remove IP addresses associated with the interface



88
89
90
91
# File 'lib/omf_rc/util/ip.rb', line 88

work :flush_ip_addrs do |resource|
  c=CommandLine.new("ip",  "addr flush dev :device")
  c.run({ :device => resource.property.if_name })
end

#interface_upObject

Bring up network interface



74
75
76
77
# File 'lib/omf_rc/util/ip.rb', line 74

work :interface_up do |resource|
  c=CommandLine.new("ip", "link set :dev up")
  c.run({ :dev => resource.property.if_name })
end

#request_ip_addrString

Retrieve IP address

Returns:

  • (String)

    IP address



20
21
22
23
24
# File 'lib/omf_rc/util/ip.rb', line 20

request :ip_addr do |resource|
  c = CommandLine.new("ip", "addr show dev :device")
  addr = c.run( { :device => resource.property.if_name })
  addr && addr.chomp.match(/inet ([[0-9]\:\/\.]+)/) && $1
end

#request_mac_addrString

Retrieve MAC address

Returns:

  • (String)

    MAC address



31
32
33
34
35
# File 'lib/omf_rc/util/ip.rb', line 31

request :mac_addr do |resource|
  c = CommandLine.new("ip", "addr show dev :device")
  addr = c.run( { :device => resource.property.if_name })
  addr && addr.chomp.match(/link\/ether ([\d[a-f][A-F]\:]+)/) && $1
end