Module: Livecd
- Defined in:
- lib/livecd.rb
Constant Summary collapse
- VERSION =
'0.9'- VM_PREFIX =
'livecd-'
Instance Method Summary collapse
- #exists_vm?(name) ⇒ Boolean
- #get_vm_state(name) ⇒ Object
- #list_vms ⇒ Object
- #run_iso(iso, opts) ⇒ Object
- #stop_all_vms ⇒ Object
- #stop_vm(name) ⇒ Object
Instance Method Details
#exists_vm?(name) ⇒ Boolean
21 22 23 |
# File 'lib/livecd.rb', line 21 def exists_vm?( name ) list_vms.include? name end |
#get_vm_state(name) ⇒ Object
25 26 27 28 29 |
# File 'lib/livecd.rb', line 25 def get_vm_state( name ) vboxinfo = `vboxmanage showvminfo --machinereadable livecd-webforpentester` state = vboxinfo.match(/VMState=\"([^\"]+)\"/) (state.nil?) ? nil : state[1] end |
#list_vms ⇒ Object
6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
# File 'lib/livecd.rb', line 6 def list_vms `vboxmanage list vms`. split("\n"). # match each line to the regex we expect map{|x| /^"([^"]+)" {[^}]*}/.match(x)}. # remove nil's compact. # get the first match map{|x| x[1]}. # find all those that have the prefix find_all{|x| x.start_with? VM_PREFIX }. # remove the prefix map{|x| x.sub VM_PREFIX, '' } end |
#run_iso(iso, opts) ⇒ Object
62 63 64 65 66 |
# File 'lib/livecd.rb', line 62 def run_iso( iso, opts ) name = find_name_for iso puts "starting vm #{name} (#{iso})" start_vm id(name), iso, opts end |
#stop_all_vms ⇒ Object
58 59 60 |
# File 'lib/livecd.rb', line 58 def stop_all_vms list_vms.each{|vm| stop_vm(vm)} end |
#stop_vm(name) ⇒ Object
31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 |
# File 'lib/livecd.rb', line 31 def stop_vm( name ) # if it exists: if exists_vm?(name) if get_vm_state(name) != 'PoweredOff' puts "stopping vm #{name}" `vboxmanage controlvm #{id(name)} poweroff` end for i in 1..4 sleep 0.5 state = get_vm_state name if state.nil? puts "Can't get state for vm '#{name}'. It may already be destroyed." break elsif state == 'poweroff' break end end puts "removing vm #{name}" `vboxmanage unregistervm #{id(name)} --delete` else $stderr.puts "Can't find vm '#{name}' to stop it." end end |