示例#1
0
def __update_server_state(vm_id):
    try:
        db = dbpools.get_nova()
        yield dbpools.execute_commit(
                db,
                "update instances set vm_state = 'stopped' ,power_state = 4 where uuid = %s",
                (vm_id)
        )
    except Exception, e:
        LOG.error("host down update vm state stopped  error: %s" % e)
示例#2
0
def get_tenant_quota(tenant_id):
    """ get tenant quota set
    :param tenant_id: id of tenant
    """
    try:
        db = dbpools.get_nova()
        sql = "select  IFNULL(sum(memory_mb),0)  as used_memorys , IFNULL(sum(vcpus),0)  as used_cores  from instances where deleted = 0 and vm_state != 'soft-delete' and  project_id = %s "
        cur = yield db.execute(sql, (tenant_id))
        used_quotas = cur.fetchone()
    except Exception, e:
        LOG.error("list tenant's quota error: %s" % e)
        raise e
示例#3
0
def __update_server_host(vm_id, new_host):
    """ update tenant' quota settings
    :param vm_id: id of virtual machine
    :param new_host: migrate to host
    """
    try:
        db = dbpools.get_nova()
        yield dbpools.execute_commit(
                db,
                "update instances set `host`=%s, node=%s, launched_on=%s"
                " where uuid = %s",
                (new_host, new_host, new_host, vm_id)
        )
    except Exception, e:
        LOG.error("update instance migrate to host: %s, error: %s" % (new_host, e))
        raise MigrateFailed()
示例#4
0
def __force_del_detach_volume(volume_id):
    """ force detach specific volume from specific server
    :param volume_id: The :id: volume
    """
    try:
        try:
            db = dbpools.get_nova()
            yield dbpools.execute_commit(
                db, "delete from block_device_mapping where volume_id = %s",
                (volume_id))
        except Exception, e:
            LOG.error("force delete server attach volume from db error: %s" %
                      e)
            raise e
    except Exception as e:
        LOG.error("force del server detach volume error: %s" % e)
示例#5
0
def get_vms_nics(vm_id=None):
    try:
        db = dbpools.get_nova()
        if vm_id:
            result = []
            sql = "select instance_uuid , network_info from instance_info_caches where  instance_uuid =  %s"
            cur = yield db.execute(sql, [vm_id])
            res = cur.fetchone()
            network_info = jsonutils.loads(res.get("network_info"))
            for r in network_info:
                result.append({
                    "name":
                    r.get("network").get("label"),
                    "ip":
                    r.get("network").get("subnets")[0].get("ips")[0].get(
                        "address"),
                    "id":
                    r.get("network").get("id"),
                    "port_id":
                    r.get("id")
                })
        else:
            result = {}
            sql = "select instance_uuid , network_info from instance_info_caches where deleted = 0"
            cur = yield db.execute(sql)
            response = cur.fetchall()
            for res in response:
                network_info = jsonutils.loads(res.get("network_info"))
                network = []
                for r in network_info:
                    network.append({
                        "name":
                        r.get("network").get("label"),
                        "ip":
                        r.get("network").get("subnets")[0].get("ips")[0].get(
                            "address"),
                        "id":
                        r.get("network").get("id")
                    })
                result[res.get("instance_uuid")] = network
    except Exception as e:
        LOG.error("get vms nics  from db error: %s" % e)
        raise e
    raise gen.Return(result)
示例#6
0
def _detach_iso_volume(volume_id, vm_id):
    LOG.debug("the vm %s is create by iso need reboot hard ", vm_id)
    db = dbpools.get_nova()
    tx = yield db.begin()
    try:
        cur = yield tx.execute(
            "select volume_id,connection_info ,disk_bus from block_device_mapping where instance_uuid = %s and device_name = '/dev/vdb'",
            (vm_id))
        sys = cur.fetchone()
        # cur.close()
        sys_volume_id = sys.get("volume_id")
        sys_connection_info = sys.get("connection_info")
        disk_bus = sys.get("disk_bus")
        yield tx.execute(
            "update  block_device_mapping  set  device_type = 'disk' , disk_bus = %s , volume_id = %s , connection_info = %s  where instance_uuid = %s  and device_name = '/dev/vda'",
            (disk_bus, sys_volume_id, sys_connection_info, vm_id))
        yield tx.execute(
            "update  block_device_mapping  set deleted = 1 where instance_uuid = %s  and device_name = '/dev/vdb'",
            (vm_id))
        yield tx.execute(
            "update  block_device_mapping  set deleted = 1 where instance_uuid = %s  and device_name = '/dev/vdc'",
            (vm_id))
    except Exception as e:
        LOG.error("detach iso volume : %s" % e)
        yield tx.rollback()
        raise e
    else:
        yield tx.commit()
        db = dbpools.get_cinder()
        tx1 = yield db.begin()
        try:
            yield tx1.execute(
                "update volume_attachment set deleted = 1 ,mountpoint = '/dev/vda'  where volume_id = %s and instance_uuid = %s",
                (volume_id, vm_id))
            yield tx1.execute(
                "update volume_attachment set deleted = 1 ,mountpoint = '/dev/vdc'  where volume_id = %s and instance_uuid = %s",
                (volume_id, vm_id))
        except Exception as e:
            LOG.error("update iso volume_attachment available: %s" % e)
            yield tx1.rollback()
            raise e
        else:
            yield tx1.commit()
        yield server_action(vm_id, Control.REBOOT, info={"type": "HARD"})
示例#7
0
def __update_instance_flavor(vm_id, **flavor):
    """ update tenant' quota settings
    :param vm_id: id of virtual machine
    :param flavor: resize flavor = {}
    """
    db = dbpools.get_nova()
    tx = yield db.begin()
    try:
        new_flavor = __FLAVOR_ATTR % (flavor['disk_capacity'], flavor['name'],
                                      flavor['id'], flavor['memory'],
                                      flavor['cores'], 0)
        yield tx.execute(
            "update instance_extra set flavor = %s where instance_uuid = %s",
            (remove_null_string(new_flavor), vm_id))
        yield tx.execute(
            "update instances set memory_mb=%s, vcpus=%s, root_gb=%s where uuid = %s",
            (flavor['memory'], flavor['cores'], flavor['disk_capacity'],
             vm_id))
    except Exception, e:
        LOG.error("update instance flavor error: %s" % e)
        yield tx.rollback()
        raise ServerOperationFailed
示例#8
0
@required("user_id")
def delete_user(user_id):
    try:
        url = "/users/%s" % user_id
        session = yield openstack.get_session()
        yield openstack.connect_request(session=session,
                                        type=openstack.TYPE_IDENTITY,
                                        url=url,
                                        method=openstack.METHOD_DELETE)
        __USER_CACHE.remove(user_id)
    except Exception, e:
        LOG.error("delete user '%s' raise an error: %s" % (user_id, e))
        raise UserOperationFailed()
    else:
        try:
            nova = dbpools.get_nova()
            sql = 'delete from instance_metadata where value = %s'
            yield nova.execute(sql, (user_id, ))
            cinder = dbpools.get_cinder()
            sql = 'delete from volume_metadata where value = %s'
            yield cinder.execute(sql, (user_id, ))
        except Exception, e:
            LOG.error("delete metadata user '%s' raise an error: %s" %
                      (user_id, e))
            raise UserNotExist


@gen.coroutine
@required("user_id", "role_name")
def set_user_role(user_id, role_name, tenant_id=None):
    try: