Class: Wire::Resource::IPAddressOnIntf

Inherits:
ResourceBase show all
Defined in:
lib/wire/resource/ipaddr_on_intf.rb

Overview

Generic IP Address on an interface Able to add and remove ips on interfaces

Instance Attribute Summary collapse

Attributes inherited from ResourceBase

#name

Instance Method Summary collapse

Constructor Details

#initialize(name, device) ⇒ IPAddressOnIntf

initialize the object with given name and device params: name ip address in cidr notation device device/interface name



25
26
27
28
29
30
31
32
33
34
35
# File 'lib/wire/resource/ipaddr_on_intf.rb', line 25

def initialize(name, device)
  super(name)

  fail(ArgumentError, 'ip may not be empty') unless name && name.size > 0
  fail(ArgumentError, 'device may not be empty') unless device && device.size > 0

  @device = device
  @executables = {
    :ip => '/sbin/ip'
  }
end

Instance Attribute Details

#deviceObject

device (bridge) device name, i.e. “eth1” executables array of paths to needed binaries



18
19
20
# File 'lib/wire/resource/ipaddr_on_intf.rb', line 18

def device
  @device
end

#executablesObject

device (bridge) device name, i.e. “eth1” executables array of paths to needed binaries



18
19
20
# File 'lib/wire/resource/ipaddr_on_intf.rb', line 18

def executables
  @executables
end

Instance Method Details

#construct_add_commandObject

constructs an ip addr add command to set up an ip returns

  • command as [String]



64
65
66
# File 'lib/wire/resource/ipaddr_on_intf.rb', line 64

def construct_add_command
  "#{@executables[:ip]} addr add #{name} dev #{device}"
end

#construct_delete_commandObject

constructs an ip addr del command to delete an ip returns

  • command as [String]



82
83
84
# File 'lib/wire/resource/ipaddr_on_intf.rb', line 82

def construct_delete_command
  "#{@executables[:ip]} addr del #{name} dev #{device}"
end

#construct_exist_commandObject

constructs an ip addr show / grep command to see if an ip address is up on a device returns

  • command as [String]



41
42
43
# File 'lib/wire/resource/ipaddr_on_intf.rb', line 41

def construct_exist_command
  "#{@executables[:ip]} addr show #{device} | grep -wq -E \"^\\W*inet #{@name}.*#{@device}\""
end

#downObject

takes an ip down on given device returns

  • [Boolean]: true: ok, false otherwise



94
95
96
97
98
99
100
# File 'lib/wire/resource/ipaddr_on_intf.rb', line 94

def down
  LocalExecution.with(construct_delete_command, [],
                      { :b_shell => false, :b_sudo => true }) do |exec_obj|
    exec_obj.run
    return (exec_obj.exitstatus == 0)
  end
end

#down?Boolean

thats the opposite of up

Returns:

  • (Boolean)


87
88
89
# File 'lib/wire/resource/ipaddr_on_intf.rb', line 87

def down?
  !(up?)
end

#exist?Boolean

runs the “exist” command returns

  • [Boolean]: true: ip is on on device, false otherwise

Returns:

  • (Boolean)


48
49
50
51
52
53
54
# File 'lib/wire/resource/ipaddr_on_intf.rb', line 48

def exist?
  LocalExecution.with(construct_exist_command, [],
                      { :b_shell => false, :b_sudo => false }) do |exec_obj|
    exec_obj.run
    return (exec_obj.exitstatus == 0)
  end
end

#to_sObject

generate a string representation



103
104
105
# File 'lib/wire/resource/ipaddr_on_intf.rb', line 103

def to_s
  "IPAddressOnIntf:[#{name},device=#{device}]"
end

#upObject

takes an ip up on given device returns

  • [Boolean]: true: ok, false otherwise



71
72
73
74
75
76
77
# File 'lib/wire/resource/ipaddr_on_intf.rb', line 71

def up
  LocalExecution.with(construct_add_command, [],
                      { :b_shell => false, :b_sudo => true }) do |exec_obj|
    exec_obj.run
    return (exec_obj.exitstatus == 0)
  end
end

#up?Boolean

same as exist?

Returns:

  • (Boolean)


57
58
59
# File 'lib/wire/resource/ipaddr_on_intf.rb', line 57

def up?
  exist?
end