Esempio n. 1
0
def upload_to_image(volume_id, force, image_name, container_format,
                    disk_format):
    """"Upload volume to image service as image.

    :param volume_id: The :id:`Volume` to upload.
    :param force: force upload or not
    :param image_name: image name
    :param container_format:  image container format
    :param disk_format:  image disk format
    :return:
    """
    try:
        info = {
            'force': force,
            'image_name': image_name,
            'container_format': container_format,
            'disk_format': disk_format
        }
        volume = yield volume_action(action="os-volume_upload_image",
                                     volume_id=volume_id,
                                     info=info)
    except Exception as e:
        LOG.error("upload volume to image error: %s" % e)
        raise VolumeOperationFailed()

    raise gen.Return(volume)
Esempio n. 2
0
def get_volume_image_metadata(volume_id):
    try:
        rs = {}
        sql = "select `key`,`value` from volume_glance_metadata where volume_id = %s"
        db = dbpools.get_cinder()
        cur = yield db.execute(sql, [volume_id])
        image_meta = cur.fetchall()
        if image_meta:
            for i in image_meta:
                rs[i.get("key")] = i.get("value")
    except Exception, e:
        LOG.error(e.message)
        raise VolumeOperationFailed()
Esempio n. 3
0
def reset_state(volume_id, volume_state):
    """Update the provided volume with the provided state.
    :param volume_id: The :id: Volume
    :param volume_state: The status of volume to alter
    """
    try:
        volume = yield volume_action(action="os-reset_status",
                                     volume_id=volume_id,
                                     info={'status': volume_state})
    except Exception as e:
        LOG.error("reset volume state error: %s" % e)
        raise VolumeOperationFailed()

    raise gen.Return(volume)
Esempio n. 4
0
def force_delete(volume_id):
    """ force delete volume
    :param volume_id: THe :id: Volume
    :return:
    """
    try:
        yield force_delete_volume_from_db(volume_id=volume_id)
        volume = yield volume_action(action="os-force_delete",
                                     volume_id=volume_id)
    except Exception as e:
        LOG.error("force delete volume error: %s" % e)
        raise VolumeOperationFailed()

    raise gen.Return(volume)
Esempio n. 5
0
def get_metadata(volume_id):
    """Update/Set a volumes metadata.

    :param volume_id: The :class:`Volume`.
    """
    try:
        url = "/volumes/%s/metadata" % volume_id
        volume_meta = yield volume_request(request_url=url,
                                           method=os.METHOD_GET,
                                           response_key='metadata')
    except Exception as e:
        LOG.error("get volume meta error: %s" % e)
        raise VolumeOperationFailed()
    else:
        metadata = {k: eval_val(v) for k, v in volume_meta.items()}

    raise gen.Return(metadata)
Esempio n. 6
0
def delete_metadata(volume_id, meta_keys):
    """Delete specified keys from volumes metadata.
    :param volume_id: The :id:`Volume`.
    :param meta_keys: A list of keys to be removed.
    """
    try:
        if not meta_keys:
            return

        for k in meta_keys:
            url = "/volumes/%s/metadata/%s" % (volume_id, k)
            yield volume_request(request_url=url,
                                 method=os.METHOD_DELETE,
                                 response_key='metadata')
    except Exception as e:
        LOG.error("delete volume metadata error: %s" % e)
        raise VolumeOperationFailed()
Esempio n. 7
0
def list_volume(detailed=True, volume_id=None, batch=None, name=None, tenant_ids=None, user_id=None, vd_type=0, available=False):
    """
    :param detailed:
    :param volume_id:
    :param batch:
    :param tenant_ids:
    :param user_id:
    :param available:
    :param name:
    :param vd_type:
    :return:
    """
    try:
        volumes = yield volume_list(volume_id=volume_id, batch=batch, name=name, tenant_ids=tenant_ids, user_id=user_id,
                                    available=available, vd_type=vd_type)
    except Exception, e:
        LOG.error("list volumes error: %s" % e)
        raise VolumeOperationFailed()
Esempio n. 8
0
def update_volume_image_metadata(volume_id, key, value):
    try:
        db = dbpools.get_cinder()
        query_sql = "select * from volume_glance_metadata where volume_id = %s and `key` = %s"
        cur = yield db.execute(query_sql, [volume_id, key])
        res = cur.fetchone()
        if res:
            yield dbpools.execute_commit(
                db,
                "update volume_glance_metadata set `value` = %s  where volume_id = %s and `key` = %s",
                (value, volume_id, key))
        else:
            yield dbpools.execute_commit(
                db,
                "insert into volume_glance_metadata (created_at,deleted,volume_id,`key`,`value`) VALUES (NOW(),0,%s,%s,%s)",
                (volume_id, key, value))
    except Exception, e:
        LOG.error(e.message)
        raise VolumeOperationFailed()
Esempio n. 9
0
def update_metadata(volume_id, metadata):
    """Delete specified keys from volumes metadata.
    :param volume_id: The :id:`Volume`.
    :param metadata: A list of keys to be removed.
    """
    try:
        if not metadata:
            return

        if metadata:
            body = {'metadata': {k: str(v) for k, v in metadata.items()}}
            url = "/volumes/%s/metadata" % volume_id
            yield volume_request(request_url=url,
                                 method=os.METHOD_PUT,
                                 request_body=body,
                                 response_key='metadata')
    except Exception as e:
        LOG.error("update volume metadata error: %s" % e)
        raise VolumeOperationFailed()
Esempio n. 10
0
def set_metadata(volume_id, metadata):
    """Update/Set a volumes metadata.

    :param volume_id: The :class:`Volume`.
    :param metadata: A list of keys to be set.
    """
    try:
        if not metadata:
            return

        body = {'metadata': metadata}
        url = "/volumes/%s/metadata" % volume_id
        yield volume_request(request_url=url,
                             method=os.METHOD_POST,
                             request_body=body,
                             response_key='metadata')
    except Exception as e:
        LOG.error("update volume error: %s" % e)
        raise VolumeOperationFailed()
Esempio n. 11
0
def update_volume(volume_id, **params):
    """ update volume's displayname or description
    :param volume_id: The :id: Volume
    :param params: Update volume info:A Dict={"displayname": "vm-1",
    "des": "desc volume"}
    :return:
    """
    try:
        volume = yield get_volume(volume_id=volume_id)
        if volume:
            volume_meta = volume['metadata'] \
                if volume['metadata'] else {}
            volume_meta['des'] = params['des']
            volume_meta['displayname'] = params['displayname']
            yield set_metadata(volume_id=volume_id,
                               metadata=volume_meta)
    except Exception, e:
        LOG.error("update volume error: %s" % e)
        raise VolumeOperationFailed()
Esempio n. 12
0
def set_volume_user(volume_id, user_id):
    result = {
        "name": "",
        "displayname": "",
        "user": ""
    }
    try:
        volume = yield get_volume(volume_id=volume_id)
        if not is_none_or_empty(user_id):
            user = yield get_user_by_id(user_id)
            result['user'] = user.get("displayname")
        if volume:
            result['name'] = volume.get("name")
            volume_meta = volume['metadata'] \
                if volume['metadata'] else {}
            volume_meta['user'] = user_id
            result['displayname'] = volume_meta.get("displayname")
            yield set_metadata(volume_id=volume_id,
                               metadata=volume_meta)
    except Exception, e:
        LOG.error("set volume user error: %s" % e)
        raise VolumeOperationFailed()
Esempio n. 13
0
def delete_volume(volume_id):
    try:
        yield volume_delete(volume_id=volume_id)
    except Exception, e:
        LOG.error("delete volume error: %s" % e)
        raise VolumeOperationFailed()
Esempio n. 14
0
def create_volume(**volume):
    """ create volume
    :param volume: A dict of volume
    :return:
    """
    if is_none_or_empty(volume['volume_type']):
        volume['volume_type'] = CONF.storage.default_type
    volume_project = volume['tenant_id']
    volume_size = calc_size(volume['size'])
    volume_num = volume['num']
    if volume['volume_type'] == "lvm":
        volume_num = 1

    disk_capacity = yield get_quota(volume_project, QUOTA_NAMES.disk_capacity)
    disks = yield get_quota(volume_project, QUOTA_NAMES.disks)

    used_disks = disks.get("quota_used")
    used_disk_capacity = disk_capacity.get("quota_used")

    used_disks += volume_num
    used_disk_capacity += volume_size * volume_num

    yield check_quota(volume_project,
                      QUOTA_NAMES.disks,
                      used_disks)
    yield check_quota(volume_project,
                      QUOTA_NAMES.disk_capacity,
                      used_disk_capacity)

    yield update_quota_used(volume_project, QUOTA_NAMES.disks,
                            used_disks)
    yield update_quota_used(volume_project, QUOTA_NAMES.disk_capacity,
                            used_disk_capacity)

    try:
        volume_name = yield number_seq("volume-sequence", "vd-")
        names = []
        for i in range(1, volume_num + 1):
            if volume_num > 1:
                name = str(volume_name) + "-" + str(i)
            else:
                name = volume_name
            names.append(name)
            metadata = {"des": "" if is_none_or_empty(volume['des']) else volume['des'],
                        "user": "" if is_none_or_empty(volume['user_id']) else volume['user_id'],
                        "displayname": "" if is_none_or_empty(volume['displayname']) else volume['displayname']}

            yield volume_create(size=volume_size,
                                tenant_id=volume_project,
                                snapshot_id=None,
                                source_volid=volume['source_volid'],
                                name=name,
                                description=str(0),
                                volume_type=volume['volume_type'],
                                source_replica=None,
                                metadata=metadata,
                                project_id=volume['tenant_id'],
                                image_ref=volume['image_ref'],
                                availability_zone=volume["host"])
    except Exception, e:
        LOG.error("create volume error: %s" % e)
        raise VolumeOperationFailed()