Class: NetVbox::Vm

Inherits:
Object
  • Object
show all
Defined in:
lib/netvbox/vm.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(vm_info) ⇒ Vm

Returns a new instance of Vm.



54
55
56
# File 'lib/netvbox/vm.rb', line 54

def initialize(vm_info)
  @vm_info = vm_info
end

Instance Attribute Details

#vm_infoObject (readonly)

Returns the value of attribute vm_info.



52
53
54
# File 'lib/netvbox/vm.rb', line 52

def vm_info
  @vm_info
end

Instance Method Details

#load_snapshotObject



80
81
82
83
84
85
86
87
# File 'lib/netvbox/vm.rb', line 80

def load_snapshot
  # VMs with 3D acceleration cannot be started remotely
  # TODO: propogate this info to a log or some output to notify user
  return if showvminfo('accelerate3d') == 'on'
  poweroff
  command = "VBoxManage snapshot \"#{@vm_info.vm_name}\" restore \"#{@vm_info.snapshot_name}\" && VBoxManage startvm \"#{@vm_info.vm_name}\" --type headless"
  my_ssh {|ssh| ssh.exec! command}
end

#poweroffObject



74
75
76
77
78
# File 'lib/netvbox/vm.rb', line 74

def poweroff
  if showvminfo('VMState') == 'running'
    my_ssh {|ssh| ssh.exec!("VBoxManage controlvm \"#{@vm_info.vm_name}\" poweroff").strip}
  end
end

#ssh_guest(username, pw, command) ⇒ Object



93
94
95
96
97
98
99
100
101
# File 'lib/netvbox/vm.rb', line 93

def ssh_guest(username, pw, command)
  begin
    hostname = vm_ip
  rescue
    return 'Guest VM IP unavailable'
  end
  ssh_info = SshConnectionInfo.new(hostname, username, pw)
  my_ssh(ssh_info) {|ssh| ssh.exec! command} || ''
end

#ssh_host(command) ⇒ Object



89
90
91
# File 'lib/netvbox/vm.rb', line 89

def ssh_host(command)
  my_ssh {|ssh| ssh.exec! command} || ''
end

#statusObject



58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/netvbox/vm.rb', line 58

def status
  vm_state = showvminfo('VMState')
  case vm_state
  when 'running'
    'running'
  when 'saved'
    'not running (saved)'
  when 'poweroff'
    'not running (power off)'
  when 'aborted'
    'not running (aborted)'
  else
    vm_state.split("\n")[0]
  end
end

#vm_ipObject



103
104
105
106
107
108
# File 'lib/netvbox/vm.rb', line 103

def vm_ip
  command = "VBoxManage guestproperty get \"#{@vm_info.vm_name}\" /VirtualBox/GuestInfo/Net/0/V4/IP"
  out = my_ssh {|ssh| ssh.exec! command}
  return out['Value:'.length..-1].strip unless (out =~ /^Value:/).nil?
  raise "Cannot get IP for #{@vm_info.vm_name}: #{out}"
end