コード例 #1
0
    def post(self, request, api_request=None):
        if api_request is None:
            api_request = request.data

        schedule = get_object_or_404(FileSyncSchedule,
                                     id=api_request['schedule'])
        latest_host_snapshot, latest_snapshot_datetime = self._fetch_latest_snapshot(
            schedule.host)
        if not latest_host_snapshot:
            return Response('主机不存在可归档的快照点。', status=status.HTTP_403_FORBIDDEN)

        task_or_response = self._create_task(latest_host_snapshot, schedule,
                                             latest_snapshot_datetime)
        if isinstance(task_or_response, Response):
            return task_or_response
        else:
            task_flow = FileSyncFlowEntrance(
                task_or_response.id,
                'file_sync_{}'.format(task_or_response.id), create_sync_flow)
            task_or_response.start_datetime = timezone.now()
            ext_config = json.loads(task_or_response.ext_config)
            ext_config['running_task'] = task_flow.generate_uuid()
            task_or_response.ext_config = json.dumps(ext_config)
            task_or_response.save(
                update_fields=['start_datetime', 'ext_config'])
            task_flow.start()
            logic_processor = BackupTaskScheduleLogicProcessor(schedule)
            schedule.last_run_date = timezone.now()
            schedule.next_run_date = logic_processor.calc_next_run(False)
            schedule.save(update_fields=['last_run_date', 'next_run_date'])
            return Response(status=status.HTTP_201_CREATED)
コード例 #2
0
    def put(self, request, schedule_id, api_request=None):
        if api_request is None:
            api_request = request.data

        _logger.debug('api_request {}'.format(api_request))
        schedule = _check_archive_schedule_valid(schedule_id)

        serializer_old = self.serializer_class(schedule)
        data_old = serializer_old.data

        update_fields = list()

        if 'enabled' in api_request:
            schedule.enabled = api_request['enabled']
            update_fields.append('enabled')
        if 'name' in api_request:
            schedule.name = api_request['name']
            update_fields.append('name')
        if 'cycle_type' in api_request:
            schedule.cycle_type = api_request['cycle_type']
            update_fields.append('cycle_type')
        if 'plan_start_date' in api_request:
            schedule.plan_start_date = datetime.datetime.strptime(
                api_request['plan_start_date'], '%Y-%m-%d %H:%M:%S')
            update_fields.append('plan_start_date')
        if 'ext_config' in api_request:
            schedule.ext_config = api_request['ext_config']
            update_fields.append('ext_config')

        logic_processor = BackupTaskScheduleLogicProcessor(schedule)
        schedule.next_run_date = logic_processor.calc_next_run(True)
        update_fields.append('next_run_date')

        schedule.save(update_fields=update_fields)

        if ('enabled' in update_fields) and (not schedule.enabled):
            end_sleep.send_robust(sender=FileSyncSchedule,
                                  schedule_id=schedule.id)

        serializer_new = self.serializer_class(schedule)
        data_new = serializer_new.data
        if json.dumps(data_old, sort_keys=True) != json.dumps(data_new,
                                                              sort_keys=True):
            _logger.info(r'alter schedule : {}'.format(
                json.dumps(
                    {
                        'schedule_id': schedule.id,
                        'old': data_old,
                        'new': data_new
                    },
                    ensure_ascii=False)))

        return Response(data_new, status=status.HTTP_202_ACCEPTED)
コード例 #3
0
 def post(self, request, api_request=None):
     if api_request is None:
         api_request = request.data
     serializer = ArchiveTaskScheduleCreateSerializer(data=api_request)
     serializer.is_valid(True)
     if ArchiveSchedule.objects.filter(deleted=False, host_id=serializer.validated_data['host']).exists():
         return Response('主机已存在导出计划', status=status.HTTP_406_NOT_ACCEPTABLE)
     schedule = ArchiveSchedule.objects.create(**serializer.validated_data)
     logic_processor = BackupTaskScheduleLogicProcessor(schedule)
     schedule.next_run_date = logic_processor.calc_next_run(True)
     schedule.save(update_fields=['next_run_date'])
     return Response(data=self.serializer_class(schedule).data, status=status.HTTP_201_CREATED)
コード例 #4
0
    def put(self, request, schedule_id, api_request=None):
        if api_request is None:
            api_request = request.data

        schedule = _check_archive_schedule_valid(schedule_id)

        serializer_old = self.serializer_class(schedule)
        data_old = serializer_old.data

        serializer = ArchiveTaskScheduleUpdateSerializer(data=api_request)
        serializer.is_valid(True)

        update_fields = list()

        if 'enabled' in serializer.validated_data:
            schedule.enabled = serializer.validated_data['enabled']
            update_fields.append('enabled')
        if 'name' in serializer.validated_data:
            schedule.name = serializer.validated_data['name']
            update_fields.append('name')
        if 'cycle_type' in serializer.validated_data:
            schedule.cycle_type = serializer.validated_data['cycle_type']
            update_fields.append('cycle_type')
        if 'plan_start_date' in serializer.validated_data:
            schedule.plan_start_date = serializer.validated_data['plan_start_date']
            update_fields.append('plan_start_date')
        if 'ext_config' in serializer.validated_data:
            schedule.ext_config = serializer.validated_data['ext_config']
            update_fields.append('ext_config')
        if 'storage_node_ident' in serializer.validated_data:
            schedule.storage_node_ident = serializer.validated_data['storage_node_ident']
            update_fields.append('storage_node_ident')

        logic_processor = BackupTaskScheduleLogicProcessor(schedule)
        schedule.next_run_date = logic_processor.calc_next_run(True)
        update_fields.append('next_run_date')

        schedule.save(update_fields=update_fields)

        if ('enabled' in update_fields) and (not schedule.enabled):
            end_sleep.send_robust(sender=ArchiveSchedule, schedule_id=schedule.id)

        serializer_new = self.serializer_class(schedule)
        data_new = serializer_new.data
        if json.dumps(data_old, sort_keys=True) != json.dumps(data_new, sort_keys=True):
            _logger.info(r'alter schedule : {}'.format(
                json.dumps({'schedule_id': schedule.id, 'old': data_old, 'new': data_new}, ensure_ascii=False)))

        return Response(data_new, status=status.HTTP_202_ACCEPTED)
コード例 #5
0
 def post(self, request, api_request=None):
     if api_request is None:
         api_request = request.data
     if FileSyncSchedule.objects.filter(
             deleted=False,
             host__ident=api_request['source_host_ident']).exists():
         return Response('主机已存在导出计划', status=status.HTTP_406_NOT_ACCEPTABLE)
     schedule = FileSyncSchedule.objects.create(
         name=api_request['name'],
         cycle_type=api_request['cycle_type'],
         plan_start_date=datetime.datetime.strptime(
             api_request['plan_start_date'], '%Y-%m-%d %H:%M:%S'),
         host=Host.objects.get(ident=api_request['source_host_ident']),
         target_host_ident=api_request['target_host_ident'],
         ext_config=api_request['ext_config'])
     logic_processor = BackupTaskScheduleLogicProcessor(schedule)
     schedule.next_run_date = logic_processor.calc_next_run(True)
     schedule.save(update_fields=['next_run_date'])
     return Response(data=self.serializer_class(schedule).data,
                     status=status.HTTP_201_CREATED)
コード例 #6
0
    def post(self, request, api_request=None):
        if api_request is None:
            api_request = request.data

        serializer = self.serializer_class(data=api_request)
        serializer.is_valid(True)
        data = serializer.data

        schedule = get_object_or_404(ArchiveSchedule, id=data['schedule'])
        latest_host_snapshot, latest_snapshot_datetime = self._fetch_latest_snapshot(schedule.host)
        if not latest_host_snapshot:
            return Response('主机不存在可导出的快照点。', status=status.HTTP_403_FORBIDDEN)

        last_host_snapshot, last_snapshot_datetime = self._get_last_export_snapshot(schedule)
        if last_host_snapshot:
            last_cmp_data = last_snapshot_datetime if last_snapshot_datetime else last_host_snapshot.start_datetime
            latest_cmp_data = latest_snapshot_datetime if latest_snapshot_datetime else latest_host_snapshot.start_datetime
            if latest_cmp_data > last_cmp_data:
                host_snapshot = latest_host_snapshot
                snapshot_datetime = latest_snapshot_datetime
            else:
                return Response('主机不存在新的可导出备份点。', status=status.HTTP_403_FORBIDDEN)
        else:
            host_snapshot = latest_host_snapshot
            snapshot_datetime = latest_snapshot_datetime

        task_or_response = self._create_task(host_snapshot, schedule, snapshot_datetime, data['force_full'])
        if isinstance(task_or_response, Response):
            return task_or_response
        else:
            task_flow = FlowEntrance(task_or_response.id, 'export_flow_{}'.format(task_or_response.id), create_exp_flow)
            task_or_response.start_datetime = timezone.now()
            task_or_response.running_task = json.dumps(task_flow.generate_uuid())
            task_or_response.save(update_fields=['start_datetime', 'running_task'])
            task_flow.start()
            logic_processor = BackupTaskScheduleLogicProcessor(schedule)
            schedule.last_run_date = timezone.now()
            schedule.next_run_date = logic_processor.calc_next_run(False)
            schedule.save(update_fields=['last_run_date', 'next_run_date'])
            return Response(status=status.HTTP_201_CREATED)
コード例 #7
0
def createtask(request):
    taskid = request.POST.get('taskid')
    taskname = request.POST.get('taskname')
    storage_node_ident = request.POST.get('storagedevice')
    cycle_type = int(request.POST.get('schedule'))  # 2仅验证一次 3按间隔时间 4每周 5每月
    starttime = request.POST.get('starttime')
    timeinterval = request.POST.get('timeinterval')
    intervalUnit = request.POST.get('intervalUnit')
    perweek = request.POST.getlist('perweek', default=[])
    perweek = list(map(int, perweek))
    monthly = request.POST.getlist('monthly', default=[])
    monthly = list(map(int, monthly))
    verify_osname = int(request.POST.get('verify_osname', 0))
    verify_osver = int(request.POST.get('verify_osver', 0))
    verify_hdd = int(request.POST.get('verify_hdd', 0))
    last_point = int(request.POST.get('last_point', 1))  # 只验证最后一个点
    if timeinterval:
        backupDayInterval = int(timeinterval) * {
            'min': 60,
            'hour': 3600,
            'day': 24 * 3600
        }[intervalUnit]
    else:
        # 不参与运算,只是为了调用calc_next_run不出错
        backupDayInterval = 0

    script1 = request.POST.get('script1')
    script2 = request.POST.get('script2')
    script3 = request.POST.get('script3')
    script4 = request.POST.get('script4')
    script5 = request.POST.get('script5')
    script6 = request.POST.get('script6')
    script7 = request.POST.get('script7')
    script8 = request.POST.get('script8')

    kvm_memory_size = request.POST.get('kvm_memory_size')
    kvm_memory_unit = request.POST.get('kvm_memory_unit')

    script_list = list()
    script_list.append(script1)
    script_list.append(script2)
    script_list.append(script3)
    script_list.append(script4)
    script_list.append(script5)
    script_list.append(script6)
    script_list.append(script7)
    script_list.append(script8)

    result = {'r': 0, 'e': '操作成功'}

    ext_config = dict()
    ext_config['timeinterval'] = timeinterval
    ext_config['IntervalUnit'] = intervalUnit
    ext_config['daysInWeek'] = perweek
    ext_config['daysInMonth'] = monthly
    ext_config['backupDayInterval'] = backupDayInterval
    ext_config['script_list'] = script_list
    ext_config['kvm_memory_size'] = kvm_memory_size
    ext_config['kvm_memory_unit'] = kvm_memory_unit
    if last_point == 1:
        ext_config['verify_last_point_only'] = True
    else:
        ext_config['verify_last_point_only'] = False
    ext_config['verify_osname'] = True if verify_osname == 1 else False
    ext_config['verify_osver'] = True if verify_osver == 1 else False
    ext_config['verify_hdd'] = True if verify_hdd == 1 else False

    if not is_functional_available('auto_verify_task'):
        result = {'r': 1, 'e': '没有授权,请联系管理员'}
        return HttpResponse(json.dumps(result, ensure_ascii=False))

    count = get_functional_int_value('auto_verify_task')
    sc_count = AutoVerifySchedule.objects.all().count()
    if sc_count >= count:
        result = {'r': 1, 'e': '自动验证授权数为{},当前已用{}'.format(count, sc_count)}
        return HttpResponse(json.dumps(result, ensure_ascii=False))

    if taskid is None:
        verify_schedule = AutoVerifySchedule.objects.create(
            user=request.user,
            name=taskname,
            plan_start_date=starttime,
            cycle_type=cycle_type,
            storage_node_ident=storage_node_ident,
            ext_config=json.dumps(ext_config, ensure_ascii=False))
        verify_schedule_id = verify_schedule.id
        desc = {'操作': '自动验证计划', '计划名称': taskname}
        SaveOperationLog(request.user, OperationLog.TYPE_AUTO_VERIFY_TASK,
                         json.dumps(desc, ensure_ascii=False),
                         get_operator(request))
    else:
        verify_schedule_id = taskid
        AutoVerifySchedule.objects.filter(id=taskid).update(
            name=taskname,
            plan_start_date=starttime,
            cycle_type=cycle_type,
            storage_node_ident=storage_node_ident,
            ext_config=json.dumps(ext_config, ensure_ascii=False))
        desc = {'操作': '更改自动验证计划', '计划名称': taskname}
        SaveOperationLog(request.user, OperationLog.TYPE_AUTO_VERIFY_TASK,
                         json.dumps(desc, ensure_ascii=False),
                         get_operator(request))

    verify_schedule = AutoVerifySchedule.objects.get(id=verify_schedule_id)
    logicProcessor = BackupTaskScheduleLogicProcessor(verify_schedule)
    verify_schedule.next_run_date = logicProcessor.calc_next_run(True)
    verify_schedule.save(update_fields=['next_run_date'])

    return HttpResponse(json.dumps(result, ensure_ascii=False))