Class: Phut::Vhost

Inherits:
Object
  • Object
show all
Extended by:
Finder
Includes:
ShellRunner
Defined in:
lib/phut/vhost.rb

Overview

Virtual host for NetTester rubocop:disable ClassLength

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Finder

find_by, find_by!

Methods included from ShellRunner

#sh, #sudo

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_addressObject (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_addressObject (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

.allObject



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


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_allObject



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

#deviceObject



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

#killObject



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

#nameObject



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

Returns:

  • (Boolean)


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_tableObject



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

#startObject 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 #{run_options}"
  else
    vhost = File.expand_path('../../bin/vhost', __dir__)
    sh "bundle exec sudo env PATH=#{ENV['PATH']} #{vhost} run #{run_options}"
  end
  sleep 1
  self.device = @device if @device
end

#stopObject



83
84
85
# File 'lib/phut/vhost.rb', line 83

def stop
  VhostDaemon.process(name, Phut.socket_dir).stop
end

#to_sObject



125
126
127
# File 'lib/phut/vhost.rb', line 125

def to_s
  "vhost (name = #{name}, IP address = #{@ip_address})"
end