Example #1
0
    def import_user(self,
                    username,
                    bill_group,
                    role=None,
                    email=None,
                    first_name=None,
                    last_name=None):
        '''import user to antilles
        Arguments:
            username
            bill_group -- bill_group name
            role       -- user | operator | admin
            email      -- default None
            first_name -- default None
            last_name  -- default None
        '''
        try:
            import pwd
            pwd.getpwnam(username)
        except KeyError:
            raise EntryNotExistsException

        user = self.database.add_user(username,
                                      bill_group,
                                      role=role,
                                      email=email,
                                      first_name=first_name,
                                      last_name=last_name)

        # event log useing "create"
        EventLog.opt_create(self.operator, EventLog.user, EventLog.create,
                            EventLog.make_list(user['id'], username))
        return user
Example #2
0
    def put(self, request, pk):
        policy = Policy(
            metric_policy=request.data.get('metric'),
            name=request.data.get('name'),
            portal=json.dumps(request.data.get('portal')),
            duration=timedelta(seconds=request.data.get('duration')),
            status=request.data.get('status'),
            level=request.data.get('level'),
            # nodes=';'.join(request.data.get('nodes', ['all'])),
            nodes=format_nodes_filter_to_db(request.data.get('nodes')),
            creator=request.user.username,
            wechat=request.data.get('wechat'),
            sound=request.data.get('sound'),
            script=request.data.get('script'),
            language=request.data.get('language', 'en'),
        )
        old_plicy = Policy.objects.get(pk=pk)
        policy.create_time = old_plicy.create_time
        policy.pk = pk
        policy.save()
        for id in request.data['targets']:
            policy.targets.add(AlarmTarget.objects.get(id=id))
        policy.save()

        # add operationlog
        EventLog.opt_create(request.user.username, EventLog.policy,
                            EventLog.update,
                            EventLog.make_list(pk, old_plicy.name))
        return Response(status=HTTP_204_NO_CONTENT)
Example #3
0
    def post(self, request):
        user = User.objects.get(username=request.data['user'])
        input_bill_group = request.data['bill_group']
        bill_group = BillGroup.objects.get(id=input_bill_group)
        deposit = Deposit.objects.create(
            user=user,
            bill_group=bill_group,
            credits=float(
                request.data['credits']
            ),
            apply_time=timezone.now(),
            approved_time=timezone.now()
        )
        bill_group.balance += deposit.credits
        bill_group.balance = round(bill_group.balance, 2)
        bill_group.save()

        EventLog.opt_create(
            self.request.user.username,
            EventLog.deposit,
            DespositEventLog.action(deposit.credits),
            EventLog.make_list(
                deposit.id,
                '{0} {1:0.2f}'.format(bill_group.name, abs(deposit.credits))
            )
        )
        return Response(deposit.as_dict())
Example #4
0
    def put(self, request, pk):
        node = Node.objects.get(id=pk)
        hostname = node.hostname
        if 'operation' in request.data:
            operation = request.data['operation']
        else:
            raise InvalidParameterException

        try:
            if operation == 'turn_off':
                power.shutdown_device(node)
                node.power_status = False
                node.save()
            else:
                power.startup_device(node,
                                     request.data.get('bootmode', 'uefi'),
                                     request.data.get('nextdevice', None),
                                     request.data.get('persistent', 'False'))
                node.power_status = True
                node.save()

        except Exception:
            raise PowerOperationException
        else:
            # add operationlog
            EventLog.opt_create(request.user.username, EventLog.node,
                                getattr(EventLog, operation),
                                EventLog.make_list(pk, hostname))

        return Response()
Example #5
0
def test_optbase_already_exists(mocker):
    mocker.patch('antilles.optlog.models.LogDetail.objects.create',
                 side_effect=ObjectDoesNotExist('an exception'))

    EventLog.opt_create('zhanghe', 'Alarm', 'Create', [[1, 'antilles']])

    assert len(OperationLog.objects.all()) == 1
    assert len(LogDetail.objects.all()) == 0
Example #6
0
 def delete(self, request, pk):
     user = User.objects.get(pk=pk)
     user.effective_time = now()
     user.fail_chances = 0
     user.save()
     EventLog.opt_create(request.user.username, EventLog.user,
                         EventLog.update,
                         EventLog.make_list(pk, user.username))
     return Response()
Example #7
0
 def update_user(self, id, data):
     name = self.get_user(id)['username']
     with self.atomic():
         self.database.update_user(id, data)
         if 'os_group' in data and self.backend:
             self.backend.modify_user_group(name, data['os_group'])
         # event log
         db_user = self.database.get_user(name)  # for event log
         EventLog.opt_create(self.operator, EventLog.user, EventLog.update,
                             EventLog.make_list(db_user['id'], name))
         return True
Example #8
0
def test_optbase():
    EventLog.opt_create('zhanghe', 'Alarm', 'Create',
                        EventLog.make_list(1, 'antilles'))
    op = OperationLog.objects.all()
    log = LogDetail.objects.all()
    assert len(op) == 1
    assert op[0].operator == 'zhanghe'
    assert op[0].operation == 'Create'
    assert len(log) == 1
    assert log[0].name == 'antilles'
    assert log[0].id == 1
Example #9
0
    def handle(self, *args, **options):
        operator = get_operator()
        from antilles.optlog.optlog import EventLog
        username = options['username']
        user = User.objects.get(username=username)
        user.effective_time = now()
        user.fail_chances = 0
        user.save()

        EventLog.opt_create(operator, EventLog.user, EventLog.update,
                            EventLog.make_list(user.id, username))
        print_green('User {} unfreeze success.'.format(username))
Example #10
0
    def delete(self, request, pk):
        try:
            policy = Policy.objects.get(pk=pk)
            policy.delete()
        except Policy.DoesNotExist:
            return Response(status=HTTP_204_NO_CONTENT)

        # add operationlog
        EventLog.opt_create(self.request.user.username,
                            EventLog.policy, EventLog.delete,
                            EventLog.make_list(pk, policy.name))
        return Response(status=HTTP_204_NO_CONTENT)
Example #11
0
    def post(self, request, pk):
        comment = request.data.get('comment', None)
        alarm = Alarm.objects.get(id=pk)
        alarm.comment = comment
        alarm.save()

        # add operationlog
        EventLog.opt_create(request.user.username, EventLog.alarm,
                            EventLog.comment,
                            EventLog.make_list(pk, alarm.policy.name))

        return Response(status=HTTP_204_NO_CONTENT)
Example #12
0
 def patch(self, request, pk):
     try:
         BillGroup.objects.filter(id=pk).update(
             **request.data
         )
     except IntegrityError as e:
         raise_from(
             BillroupAlreadyExistsException, e
         )
     EventLog.opt_create(
         request.user.username, EventLog.billgroup, EventLog.update,
         [(pk, request.data['name'])]
     )
     return Response(BillGroup.objects.get(id=pk).as_dict())
Example #13
0
 def post(self, request):
     valid_param = getcallargs(BillGroup.objects.create, **request.data)
     logger.info(valid_param)
     try:
         billgroup = BillGroup.objects.create(**request.data)
         EventLog.opt_create(
             request.user.username, EventLog.billgroup, EventLog.create,
             [(billgroup.id, billgroup.name)]
         )
     except IntegrityError as e:
         raise_from(
             BillroupAlreadyExistsException, e
         )
     return Response(billgroup.as_dict())
Example #14
0
    def delete(self, request, pk):
        obj = BillGroup.objects.get(id=pk)

        try:
            data = obj.as_dict()
            obj.delete()
        except ProtectedError as e:
            raise_from(
                RemoveBillgroupHasMemberException, e
            )
        EventLog.opt_create(
            request.user.username, EventLog.billgroup, EventLog.delete,
            [(data['id'], data['name'])]
        )
        return Response(data)
Example #15
0
    def delete(self, request, pk, format=None):
        job = self.get_object(pk)
        user = request.user.username
        queryparams = request.query_params
        if queryparams.get("role") is None:
            if job.submiter != user:
                return Response(status=status.HTTP_403_FORBIDDEN)
        job.isdeleted = True
        job.save()

        # add  operationlog
        EventLog.opt_create(
            request.user.username, EventLog.job, EventLog.delete,
            EventLog.make_list(pk, job.jobname)
        )
        return Response(status=status.HTTP_200_OK)
Example #16
0
    def post(self, request):
        params = self.params(request.data)

        index = None
        for i, f in enumerate(params['filters']):
            if f['prop'] == 'id':
                index = i
                break
        if not params['filters'][index]['values']:
            del params['filters'][index]

        query = self.get_query(request)
        query = self.filters(query, params['filters'])  # filter

        # add operationlog
        [
            EventLog.opt_create(
                request.user.username, EventLog.alarm, params['action'],
                EventLog.make_list(alarm.id, alarm.policy.name))
            for alarm in query
        ]

        if params['action'] == 'confirm':
            query = query.filter(status__in=['present'])
            query.update(status=Alarm.CONFIRMED)
        elif params['action'] == 'solve':
            query.update(status=Alarm.RESOLVED)
        elif params['action'] == 'delete':
            query.delete()

        return Response({
            'ret': 'success',
        }, status=HTTP_200_OK)
Example #17
0
    def post(self, request, pk):
        freeze_time = {
            'days': request.data.get('days'),
            'hours': request.data.get('hours'),
        }
        freeze_time = timedelta(**freeze_time)
        user = User.objects.get(pk=pk)
        if user.role >= User.get_role_value('admin'):
            raise ConldNotFreezeAdminUserException
        user.fail_chances = 0
        user.effective_time = now() + freeze_time
        user.save()

        EventLog.opt_create(request.user.username, EventLog.user,
                            EventLog.update,
                            EventLog.make_list(pk, user.username))

        return Response()
Example #18
0
    def post(self, request, format=None):
        user = request.user.username
        workspace, billgroup = getcurrentuserinfo(request.user)
        data = request.data
        job_key_list = [i.name for i in Job._meta.fields]
        # action is create
        data["operatestatus"] = "creating"
        data["submiter"] = user
        data["workspace"] = workspace
        data["billgroup"] = billgroup
        data["qtime"] = int(time.time())
        data["json_body"] = json.dumps(request.data)

        if "resumejobid" in data and data["resumejobid"]:
            job = JobTemplate.objects.get(pk=data["resumejobid"],
                                          user_id=request.user.id)
            job.isdeleted = True
            job.save()
        new_job_dict = {k: v for k, v in data.iteritems() if k in job_key_list}
        new_job = Job.objects.create(**new_job_dict)
        data["id"] = new_job.id

        # letrain template
        if isinstance(data['template_id'], int):
            template_file = JobTemplate.objects.get(
                id=data['template_id']).template_file
        else:
            logger.error('Invalid template id')
            raise ParameterException

        jobfilename = self.validate_post(request, data, workspace,
                                         template_file)
        if not jobfilename:
            raise SubmitJobException
        data['jobfilename'] = jobfilename
        task = {"method": "create_job", "type": "job", "args": data}
        jsontask = json.dumps(task)
        RPCClient().cast("job", jsontask)

        # add  operationlog
        EventLog.opt_create(request.user.username, EventLog.job,
                            EventLog.create,
                            EventLog.make_list(new_job.id, new_job.jobname))
        return Response(data, status=status.HTTP_201_CREATED)
Example #19
0
 def remove_user(self, pk):
     '''remove user
     Arguments:
         user id
     '''
     name = self.database.get_user(pk)['username']
     with self.atomic():
         # remove database
         self.database.remove_user(pk)
         # remove backend
         if self.backend:
             user = self.backend.get_user(name)
             if user is None:
                 return True
             self.backend.remove_user(name)
     # event log
     EventLog.opt_create(self.operator, EventLog.user, EventLog.delete,
                         EventLog.make_list(pk, name))
     return True
Example #20
0
    def handle(self, *args, **options):
        username = options['username']
        role = options['role']
        operator = get_operator()

        try:
            user = User.objects.get(username=username)
        except User.DoesNotExist:
            print_red('The user {0} does not exist.'.format(username))
            sys.exit(1)

        if User.ROLES[role] != user.role:
            user.role = User.ROLES[role]
            user.save()

        print_green('change role to {0} finished for user {1}.'.format(
            role, username))

        from antilles.optlog.optlog import EventLog
        EventLog.opt_create(operator, EventLog.user, EventLog.update,
                            EventLog.make_list(user.id, username))
Example #21
0
    def add_user(self,
                 username,
                 bill_group,
                 role=None,
                 email=None,
                 first_name=None,
                 last_name=None,
                 os_group=None,
                 password=None):

        verify.username(username)
        # add to db
        user = self.database.add_user(username,
                                      bill_group,
                                      role=role,
                                      email=email,
                                      first_name=first_name,
                                      last_name=last_name)
        # add to backend
        if self.backend:
            if password:
                verify.password(password, do_raise=True)
            if os_group:
                group = self.backend.get_group(os_group)
                if group is None:
                    raise InvalidOSGroupException(msg='unknow osgroup')
                gid = group.gid
            else:
                gid = None

            # add to backend
            self.backend.add_user(username, password=password, gid=gid)

        user = self.get_user(username)
        # event log
        EventLog.opt_create(self.operator, EventLog.user, EventLog.create,
                            EventLog.make_list(user['id'], username))
        return user
Example #22
0
    def _cancel_job(self, request, pk):
        user = request.user.username
        data = request.data
        queryparams = request.query_params
        if "action" not in data:
            raise ParameterException
        if data["action"] != "cancel":
            raise ParameterException

        job = self.get_object(pk)
        if queryparams.get("role") is None:
            if job.submiter != user:
                return Response(status=status.HTTP_403_FORBIDDEN)
            if job.isdeleted:
                return Response(status=status.HTTP_404_NOT_FOUND)

        if job.status != "running" and job.status != "waiting" \
                and job.status != "holding" and job.status != "suspending" \
                and job.status != "queueing":
            return Response({
                "message": "job is not in the state available for cancellation"
            }, status=status.HTTP_404_NOT_FOUND)
        job.operatestatus = "cancelling"
        job.save()

        task = {"method": "cancel_job", "type": "job"}
        task["args"] = {"user": user, "id": job.id, "jobid": job.jobid}
        jsontask = json.dumps(task)
        RPCClient().cast("job", jsontask)

        # add  operationlog
        EventLog.opt_create(
            request.user.username, EventLog.job, EventLog.cancel,
            EventLog.make_list(pk, job.jobname)
        )
        return Response({"id": job.id, "jobid": job.jobid,
                         "status": "cancelling"}, status=status.HTTP_200_OK)
Example #23
0
    def post(self, request):
        try:
            duration = int(request.data.get('duration'))
        except ValueError:
            raise InvalidParameterException('Param duration must be integer.')
        if duration < 1:
            raise InvalidParameterException('Param duration must >= 1')

        policy = Policy(
            metric_policy=request.data.get('metric'),
            name=request.data.get('name'),
            portal=json.dumps(request.data.get('portal')),
            duration=timedelta(seconds=request.data.get('duration')),
            status=request.data.get('status'),
            level=request.data.get('level'),
            # nodes=';'.join(request.data.get('nodes', ['all'])),
            nodes=format_nodes_filter_to_db(request.data.get('nodes')),
            creator=request.user.username,
            wechat=request.data.get('wechat'),
            sound=request.data.get('sound'),
            script=request.data.get('script', ''),
            language=request.data.get('language', 'en'),
        )
        try:
            policy.save()
            for id in request.data.get('targets', []):
                policy.targets.add(AlarmTarget.objects.get(id=id))
            policy.save()
            # add operationlog
            EventLog.opt_create(request.user.username, EventLog.policy,
                                EventLog.create,
                                EventLog.make_list(policy.id, policy.name))
        except IntegrityError as e:
            logger.info(e, exc_info=True)
            raise PolicyExistsException
        return Response(status=HTTP_204_NO_CONTENT)
Example #24
0
    def post(self, request, format=None):
        import time
        user = request.user.username
        workspace, billgroup = self.getcurrentuserinfo(request.user)

        data = request.data
        if not self.validate_post(data):
            raise SubmitJobException

        job_key_list = [i.name for i in Job._meta.fields]

        action = data["action"]
        if action == "create":
            data["operatestatus"] = "creating"
            data["submiter"] = user
            data["workspace"] = workspace
            data["billgroup"] = billgroup
            data["qtime"] = int(time.time())
            data["json_body"] = json.dumps(request.data)

            if "resumejobid" in data and data["resumejobid"]:
                job = self.get_object(data["resumejobid"])
                job.isdeleted = True
                job.save()

            try:
                new_job_dict = {
                    k: v for k, v in data.iteritems() if k in job_key_list
                }
                new_job = Job.objects.create(**new_job_dict)

                data["id"] = new_job.id
                task = {"method": "create_job", "type": "job"}
                task["args"] = data
                jsontask = json.dumps(task)
                RPCClient().cast("job", jsontask)

                # add  operationlog
                EventLog.opt_create(
                    request.user.username, EventLog.job, EventLog.create,
                    EventLog.make_list(
                        new_job.id, new_job.jobname)
                )
                return Response(data, status=status.HTTP_201_CREATED)
            except Exception as e:
                logger.error("create new job error: {}".format(e))

        elif action == "rerun":
            id = data["id"]
            job = self.get_object(id)

            data = job.as_dict()
            data.pop('id')
            data["operatestatus"] = "creating"
            data["jobstatus"] = ""
            data["submiter"] = user
            data["qtime"] = int(time.time())
            data["starttime"] = 0
            data["endtime"] = 0
            # data["type"] = "rerunjobid" + data["jobid"]
            data["jobid"] = ""
            data["workspace"] = workspace
            data["billgroup"] = billgroup

            try:
                rerun_job_dict = {
                    k: v for k, v in data.iteritems() if k in job_key_list
                }
                rerun_job = Job.objects.create(**rerun_job_dict)

                data["id"] = rerun_job.id
                task = {"method": "rerun_job", "type": "job"}
                task["args"] = data
                jsontask = json.dumps(task)
                RPCClient().cast("job", jsontask)

                # add link
                import os
                workingdir = data['workingdir'].replace("MyFolder",
                                                        data["workspace"], 1)
                output_temp = os.path.join(workingdir, '{}-{}.out')
                origin_output = output_temp.format(data["jobname"], id)
                present_output = output_temp.format(data["jobname"],
                                                    data["id"])
                # new add
                uid = request.user.uid
                gid = request.user.gid

                if os.path.exists(origin_output):
                    logger.debug(
                        origin_output + '__link to___' + present_output
                    )
                    os.symlink(origin_output, present_output)
                    # new add
                    os.lchown(present_output, uid, gid)
                    with open(origin_output, 'w'):
                        pass

                # add operationlog
                EventLog.opt_create(
                    request.user.username, EventLog.job, EventLog.rerun,
                    EventLog.make_list(rerun_job.id, rerun_job.jobname)
                )
                return Response(data, status=status.HTTP_201_CREATED)

            except Exception as e:
                logger.error("create rerun job error: {}".format(e))