Class: Vbmanager
- Inherits:
-
Object
- Object
- Vbmanager
- Defined in:
- lib/droidrunner/vbmanager.rb
Overview
< Thor
Constant Summary collapse
- @@current_id =
1- @@locked_machines =
Array.new
- @@starting_id =
5555
Instance Attribute Summary collapse
-
#port ⇒ Object
Returns the value of attribute port.
Class Method Summary collapse
- .get_existing_pool_images ⇒ Object
- .get_running_pool_images ⇒ Object
- .list_running_vms(silent: false) ⇒ Object
-
.list_vms(silent: false) ⇒ Object
desc “list_vms”,“Lists Virtual Box VMs”.
Instance Method Summary collapse
- #clone_vm ⇒ Object
- #configure_vm(accelerated: false, memory: 1024) ⇒ Object
- #create_snapshot ⇒ Object
- #del_vm ⇒ Object
-
#import_ovf ⇒ Object
desc “import_ovf VMNAME”,“Imports a VM image”.
-
#initialize(vm_image: nil, is_pool: false, reuse: false) ⇒ Vbmanager
constructor
A new instance of Vbmanager.
- #load_snapshot ⇒ Object
-
#start_vm(headless: true) ⇒ Object
desc “start_vm”,“Starts a VM”.
- #stop_vm ⇒ Object
Constructor Details
#initialize(vm_image: nil, is_pool: false, reuse: false) ⇒ Vbmanager
Returns a new instance of Vbmanager.
10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 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 57 58 59 |
# File 'lib/droidrunner/vbmanager.rb', line 10 def initialize(vm_image:nil,is_pool:false,reuse:false) @vm_image=vm_image @is_pool = is_pool puts "Locked machines : #{@@locked_machines}" running = Vbmanager.get_running_pool_images puts "Running VMS : #{running.inspect}" vms = Vbmanager.get_existing_pool_images puts "Registered VMS : #{vms.inspect}" not_running = vms - running not_running -= @@locked_machines puts "Available VMS : #{not_running.inspect}" if vms.size != 0 max = vms.max @@current_id = max + 1 if max >= @@current_id end puts "Setted current VM id to #{@@current_id}..." if !reuse @id = @@current_id @@current_id +=1 @vm_clone = "#{@vm_image}-#{@id}" if !is_pool @vm_clone = "#{@vm_image}-pool#{@id}" if is_pool @@locked_machines.push(@id) @configured = false @is_reused = false else if not_running.size == 0 raise "No available machines for reuse!!!" end @is_reused = true @id = not_running[0] @vm_clone = "#{vm_image}-pool#{@id}" @@locked_machines.push(@id) @configured = true end @last_snap = "#{@vm_clone}-snap" @port = @@starting_id + @id puts "Setted ID to #{@id}" puts "Setted VM Clone name to #{@vm_clone}..." puts "Setted port to #{@port}" end |
Instance Attribute Details
#port ⇒ Object
Returns the value of attribute port.
5 6 7 |
# File 'lib/droidrunner/vbmanager.rb', line 5 def port @port end |
Class Method Details
.get_existing_pool_images ⇒ Object
85 86 87 88 89 90 91 92 |
# File 'lib/droidrunner/vbmanager.rb', line 85 def self.get_existing_pool_images ids = Array.new list = list_vms(silent:true) list.scan(/#{@vm_image}-pool([\d]+)/) { |id| ids.push(id[0].to_i) } ids end |
.get_running_pool_images ⇒ Object
76 77 78 79 80 81 82 83 |
# File 'lib/droidrunner/vbmanager.rb', line 76 def self.get_running_pool_images ids = Array.new list = list_running_vms(silent:true) list.scan(/#{@vm_image}-pool([\d]+)/) { |id| ids.push(id.to_i) } ids end |
.list_running_vms(silent: false) ⇒ Object
69 70 71 72 73 74 |
# File 'lib/droidrunner/vbmanager.rb', line 69 def self.list_running_vms(silent:false) puts "Listing running vms" if !silent ret = %x(vboxmanage list runningvms) puts ret if !silent ret end |
.list_vms(silent: false) ⇒ Object
desc “list_vms”,“Lists Virtual Box VMs”
62 63 64 65 66 67 |
# File 'lib/droidrunner/vbmanager.rb', line 62 def self.list_vms(silent:false) puts "Listing vms" if !silent ret = %x(VBoxManage list vms) puts ret if !silent ret end |
Instance Method Details
#clone_vm ⇒ Object
123 124 125 126 127 128 |
# File 'lib/droidrunner/vbmanager.rb', line 123 def clone_vm puts "Cloning VM #{@vm_image} to #{@vm_clone}..." puts %x(VBoxManage clonevm --mode machine #{@vm_image} --name #{@vm_clone} --register) puts "Unlocking machine : #{@vm_clone}" if !@is_reused && @is_pool @@locked_machines.delete(@id) if !@is_reused && @is_pool end |
#configure_vm(accelerated: false, memory: 1024) ⇒ Object
100 101 102 103 104 105 106 107 108 109 110 |
# File 'lib/droidrunner/vbmanager.rb', line 100 def configure_vm(accelerated:false, memory:1024) puts "Setting Network... NAT" puts %x(VBoxManage modifyvm #{@vm_clone} --nic1 nat) puts "Setting adb port forwarding...#{@port}" puts %x(VBoxManage modifyvm #{@vm_clone} --natpf1 #{@vm_clone}-adb,tcp,*,#{@port},*,5555) puts "Setting 3D Acceleration... #{accelerated}" puts %x(VBoxManage modifyvm #{@vm_clone} --accelerate3d off) if !accelerated puts "Setting Memory... #{memory}" puts %x(VBoxManage modifyvm #{@vm_clone} --memory #{memory}) @configured = true end |
#create_snapshot ⇒ Object
112 113 114 115 116 |
# File 'lib/droidrunner/vbmanager.rb', line 112 def create_snapshot puts "WARNING: #{@vm_clone} was not configured!" if !@configured puts "Creating a snapshot #{@last_snap} for machine #{@vm_clone}" puts %x(VBoxManage snapshot #{@vm_clone} take #{@last_snap}) end |
#del_vm ⇒ Object
142 143 144 |
# File 'lib/droidrunner/vbmanager.rb', line 142 def del_vm puts %x(VBoxManage unregistervm #{@vm_clone} --delete) end |
#import_ovf ⇒ Object
desc “import_ovf VMNAME”,“Imports a VM image”
95 96 97 98 |
# File 'lib/droidrunner/vbmanager.rb', line 95 def import_ovf puts "Importing vm image: #{@vm_image}" puts %x(VBoxManage import #{@vm_image}) end |
#load_snapshot ⇒ Object
118 119 120 121 |
# File 'lib/droidrunner/vbmanager.rb', line 118 def load_snapshot puts "Loading snapshot #{@last_snap} for machine #{@vm_clone}" puts %x(VBoxManage snapshot #{@vm_clone} restore #{@last_snap}) end |
#start_vm(headless: true) ⇒ Object
desc “start_vm”,“Starts a VM”
131 132 133 134 135 136 |
# File 'lib/droidrunner/vbmanager.rb', line 131 def start_vm(headless:true) puts "WARNING: #{@vm_clone} was not configured!" if !@configured puts "Starting VM #{@vm_clone}" puts %x(VBoxManage startvm #{@vm_clone} --type headless) if headless puts %x(VBoxManage startvm #{@vm_clone}) if !headless end |
#stop_vm ⇒ Object
138 139 140 |
# File 'lib/droidrunner/vbmanager.rb', line 138 def stop_vm puts %x(VBoxManage controlvm #{@vm_clone} poweroff soft) end |