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/image_pool.rb,
lib/fog/opennebula/requests/compute/vm_destroy.rb,
lib/fog/opennebula/requests/compute/vm_suspend.rb,
lib/fog/opennebula/requests/compute/list_groups.rb,
lib/fog/opennebula/requests/compute/vm_allocate.rb,
lib/fog/opennebula/requests/compute/vm_shutdown.rb,
lib/fog/opennebula/requests/compute/list_networks.rb,
lib/fog/opennebula/requests/compute/template_pool.rb,
lib/fog/opennebula/requests/compute/vm_disk_snapshot.rb

Overview

class Real

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Real

Returns a new instance of Real.



129
130
131
132
# File 'lib/fog/opennebula/compute.rb', line 129

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

Instance Method Details

#clientObject



125
126
127
# File 'lib/fog/opennebula/compute.rb', line 125

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

#image_pool(filter = { }) ⇒ Object



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

def image_pool(filter = { })

  images = ::OpenNebula::ImagePool.new(client)
  if filter[:mine].nil?
    images.info!
  else
    images.info_mine!
  end # if filter[:mine].nil?

  if not filter[:id].nil?
    images.each do |i|
      if filter[:id] == i.id
        return [ i ] # return an array with only one element - found image
      end
    end
  end
  images
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
24
25
26
27
28
29
30
31
# 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| 
    if filter[:network] && filter[:network].is_a?(String) && !filter[:network].empty?
      next if network.to_hash["VNET"]["NAME"] != filter[:network]
    end
    if filter[:network_uname] && filter[:network_uname].is_a?(String) && !filter[:network_uname].empty?
      next if network.to_hash["VNET"]["UNAME"] != filter[:network_uname]
    end
    if filter[:network_uid] && filter[:network_uid].is_a?(String) && !filter[:network_uid].empty?
      next if network.to_hash["VNET"]["UID"] != filter[:network_uid]
    end
    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



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/fog/opennebula/requests/compute/list_networks.rb', line 33

def network_to_attributes(net)
  return if net.nil?
  h = {
    :id    => net["VNET"]["ID"],
    :name  => net["VNET"]["NAME"],
    :uid   => net["VNET"]["UID"],
    :uname => net["VNET"]["UNAME"],
    :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)


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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
# File 'lib/fog/opennebula/requests/compute/template_pool.rb', line 33

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
    if filter[:uname] && filter[:uname].is_a?(String) && !filter[:uname].empty?
      next if t.to_hash["VMTEMPLATE"]["UNAME"] != filter[:uname]
    end
    if filter[:uid] && filter[:uid].is_a?(String) && !filter[:uid].empty?
      next if t.to_hash["VMTEMPLATE"]["UID"] != filter[:uid]
    end

    h = Hash[
      :id => t.to_hash["VMTEMPLATE"]["ID"],
      :name => t.to_hash["VMTEMPLATE"]["NAME"],
      :content => t.template_str,
      :USER_VARIABLES => "" # Default if not set in template
    ]
    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|
        if n["NETWORK_ID"]
          vnet = networks.get(n["NETWORK_ID"].to_s)
        elsif n["NETWORK"]
          vnet = networks.get_by_name(n["NETWORK"].to_s)
        else
          next
        end
        h["NIC"] << interfaces.new({ :vnet => vnet, :model => n["MODEL"] || "virtio" })
      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?
      n = networks.get_by_filter({
        :id => nics["NETWORK_ID"],
        :network => nics["NETWORK"],
        :network_uname => nics["NETWORK_UNAME"],
        :network_uid => nics["NETWORK_UID"]
      })
      n.each do |i|
        h["NIC"] << interfaces.new({ :vnet => i })
      end
    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_disk_snapshot(id, disk_id, image_name) ⇒ Object



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

def vm_disk_snapshot(id, disk_id, image_name)
  vmpool = ::OpenNebula::VirtualMachinePool.new(client)
  vmpool.info!(-2,id,id,-1)

  rc = 0
  vmpool.each do |vm|
    rc = vm.disk_snapshot_create(disk_id, image_name)
    if(rc.is_a? ::OpenNebula::Error)
      raise(rc)
    end
  end
  rc
end

#vm_resume(id) ⇒ Object



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

def vm_resume(id)
  vmpool = ::OpenNebula::VirtualMachinePool.new(client)
 vmpool.info!(-2,id,id,-1)

  vmpool.each do |vm|
    vm.resume
  end
end

#vm_shutdown(id) ⇒ Object



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

def vm_shutdown(id)
  vmpool = ::OpenNebula::VirtualMachinePool.new(client)
  vmpool.info!(-2,id,id,-1)

  vmpool.each do |vm|
    vm.shutdown
  end
end

#vm_stop(id) ⇒ Object



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

def vm_stop(id)
  vmpool = ::OpenNebula::VirtualMachinePool.new(client)
  vmpool.info!(-2,id,id,-1)
  vmpool.each do |vm|
    vm.stop
  end
end

#vm_suspend(id) ⇒ Object



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

def vm_suspend(id)
  vmpool = ::OpenNebula::VirtualMachinePool.new(client)
 vmpool.info!(-2,id,id,-1)

  vmpool.each do |vm|
    vm.suspend
  end
end