Class: CCBuilder::ONEClient

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

Defined Under Namespace

Classes: Error

Instance Method Summary collapse

Constructor Details

#initialize(verbose = false) ⇒ ONEClient

Returns a new instance of ONEClient.



15
16
17
18
19
20
# File 'lib/ONEClient.rb', line 15

def initialize(verbose = false)
  @verbose = verbose
  @one_client = OpenNebula::Client.new(nil)
rescue OpenNebula::Error => e
  raise Error.new("OpenNebula: #{e.message}")
end

Instance Method Details

#create(one_vm_descriptor, n = 1) ⇒ Object

API-method create



23
24
25
26
27
28
# File 'lib/ONEClient.rb', line 23

def create(one_vm_descriptor, n = 1)
  n.times do |i|
    create_single(one_vm_descriptor)
    puts "created VM #i" if @verbose
  end
end

#create_single(one_vm_descriptor) ⇒ Object



30
31
32
33
34
35
36
# File 'lib/ONEClient.rb', line 30

def create_single(one_vm_descriptor)
  vm = OpenNebula::VirtualMachine.new(OpenNebula::VirtualMachine.build_xml, @one_client)
  template=File.read(one_vm_descriptor)
 vm.allocate(template)
rescue
  raise Error.new("OpenNebula: Can not read template: #{one_vm_descriptor}")
end

#delete(id_low, id_high = 0) ⇒ Object

API-method delete



75
76
77
78
79
80
81
# File 'lib/ONEClient.rb', line 75

def delete(id_low, id_high = 0)
  id_high = id_low if id_high < id_low
  Range.new(id_low,id_high).each do |id|
    delete_single(id)
    puts "deleted VM #{id}" if @verbose
  end
end

#delete_single(id) ⇒ Object



83
84
85
86
87
88
# File 'lib/ONEClient.rb', line 83

def delete_single(id)
  vm = OpenNebula::VirtualMachine.new_with_id(id, @one_client)
  vm.finalize
rescue OpenNebula::Error => e
  raise Error.new("OpenNebula: #{e.message}")
end

#listObject

API-method list



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/ONEClient.rb', line 39

def list
  n = 3
  vmpool = OpenNebula::VirtualMachinePool.new(@one_client, -1)
  result = vmpool.info
  vms = Array.new 
  queue = Queue.new # queue to hold ids which need to be looked up
  threads = Array.new
  n.times do |i| # Launch n threads which do the lookups#        
    threads[i] = Thread.new do 
      id = queue.pop
      until id == 0
        vm = OpenNebula::VirtualMachine.new_with_id(id, @one_client)
        vm.info
        ip = vm.template_str.scan(/IP=(\d+\.\d+\.\d+\.\d+),/)[0][0]
        status = vm.status
        vmhost = vm["HISTORY/HOSTNAME"]
        host = Resolv::getname(ip)
        Thread.pass
        puts "VM ##{id} with ip #{ip} resolved to name #{host}" if @verbose
        vms << { 'id' => id, 'ip' => ip, 'host' => host, 'status' => status, 'vmhost' => vmhost }
        STDOUT.flush
        id = queue.pop
      end 
    end
  end
  vmpool.map do |vm|
    queue << vm.id
  end
  n.times { queue << 0 } # send 0 to terminate threads 
  n.times { |i| threads[i].join } # wait until threads finished
  return vms
ensure
  n.times { |i| threads[i].terminate } 
end