module Backends::Opennebula::Helpers::ComputeParseHelper

Constants

CONTEXTUALIZATION_ATTR_KEY
CONTEXTUALIZATION_ATTR_UD
CONTEXTUALIZATION_MIXIN_KEY
CONTEXTUALIZATION_MIXIN_UD
NETWORK_GENERATED_PREFIX
STORAGE_GENERATED_PREFIX

Public Instance Methods

compute_parse_backend_obj(backend_compute) click to toggle source
# File lib/backends/opennebula/helpers/compute_parse_helper.rb, line 12
def compute_parse_backend_obj(backend_compute)
  compute = Occi::Infrastructure::Compute.new

  # include some basic mixins
  compute.mixins << 'http://opennebula.org/occi/infrastructure#compute'

  # include mixins stored in ON's VM template
  unless backend_compute['USER_TEMPLATE/OCCI_COMPUTE_MIXINS'].blank?
    backend_compute_mixins = backend_compute['USER_TEMPLATE/OCCI_COMPUTE_MIXINS'].split(' ')
    backend_compute_mixins.each do |mixin|
      compute.mixins << mixin unless mixin.blank?
    end
  end

  if backend_compute['TEMPLATE/CONTEXT']
    if backend_compute['TEMPLATE/CONTEXT/SSH_PUBLIC_KEY'] || backend_compute['TEMPLATE/CONTEXT/SSH_KEY']
      compute.mixins << CONTEXTUALIZATION_MIXIN_KEY
    end

    if backend_compute['TEMPLATE/CONTEXT/USER_DATA']
      compute.mixins << CONTEXTUALIZATION_MIXIN_UD
    end
  end

  # include basic OCCI attributes
  basic_attrs = compute_parse_basic_attrs(backend_compute)
  compute.attributes.merge! basic_attrs

  # include ONE-specific attributes
  one_attrs = compute_parse_one_attrs(backend_compute)
  compute.attributes.merge! one_attrs

  # include contextualization attributes
  context_attrs = compute_parse_context_attrs(backend_compute)
  compute.attributes.merge! context_attrs

  # include state information and available actions
  result = compute_parse_state(backend_compute)
  compute.state = result.state
  result.actions.each { |a| compute.actions << a }

  # include storage and network links
  result = compute_parse_links(backend_compute, compute)
  result.each { |link| compute.links << link }

  compute
end
compute_parse_basic_attrs(backend_compute) click to toggle source
# File lib/backends/opennebula/helpers/compute_parse_helper.rb, line 60
def compute_parse_basic_attrs(backend_compute)
  compute_attrs = Occi::Core::Attributes.new

  compute_attrs['occi.core.id']    = backend_compute['ID']
  compute_attrs['occi.core.title'] = backend_compute['NAME']
  compute_attrs['occi.core.summary'] = backend_compute['USER_TEMPLATE/DESCRIPTION'] unless backend_compute['USER_TEMPLATE/DESCRIPTION'].blank?

  compute_attrs['occi.compute.cores'] = (backend_compute['TEMPLATE/VCPU'] || 1).to_i
  compute_attrs['occi.compute.memory'] = (backend_compute['TEMPLATE/MEMORY'].to_f / 1024)
  compute_attrs['occi.compute.speed'] = ((backend_compute['TEMPLATE/CPU'] || 1).to_f / compute_attrs['occi.compute.cores'])

  compute_attrs['occi.compute.architecture'] = 'x64' if backend_compute['TEMPLATE/OS/ARCH'] == 'x86_64'
  compute_attrs['occi.compute.architecture'] = 'x86' if backend_compute['TEMPLATE/OS/ARCH'] == 'i686'

  compute_attrs
end
compute_parse_context_attrs(backend_compute) click to toggle source
# File lib/backends/opennebula/helpers/compute_parse_helper.rb, line 92
def compute_parse_context_attrs(backend_compute)
  context_attrs = Occi::Core::Attributes.new

  if backend_compute['TEMPLATE/CONTEXT']
    context_attrs[CONTEXTUALIZATION_ATTR_KEY] = backend_compute['TEMPLATE/CONTEXT/SSH_PUBLIC_KEY'] || backend_compute['TEMPLATE/CONTEXT/SSH_KEY']

    # re-encode cloud-init configuration files as Base64
    context_attrs[CONTEXTUALIZATION_ATTR_UD] = if backend_compute['TEMPLATE/CONTEXT/USER_DATA'] && backend_compute['TEMPLATE/CONTEXT/USER_DATA'].match(/^\s*#cloud-config\s*$/)
      Base64.strict_encode64(backend_compute['TEMPLATE/CONTEXT/USER_DATA'])
    else
      backend_compute['TEMPLATE/CONTEXT/USER_DATA']
    end
  end

  context_attrs
end
compute_parse_one_attrs(backend_compute) click to toggle source
# File lib/backends/opennebula/helpers/compute_parse_helper.rb, line 77
def compute_parse_one_attrs(backend_compute)
  compute_attrs = Occi::Core::Attributes.new

  compute_attrs['org.opennebula.compute.id'] = backend_compute['ID']
  compute_attrs['org.opennebula.compute.cpu'] = backend_compute['TEMPLATE/CPU'].to_f if backend_compute['TEMPLATE/CPU']
  compute_attrs['org.opennebula.compute.kernel'] = backend_compute['TEMPLATE/OS/KERNEL'] if backend_compute['TEMPLATE/OS/KERNEL']
  compute_attrs['org.opennebula.compute.initrd'] = backend_compute['TEMPLATE/OS/INITRD'] if backend_compute['TEMPLATE/OS/INITRD']
  compute_attrs['org.opennebula.compute.root'] = backend_compute['TEMPLATE/OS/ROOT'] if backend_compute['TEMPLATE/OS/ROOT']
  compute_attrs['org.opennebula.compute.kernel_cmd'] = backend_compute['TEMPLATE/OS/KERNEL_CMD'] if backend_compute['TEMPLATE/OS/KERNEL_CMD']
  compute_attrs['org.opennebula.compute.bootloader'] = backend_compute['TEMPLATE/OS/BOOTLOADER'] if backend_compute['TEMPLATE/OS/BOOTLOADER']
  compute_attrs['org.opennebula.compute.boot'] = backend_compute['TEMPLATE/OS/BOOT'] if backend_compute['TEMPLATE/OS/BOOT']

  compute_attrs
end
compute_parse_state(backend_compute) click to toggle source
# File lib/backends/opennebula/helpers/compute_parse_helper.rb, line 109
def compute_parse_state(backend_compute)
  result = Hashie::Mash.new

  # In ON 4.4:
  #    VM_STATE=%w{INIT PENDING HOLD ACTIVE STOPPED SUSPENDED DONE FAILED
  #       POWEROFF UNDEPLOYED}
  #
  #    LCM_STATE=%w{LCM_INIT PROLOG BOOT RUNNING MIGRATE SAVE_STOP SAVE_SUSPEND
  #        SAVE_MIGRATE PROLOG_MIGRATE PROLOG_RESUME EPILOG_STOP EPILOG
  #        SHUTDOWN CANCEL FAILURE CLEANUP_RESUBMIT UNKNOWN HOTPLUG SHUTDOWN_POWEROFF
  #        BOOT_UNKNOWN BOOT_POWEROFF BOOT_SUSPENDED BOOT_STOPPED CLEANUP_DELETE
  #        HOTPLUG_SNAPSHOT HOTPLUG_NIC HOTPLUG_SAVEAS HOTPLUG_SAVEAS_POWEROFF
  #        HOTPLUG_SAVEAS_SUSPENDED SHUTDOWN_UNDEPLOY EPILOG_UNDEPLOY
  #        PROLOG_UNDEPLOY BOOT_UNDEPLOY}
  #
  case backend_compute.state_str
  when 'ACTIVE'
    # ACTIVE is a very broad term, look at lcm_state_str too
    if backend_compute.lcm_state_str == 'RUNNING'
      result.state = 'active'
      result.actions = %whttp://schemas.ogf.org/occi/infrastructure/compute/action#stop http://schemas.ogf.org/occi/infrastructure/compute/action#restart http://schemas.ogf.org/occi/infrastructure/compute/action#suspend|
    else
      result.state = 'inactive'
      result.actions = []
    end
  when 'FAILED'
    result.state = 'error'
    result.actions = %whttp://schemas.ogf.org/occi/infrastructure/compute/action#restart|
  when 'STOPPED', 'SUSPENDED', 'POWEROFF'
    result.state = 'suspended'
    result.actions = %whttp://schemas.ogf.org/occi/infrastructure/compute/action#start|
  when 'PENDING'
    result.state = 'waiting'
    result.actions = []
  else
    result.state = 'inactive'
    result.actions = []
  end

  result
end