Class: Vbmanager

Inherits:
Object
  • Object
show all
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

Class Method Summary collapse

Instance Method Summary collapse

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

#portObject

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_imagesObject



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_imagesObject



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_vmObject



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_snapshotObject



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_vmObject



142
143
144
# File 'lib/droidrunner/vbmanager.rb', line 142

def del_vm
    puts %x(VBoxManage unregistervm #{@vm_clone} --delete)
end

#import_ovfObject

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_snapshotObject



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_vmObject



138
139
140
# File 'lib/droidrunner/vbmanager.rb', line 138

def stop_vm
    puts %x(VBoxManage controlvm #{@vm_clone} poweroff soft)
end