Class: VirtualBox::Net

Inherits:
Object
  • Object
show all
Defined in:
lib/virtual_box/net.rb

Overview

Descriptor for a virtual network managed by Virtual Box.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Net

Creates a virtual network specification rule based on the given attributes.

The network is not automatically added to VirtualBox.



42
43
44
# File 'lib/virtual_box/net.rb', line 42

def initialize(options = {})
  options.each { |k, v| self.send :"#{k}=", v }
end

Instance Attribute Details

#if_nameString (readonly)

The name of the host’s virtual NIC that’s connected to this network.

VirtualBox’s CLI does not provide a way to set the NIC name. Therefore, this attribute is read-only, and it is automatically set when the network is registered with VirtualBox.



19
20
21
# File 'lib/virtual_box/net.rb', line 19

def if_name
  @if_name
end

#ipString

The IP address received by the host computer on this virtual network.



7
8
9
# File 'lib/virtual_box/net.rb', line 7

def ip
  @ip
end

#macString (readonly)

The MAC address of the host’s virtual NIC that’s connected to this network.

VirtualBox’s CLI does not provide a way to set the MAC. Therefore, this attribute is read-only, and it is automatically set when the network is registered with VirtualBox.



35
36
37
# File 'lib/virtual_box/net.rb', line 35

def mac
  @mac
end

#nameString (readonly)

The name of the VirtualBox internal network.

VirtualBox’s CLI does not provide a way to set the internal network name. Therefore, this attribute is read-only, and it is automatically set when the network is registered with VirtualBox.



27
28
29
# File 'lib/virtual_box/net.rb', line 27

def name
  @name
end

#netmaskString

The network mask received by the host computer on this virtual network.



11
12
13
# File 'lib/virtual_box/net.rb', line 11

def netmask
  @netmask
end

Class Method Details

.allArray<VirtualBox::Dhcp>

The DHCP servers added to with VirtualBox.



114
115
116
117
118
119
120
121
122
# File 'lib/virtual_box/net.rb', line 114

def self.all
  result = VirtualBox.run_command ['VBoxManage', '--nologo', 'list', '--long',
                                   'hostonlyifs']
  if result.status != 0
    raise 'Unexpected error code returned by VirtualBox'
  end
  result.output.split("\n\n").
                map { |net_info| new.from_net_info(net_info) }
end

Instance Method Details

#addVirtualBox::Net

Adds this virtual network specification to VirtualBox.



65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
# File 'lib/virtual_box/net.rb', line 65

def add
  unless if_name.nil?
    raise "Virtual network already added to VirtualBox"
  end
  
  # Create the network and pull its name.
  result = VirtualBox.run_command ['VBoxManage', '--nologo', 'hostonlyif',
      'create']
  if result.status != 0
    raise 'Unexpected error code returned by VirtualBox'
  end
  unless match = /^interface\s+'(.*)'\s+.*created/i.match(result.output)
    raise "VirtualBox output does not include interface name"
  end
  @if_name = match[1]
  
  # Now list all the networks to pull the rest of the information.
  networks = self.class.all
  network = networks.find { |net| net.if_name == if_name }
  @name = network.name
  @mac = network.mac
  
  if (ip && ip != network.ip) || (netmask && netmask != network.netmask)
    result = VirtualBox.run_command ['VBoxManage', '--nologo', 'hostonlyif',
        'ipconfig', if_name, '--ip', ip, '--netmask', netmask]
    if result.status != 0
      raise 'Unexpected error code returned by VirtualBox'
    end
  else
    self.ip = network.ip
    self.netmask = network.netmask
  end
  
  self
end

#from_net_info(net_info) ⇒ VirtualBox::Net

Parses information about a DHCP server returned by VirtualBox.

The parsed information is used to replace this network’s specification.



130
131
132
133
134
135
136
137
138
139
140
141
# File 'lib/virtual_box/net.rb', line 130

def from_net_info(net_info)
  info = Hash[net_info.split("\n").map { |line|
    line.split(':', 2).map(&:strip)
  }]
  
  @if_name = info['Name']
  @name = info['VBoxNetworkName']
  @mac = info['HardwareAddress'].upcase.gsub(/[^0-9A-F]/, '')
  self.ip = info['IPAddress']
  self.netmask = info['NetworkMask']
  self
end

#live?Boolean

True if this virtual network has been added to VirtualBox.



56
57
58
59
60
# File 'lib/virtual_box/net.rb', line 56

def live?
  networks = self.class.all
  network = networks.find { |net| net.if_name == if_name }
  network ? true : false
end

#removeVirtualBox::Net

Removes this virtual network from VirtualBox’s database.



104
105
106
107
108
109
# File 'lib/virtual_box/net.rb', line 104

def remove
  unless if_name.nil?
    VirtualBox.run_command ['VBoxManage', 'hostonlyif', 'remove', if_name]
  end
  self
end

#to_hashHash<Symbol, Object>

Hash capturing this specification. Can be passed to Net#new.



50
51
52
# File 'lib/virtual_box/net.rb', line 50

def to_hash
  { :ip => ip, :netmask => netmask }
end