示例#1
0
 def get_resource(self, key, extras):
     """
     Retrieve one resource
     """
     context = extras['nova_ctx']
     try:
         (loc, identifier) = key.rsplit('/', 1)
     except ValueError:
         raise AttributeError("Unexpected format for key %s" % key)
     loc = loc + '/'
     LOG.debug("Getting resource at %s with id: %s", loc, identifier)
     try:
         # XXX should use regular expressions?
         if loc == infrastructure.COMPUTE.location:
             compute_vm = vm.get_vm(identifier, context)
             return self._construct_occi_compute(compute_vm, extras)
         elif loc == infrastructure.STORAGE.location:
             vol = storage.get_storage(identifier, context)
             return self._construct_occi_storage(vol, extras)
         elif loc in [infrastructure.STORAGELINK.location,
                      infrastructure.NETWORKINTERFACE.location]:
             (compute_id, other_id) = identifier.split('_', 1)
             compute_vm = vm.get_vm(compute_id, context)
             occi_vm = self._construct_occi_compute(compute_vm, extras)
             # look for the link
             for link in occi_vm.links:
                 if link.identifier == key:
                     return link
         elif loc == infrastructure.NETWORK.location:
             return self.nets[identifier]
     except exceptions.HTTPError:
         # the nova_glue did not find the resource, just ignore
         pass
     # not found!
     raise KeyError(key)
示例#2
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
示例#3
0
def add_floating_ip(uid, pool_name, context):
    """
    Adds an ip to an VM instance.

    uid -- id of the VM.
    pool_name -- name of the pool
    context -- The os context.
    """
    vm_instance = vm.get_vm(uid, context)

    cached_nwinfo = 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')

    float_address = NETWORK_API.allocate_floating_ip(context, pool_name)

    try:
        address = fixed_ips[0]['address']
        NETWORK_API.associate_floating_ip(context, vm_instance, float_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)
    return float_address
示例#4
0
文件: net.py 项目: EGI-FCTF/occi-os
def add_floating_ip(uid, pool_name, context):
    """
    Adds an ip to an VM instance.

    uid -- id of the VM.
    pool_name -- name of the pool
    context -- The os context.
    """
    vm_instance = vm.get_vm(uid, context)

    # FIXME: currently quantum driver has a notimplemented here :-(
    # fixed_ips = NETWORK_API.get_fixed_ip(uid, context)
    # NOTE(aloga): nova-network is still suported in Grizzly, so we
    # should not drop support for it.
    tmp = NETWORK_API.get_instance_nw_info(context, vm_instance)[0]
    fixed_ip = tmp.fixed_ips()[0]['address']

    float_address = NETWORK_API.allocate_floating_ip(context, pool_name)

    try:
        address = fixed_ip
        NETWORK_API.associate_floating_ip(context, vm_instance,
                                          float_address, address)
    except Exception as e:
        raise AttributeError(e.message)
    return float_address
示例#5
0
def add_floating_ip(uid, pool_name, context):
    """
    Adds an ip to an VM instance.

    uid -- id of the VM.
    pool_name -- name of the pool
    context -- The os context.
    """
    vm_instance = vm.get_vm(uid, context)

    cached_nwinfo = 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')

    float_address = NETWORK_API.allocate_floating_ip(context, pool_name)

    try:
        address = fixed_ips[0]['address']
        NETWORK_API.associate_floating_ip(context, vm_instance,
                                          float_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)
    return float_address
示例#6
0
def add_floating_ip(uid, pool_name, context):
    """
    Adds an ip to an VM instance.

    uid -- id of the VM.
    pool_name -- name of the pool
    context -- The os context.
    """
    vm_instance = vm.get_vm(uid, context)

    # FIXME: currently quantum driver has a notimplemented here :-(
    # fixed_ips = NETWORK_API.get_fixed_ip(uid, context)
    # NOTE(aloga): nova-network is still suported in Grizzly, so we
    # should not drop support for it.
    tmp = NETWORK_API.get_instance_nw_info(context, vm_instance)[0]
    fixed_ip = tmp.fixed_ips()[0]['address']

    float_address = NETWORK_API.allocate_floating_ip(context, pool_name)

    try:
        address = fixed_ip
        NETWORK_API.associate_floating_ip(context, vm_instance, float_address,
                                          address)
    except Exception as e:
        raise AttributeError(e.message)
    return float_address
示例#7
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.
    """
    vm_instance = vm.get_vm(uid, context)

    try:
        NETWORK_API.disassociate_floating_ip(context, vm_instance, address)
        NETWORK_API.release_floating_ip(context, address)
    except Exception as e:
        raise AttributeError(e.message)
示例#8
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.
    """
    vm_instance = vm.get_vm(uid, context)

    try:
        NETWORK_API.disassociate_floating_ip(context, vm_instance, address)
        NETWORK_API.release_floating_ip(context, address)
    except Exception as e:
        raise AttributeError(e.message)
示例#9
0
    def _construct_occi_compute(self, identifier, extras):
        """
        Construct a OCCI compute instance.

        First item in result list is entity self!

        Adds it to the cache too!
        """
        result = []
        context = extras['nova_ctx']

        instance = vm.get_vm(identifier, context)

        # 1. get identifier
        iden = infrastructure.COMPUTE.location + identifier
        entity = core_model.Resource(iden, infrastructure.COMPUTE,
                                     [os_addon.OS_VM])
        result.append(entity)

        # 2. os and res templates
        flavor_id = int(instance['instance_type_id'])
        res_tmp = self.get_category('/' + str(flavor_id) + '/', extras)
        if res_tmp:
            entity.mixins.append(res_tmp)

        os_id = instance['image_ref']
        image_id = vm.retrieve_image(os_id, context)['id']
        image_tmp = self.get_category('/' + image_id + '/', extras)
        if image_tmp:
            entity.mixins.append(image_tmp)

        # 3. network links & get links from cache!
        net_links = net.get_network_details(identifier, context)
        for item in net_links['public']:
            link = self._construct_network_link(item, entity, self.pub_net,
                                                extras)
            result.append(link)
        for item in net_links['admin']:
            link = self._construct_network_link(item, entity, self.adm_net,
                                                extras)
            result.append(link)

        # core.id and cache it!
        entity.attributes['occi.core.id'] = identifier
        entity.extras = self.get_extras(extras)
        self.cache[(entity.identifier, context.user_id)] = entity

        return result
示例#10
0
    def _construct_occi_compute(self, identifier, extras):
        """
        Construct a OCCI compute instance.

        First item in result list is entity self!

        Adds it to the cache too!
        """
        result = []
        context = extras['nova_ctx']

        instance = vm.get_vm(identifier, context)

        # 1. get identifier
        iden = infrastructure.COMPUTE.location + identifier
        entity = core_model.Resource(iden, infrastructure.COMPUTE,
                                     [os_addon.OS_VM])
        result.append(entity)

        # 2. os and res templates
        flavor_id = instance['instance_type'].flavorid
        res_tmp = self.get_category('/' + flavor_id + '/', extras)
        if res_tmp:
            entity.mixins.append(res_tmp)

        os_id = instance['image_ref']
        image_id = storage.get_image(os_id, context)['id']
        image_tmp = self.get_category('/' + image_id + '/', extras)
        if image_tmp:
            entity.mixins.append(image_tmp)

        # 3. network links & get links from cache!
        net_links = net.get_network_details(identifier, context)
        for item in net_links['public']:
            link = self._construct_network_link(item, entity, self.pub_net,
                                                extras)
            result.append(link)
        for item in net_links['admin']:
            link = self._construct_network_link(item, entity, self.adm_net,
                                                extras)
            result.append(link)

        # core.id and cache it!
        entity.attributes['occi.core.id'] = identifier
        entity.extras = self.get_extras(extras)
        self.cache[(entity.identifier, context.user_id)] = entity

        return result
示例#11
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.
    """
    vm_instance = vm.get_vm(uid, context)

    try:
        NETWORK_API.disassociate_floating_ip(context, vm_instance, address)
        NETWORK_API.release_floating_ip(context, address)
    except exception.FloatingIpNotAssociated:
        raise AttributeError('Unable to disassociate an unassociated '
                             'floating up!')
示例#12
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.
    """
    vm_instance = vm.get_vm(uid, context)

    try:
        NETWORK_API.disassociate_floating_ip(context, vm_instance, address)
        NETWORK_API.release_floating_ip(context, address)
    except exception.FloatingIpNotAssociated:
        raise AttributeError('Unable to disassociate an unassociated '
                             'floating up!')
示例#13
0
def get_network_details(uid, context):
    """
    Extracts the VMs network adapter information.

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

    result = {'public': [], 'admin': []}
    try:
        net_info = NETWORK_API.get_instance_nw_info(context, vm_instance)[0]
    except IndexError:
        LOG.warn('Unable to retrieve network information - this is because '
                 'of OpenStack!!')
        return result
    gw = net_info['network']['subnets'][0]['gateway']['address']
    mac = net_info['address']

    if len(net_info['network']['subnets'][0]['ips']) == 0:
        tmp = {'floating_ips': [], 'address': '0.0.0.0'}
    else:
        tmp = net_info['network']['subnets'][0]['ips'][0]
    for item in tmp['floating_ips']:
        result['public'].append({
            'interface': 'eth0',
            'mac': 'aa:bb:cc:dd:ee:ff',
            'state': 'active',
            'address': item['address'],
            'gateway': '0.0.0.0',
            'allocation': 'static'
        })
    result['admin'].append({
        'interface': 'eth0',
        'mac': mac,
        'state': 'active',
        'address': tmp['address'],
        'gateway': gw,
        'allocation': 'static'
    })

    return result
示例#14
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)

        # set state and applicable actions!
        # TODO: map to OCCI state!
        state, actions = vm.get_vm_state(uid, context)
        entity.attributes['occi.compute.state'] = state
        entity.actions = actions

        # set up to date attributes
        entity.attributes['occi.compute.hostname'] = instance['hostname']
        # TODO: check to get arch from OS!
        entity.attributes['occi.compute.architecture'] = 'x86'
        entity.attributes['occi.compute.cores'] = str(instance['vcpus'])
        entity.attributes['occi.compute.speed'] = str(0.0)  # N/A in instance
        value = str(float(instance['memory_mb']) / 1024)
        entity.attributes['occi.compute.memory'] = value
示例#15
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)

        # set state and applicable actions!
        # TODO: map to OCCI state!
        state, actions = vm.get_vm_state(uid, context)
        entity.attributes['occi.compute.state'] = state
        entity.actions = actions

        # set up to date attributes
        entity.attributes['occi.compute.hostname'] = instance['hostname']
        # TODO: check to get arch from OS!
        entity.attributes['occi.compute.architecture'] = 'x86'
        entity.attributes['occi.compute.cores'] = str(instance['vcpus'])
        entity.attributes['occi.compute.speed'] = str(0.0)  # N/A in instance
        value = str(float(instance['memory_mb']) / 1024)
        entity.attributes['occi.compute.memory'] = value
示例#16
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)

        # set state and applicable actions!
        # TODO: map to OCCI state!
        state, actions = vm.get_vm_state(uid, context)
        entity.attributes["occi.compute.state"] = state
        entity.actions = actions

        # set up to date attributes
        entity.attributes["occi.compute.hostname"] = instance["hostname"]
        # TODO: check to get arch from OS!
        entity.attributes["occi.compute.architecture"] = "x86"
        entity.attributes["occi.compute.cores"] = str(instance["vcpus"])
        entity.attributes["occi.compute.speed"] = str(0.0)  # N/A in instance
        value = str(float(instance["memory_mb"]) / 1024)
        entity.attributes["occi.compute.memory"] = value
示例#17
0
def get_network_details(uid, context):
    """
    Extracts the VMs network adapter information.

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

    result = {'public': [], 'admin': []}
    try:
        net_info = NETWORK_API.get_instance_nw_info(context, vm_instance)[0]
    except IndexError:
        LOG.warn('Unable to retrieve network information - this is because '
                 'of OpenStack!!')
        return result
    gw = net_info['network']['subnets'][0]['gateway']['address']
    mac = net_info['address']

    if len(net_info['network']['subnets'][0]['ips']) == 0:
        tmp = {'floating_ips': [], 'address': '0.0.0.0'}
    else:
        tmp = net_info['network']['subnets'][0]['ips'][0]
    for item in tmp['floating_ips']:
        result['public'].append({'interface': 'eth0',
                                 'mac': 'aa:bb:cc:dd:ee:ff',
                                 'state': 'active',
                                 'address': item['address'],
                                 'gateway': '0.0.0.0',
                                 'allocation': 'static'})
    result['admin'].append({'interface': 'eth0',
                            'mac': mac,
                            'state': 'active',
                            'address': tmp['address'],
                            'gateway': gw,
                            'allocation': 'static'})

    return result
示例#18
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: ' + repr(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.hostname'] = instance['hostname']
        entity.attributes['occi.compute.architecture'] =\
            storage.get_image_architecture(uid, extras['nova_ctx'])
        entity.attributes['occi.compute.cores'] = str(instance['vcpus'])
        entity.attributes['occi.compute.speed'] = str(0.0)  # N/A in instance
        value = str(float(instance['memory_mb']) / 1024)
        entity.attributes['occi.compute.memory'] = value
示例#19
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: ' + repr(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.hostname'] = instance['hostname']
        entity.attributes['occi.compute.architecture'] =\
            storage.get_image_architecture(uid, extras['nova_ctx'])
        entity.attributes['occi.compute.cores'] = str(instance['vcpus'])
        entity.attributes['occi.compute.speed'] = str(0.0)  # N/A in instance
        value = str(float(instance['memory_mb']) / 1024)
        entity.attributes['occi.compute.memory'] = value
示例#20
0
    def retrieve(self, entity, extras):
        """
        Add OpenStack related actions.
        """
        uid = entity.attributes['occi.core.id']
        context = extras['nova_ctx']

        # set additional actions
        if 'occi.compute.state' in entity.attributes and entity.attributes[
                'occi.compute.state'] == 'active':
            entity.actions.append(os_addon.OS_CREATE_IMAGE)
            entity.actions.append(os_addon.OS_CHG_PWD)

        # add VNC link if available
        console = vm.get_vnc(uid, context)
        if console:
            entity.attributes['org.openstack.compute.console.vnc'] =\
                console['url']
        else:
            entity.attributes['org.openstack.compute.console.vnc'] = 'N/A'

        # also expose the exact openstack state
        entity.attributes['org.openstack.compute.state'] = \
            vm.get_vm(uid, context)['vm_state']
示例#21
0
    def retrieve(self, entity, extras):
        """
        Add OpenStack related actions.
        """
        uid = entity.attributes['occi.core.id']
        context = extras['nova_ctx']

        # set additional actions
        if 'occi.compute.state' in entity.attributes and entity.attributes[
                'occi.compute.state'] == 'active':
            entity.actions.append(os_addon.OS_CREATE_IMAGE)
            entity.actions.append(os_addon.OS_CHG_PWD)

        # add VNC link if available
        console = vm.get_vnc(uid, context)
        if console:
            entity.attributes['org.openstack.compute.console.vnc'] =\
                console['url']
        else:
            entity.attributes['org.openstack.compute.console.vnc'] = 'N/A'

        # also expose the exact openstack state
        entity.attributes['org.openstack.compute.state'] = \
            vm.get_vm(uid, context)['vm_state']
示例#22
0
    def _construct_occi_compute(self, identifier, extras):
        """
        Construct a OCCI compute instance.

        First item in result list is entity self!

        Adds it to the cache too!
        """
        result = []
        context = extras['nova_ctx']

        instance = vm.get_vm(identifier, context)

        # 1. get identifier
        iden = infrastructure.COMPUTE.location + identifier
        entity = core_model.Resource(iden, infrastructure.COMPUTE,
                                     [os_addon.OS_VM])
        result.append(entity)

        # 2. os and res templates
        flavor_id = int(instance['instance_type_id'])
        res_tmp = self.get_category('/' + str(flavor_id) + '/', extras)
        if res_tmp:
            entity.mixins.append(res_tmp)

        image_id = instance['image_ref']
        image_tmp = self.get_category('/' + image_id + '/', extras)
        if image_tmp:
            entity.mixins.append(image_tmp)

        # 3. network links & get links from cache!
        net_links = net.get_network_details(identifier, context)
        for item in net_links['public']:
            link = self._construct_network_link(item, entity, self.pub_net,
                                                extras)
            result.append(link)
        for item in net_links['admin']:
            link = self._construct_network_link(item, entity, self.adm_net,
                                                extras)
            result.append(link)

        # 4. storage links from the storage (and cache them)
        stors = storage.get_storage_volumes(context)
        for item in stors:
            if item['status'] == 'in-use' and item['instance_uuid'] \
                    == identifier:
                stor = core_model.Resource(
                    infrastructure.STORAGE.location +
                    item['id'],
                    infrastructure.STORAGE,
                    [])
                stor.attributes['occi.core.id'] = item['id']
                link = core_model.Link(infrastructure.STORAGELINK.location +
                                       str(uuid.uuid4()),
                                       infrastructure.STORAGELINK, [], entity,
                                       stor)
                link.attributes = {
                    'occi.storagelink.deviceid': item['mountpoint'],
                    'occi.storagelink.mountpoint': item['mountpoint'],
                    'occi.storagelink.state': 'active'}
                link.extras = self.get_extras(extras)
                entity.links.append(link)
                result.append(link)
                self.cache[(link.identifier, context.user_id)] = link

        # core.id and cache it!
        entity.attributes['occi.core.id'] = identifier
        entity.extras = self.get_extras(extras)
        self.cache[(entity.identifier, context.user_id)] = entity

        return result