Class: Fog::Compute::OpenNebula::Real

Inherits:
Object
  • Object
show all
Includes:
Collections
Defined in:
lib/fog/opennebula/requests/compute/get_vnc_console.rb,
lib/fog/opennebula/compute.rb,
lib/fog/opennebula/requests/compute/vm_stop.rb,
lib/fog/opennebula/requests/compute/list_vms.rb,
lib/fog/opennebula/requests/compute/vm_resume.rb,
lib/fog/opennebula/requests/compute/vm_destroy.rb,
lib/fog/opennebula/requests/compute/list_groups.rb,
lib/fog/opennebula/requests/compute/vm_allocate.rb,
lib/fog/opennebula/requests/compute/list_networks.rb,
lib/fog/opennebula/requests/compute/template_pool.rb

Overview

class Real

Defined Under Namespace

Classes: Mock

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Real

Returns a new instance of Real.



50
51
52
53
# File 'lib/fog/opennebula/compute.rb', line 50

def initialize(options={})
  require 'opennebula'
  @client = ::OpenNebula::Client.new("#{options[:opennebula_username]}:#{options[:opennebula_password]}", options[:opennebula_endpoint])
end

Instance Method Details

#clientObject



46
47
48
# File 'lib/fog/opennebula/compute.rb', line 46

def client
  return @client
end

#get_vnc_console(server_id, console_type, onevm_object) ⇒ Object



28
29
30
31
32
33
34
35
36
37
38
# File 'lib/fog/opennebula/requests/compute/get_vnc_console.rb', line 28

def get_vnc_console(server_id, console_type, onevm_object)
  logger = Fog::Logger.new
  $conf = {"vnc_proxy_port" => "29876", "vnc_proxy_ipv6" => "", "vnc_proxy_support_wss" => "", "vnc_proxy_cert" => "", "vnc_proxy_key" => ""}
  $vnc = OpenNebulaVNC.new($conf, logger)
  ret = startvnc(onevm_object,$vnc)

  response = Excon::Response.new
  response.status = ret[0]
  response.body = ret[1]
  response
end

#list_groups(filter = {}) ⇒ Object



5
6
7
8
9
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
# File 'lib/fog/opennebula/requests/compute/list_groups.rb', line 5

def list_groups(filter = {})

  groups=[]
  grouppool = ::OpenNebula::GroupPool.new(client)
  grouppool.info

  # {
  #   "GROUP"=>{
  #     "ID"=>"0", 
  #      "NAME"=>"oneadmin", 
  #      "USERS"=>{"ID"=>["0", "1"]}, 
  #      "DATASTORE_QUOTA"=>{}, 
  #      "NETWORK_QUOTA"=>{}, 
  #      "VM_QUOTA"=>{}, 
  #      "IMAGE_QUOTA"=>{}
  #    }
  #}

  grouppool.each do |group| 
    filter_missmatch = false

    unless (filter.empty?)
      filter.each do |k,v|
        if group["#{k.to_s.upcase}"] && group["#{k.to_s.upcase}"] != v.to_s
          filter_missmatch = true
          break
        end
      end 
      next if filter_missmatch
    end 
    groups << {:id => group["ID"], :name => group["NAME"]}
  end
  groups
end

#list_networks(filter = { }) ⇒ Object



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/fog/opennebula/requests/compute/list_networks.rb', line 8

def list_networks(filter = { })

  networks=[]
    netpool = ::OpenNebula::VirtualNetworkPool.new(client)
    if filter[:id].nil?
      netpool.info!(-2,-1,-1)
    elsif filter[:id]
      filter[:id] = filter[:id].to_i if filter[:id].is_a?(String)
      netpool.info!(-2, filter[:id], filter[:id])
    end # if filter[:id].nil?
 
  netpool.each do |network| 
	    networks << network_to_attributes(network.to_hash)
	  end
  networks
end

#list_vms(filter = {}) ⇒ Object



7
8
9
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
# File 'lib/fog/opennebula/requests/compute/list_vms.rb', line 7

def list_vms(filter={})
  vms=[]
  vmpool = ::OpenNebula::VirtualMachinePool.new(client)
  if filter[:id].nil?
    vmpool.info!(-2,-1,-1,-1)
  elsif filter[:id]
    filter[:id] = filter[:id].to_i if filter[:id].is_a?(String)
    vmpool.info!(-2, filter[:id], filter[:id], -1)
  end # filter[:id].nil?

  vmpool.each do |vm|
    one = vm.to_hash
    data = {}
    data["onevm_object"] = vm
    data["status"] =  vm.state
    data["state"]  =  vm.lcm_state_str
    data["id"]     =  vm.id
    data["gid"]    =  vm.gid
    data["uuid"]   =  vm.id
    data["name"]   =  one["VM"]["NAME"] unless one["VM"]["NAME"].nil?
    data["user"]   =  one["VM"]["UNAME"] unless one["VM"]["UNAME"].nil?
    data["group"]  =  one["VM"]["GNAME"] unless one["VM"]["GNAME"].nil?

    unless ( one["VM"]["TEMPLATE"].nil? ) then
      data["cpu"]    =  one["VM"]["TEMPLATE"]["VCPU"] unless one["VM"]["TEMPLATE"]["VCPU"].nil?
      data["memory"] =  one["VM"]["TEMPLATE"]["MEMORY"] unless one["VM"]["TEMPLATE"]["MEMORY"].nil?
      unless (one["VM"]["TEMPLATE"]["NIC"].nil?) then
        if one["VM"]["TEMPLATE"]["NIC"].is_a?(Array)
          data["ip"]=one["VM"]["TEMPLATE"]["NIC"][0]["IP"]
          data["mac"]=one["VM"]["TEMPLATE"]["NIC"][0]["MAC"]
        else
          data["ip"]=one["VM"]["TEMPLATE"]["NIC"]["IP"] unless one["VM"]["TEMPLATE"]["NIC"]["IP"].nil?
          data["mac"]=one["VM"]["TEMPLATE"]["NIC"]["MAC"] unless one["VM"]["TEMPLATE"]["NIC"]["MAC"].nil?
        end
      end # unless (one["VM"]["TEMPLATE"]["NIC"].nil?) then
    end # unless ( one["VM"]["TEMPLATE"].nil? ) then 

    vms << data
  end # vmpool.each
  vms
end

#network_to_attributes(net) ⇒ Object



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/fog/opennebula/requests/compute/list_networks.rb', line 25

def network_to_attributes(net)
  return if net.nil?
	  #{"VNET"=>{"ID"=>"155", "UID"=>"0", "GID"=>"1", "UNAME"=>"oneadmin", "GNAME"=>"users", "NAME"=>"vlan99-2", "PERMISSIONS"=>{"OWNER_U"=>"1", "OWNER_M"=>"1", "OWNER_A"=>"0", "GROUP_U"=>"1", "GROUP_M"=>"0", "GROUP_A"=>"0", "OTHER_U"=>"0", "OTHER_M"=>"0", "OTHER_A"=>"0"}, "CLUSTER_ID"=>"-1", "CLUSTER"=>{}, "TYPE"=>"0", "BRIDGE"=>"ovsbr", "VLAN"=>"1", "PHYDEV"=>{}, "VLAN_ID"=>"99", "GLOBAL_PREFIX"=>{}, "SITE_PREFIX"=>{}, "RANGE"=>{"IP_START"=>"10.10.99.127", "IP_END"=>"10.10.99.249"}, "TOTAL_LEASES"=>"0", "TEMPLATE"=>{"DESCRIPTION"=>"test", "NETWORK_ADDRESS"=>"10.10.99.0", "NETWORK_MASK"=>"255.255.255.0"}}}
  h = {
    :id    	 => net["VNET"]["ID"],
    :name  	 => net["VNET"]["NAME"],
	    :uid   	 => net["VNET"]["UID"],
	    :gid   	 => net["VNET"]["GID"],
  }

	  h[:description] = net["VNET"]["TEMPLATE"]["DESCRIPTION"] unless net["VNET"]["TEMPLATE"]["DESCRIPTION"].nil?
	  h[:vlan] 	  = net["VNET"]["VLAN_ID"] unless (net["VNET"]["VLAN_ID"].nil? || net["VNET"]["VLAN_ID"].empty?)

	  return h
end

#startvnc(onevm_object, vnc) ⇒ Object

def get_vnc_console



40
41
42
# File 'lib/fog/opennebula/requests/compute/get_vnc_console.rb', line 40

def startvnc(onevm_object, vnc)
    return vnc.proxy(onevm_object)
end

#template_pool(filter = { }) ⇒ Object

Raises:

  • (Fog::Compute::OpenNebula::NotFound)


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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/fog/opennebula/requests/compute/template_pool.rb', line 30

def template_pool(filter = { })

  templates = ::OpenNebula::TemplatePool.new(client)
  if filter[:id].nil?
    templates.info!(-2,-1,-1)
  elsif filter[:id]
    filter[:id] = filter[:id].to_i if filter[:id].is_a?(String)
    templates.info!(-2, filter[:id], filter[:id])
  end # if filter[:id].nil?

  templates = templates.map do |t| 
    # filtering by name
    # done here, because OpenNebula:TemplatePool does not support something like .delete_if
    if filter[:name] && filter[:name].is_a?(String) && !filter[:name].empty?
        next if t.to_hash["VMTEMPLATE"]["NAME"] != filter[:name]
    end

    h = Hash[
      :id => t.to_hash["VMTEMPLATE"]["ID"], 
      :name => t.to_hash["VMTEMPLATE"]["NAME"], 
      :content => t.template_str
    ]
    h.merge! t.to_hash["VMTEMPLATE"]["TEMPLATE"]

    # h["NIC"] has to be an array of nic objects
    nics = h["NIC"] unless h["NIC"].nil?
    h["NIC"] = [] # reset nics to a array
    if nics.is_a? Array
      nics.each do |n|
        n["model"] = "virtio" if n["model"].nil?
        n["uuid"] = "0" if n["uuid"].nil? # is it better is to remove this NIC?
        h["NIC"] << interfaces.new({ :vnet => networks.get(n["uuid"]), :model => n["model"]})
      end
    elsif nics.is_a? Hash
      nics["model"] = "virtio" if nics["model"].nil?
      nics["uuid"] = "0" if nics["uuid"].nil? # is it better is to remove this NIC?
      h["NIC"] << interfaces.new({ :vnet => networks.get(nics["uuid"]), :model => nics["model"]})
    else
      # should i break?
    end

  
    # every key should be lowercase
    ret_hash = {}
    h.each_pair do |k,v| 
      ret_hash.merge!({k.downcase => v}) 
    end
    ret_hash
  end
  
  templates.delete nil
  raise Fog::Compute::OpenNebula::NotFound, "Flavor/Template not found" if templates.empty?
  templates
end

#vm_allocate(attr = { }) ⇒ Object



5
6
7
8
9
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
60
61
62
63
64
65
66
# File 'lib/fog/opennebula/requests/compute/vm_allocate.rb', line 5

def vm_allocate(attr={ })

  if(attr[:flavor].nil?)
    raise(ArgumentError.new("Attribute flavor is nil! #{attr.inspect}"))
  end
  if(attr[:name].nil? || attr[:name].empty?)
    raise(ArgumentError.new("Attribute name is nil or empty! #{attr.inspect}"))
  end

  xml = ::OpenNebula::VirtualMachine.build_xml
  vm  = ::OpenNebula::VirtualMachine.new(xml, client)
  rc = vm.allocate(attr[:flavor].to_s + "\nNAME=" + attr[:name])

  # irb(main):050:0> vm.allocate(s.flavor.to_s + "\nNAME=altest5")
  # => #<OpenNebula::Error:0x00000002a50760 @message="[VirtualMachineAllocate] User [42] : Not authorized to perform CREATE VM.", @errno=512>
  # irb(main):051:0> a = vm.allocate(s.flavor.to_s + "\nNAME=altest5")
  # => #<OpenNebula::Error:0x00000002ac0998 @message="[VirtualMachineAllocate] User [42] : Not authorized to perform CREATE VM.", @errno=512>
  # irb(main):052:0> a.class

  if(rc.is_a? ::OpenNebula::Error) 
    raise(rc)
  end


  # -1 - do not change the owner
  vm.chown(-1,attr[:gid].to_i) unless attr[:gid].nil?

  # TODO
  # check if vm is created vmid.class == One error class
  vm.info!

  one = vm.to_hash
  data = {}
  data["onevm_object"] = vm
  data["status"] =  vm.state
  data["state"]  =  vm.lcm_state_str
  data["id"]     =  vm.id
  data["uuid"]   =  vm.id
  data["gid"]    =  vm.gid
  data["name"]   =  one["VM"]["NAME"] unless one["VM"]["NAME"].nil?
  data["user"]   =  one["VM"]["UNAME"] unless one["VM"]["UNAME"].nil?
  data["group"]  =  one["VM"]["GNAME"] unless one["VM"]["GNAME"].nil?

  unless ( one["VM"]["TEMPLATE"].nil? ) then
    temp = one["VM"]["TEMPLATE"]
    data["cpu"]    =  temp["VCPU"] 	unless temp["VCPU"].nil?
    data["memory"] =  temp["MEMORY"] 	unless temp["MEMORY"].nil?
    unless (temp["NIC"].nil?) then
      if one["VM"]["TEMPLATE"]["NIC"].is_a?(Array)
        data["mac"]	=	temp["NIC"][0]["MAC"] 	unless temp["NIC"][0]["MAC"].nil?
        data["ip"]	=	temp["NIC"][0]["IP"] 	unless temp["NIC"][0]["IP"].nil?
      else
        data["mac"]	=	temp["NIC"]["MAC"] 	unless temp["NIC"]["MAC"].nil?
        data["ip"]	=	temp["NIC"]["IP"] 	unless temp["NIC"]["IP"].nil?
      end
    end
  end

  data
rescue => err
  raise(err)
end

#vm_destroy(id) ⇒ Object



5
6
7
8
9
10
11
12
13
# File 'lib/fog/opennebula/requests/compute/vm_destroy.rb', line 5

def vm_destroy(id)
  vmpool = ::OpenNebula::VirtualMachinePool.new(client)
	  vmpool.info!(-2,id,id,-1)
  
	  vmpool.each do |vm|
    # true => delete and recreate vm
	    vm.delete(false)
  end
end

#vm_resume(id) ⇒ Object



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/fog/opennebula/requests/compute/vm_resume.rb', line 6

def vm_resume(id)

  vmpool = ::OpenNebula::VirtualMachinePool.new(client)
	  vmpool.info!(-2,id,id,-1)
	  puts "#{vmpool.entries.class} #{vmpool.entries.methods}"
	  puts "#{vmpool.entries.inspect} #{vmpool.entries.methods}"
  
	  vmpool.each do |vm|
	    vm.resume
  end
  ##if(attr[:id].nil?) 
	  ##  raise(ArgumentError.new("Attribute :id is nil or empty! #{attr.inspect}"))
	  ##end

  #vmpool = ::OpenNebula::VirtualMachinePool.new(client)
	  #vmpool.info!

	  #vmpool.each do |vm|
	  #        if vm.id == id then
	  #      	  vm.resume
	  #      	  return true
	  #        end
	  #end
	  #false
end

#vm_stop(id) ⇒ Object



6
7
8
9
10
11
12
13
14
15
# File 'lib/fog/opennebula/requests/compute/vm_stop.rb', line 6

def vm_stop(id)
  vmpool = ::OpenNebula::VirtualMachinePool.new(client)
	  vmpool.info!(-2,id,id,-1)
	  puts "#{vmpool.entries.class} #{vmpool.entries.methods}"
	  puts "#{vmpool.entries.inspect} #{vmpool.entries.methods}"
  
	  vmpool.each do |vm|
	    vm.stop
  end
end