Class: Phut::Vhost
- Inherits:
-
Object
- Object
- Phut::Vhost
- Extended by:
- Finder
- Includes:
- ShellRunner
- Defined in:
- lib/phut/vhost.rb
Overview
Virtual host for NetTester rubocop:disable ClassLength
Instance Attribute Summary collapse
-
#ip_address ⇒ Object
readonly
Returns the value of attribute ip_address.
-
#mac_address ⇒ Object
readonly
Returns the value of attribute mac_address.
Class Method Summary collapse
Instance Method Summary collapse
- #device ⇒ Object
- #device=(device_name) ⇒ Object
-
#initialize(name:, ip_address:, mac_address:, device: nil, promisc: false, arp_entries: nil) ⇒ Vhost
constructor
rubocop:disable ParameterLists.
- #kill ⇒ Object
- #name ⇒ Object
- #packets_received_from(source) ⇒ Object
- #packets_sent_to(dest) ⇒ Object
-
#running? ⇒ Boolean
rubocop:enable LineLength.
- #send_packet(destination) ⇒ Object
- #set_default_arp_table ⇒ Object
-
#start ⇒ Object
(also: #run)
rubocop:disable LineLength.
- #stop ⇒ Object
- #to_s ⇒ Object
Methods included from Finder
Methods included from ShellRunner
Constructor Details
#initialize(name:, ip_address:, mac_address:, device: nil, promisc: false, arp_entries: nil) ⇒ Vhost
rubocop:disable ParameterLists
48 49 50 51 52 53 54 55 56 |
# File 'lib/phut/vhost.rb', line 48 def initialize(name:, ip_address:, mac_address:, device: nil, promisc: false, arp_entries: nil) @name = name @ip_address = ip_address @mac_address = mac_address @device = device @promisc = promisc @arp_entries = arp_entries end |
Instance Attribute Details
#ip_address ⇒ Object (readonly)
Returns the value of attribute ip_address.
14 15 16 |
# File 'lib/phut/vhost.rb', line 14 def ip_address @ip_address end |
#mac_address ⇒ Object (readonly)
Returns the value of attribute mac_address.
15 16 17 |
# File 'lib/phut/vhost.rb', line 15 def mac_address @mac_address end |
Class Method Details
.all ⇒ Object
17 18 19 20 21 22 23 24 25 |
# File 'lib/phut/vhost.rb', line 17 def self.all Dir.glob(File.join(Phut.socket_dir, 'vhost.*.ctl')).map do |each| vhost = DRbObject.new_with_uri("drbunix:#{each}") new(name: vhost.name, ip_address: vhost.ip_address, mac_address: vhost.mac_address, device: vhost.device) end end |
.connect_link ⇒ Object
38 39 40 41 42 43 44 45 |
# File 'lib/phut/vhost.rb', line 38 def self.connect_link all.each do |each| Link.all.each do |link| device = link.device(each.name) each.device = device if device end end end |
.create(*args) ⇒ Object
27 28 29 |
# File 'lib/phut/vhost.rb', line 27 def self.create(*args) new(*args).tap(&:start) end |
.destroy_all ⇒ Object
31 32 33 34 35 36 |
# File 'lib/phut/vhost.rb', line 31 def self.destroy_all ::Dir.glob(File.join(Phut.socket_dir, 'vhost.*.ctl')).each do |each| /vhost\.(\S+)\.ctl/=~ each VhostDaemon.process(Regexp.last_match(1), Phut.socket_dir).kill end end |
Instance Method Details
#device ⇒ Object
92 93 94 |
# File 'lib/phut/vhost.rb', line 92 def device VhostDaemon.process(name, Phut.socket_dir).device end |
#device=(device_name) ⇒ Object
96 97 98 |
# File 'lib/phut/vhost.rb', line 96 def device=(device_name) VhostDaemon.process(name, Phut.socket_dir).device = device_name end |
#kill ⇒ Object
87 88 89 90 |
# File 'lib/phut/vhost.rb', line 87 def kill sh "bundle exec vhost stop -n #{name} -S #{Phut.socket_dir}" sleep 1 end |
#name ⇒ Object
61 62 63 |
# File 'lib/phut/vhost.rb', line 61 def name @name || @ip_address end |
#packets_received_from(source) ⇒ Object
111 112 113 114 115 116 |
# File 'lib/phut/vhost.rb', line 111 def packets_received_from(source) VhostDaemon.process(name, Phut.socket_dir).stats[:rx].select do |each| (each[:source_mac].to_s == source.mac_address) && (each[:source_ip_address].to_s == source.ip_address) end end |
#packets_sent_to(dest) ⇒ Object
104 105 106 107 108 109 |
# File 'lib/phut/vhost.rb', line 104 def packets_sent_to(dest) VhostDaemon.process(name, Phut.socket_dir).stats[:tx].select do |each| (each[:destination_mac].to_s == dest.mac_address) && (each[:destination_ip_address].to_s == dest.ip_address) end end |
#running? ⇒ Boolean
rubocop:enable LineLength
79 80 81 |
# File 'lib/phut/vhost.rb', line 79 def running? VhostDaemon.process(name, Phut.socket_dir).running? end |
#send_packet(destination) ⇒ Object
100 101 102 |
# File 'lib/phut/vhost.rb', line 100 def send_packet(destination) VhostDaemon.process(name, Phut.socket_dir).send_packets(destination, 1) end |
#set_default_arp_table ⇒ Object
118 119 120 121 122 123 |
# File 'lib/phut/vhost.rb', line 118 def set_default_arp_table arp_table = Vhost.all.each_with_object({}) do |each, hash| hash[each.ip_address] = each.mac_address end VhostDaemon.process(name, Phut.socket_dir).arp_table = arp_table end |
#start ⇒ Object Also known as: run
rubocop:disable LineLength
66 67 68 69 70 71 72 73 74 75 |
# File 'lib/phut/vhost.rb', line 66 def start if ENV['rvm_path'] sh "rvmsudo vhost run #{}" else vhost = File.('../../bin/vhost', __dir__) sh "bundle exec sudo env PATH=#{ENV['PATH']} #{vhost} run #{}" end sleep 1 self.device = @device if @device end |
#stop ⇒ Object
83 84 85 |
# File 'lib/phut/vhost.rb', line 83 def stop VhostDaemon.process(name, Phut.socket_dir).stop end |
#to_s ⇒ Object
125 126 127 |
# File 'lib/phut/vhost.rb', line 125 def to_s "vhost (name = #{name}, IP address = #{@ip_address})" end |