Class: Phut::Netns

Inherits:
Object
  • Object
show all
Includes:
ShellRunner
Defined in:
lib/phut/netns.rb

Overview

‘ip netns …` command runner

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from ShellRunner

#sh

Constructor Details

#initialize(options, name, logger = NullLogger.new) ⇒ Netns

Returns a new instance of Netns.



12
13
14
15
16
# File 'lib/phut/netns.rb', line 12

def initialize(options, name, logger = NullLogger.new)
  @name = name
  @options = options
  @logger = logger
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(message, *_args) ⇒ Object



33
34
35
# File 'lib/phut/netns.rb', line 33

def method_missing(message, *_args)
  @options.__send__ :[], message
end

Instance Attribute Details

#nameObject (readonly)

Returns the value of attribute name.



9
10
11
# File 'lib/phut/netns.rb', line 9

def name
  @name
end

#network_deviceObject

Returns the value of attribute network_device.



10
11
12
# File 'lib/phut/netns.rb', line 10

def network_device
  @network_device
end

Instance Method Details

#maybe_stopObject

rubocop:enable AbcSize



29
30
31
# File 'lib/phut/netns.rb', line 29

def maybe_stop
  sh "sudo ip netns delete #{name}"
end

#runObject

rubocop:disable AbcSize



19
20
21
22
23
24
25
26
# File 'lib/phut/netns.rb', line 19

def run
  sh "sudo ip netns add #{name}"
  sh "sudo ip link set dev #{network_device} netns #{name}"
  sh "sudo ip netns exec #{name} ifconfig lo 127.0.0.1"
  sh "sudo ip netns exec #{name}"\
     " ifconfig #{network_device} #{ip} netmask #{netmask}"
  sh "sudo ip netns exec #{name} route add -net #{net} gw #{gateway}"
end