Пример #1
0
def get_image_architecture(uid, context):
    """
    Extract architecture from either:
    - image name, title or metadata. The architecture is sometimes
      encoded in the image's name
    - db::glance::image_properties could be used reliably so long as the
      information is supplied when registering an image with glance.
    - else return a default of x86

    uid -- id of the instance!
    context -- The os context.
    """
    instance = vm.get_vm(uid, context)

    arch = ""
    uid = instance["image_ref"]
    img = IMAGE_API.show(context, uid)
    img_properties = img["properties"]
    if "arch" in img_properties:
        arch = img["properties"]["arch"]
    elif "architecture" in img_properties:
        arch = img["properties"]["architecture"]

    if arch == "":
        # if all attempts fail set it to a default value
        arch = "x86"
    return arch
Пример #2
0
    def retrieve(self, entity, extras):
        """
        Retrieve a VM.
        """
        uid = entity.attributes['occi.core.id']
        context = extras['nova_ctx']
        instance = vm.get_vm(uid, context)

        LOG.debug('Retrieving an Virtual machine: ', uid)

        # set state and applicable actions!
        state, actions = vm.get_occi_state(uid, context)
        entity.attributes['occi.compute.state'] = state
        entity.actions = actions

        # set up to date attributes
        entity.attributes['occi.compute.cores'] = str(instance['vcpus'])
        value = str(float(instance['memory_mb']) / 1024)
        entity.attributes['occi.compute.memory'] = value

        #Now we have the instance state, get its updated network info
        net_info = net.get_adapter_info(uid, context)
        attach_to_default_network(net_info, entity, extras)

        # add consoles and storage links
        set_console_info(entity, uid, extras)
Пример #3
0
def remove_floating_ip(uid, address, context):
    """
    Remove a given address from an VM instance.

    uid -- Id of the VM.
    address -- The ip address.
    context -- The os context.
    """
    # TODO: check exception handling!
    vm_instance = vm.get_vm(uid, context)

    NETWORK_API.disassociate_floating_ip(context, vm_instance, address)
    NETWORK_API.release_floating_ip(context, address)
Пример #4
0
def add_flaoting_ip_to_vm(uid, attributes, context):
    """
    Adds an ip to an VM instance.

    uid -- id of the VM.
    attributes -- the call attributes (dict)
    context -- The os context.
    """
    vm_instance = vm.get_vm(uid, context)

    cached_nwinfo = compute_utils.get_nw_info_for_instance(vm_instance)
    if not cached_nwinfo:
        raise AttributeError('No nw_info cache associated with instance')

    fixed_ips = cached_nwinfo.fixed_ips()
    if not fixed_ips:
        raise AttributeError('No fixed ips associated to instance')

    if 'org.openstack.network.floating.pool' not in attributes:
        pool = None
    else:
        pool = attributes['org.openstack.network.floating.pool']

    float_address = NETWORK_API.allocate_floating_ip(context, pool)

    if len(fixed_ips) > 1:
        LOG.warn('multiple fixed_ips exist, using the first')

    try:
        address = fixed_ips[0]['address']
        NETWORK_API.associate_floating_ip(context, vm_instance,
                                          floating_address=float_address,
                                          fixed_address=address)
    except exception.FloatingIpAssociated:
        msg = 'floating ip is already associated'
        raise AttributeError(msg)
    except exception.NoFloatingIpInterface:
        msg = 'l3driver call to add floating ip failed'
        raise AttributeError(msg)
    except Exception as error:
        msg = 'Error. Unable to associate floating ip' + str(error)
        raise AttributeError(msg)
    return float_address
Пример #5
0
def get_adapter_info(uid, context):
    """
    Extracts the VMs network adapter information: interface name,
    IP address, gateway and mac address.

    uid -- Id of the VM.
    context -- The os context.
    """
    vm_instance = vm.get_vm(uid, context)

    # TODO(dizz): currently this assumes one adapter on the VM.
    # It's likely that this will not be the case when using Quantum

    vm_net_info = {'vm_iface': '', 'address': '', 'gateway': '', 'mac': '',
                   'allocation': ''}

    temp = NETWORK_API.get_instance_nw_info(context, vm_instance)

    # catches an odd error whereby no network info is returned back
    if len(temp) <= 0:
        LOG.warn('No network info was returned either live or cached.')
        return vm_net_info

    vm_net_info['vm_iface'] = temp[0]['network']['meta']['bridge_interface']

    # OS-specific if a VM is stopped it has no IP address
    if len(temp[0]['network']['subnets'][0]['ips']) > 0:
        adr = temp[0]['network']['subnets'][0]['ips'][0]['address']
        vm_net_info['address'] = adr
    else:
        vm_net_info['address'] = ''
    gateway = temp[0]['network']['subnets'][0]['gateway']['address']
    vm_net_info['gateway'] = gateway
    vm_net_info['mac'] = temp[0]['address']
    if temp[0]['network']['subnets'][0]['ips'][0]['type'] == 'fixed':
        vm_net_info['allocation'] = 'static'
    else:
        vm_net_info['allocation'] = 'dynamic'
    return vm_net_info