Beispiel #1
0
def add_instance_actions_when_vm_image_check(data):
    '''
        插入虚拟机的步骤信息
    :param data:
    :return:
    '''
    action = data['action']
    instance_uuid = data['instance_uuid']
    request_id = data['request_id']
    user_id = data['user_id']
    message = data['message']
    task_id = data['task_id']
    insert_data = {
        'action': action,
        'instance_uuid': instance_uuid,
        'request_id': request_id,
        'task_id': task_id,
        'user_id': user_id,
        'message': message,
        'start_time': get_datetime_str()
    }
    ret = InstanceActionsServices().add_instance_action_info(insert_data)
    if ret['last_id'] > 0:
        action_id = ret.get('last_id')
        return True, action_id
    return False, 0
Beispiel #2
0
def add_instance_actions_test(data):
    '''
        插入虚拟机的步骤信息
    :param data:
    :return:
    '''
    action = data['action']
    instance_uuid = data['instance_uuid']
    task_id = data['task_id']
    request_id = data['request_id']
    user_id = data['user_id']
    message = data['message']
    status = data['status']
    finish_time = data['finish_time']  #add
    insert_data = {
        'action': action,
        'instance_uuid': instance_uuid,
        'task_id': task_id,
        'request_id': request_id,
        'user_id': user_id,
        'status': status,
        'message': message,
        'start_time': get_datetime_str(),
        'finish_time': finish_time  #add
    }
    ret = InstanceActionsServices().add_instance_action_info(insert_data)
    if ret['last_id'] > 0:
        return True
    return False
Beispiel #3
0
def hostpool_add(net_area_id):
    name = request.values.get('name')
    least_host_num = request.values.get('least_host_num')
    hostpool_type = request.values.get('hostpool_type')  # '0'一般类型,'1'特殊类型
    app_code = request.values.get('app_code')

    if not net_area_id or not name or not least_host_num:
        logging.error('the params is invalid when add hostpool')
        return json_helper.format_api_resp(code=ErrorCode.PARAM_ERR)

    # 名字不重复
    ret = hostpool_s.HostPoolService().check_name_exist(net_area_id, name)
    if ret > 0:
        logging.error('hostpool name is duplicated when add hostpool')
        return json_helper.format_api_resp(code=ErrorCode.DUPLICATED_ERR,
                                           msg='集群名不能重复,请更换集群名')

    if int(hostpool_type) == 0:
        app_code = "公共资源池"

    insert_data = {
        'name': name,
        'displayname': name,
        'isdeleted': '0',
        'net_area_id': net_area_id,
        'least_host_num': least_host_num,
        'app_code': app_code,
        'hostpool_type': hostpool_type,
        'created_at': get_datetime_str()
    }
    ret = hostpool_s.HostPoolService().add_hostpool(insert_data)
    if ret.get('row_num') <= 0:
        return json_helper.format_api_resp(code=ErrorCode.SYS_ERR)
    return json_helper.format_api_resp(code=ErrorCode.SUCCESS)
Beispiel #4
0
def update_v2v_actions(request_id, status):
    '''
     更新v2v任务状态
    '''
    update_data = {'status': status, 'finish_time': get_datetime_str()}
    where_data = {
        'request_id': request_id,
    }
    ret = v2vTaskService().update_v2v_status(update_data, where_data)
    if ret > 0:
        return True
    return False
Beispiel #5
0
def updata_v2v_retry(request_id, status):
    '''
            retry则更新status的值为0
            :param request_id:
            :param retry:
            :return:
            '''
    update_data = {
        'status': status,
        'message': '正在重试',
        'start_time': get_datetime_str(),
        'finish_time': None
    }

    where_data = {'request_id': request_id}
    ret = v2vTaskService().update_v2v_status(update_data, where_data)
    if ret > 0:
        return True
    return False
Beispiel #6
0
def update_instance_actions(request_id, action, status, message):
    '''
        更新虚拟机的步骤信息
    :param request_id:
    :param action:
    :param status:
    :param message:
    :return:
    '''
    update_data = {
        'status': status,
        'finish_time': get_datetime_str(),
        'message': message
    }
    where_data = {'request_id': request_id, 'action': action}
    ret = InstanceActionsServices().update_instance_action_status(
        update_data, where_data)
    if ret > 0:
        return True
    return False
Beispiel #7
0
def update_instance_actions_when_image_check(request_id, action, status,
                                             message, task_id, action_id):
    '''
        更新虚拟机的镜像确认步骤信息
    :param request_id:
    :param action:
    :param status:
    :param message:
    :return:
    '''
    update_data = {
        'status': status,
        'finish_time': get_datetime_str(),
        'message': message,
        'task_id': task_id
    }
    where_data = {'request_id': request_id, 'action': action, 'id': action_id}
    ret = InstanceActionsServices().update_instance_action_status(
        update_data, where_data)
    if ret > 0:
        return True
    return False
Beispiel #8
0
def hostpool_update(hostpool_id):
    name = request.values.get('name')
    least_host_num = request.values.get('least_host_num')
    if not hostpool_id or not name or not least_host_num:
        logging.error('the params is invalid when update hostpool')
        return json_helper.format_api_resp(code=ErrorCode.PARAM_ERR)

    old_hostpool = hostpool_s.HostPoolService().get_hostpool_info(hostpool_id)
    if not old_hostpool:
        logging.error(
            'the hostpool %s is not exist in db when upudate hostpool',
            hostpool_id)
        return json_helper.format_api_resp(code=ErrorCode.SYS_ERR)

    # 名字不重复
    if old_hostpool['name'] != name:
        ret = hostpool_s.HostPoolService().check_name_exist(
            old_hostpool['net_area_id'], name)
        if ret > 0:
            logging.error('hostpool name is duplicated when add hostpool')
            return json_helper.format_api_resp(code=ErrorCode.DUPLICATED_ERR,
                                               msg='集群名不能重复,请更换集群名')

    update_data = {
        'name': name,
        'displayname': name,
        'least_host_num': least_host_num,
        'updated_at': get_datetime_str()
    }
    where_data = {
        'id': hostpool_id,
    }
    ret = hostpool_s.HostPoolService().update_hostpool_info(
        update_data, where_data)
    if ret < 0:
        logging.error("update hostpool error, update_data:%s, where_data:%s",
                      str(update_data), str(where_data))
        return json_helper.format_api_resp(code=ErrorCode.SYS_ERR)
    return json_helper.format_api_resp(code=ErrorCode.SUCCESS)
Beispiel #9
0
def add_instance_actions(data):
    '''
        插入虚拟机的步骤信息
    :param data:
    :return:
    '''
    action = data['action']
    instance_uuid = data['instance_uuid']
    request_id = data['request_id']
    user_id = data['user_id']
    message = data['message']
    insert_data = {
        'action': action,
        'instance_uuid': instance_uuid,
        'request_id': request_id,
        'user_id': user_id,
        'message': message,
        'start_time': get_datetime_str()
    }
    ret = InstanceActionsServices().add_instance_action_info(insert_data)
    if ret['last_id'] > 0:
        return True
    return False
Beispiel #10
0
def delete_group(group_id):
    '''
    删除用户组的流程:查询instance_group表,如果group在表中,就在tb_group表中将group的isdeleted改为1,
    然后在user_group表中删除所有group_id的行,在access表中删除group信息
    如果group不在instance_group表中,则直接删除group
    '''
    def _check_ins_exist_in_group(group_id):
        '''
            检查组下是否有VM
        :param group_id:
        :return:
        '''
        instances = ins_s.get_instances_in_group(group_id)
        if len(instances) > 0:
            return True
        return False

    if not group_id:
        logging.info('no group_id when delete group')
        return json_helper.format_api_resp(code=ErrorCode.PARAM_ERR)

    group_num, group_data = group_s.GroupService().get_group_info(group_id)
    if group_num <= 0:
        logging.error('no group %s info in db when delete group', group_id)
        return json_helper.format_api_resp(code=ErrorCode.SYS_ERR)

    # 只能超级管理员组成员和组所有者才能删除,超级管理员组ID为1
    user_group_ids = current_user_group_ids()
    if group_data[0]['owner'] != get_user(
    )['user_id'] and 1 not in user_group_ids:
        logging.error(
            'only allow super group member or group owner %s to delete group %s',
            group_data[0]['owner'], group_id)
        return json_helper.format_api_resp(code=ErrorCode.SYS_ERR,
                                           msg='您没有权限删除该应用组!')

    # 该应用组下没有VM才能删除
    if _check_ins_exist_in_group(group_id):
        logging.error(
            'no allow delete group %s which has vms when delete group',
            group_id)
        return json_helper.format_api_resp(code=ErrorCode.SYS_ERR,
                                           msg='该应用组分配有虚拟机,不能删除该组!')

    # 删除access中所有group_id的数据,然后插入前端提交的数据
    delete_access = access.delete_access_info(group_id)
    if delete_access < 0:
        return json_helper.format_api_resp(code=ErrorCode.SYS_ERR)
    rest = ins_g_s.InstanceGroupService().query_data(
        where_field="group_id", where_field_value=group_id)
    # 如果instance_group表中没有group_id,就直接在group表中删除它
    if not rest:
        group.delete_group(group_id)

    else:
        update_data = {'isdeleted': '1', 'deleted_at': get_datetime_str()}
        where_data = {
            'id': group_id,
        }
        ret = group_s.update_group_info(update_data, where_data)
        if ret < 0:
            logging.error("update group error, update_data:%s, where_data:%s",
                          str(update_data), str(where_data))
            return json_helper.format_api_resp(code=ErrorCode.SYS_ERR)
    # 不管group在不在Instance_group表中,都删除它下面的所有用户
    delete_users = user_g_s.delete_users_in_group(group_id)
    if delete_users < 0:
        return json_helper.format_api_resp(code=ErrorCode.SYS_ERR)
    return json_helper.format_api_resp(code=ErrorCode.SUCCESS)
Beispiel #11
0
def add_group():
    name = request.values.get('name')
    owner = request.values.get('owner')
    cpu = request.values.get('cpu')
    mem = request.values.get('mem')
    disk = request.values.get('disk')
    vm = request.values.get('vm')
    env = request.values.get('dc_type')
    role_id = request.values.get('role_id')
    area_str = request.values.get('area_str')
    p_cluster_id = request.values.get('p_cluster_id')

    if not name or not owner or not area_str or not str(env):
        logging.info('no name or owner or area_str or env when add group')
        return json_helper.format_api_resp(code=ErrorCode.PARAM_ERR)

    role_exist = role_service.RoleService().query_role('id', role_id)
    if not role_exist:
        logging.info('no such role %s when add group', role_id)
        return json_helper.format_api_resp(code=ErrorCode.PARAM_ERR)

    # 应用组名不能重复
    if _check_duplicated_group_name(name, str(env)):
        logging.error('duplicated group name %s when add group', name)
        return json_helper.format_api_resp(code=ErrorCode.DUPLICATED_ERR,
                                           msg="应用组名不能重复,请更改应用组名!")

    params_u = {
        'WHERE_AND': {
            '=': {
                'userid': owner,
                'isdeleted': '0',
            },
        },
    }
    user_num, user_data = user_s.UserService().query_data(**params_u)
    if user_num <= 0:
        username = ''
        # 用户不存在,新增
        user_data = {
            'userid': owner,
            'username': username,
            'status': '0',
            'created_at': get_datetime_str()
        }
        user_ret = user_s.UserService().add_user(user_data)

        # 记录安全日志
        field_data = {'User_name': owner or None, 'Oper_type': 'add'}
        if user_ret.get('row_num') > 0:
            field_data.update({'Oper_result': '1 Success'})
            CloudLogger.audit(AuditType.USERMGR, field_data)
        else:
            field_data.update({
                'Oper_result': '0 Fail',
                'fail_reason': 'insert new user info to db fail'
            })
            CloudLogger.audit(AuditType.USERMGR, field_data)
            return False, 'add new user info to db fail'
        # logging.error('no user %s info when add group', owner)
        # return json_helper.format_api_resp(code=ErrorCode.NOT_EXIST_USER, msg='不存在该用户,请检查用户ID')
    else:
        username = user_data[0]['username']

    insert_data = {
        'name': name,
        'displayname': name,
        'isdeleted': '0',
        'owner': owner,
        'cpu': cpu,
        'mem': mem,
        'disk': disk,
        'vm': vm,
        'dc_type': str(env),
        'p_cluster_id': p_cluster_id,
        'created_at': get_datetime_str()
    }
    ret = group_s.GroupService().add_group_info(insert_data)
    if ret.get('row_num') <= 0:
        return json_helper.format_api_resp(code=ErrorCode.SYS_ERR)

    group_id = ret.get('last_id')
    ret_result = access_service.add_access_list(int(group_id), int(role_id),
                                                str(area_str))
    if ret_result.get('row_num') <= 0:
        return json_helper.format_api_resp(code=ErrorCode.SYS_ERR)

    user_group_data = {
        'user_id': owner,
        'user_name': username,
        'group_id': group_id,
        'group_name': name,
        'role_id': role_id,
        'status': '0',
        'created_at': get_datetime_str(),
        'expire_at': get_datetime_str(),  # todo
    }
    ret_u_g = user_g_s.UserGroupService().add_user_group(user_group_data)
    if ret_u_g.get('row_num') <= 0:
        logging.error(
            'add user group info error when add group, insert_data:%s',
            user_group_data)
        return json_helper.format_api_resp(code=ErrorCode.SYS_ERR)

    return json_helper.format_api_resp(code=ErrorCode.SUCCESS)
Beispiel #12
0
def update_group(group_id):
    '''
        修改应用组信息
    :param group_id:
    :return:
    '''
    name_n = request.values.get('name')
    owner_n = request.values.get('owner')
    cpu_n = request.values.get('cpu')
    mem_n = request.values.get('mem')
    disk_n = request.values.get('disk')
    vm_n = request.values.get('vm')
    area_str_n = request.values.get('area_str')
    role_id_c = request.values.get('role_id')
    p_cluster_id = request.values.get('p_cluster_id')

    if not cpu_n or not mem_n or not disk_n or not vm_n or not area_str_n or not role_id_c:
        logging.info('param is invalid when update group')
        return json_helper.format_api_resp(code=ErrorCode.PARAM_ERR)

    group_num_c, group_data_c = group_s.GroupService().get_group_info(group_id)
    if group_num_c <= 0:
        logging.error('no group %s info when update group', group_id)
        return json_helper.format_api_resp(code=ErrorCode.SYS_ERR)

    update_data = {
        'cpu': cpu_n,
        'mem': mem_n,
        'disk': disk_n,
        'vm': vm_n,
        'updated_at': get_datetime_str()
    }

    # 只能超级管理员组成员和组所有者才能修改组名和所有者,超级管理员组ID为1
    user_group_ids = current_user_group_ids()
    if group_data_c[0]['owner'] != get_user(
    )['user_id'] and 1 not in user_group_ids:
        if name_n or owner_n:
            logging.error(
                'only allow super group member or group owner %s to update group %s name or owner',
                group_data_c[0]['owner'], group_id)
            return json_helper.format_api_resp(code=ErrorCode.SYS_ERR,
                                               msg='您没有权限修改该应用组的组名和所有者!')

    if name_n:
        if group_data_c[0]['name'] != name_n and _check_duplicated_group_name(
                name_n):
            logging.error('duplicated group name %s when update group', name_n)
            return json_helper.format_api_resp(code=ErrorCode.DUPLICATED_ERR,
                                               msg="应用组名不能重复,请更改应用组名!")

        update_data['name'] = name_n
    if owner_n:
        # 新所有者一定要在该应用组里
        group_user_num, group_user_data = user_g_s.UserGroupService(
        ).get_alluser_group(group_id)
        in_group_flag = False
        for _group in group_user_data:
            if _group['user_id'] == owner_n:
                in_group_flag = True
                break

        if not in_group_flag:
            logging.error(
                'new owner %s must exist in group %s when update group',
                owner_n, group_id)
            return json_helper.format_api_resp(code=ErrorCode.SYS_ERR,
                                               msg='应用组新所有者必须属于该组,请先添加入组!')

        update_data['owner'] = owner_n

    if p_cluster_id:
        update_data['p_cluster_id'] = p_cluster_id

    where_data = {
        'id': group_id,
    }
    ret = group_s.update_group_info(update_data, where_data)
    if ret < 0:
        logging.error("update group error, update_data:%s, where_data:%s",
                      str(update_data), str(where_data))
        return json_helper.format_api_resp(code=ErrorCode.SYS_ERR)

    # 删除access中所有group_id的数据,然后插入前端提交的数据
    delete_access = access.delete_access_info(group_id)
    if delete_access < 0:
        return json_helper.format_api_resp(code=ErrorCode.SYS_ERR)

    ret_result = access_service.add_access_list(int(group_id), int(role_id_c),
                                                str(area_str_n))
    if ret_result < 0:
        return json_helper.format_api_resp(code=ErrorCode.SYS_ERR)
    return json_helper.format_api_resp(code=ErrorCode.SUCCESS)
Beispiel #13
0
def net_area_add():
    datacenter_id = request.values.get('datacenter_id')
    name = request.values.get('name')
    imagecache01 = request.values.get('imagecache01')
    imagecache02 = request.values.get('imagecache02')

    if not name or not datacenter_id or not imagecache01 or not imagecache02:
        logging.info('no area_name or datacenter_id when add net_area')
        return json_helper.format_api_resp(code=ErrorCode.PARAM_ERR)

    dc_data = dc_s.DataCenterService().get_datacenter_info(datacenter_id)
    if not dc_data:
        logging.error('datacenter %s no exist in db when add net_area',
                      datacenter_id)
        return json_helper.format_api_resp(code=ErrorCode.SYS_ERR)

    # 同一环境下机房下的网络区域不能重名
    name_exist = net_area_s.check_name_exist_in_same_dc_type(
        name, dc_data['dc_type'])
    if name_exist:
        logging.error('name %s in dc_type %s is duplicated when add net_area',
                      name, dc_data['dc_type'])
        return json_helper.format_api_resp(code=ErrorCode.DUPLICATED_ERR,
                                           msg='同环境机房下的网络区域名不能重复,请修改网络区域名')

    # 两台imagecache不能重复
    if imagecache01 == imagecache02:
        logging.info('imagecache address duplicated')
        return json_helper.format_api_resp(code=ErrorCode.PARAM_ERR,
                                           msg="镜像缓存服务器地址重复")

    insert_data = {
        'datacenter_id': datacenter_id,
        'name': name,
        'displayname': name,
        'created_at': get_datetime_str(),
    }
    ret = net_area_s.NetAreaService().add_net_area(insert_data)
    if ret == -1:
        return json_helper.format_api_resp(code=ErrorCode.SYS_ERR)

    net_area_id = ret.get('last_id')
    insert_imagecache01 = {
        "net_area_id": net_area_id,
        "imagecache_ip": imagecache01,
        "create_at": get_datetime_str()
    }
    ret = imca_s.ImageCacheService().add_imagecache_info(insert_imagecache01)
    if ret.get('row_num') <= 0:
        logging.error("add imagecache01 error, insert_data:%s",
                      str(insert_data))
        return json_helper.format_api_resp(code=ErrorCode.SYS_ERR,
                                           msg="新增cache01地址失败")

    insert_imagecache02 = {
        "net_area_id": net_area_id,
        "imagecache_ip": imagecache02,
        "create_at": get_datetime_str()
    }
    ret = imca_s.ImageCacheService().add_imagecache_info(insert_imagecache02)
    if ret.get('row_num') <= 0:
        logging.error("add imagecache02 error, insert_data:%s",
                      str(insert_data))
        return json_helper.format_api_resp(code=ErrorCode.SYS_ERR,
                                           msg="新增cache02地址失败")

    return json_helper.format_api_resp(code=ErrorCode.SUCCESS, msg="新增网络区域成功")
Beispiel #14
0
def net_area_update(net_area_id):
    name = request.values.get('name')
    imagecache01 = request.values.get('imagecache01')
    imagecache02 = request.values.get('imagecache02')
    if not net_area_id or not name or not imagecache01 or not imagecache02:
        logging.error('the params is invalid when update net area')
        return json_helper.format_api_resp(code=ErrorCode.PARAM_ERR)

    net_area_data = net_area_s.NetAreaService().get_net_area_info(net_area_id)
    if not net_area_data:
        logging.error('net area %s is no exist in db when update net area',
                      net_area_id)
        return json_helper.format_api_resp(code=ErrorCode.SYS_ERR)

    if net_area_data['name'] != name:
        dc_data = dc_s.DataCenterService().get_datacenter_info(
            net_area_data['datacenter_id'])
        if not dc_data:
            logging.error(
                'datacenter %s is no exist in db when update net area',
                net_area_data['datacenter_id'])
            return json_helper.format_api_resp(code=ErrorCode.SYS_ERR)

        # 同一环境下机房下的网络区域不能重名
        name_exist = net_area_s.check_name_exist_in_same_dc_type(
            name, dc_data['dc_type'])
        if name_exist:
            logging.error(
                'name %s in dc_type %s is duplicated when update net area',
                name, dc_data['dc_type'])
            return json_helper.format_api_resp(code=ErrorCode.DUPLICATED_ERR,
                                               msg='同环境机房下的网络区域名不能重复,请修改网络区域名')

    # 两台imagecache不能重复
    if imagecache01 == imagecache02:
        logging.info('imagecache address duplicated')
        return json_helper.format_api_resp(code=ErrorCode.PARAM_ERR,
                                           msg="镜像缓存服务器地址重复")

    update_data = {
        'name': name,
        'displayname': name,
        'updated_at': get_datetime_str()
    }
    where_data = {
        'id': net_area_id,
    }
    ret = net_area_s.NetAreaService().update_net_area_info(
        update_data, where_data)
    if ret < 0:
        logging.error("update net area error, update_data:%s, where_data:%s",
                      str(update_data), str(where_data))
        return json_helper.format_api_resp(code=ErrorCode.SYS_ERR)

    imagecache_data = imca_s.ImageCacheService(
    ).get_imagecache_info_by_net_area_id(net_area_id)
    imagecache01_data = imagecache_data[0]
    imagecache02_data = imagecache_data[1]
    exist_cache_ids = [imagecache01_data['id'], imagecache02_data['id']]
    # update imagecache01
    where_data = {"id": exist_cache_ids[0]}
    update_data = {"imagecache_ip": imagecache01}
    ret = imca_s.ImageCacheService().update_imagecache_info(
        update_data, where_data)
    # update imagecache02
    where_data = {"id": exist_cache_ids[1]}
    update_data = {"imagecache_ip": imagecache02}
    ret = imca_s.ImageCacheService().update_imagecache_info(
        update_data, where_data)
    return json_helper.format_api_resp(code=ErrorCode.SUCCESS)