コード例 #1
0
def _instance_db_info(uuid, vmname, vm_app_info, owner, flavor_id, group_id,
                      host, mac, vmdisk, ip_id, vmostype, requestid, ver_data):
    vm_ostype_todb = ''
    vmhost = ho_s.HostService().get_host_info_by_hostip(host)
    # 往instance表添加记录
    instance_data = {
        'uuid': uuid,
        'name': vmname,
        'displayname': vmname,
        'description': '',
        'status': VMStatus.CONVERTING,
        'typestatus': VMTypeStatus.NORMAL,
        'isdeleted': '0',
        'app_info': vm_app_info,
        'owner': owner,
        'created_at': get_datetime_str(),
        'create_source': VMCreateSource.ESX
    }
    ret = ins_s.InstanceService().add_instance_info(instance_data)
    if ret.get('row_num') <= 0:
        message = 'add instance info error when create instance'
        logging.info(
            'add instance info error when create instance, insert_data: %s',
            instance_data)
        return False, message

    instance_id = ret.get('last_id')

    if vmostype == 'Windows':
        vm_ostype_todb = 'windows'
    elif vmostype == 'Linux':
        vm_ostype_todb = 'linux'

    # 往v2v_instance_info表添加记录
    v2v_instance_data = {
        'instance_id': instance_id,
        'os_type': vm_ostype_todb,
        'isdeleted': '0',
        'created_at': get_datetime_str(),
        'request_id': requestid,
        'os_version': ver_data
    }
    ret_v2v_instance = v2v_in_i.v2vInstanceinfo().add_v2v_instance_info(
        v2v_instance_data)
    if ret_v2v_instance.get('row_num') <= 0:
        logging.info(
            'add v2v_instance info error when create instance, v2v_instance_data: %s',
            v2v_instance_data)
        message = 'add v2v_instance info error when create instance'
        return False, message

    # 往instance_flavor表添加记录
    instance_flavor_data = {
        'instance_id': instance_id,
        'flavor_id': flavor_id,
        'created_at': get_datetime_str()
    }
    ret1 = ins_f_s.InstanceFlavorService().add_instance_flavor_info(
        instance_flavor_data)
    if ret1.get('row_num') <= 0:
        logging.info(
            'add instance_flavor info error when create instance, insert_data: %s',
            instance_flavor_data)
        message = 'add instance_flavor info error when create instance'
        return False, message

    # 往instance_group表添加记录
    instance_group_data = {
        'instance_id': instance_id,
        'group_id': group_id,
        'created_at': get_datetime_str()
    }
    ret2 = ins_g_s.InstanceGroupService().add_instance_group_info(
        instance_group_data)
    if ret2.get('row_num') <= 0:
        logging.info(
            'add instance_group info error when create instance, insert_data: %s',
            instance_group_data)
        message = 'add instance_group info error when create instance'
        return False, message

    # 往instance_host表添加记录
    instance_host_data = {
        'instance_id': instance_id,
        'instance_name': vmname,
        'host_id': vmhost['id'],
        'host_name': vmhost['name'],
        'isdeleted': '0',
        'created_at': get_datetime_str()
    }
    ret3 = ins_h_s.InstanceHostService().add_instance_host_info(
        instance_host_data)
    if ret3.get('row_num') <= 0:
        logging.info(
            'add instance_host info error when create instance, insert_data: %s',
            instance_host_data)
        message = 'add instance_host info error when create instance'
        return False, message

    # 往instance_ip表添加记录
    instance_ip_data = {
        'instance_id': instance_id,
        'ip_id': ip_id,
        'mac': mac,
        'type': InstanceNicType.MAIN_NETWORK_NIC,
        'isdeleted': '0',
        'created_at': get_datetime_str()
    }
    ret4 = ins_ip_s.InstanceIPService().add_instance_ip_info(instance_ip_data)
    if ret4.get('row_num') <= 0:
        logging.info(
            'add instance_ip info error when create instance, insert_data: %s',
            instance_ip_data)
        message = 'add instance_ip info error when create instance'
        return False, message

    # 往instance_disk表添加记录
    instance_disk_data = {
        'instance_id': instance_id,
        'size_gb': vmdisk,
        'mount_point': '',
        'dev_name': '',
        'isdeleted': '0',
        'created_at': get_datetime_str()
    }
    ret5 = ins_d_s.InstanceDiskService().add_instance_disk_info(
        instance_disk_data)
    if ret5.get('row_num') <= 0:
        logging.info(
            'add instance_disk info error when create instance, insert_data: %s',
            instance_disk_data)
        message = 'add instance_disk info error when create instance'
        return False, message
    message = "信息入库完成"
    return True, message
コード例 #2
0
def _create_instance_info(task_id, instance_name, app_info, owner, password,
                          flavor_id, group_id, vm_host, flavor_info,
                          image_data, ip_data, vm_disk_gb, mount_point,
                          instance_system, net_area_id, segment_data, vm_env,
                          user_id):
    uuid = randomUUID()
    request_id = ins_s.generate_req_id()
    # 往instance表添加记录
    logging.info(
        '创建VM 步骤10-1:插入instance表 task %s : insert instance table start when create instance',
        task_id)
    instance_data = {
        'uuid': uuid,
        'name': instance_name,
        'displayname': instance_name,
        'description': '',
        'status': VMStatus.CREATING,
        'typestatus': VMTypeStatus.NORMAL,
        'create_source': VMCreateSource.CLOUD_SOURCE,
        'isdeleted': '0',
        'app_info': app_info,
        'owner': owner,
        'created_at': get_datetime_str(),
        'password': encrypt_helper.encrypt(str(password)),  # 密码加密
        'request_id': request_id,
        'task_id': task_id
    }
    ret = ins_s.InstanceService().add_instance_info(instance_data)
    if ret.get('row_num') <= 0:
        logging.error(
            'task %s : add instance info error when create instance, insert_data: %s',
            task_id, instance_data)
        return json_helper.format_api_resp(code=ErrorCode.SYS_ERR)
    logging.info(
        '创建VM 步骤10-1:插入instance表成功 '
        'task %s : insert instance table successful when create instance',
        task_id)

    instance_id = ret.get('last_id')

    # 往instance_flavor表添加记录
    logging.info(
        '创建VM 步骤10-2:插入instance_flavor表 '
        'task %s : insert instance_flavor table start when create instance',
        task_id)
    instance_flavor_data = {
        'instance_id': instance_id,
        'flavor_id': flavor_id,
        'created_at': get_datetime_str()
    }
    ret1 = ins_f_s.InstanceFlavorService().add_instance_flavor_info(
        instance_flavor_data)
    if ret1.get('row_num') <= 0:
        logging.error(
            'task %s : add instance_flavor info error when create instance, insert_data: %s',
            task_id, instance_flavor_data)
        return json_helper.format_api_resp(code=ErrorCode.SYS_ERR)
    logging.info(
        '创建VM 步骤10-2:插入instance_flavor表成功 '
        'task %s : insert instance_flavor table successful when create instance',
        task_id)

    # 往instance_group表添加记录
    logging.info(
        '创建VM 步骤10-3:插入instance_group表 task %s : insert instance_group table start when create instance',
        task_id)
    instance_group_data = {
        'instance_id': instance_id,
        'group_id': group_id,
        'created_at': get_datetime_str()
    }
    ret2 = ins_g_s.InstanceGroupService().add_instance_group_info(
        instance_group_data)
    if ret2.get('row_num') <= 0:
        logging.error(
            'task %s : add instance_group info error when create instance, insert_data: %s',
            task_id, instance_group_data)
        return json_helper.format_api_resp(code=ErrorCode.SYS_ERR)
    logging.info(
        '创建VM 步骤10-3:插入instance_group表成功 task %s : insert instance_group table successful when create instance',
        task_id)

    # 往instance_host表添加记录
    logging.info(
        '创建VM 步骤10-4:插入instance_host表 task %s : insert instance_host table start when create instance',
        task_id)
    instance_host_data = {
        'instance_id': instance_id,
        'instance_name': instance_name,
        'host_id': vm_host['host_id'],
        'host_name': vm_host['name'],
        'isdeleted': '0',
        'created_at': get_datetime_str()
    }
    ret3 = ins_h_s.InstanceHostService().add_instance_host_info(
        instance_host_data)
    if ret3.get('row_num') <= 0:
        logging.error(
            'task %s : add instance_host info error when create instance, insert_data: %s',
            task_id, instance_host_data)
        return json_helper.format_api_resp(code=ErrorCode.SYS_ERR)
    logging.info(
        '创建VM 步骤10-4:插入instance_host表成功 '
        'task %s : insert instance_host table successful when create instance',
        task_id)

    # host预分配资源
    logging.info(
        '创建VM 步骤10-5:host预分配资源 task %s : pre allocate host resource start when create instance',
        task_id)
    ret4 = host_s.pre_allocate_host_resource(vm_host['host_id'],
                                             flavor_info['vcpu'],
                                             flavor_info['memory_mb'],
                                             flavor_info['root_disk_gb'])
    if ret4 != 1:
        logging.error(
            'task %s : pre allocate host resource to db error when create instance',
            task_id)
        return json_helper.format_api_resp(code=ErrorCode.SYS_ERR)
    logging.info(
        '创建VM 步骤10-5:host预分配资源成功 '
        'task %s : pre allocate host resource successful when create instance',
        task_id)

    # 往instance_image表添加记录
    logging.info(
        '创建VM 步骤10-6:插入instance_image表 task %s : insert instance_image table start when create instance',
        task_id)
    for _image in image_data:
        instance_image_data = {
            'instance_id': instance_id,
            'image_id': _image['id'],
            'created_at': get_datetime_str()
        }
        ret5 = ins_img_s.InstanceImageService().add_instance_image_info(
            instance_image_data)
        if ret5.get('row_num') <= 0:
            logging.error(
                'task %s : add instance_image info error when create instance, insert_data: %s',
                task_id, instance_image_data)
            return json_helper.format_api_resp(code=ErrorCode.SYS_ERR)
    logging.info(
        '创建VM 步骤10-6:插入instance_image表成功 '
        'task %s : insert instance_image table successful when create instance',
        task_id)

    # 往instance_ip表添加记录
    logging.info(
        '创建VM 步骤10-7:插入instance_ip表 '
        'task %s : insert instance_ip table start when create instance',
        task_id)
    mac = randomMAC()
    data_ip_address = ip_data['ip_address']
    instance_ip_data = {
        'instance_id': instance_id,
        'ip_id': ip_data['id'],
        'mac': mac,
        'type': InstanceNicType.MAIN_NETWORK_NIC,
        'isdeleted': '0',
        'created_at': get_datetime_str()
    }
    ret6 = ins_ip_s.InstanceIPService().add_instance_ip_info(instance_ip_data)
    if ret6.get('row_num') <= 0:
        logging.error(
            'task %s : add instance_ip info error when create instance, insert_data: %s',
            task_id, instance_ip_data)
        return json_helper.format_api_resp(code=ErrorCode.SYS_ERR)
    logging.info(
        '创建VM 步骤10-7:插入instance_ip表成功 '
        'task %s : insert instance_ip table successful when create instance',
        task_id)

    # 标识该IP为已使用
    logging.info(
        '创建VM 步骤10-8:设置IP为已使用 task %s : set ip used start when create instance',
        task_id)
    update_data = {'status': IPStatus.USED}
    where_data = {'id': ip_data['id']}
    ret7 = ip_service.IPService().update_ip_info(update_data, where_data)
    if ret7 <= 0:
        logging.info(
            'task %s : update ip info error when create instance, update_data: %s, where_data: %s',
            task_id, update_data, where_data)
        return json_helper.format_api_resp(code=ErrorCode.SYS_ERR)
    logging.info(
        '创建VM 步骤10-8:设置IP为已使用成功 task %s : set ip used successful when create instance',
        task_id)

    # 拼装消息需要的镜像信息
    logging.info(
        '创建VM 步骤10-9:拼装所需的镜像信息 '
        'task %s : piece together need image info start when create instance',
        task_id)
    image_list = []
    # 数据盘数量
    data_image_num = 0
    for _image in image_data:
        _image_type = _image['type']
        _info = {
            "disk_format": _image['format'],
            "url": _image['url'],
            # "md5sum": _image['md5'],
            "image_size_gb": _image['size_gb']  # 镜像预分配大小
        }
        # 系统盘
        if _image_type == ImageType.SYSTEMDISK:
            _disk_name = instance_name + '.img'
            _info['image_dir_path'] = '/app/image/' + uuid + '/' + _disk_name
            _info['disk_name'] = _disk_name
            _info['disk_size_gb'] = None
            _info['dev_name'] = 'vda'

            # 如果只有一块盘且为系统盘,则预先分配一块数据盘的数据存入instance_disk表
            if len(image_data) == 1:
                logging.info(
                    'task %s : pre insert instance_disk table that has only one system disk start when create '
                    'instance', task_id)
                instance_disk_data = {
                    'instance_id': instance_id,
                    'size_gb': vm_disk_gb,
                    'mount_point': mount_point,
                    'dev_name': 'vdb',
                    'isdeleted': '0',
                    'created_at': get_datetime_str()
                }
                ret9 = ins_d_s.InstanceDiskService().add_instance_disk_info(
                    instance_disk_data)
                if ret9.get('row_num') <= 0:
                    logging.info(
                        'task %s : pre add instance_disk info that has only one system disk error when create '
                        'instance, insert_data: %s', task_id,
                        instance_disk_data)
                    return json_helper.format_api_resp(code=ErrorCode.SYS_ERR)
                logging.info(
                    'task %s : pre insert instance_disk table that has only one system disk successful when '
                    'create instance', task_id)
        else:
            # 数据盘
            _disk_name = instance_name + '.disk' + str(data_image_num + 1)
            _disk_dev_name = _get_vd_map(data_image_num)
            _info['image_dir_path'] = '/app/image/' + uuid + '/' + _disk_name
            _info['disk_name'] = _disk_name
            _info['disk_size_gb'] = vm_disk_gb
            _info['dev_name'] = _disk_dev_name
            data_image_num += 1

            # 往instance_disk表添加记录
            logging.info(
                'task %s : insert instance_disk table start when create instance',
                task_id)
            instance_disk_data = {
                'instance_id': instance_id,
                'size_gb': vm_disk_gb,
                'mount_point': mount_point,
                'dev_name': _disk_dev_name,
                'isdeleted': '0',
                'created_at': get_datetime_str()
            }
            ret8 = ins_d_s.InstanceDiskService().add_instance_disk_info(
                instance_disk_data)
            if ret8.get('row_num') <= 0:
                logging.info(
                    'task %s : add instance_disk info error when create instance, insert_data: %s',
                    task_id, instance_disk_data)
                return json_helper.format_api_resp(code=ErrorCode.SYS_ERR)
            logging.info(
                'task %s : insert instance_disk table successful when create instance',
                task_id)

        image_list.append(_info)
    logging.info(
        '创建VM 步骤10-9:拼装所需的镜像信息成功 '
        'task %s : piece together need image info successful when create instance',
        task_id)

    # 发送异步消息到队列
    logging.info(
        '创建VM 步骤10-10:发送异步消息给队列 '
        'task %s : send kafka message start when create instance', task_id)
    data = {
        "routing_key": "INSTANCE.CREATE",
        "send_time": get_datetime_str(),
        "data": {
            "task_id":
            task_id,
            "request_id":
            request_id,
            "host_ip":
            vm_host['ipaddress'],
            "uuid":
            uuid,
            "hostname":
            instance_name,  # 实例名
            "memory_mb":
            flavor_info['memory_mb'],
            "vcpu":
            flavor_info['vcpu'],
            "ostype":
            instance_system,
            "user_id":
            user_id,
            "disks":
            image_list,
            "disk_size":
            vm_disk_gb,
            "image_name":
            _image['url'].split('/')[-1],
            "net_area_id":
            net_area_id,
            "networks": [{
                "net_card_name": "br_bond0." + segment_data['vlan'],
                "ip": data_ip_address,
                "netmask": segment_data['netmask'],
                "dns1": segment_data['dns1'],
                "dns2": segment_data['dns2'],
                "mac": mac,
                "gateway": segment_data['gateway_ip'],
                "env": vm_env  # SIT STG PRD DR
            }]
        }
    }
    ret_kafka = send_async_msg(KAFKA_TOPIC_NAME, data)
コード例 #3
0
def v2v_openstack_del():

    #获取入参信息
    delete = request.values.get('delete')
    request_his = request.values.get('request_id')
    source = v2v_op.v2vTaskService().get_v2v_task_by_requestid(
        request_his)['source']

    if not source:
        return json_helper.format_api_resp(code=ErrorCode.PARAM_ERR,
                                           msg='v2v来源缺失')

    if delete != '1':
        return json_helper.format_api_resp(code=ErrorCode.PARAM_ERR,
                                           msg='参数错误')

    #判断当前任务是否完成
    data, message = v2v_op.get_v2v_deleteable(request_his)
    if data != '2':
        return_msg = message
        return json_helper.format_api_resp(code=ErrorCode.PARAM_ERR,
                                           msg=return_msg)
    else:
        if source == VMCreateSource.OPENSTACK:
            del_res, del_msg = del_action(request_his)
            if del_res == False:
                return json_helper.format_api_resp(code=ErrorCode.SYS_ERR,
                                                   msg=del_msg)
        else:
            tag, errmsg = esx_del_action(request_his)
            if not tag:
                return json_helper.format_api_resp(code=ErrorCode.SYS_ERR,
                                                   msg=errmsg)

        v2v_task = v2v_op.v2vTaskService().get_v2v_task_by_requestid(
            request_his)
        v2v_vm_uuid = v2v_task['vm_uuid']
        #更新instance表
        instance_info = ins_s.InstanceService().get_instance_info_by_uuid(
            v2v_vm_uuid)
        instance_id = instance_info['id']
        update_data = {'isdeleted': '1', 'deleted_at': get_datetime_str()}
        where_data = {'id': instance_id}
        ret = ins_s.InstanceService().update_instance_info(
            update_data, where_data)
        if ret != 1:
            logging.error('删除instance %s 错误', instance_id)
            return json_helper.format_api_resp(code=ErrorCode.SYS_ERR,
                                               msg='删除instance错误')

        # instance_flavor
        ret_f = ins_f_s.InstanceFlavorService().delete_instance_flavor(
            instance_id)
        if ret_f != 1:
            logging.error('delete instance %s flavor error', instance_id)
            return json_helper.format_api_resp(code=ErrorCode.SYS_ERR,
                                               msg='删除instance flavor错误')

        # instance_group
        ret_g = ins_g_s.InstanceGroupService().delete_instance_group_info(
            instance_id)
        if ret_g != 1:
            logging.error('delete instance %s group error', instance_id)
            return json_helper.format_api_resp(code=ErrorCode.SYS_ERR,
                                               msg='删除instance group错误')

        # instance_host
        update_data_h = {'isdeleted': '1', 'deleted_at': get_datetime_str()}
        where_data_h = {'instance_id': instance_id}
        ret_h = ins_h_s.InstanceHostService().update_instance_host_info(
            update_data_h, where_data_h)
        if ret_h != 1:
            logging.error('delete instance %s host error', instance_id)
            return json_helper.format_api_resp(code=ErrorCode.SYS_ERR,
                                               msg='删除instance host错误')

        # instance_disk
        update_data_d = {'isdeleted': '1', 'deleted_at': get_datetime_str()}
        where_data_d = {'instance_id': instance_id}
        ret_d = ins_d_s.InstanceDiskService().update_instance_disk_info(
            update_data_d, where_data_d)
        if ret_d != 1:
            logging.error('delete instance %s disk error', instance_id)
            return json_helper.format_api_resp(code=ErrorCode.SYS_ERR,
                                               msg='删除instance disk错误')

        # instance_ip
        update_data_ip = {'isdeleted': '1', 'deleted_at': get_datetime_str()}
        where_data_ip = {'instance_id': instance_id}
        ret_i_ip = ins_ip_s.InstanceIPService().update_instance_ip_info(
            update_data_ip, where_data_ip)
        if ret_i_ip != 1:
            logging.error('delete instance %s ip error', instance_id)
            return json_helper.format_api_resp(code=ErrorCode.SYS_ERR,
                                               msg='删除instance ip错误')

        #更新v2v_instance_info
        update_data = {'isdeleted': '1', 'deleted_at': get_datetime_str()}
        where_data = {'instance_id': instance_id}
        ret_v2v_in_i = v2v_in_i.v2vInstanceinfo().update_v2v_status(
            update_data, where_data)
        if ret_v2v_in_i != 1:
            logging.error('delete v2v instance info error')
            return json_helper.format_api_resp(code=ErrorCode.SYS_ERR,
                                               msg='删除v2v instance info错误')

        # 删除vm的ip
        ip_data = ins_s.get_ip_of_instance(instance_id)
        ip_id = ip_data['id']
        if ip_data:
            ip_s.del_ip_info(ip_id)

        #更新v2v_task表
        where_v2v = {'request_id': request_his}
        update_v2v = {'destory': '1'}
        v2v_op.v2vTaskService().update_v2v_status(update_v2v, where_v2v)

        return json_helper.format_api_resp(code=ErrorCode.SUCCESS,
                                           msg='删除v2v任务完成')