module Backends::Opennebula::Helpers::ComputeCreateHelper

Constants

COMPUTE_BASE64_REGEXP
COMPUTE_SSH_REGEXP
COMPUTE_USER_DATA_SIZE_LIMIT

Public Instance Methods

compute_create_with_os_tpl(compute) click to toggle source
# File lib/backends/opennebula/helpers/compute_create_helper.rb, line 9
def compute_create_with_os_tpl(compute)
  @logger.debug "[Backends] [OpennebulaBackend] Deploying #{compute.inspect}"

  # include some basic mixins
  # WARNING: adding mix-ins will re-set their attributes
  attr_backup = Occi::Core::Attributes.new(compute.attributes)
  compute.mixins << 'http://opennebula.org/occi/infrastructure#compute'
  compute.attributes = attr_backup

  os_tpl_mixins = compute.mixins.get_related_to(Occi::Infrastructure::OsTpl.mixin.type_identifier)
  os_tpl = os_tpl_mixins.first

  @logger.debug "[Backends] [OpennebulaBackend] Deploying with OS template: #{os_tpl.term}"
  os_tpl = os_tpl_list_term_to_id(os_tpl.term)

  template_alloc = ::OpenNebula::Template.build_xml(os_tpl)
  template = ::OpenNebula::Template.new(template_alloc, @client)
  rc = template.info
  check_retval(rc, Backends::Errors::ResourceRetrievalError)

  template.delete_element('TEMPLATE/NAME')
  template.add_element('TEMPLATE',  'NAME' => compute.title)

  if compute.cores
    template.delete_element('TEMPLATE/VCPU')
    template.add_element('TEMPLATE',  'VCPU' => compute.cores.to_i)
  end

  if compute.memory
    memory = compute.memory.to_f * 1024
    template.delete_element('TEMPLATE/MEMORY')
    template.add_element('TEMPLATE',  'MEMORY' => memory.to_i)
  end

  if compute.architecture
    template.delete_element('TEMPLATE/ARCHITECTURE')
    template.add_element('TEMPLATE',  'ARCHITECTURE' => compute.architecture)
  end

  if compute.speed
    calc_speed = compute.speed.to_f * (template['TEMPLATE/VCPU'] || 1).to_i
    template.delete_element('TEMPLATE/CPU')
    template.add_element('TEMPLATE',  'CPU' => calc_speed)
  end

  compute_create_check_context(compute)
  compute_create_add_context(compute, template)
  compute_create_add_description(compute, template)

  mixins = compute.mixins.to_a.map { |m| m.type_identifier }
  template.add_element('TEMPLATE',  'OCCI_COMPUTE_MIXINS' => mixins.join(' '))

  # remove template-specific values
  template.delete_element('ID')
  template.delete_element('UID')
  template.delete_element('GID')
  template.delete_element('UNAME')
  template.delete_element('GNAME')
  template.delete_element('REGTIME')
  template.delete_element('PERMISSIONS')
  template.delete_element('TEMPLATE/TEMPLATE_ID')

  template = template.template_str
  @logger.debug "[Backends] [OpennebulaBackend] Template #{template.inspect}"

  vm_alloc = ::OpenNebula::VirtualMachine.build_xml
  backend_object = ::OpenNebula::VirtualMachine.new(vm_alloc, @client)

  rc = backend_object.allocate(template)
  check_retval(rc, Backends::Errors::ResourceCreationError)

  rc = backend_object.info
  check_retval(rc, Backends::Errors::ResourceRetrievalError)

  compute_id = backend_object['ID']
  rc = backend_object.update("OCCI_ID=\"#{compute_id}\"", true)
  check_retval(rc, Backends::Errors::ResourceActionError)

  compute_id
end