Class: Vgrnt::Util::VirtualBox

Inherits:
Object
  • Object
show all
Defined in:
lib/vgrnt/util/virtualbox.rb

Class Method Summary collapse

Class Method Details

.machineSSH(target) ⇒ Object



6
7
8
9
10
11
12
13
14
15
16
17
18
# File 'lib/vgrnt/util/virtualbox.rb', line 6

def self.machineSSH(target)
  machine = self.runningMachines()[target]
  return nil unless machine && machine[:state] == 'running'

  # Forwarding(0)="ssh,tcp,127.0.0.1,2222,,22"
  # Forwarding(1)="ssh,tcp,,2222,,22"
  ssh_info = machine[:showvminfo].scan( /^Forwarding\(\d+\)="ssh,tcp,([0-9.]*),([0-9]+),/ ).first

  return {
      :ssh_ip => ssh_info[0].empty? ? '127.0.0.1' : ssh_info[0],
      :ssh_port => ssh_info[1]
  }
end

.runningMachinesObject



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/vgrnt/util/virtualbox.rb', line 28

def self.runningMachines
  machines = {}

  ids = Dir.glob(".vagrant/machines/*/*/id")
  ids.each do |id_file|
    machine_name = id_file[ /^.vagrant\/machines\/(\w+)\/\w+\/id$/ ,1]
    machine_id = IO.read(id_file)
    
    machine_info = self.showvminfo(machine_id)

    machines[machine_name] = {
        :id =>  machine_id,
        :showvminfo => machine_info,
        :state => machine_info.scan( /^VMState="(.*)"$/ ).first.first   # VMState="running"
    }
  end
  return machines
end

.showvminfo(machine_id) ⇒ Object



24
25
26
# File 'lib/vgrnt/util/virtualbox.rb', line 24

def self.showvminfo(machine_id)
  return `#{self.showvminfo_command(machine_id)}`
end

.showvminfo_command(machine_id) ⇒ Object



20
21
22
# File 'lib/vgrnt/util/virtualbox.rb', line 20

def self.showvminfo_command(machine_id)
  return "VBoxManage showvminfo #{machine_id} --machinereadable"
end