module Backends::Opennebula::Helpers::ComputeCreateHelper

Constants

COMPUTE_BASE64_REGEXP
COMPUTE_DN_BASED_AUTHS
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 10
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)

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

  # update template
  compute_create_set_attrs(compute, template)
  compute_create_check_context(compute)
  compute_create_add_context(compute, template)
  compute_create_add_description(compute, template)
  compute_create_add_custom_template_vars(compute, template)

  # 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')

  # convert template structure to a pure String
  template = template.template_str

  # add inline links
  template = compute_create_add_inline_links(compute, template)

  @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