Example #1
0
    def user_instance(self, one_db_data):
        self.hostpool = one_db_data['hostpool_name']
        self.instance_id = one_db_data['id']
        self.name = one_db_data['instance_name']
        self.displayname = one_db_data['displayname']
        self.uuid = one_db_data['uuid']
        self.status = one_db_data['status']
        self.owner = one_db_data['owner']
        self.ip_address = one_db_data['ip_address']
        self.group_id = one_db_data['group_id']
        self.host_ip = one_db_data['host_ip']
        self.dc_type = one_db_data['dc_type']
        self.request_id = one_db_data['request_id']
        self.app_info = one_db_data['app_info']
        self.hostpool_id = one_db_data['hostpool_id']
        self.flavor_id = one_db_data['flavor_id']
        self.create_source = one_db_data['create_source']
        # v2v机器则在v2v_instance_info表获取system信息
        if one_db_data['create_source'] != '0':
            self.system = v2v_ins.v2vInstanceinfo(
            ).get_v2v_instance_info_by_instance_id(
                one_db_data['id'])['os_type']
        else:
            self.system = one_db_data['system']

        return self
def instance_os_version_modify(instance_id):
    '''
        修改os版本为unknown的虚拟机
    :param instance_id:
    :json param :
    :return:
    '''
    req_json = request.data
    req_data = json_helper.loads(req_json)
    n_os_version = req_data["os_version"]

    if not instance_id or not str(n_os_version):
        logging.info(
            'no instance_id or os_version when modify instance os version')
        return json_helper.format_api_resp(code=ErrorCode.PARAM_ERR)

    ins_data = ins_s.InstanceService().get_instance_info(instance_id)
    if not ins_data:
        logging.error(
            'instance %s info is no exist in db when modify instance os version',
            instance_id)
        return json_helper.format_api_resp(code=ErrorCode.SYS_ERR)

    # VM创建来源
    create_source = ins_data['create_source']

    # 本平台
    if create_source == VMCreateSource.OPENSTACK or create_source == VMCreateSource.ESX:
        # v2v迁移的VM
        ins_v2v_data = v2v_i_i_s.v2vInstanceinfo(
        ).get_v2v_instance_info_by_instance_id(instance_id)
        if ins_v2v_data:
            if ins_v2v_data['os_version'] and ins_v2v_data[
                    'os_version'] == "unknown":
                # 更新v2v虚拟机os版本
                update_data = {'os_version': n_os_version}
                where_data = {'instance_id': instance_id}
                v2v_i_i_s.v2vInstanceinfo().update_v2v_status(
                    update_data, where_data)
    else:
        # todo: kvm2.0需要增加对平台自身创建虚拟机操作系统版本校验,unknown的可以修改
        pass

    return json_helper.format_api_resp(code=ErrorCode.SUCCESS,
                                       msg="操作系统版本更新成功")
Example #3
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
def instance_detail(instance_id):
    '''
        获取虚机详情
    :param instance_id:
    :return:
    '''
    if not instance_id:
        logging.info('no instance_id when get instance detail')
        return json_helper.format_api_resp(code=ErrorCode.PARAM_ERR)

    ins_data = ins_s.InstanceService().get_instance_info(instance_id)
    if not ins_data:
        logging.error(
            'instance %s info is no exist in db when get instance detail',
            instance_id)
        return json_helper.format_api_resp(code=ErrorCode.SYS_ERR)

    # VM创建来源
    create_source = ins_data['create_source']

    ins_flavor = ins_s.get_flavor_of_instance(instance_id)
    if not ins_flavor:
        logging.error(
            'instance %s flavor info is no exist in db when get instance detail',
            instance_id)
        cpu = None
        memory_mb = None
        root_disk_gb = None
    else:
        cpu = ins_flavor['vcpu']
        memory_mb = ins_flavor['memory_mb']
        root_disk_gb = ins_flavor['root_disk_gb']

    system = None
    sys_version = None
    image_name = None
    # 本平台
    if create_source == VMCreateSource.CLOUD_SOURCE:
        ins_images = ins_s.get_images_of_instance(instance_id)
        if not ins_images:
            logging.error(
                'instance %s image info is no exist in db when get instance detail',
                instance_id)
        else:
            system = ins_images[0]['system']
            sys_version = ins_images[0]['version']
            image_name = ins_images[0]['displayname']
    elif create_source == VMCreateSource.OPENSTACK or create_source == VMCreateSource.ESX:
        # v2v迁移的VM
        ins_v2v_data = v2v_i_i_s.v2vInstanceinfo(
        ).get_v2v_instance_info_by_instance_id(instance_id)
        if ins_v2v_data:
            system = ins_v2v_data['os_type']
            sys_version = ins_v2v_data['os_version']

    ins_group = ins_s.get_group_of_instance(instance_id)
    if not ins_group:
        logging.error(
            'instance %s group info is no exist in db when get instance detail',
            instance_id)
        group_name = None
    else:
        group_name = ins_group['name']

    ins_ip = ins_s.get_ip_of_instance(instance_id)
    if not ins_ip:
        logging.error(
            'instance %s ip info is no exist in db when get instance detail',
            instance_id)
        ip_address = None
    else:
        ip_address = ins_ip['ip_address']

    ins_netarea = ins_s.get_netarea_of_instance(instance_id)
    if not ins_netarea:
        logging.error(
            'instance %s net area info is no exist in db when get instance detail',
            instance_id)
        net_area = None
    else:
        net_area = ins_netarea['name']

    ins_disks = ins_s.get_disks_of_instance(instance_id)
    if not ins_disks:
        logging.error(
            'instance %s disks info is no exist in db when get instance detail',
            instance_id)
        disk_gb = None
    else:
        # disk_gb = ins_disks[0]['size_gb']
        disk_gb = sum([int(i['size_gb']) for i in ins_disks])

    ins_datacenter = ins_s.get_datacenter_of_instance(instance_id)
    if not ins_datacenter:
        logging.error(
            'instance %s datacenter info is no exist in db when get instance detail',
            instance_id)
        dc_type = None
    else:
        dc_type = ins_datacenter['dc_type']

    data = {
        'instance_name': ins_data['name'],
        'uuid': ins_data['uuid'],
        'ip_address': ip_address,
        'dc_type': dc_type,
        'net_area': net_area,
        'system': system,
        'sys_version': sys_version,
        'image_name': image_name,
        'cpu': cpu,
        'memory_mb': memory_mb,
        'root_disk_gb': root_disk_gb,
        'disk_gb': disk_gb,
        'app_info': ins_data['app_info'],
        'owner': ins_data['owner'],
        'group_name': group_name,
        'create_source': create_source
    }
    return json_helper.format_api_resp(code=ErrorCode.SUCCESS, data=data)
Example #5
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任务完成')