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

Inherits:
Object
  • Object
show all
Includes:
Collections
Defined in:
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/get_vnc_console.rb,
lib/fog/opennebula/requests/compute/vm_disk_snapshot.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Real

Returns a new instance of Real.



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

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

Instance Attribute Details

#clientObject (readonly)

Returns the value of attribute client.



122
123
124
# File 'lib/fog/opennebula/compute.rb', line 122

def client
  @client
end

Instance Method Details

#get_vnc_console(_server_id, _console_type, onevm_object) ⇒ Object



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

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



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

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

  unless 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



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

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.to_s] && group[k.to_s.upcase.to_s] != 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



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

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

  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



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

def list_vms(filter = {})
  vms = []
  vmpool = ::OpenNebula::VirtualMachinePool.new(client)
  if filter[:id].nil?
    vmpool.info_search(:extended => true)
  elsif filter[:id]
    filter[:id] = filter[:id].to_i if filter[:id].is_a?(String)
    vmpool.info_search(:start_id => filter[:id], :end_id => filter[:id], :extended => true)
  end

  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?
      data['cpu']    =  one['VM']['TEMPLATE']['CPU'] unless one['VM']['TEMPLATE']['CPU'].nil?
      data['vcpu']   =  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?
        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
    end

    vms << data
  end
  vms
end

#network_to_attributes(net) ⇒ Object



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

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?

  h
end

#startvnc(onevm_object, vnc) ⇒ Object



48
49
50
# File 'lib/fog/opennebula/requests/compute/get_vnc_console.rb', line 48

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

#template_pool(filter = {}) ⇒ Object

Raises:

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


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
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/fog/opennebula/requests/compute/template_pool.rb', line 9

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

  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
    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



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

def vm_allocate(attr = {})
  if attr[:flavor].nil?
    raise ArgumentError, "Attribute flavor is nil! #{attr.inspect}"
  end
  if attr[:name].nil? || attr[:name].empty?
    raise ArgumentError, "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] + '"')

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

  # -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?
    temp = one['VM']['TEMPLATE']
    data['cpu']    =  temp['CPU']	unless temp['CPU'].nil?
    data['vcpu']   =  temp['VCPU']	unless temp['VCPU'].nil?
    data['memory'] =  temp['MEMORY']	unless temp['MEMORY'].nil?
    unless temp['NIC'].nil?
      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 StandardError => err
  raise(err)
end

#vm_destroy(id) ⇒ Object



9
10
11
12
13
14
15
16
17
# File 'lib/fog/opennebula/requests/compute/vm_destroy.rb', line 9

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



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

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)
    raise(rc) if rc.is_a? ::OpenNebula::Error
  end
  rc
end

#vm_resume(id) ⇒ Object



9
10
11
12
13
14
# File 'lib/fog/opennebula/requests/compute/vm_resume.rb', line 9

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

  vmpool.each(&:resume)
end

#vm_shutdown(id) ⇒ Object



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

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

  vmpool.each(&:shutdown)
end

#vm_stop(id) ⇒ Object



9
10
11
12
13
# File 'lib/fog/opennebula/requests/compute/vm_stop.rb', line 9

def vm_stop(id)
  vmpool = ::OpenNebula::VirtualMachinePool.new(client)
  vmpool.info(-2, id, id, -1)
  vmpool.each(&:poweroff)
end

#vm_suspend(id) ⇒ Object



9
10
11
12
13
14
# File 'lib/fog/opennebula/requests/compute/vm_suspend.rb', line 9

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

  vmpool.each(&:suspend)
end