Ejemplo n.º 1
0
class Role(db.Model):
    id = db.Column(db.Integer, primary_key=True)
    name = db.Column(db.String(32), nullable=True, default='')
    type = db.Column(db.Integer, nullable=True, default=0)

    created_time = db.Column(db.DateTime, nullable=False)

    status = db.Column(db.Integer, nullable=False, default=0)

    users = db.relationship('User', backref='role', lazy='dynamic')
Ejemplo n.º 2
0
class WebGroup(db.Model):
    STATUS_OK = 0
    STATUS_DELETE = 1

    id = db.Column(db.BigInteger, primary_key=True)
    pid = db.Column(db.BigInteger, nullable=False)
    name = db.Column(db.String(32), nullable=False, default='')
    created_time = db.Column(db.DateTime, nullable=False)

    manager = db.Column(db.String(64), nullable=False, default='')

    status = db.Column(db.Integer, nullable=False)

    assets = db.relationship('WebAsset', backref="group", lazy="dynamic")
Ejemplo n.º 3
0
class SysAsset(db.Model, AsDictMixin):
    WEIGHT_HIGH = 10
    WEIGHT_MEDIUM = 6
    WEIGHT_LOW = 1

    STATUS_NEW = 0
    STATUS_ACK = 1
    STATUS_DELETE = 2

    id = db.Column(db.BigInteger, primary_key=True)
    sn = db.Column(db.String(128), nullable=False, default='')
    name = db.Column(db.String(32), nullable=False, default='')
    ip = db.Column(db.String(256), nullable=False, default='')
    mac = db.Column(db.String(32), nullable=False, default='')
    os = db.Column(db.String(128), nullable=False, default='')
    group_id = db.Column(db.BigInteger, db.ForeignKey('sys_group.id'))

    manager = db.Column(db.String(64), nullable=False, default='')

    weight = db.Column(db.Integer, nullable=False)

    created_time = db.Column(db.DateTime, nullable=False)
    last_discove_time = db.Column(db.DateTime, nullable=False)

    status = db.Column(db.Integer, nullable=False)
    applications = db.relationship('Application', backref='asset', lazy='dynamic')

    @classmethod
    def stats(cls):
        return {
            'total' : cls.query.filter(cls.status.notin_([cls.STATUS_DELETE])).count(),
            '24_hour' : cls.query.filter(cls.created_time>=timezone.now() - timedelta(days=1), cls.status.notin_([cls.STATUS_DELETE])).count(),
        }

    @classmethod
    def all(cls):
        return cls.query.filter(cls.status.notin_([cls.STATUS_DELETE])).all()

    @classmethod
    def get_by_key(cls, value, key='id'):
        return db.session.query(cls).filter_by(**{key : value}).first()

    @classmethod
    def create_or_replace(cls, **kwargs):
        ip = kwargs.get('ip', '')
        obj = cls.get_by_key(ip, key='ip')
        try:

            if obj is None or obj.status == cls.STATUS_DELETE:
                obj = cls()
                obj.ip = ip
                obj.status = cls.STATUS_NEW
                obj.created_time = timezone.now()
                obj.weight = cls.WEIGHT_LOW

            obj.last_discove_time = timezone.now()
            obj.name = kwargs.get('name', obj.name)
            obj.os = kwargs.get('os', obj.os)
            obj.mac = kwargs.get('mac', obj.mac)

            db.session.add(obj)
            db.session.commit()

            for app in kwargs.get('apps', []):
                Application.create_or_replace(app.get('name', ''),
                    app.get('protocol', ''),
                    app.get('port', 0),
                    app.get('state', ''),
                    app.get('product', ''),
                    app.get('version', ''),
                    obj,
                )
        except BaseException as e:
            logger.error(e)
            logger.exception(traceback.format_exc())
            db.session.rollback()

        return obj

    @classmethod
    def delete_by_key(cls, id):
        obj = cls.get_by_key.get(id)
        if obj:
            obj.status = cls.STATUS_DELETE
            db.session.add(obj)
            db.session.commit()

        return obj
Ejemplo n.º 4
0
class Job(db.Model, AsDictMixin):

    STATUS_WATING = 1
    STATUS_PREPROCESS = 2
    STATUS_DOING = 3
    STATUS_CANCEL = 4
    STATUS_SUCCESS = 5
    STATUS_FAILURE = 6
    STATUS_DELETE = 7

    PROGRESS_PREPROCESS = 0
    PROGRESS_DOING = 5
    PROGRESS_COMPLATE = 100

    id = db.Column(db.BigInteger, primary_key=True)
    name = db.Column(db.String(64), nullable=False, default='')
    user_id = db.Column(db.Integer, db.ForeignKey('user.id'))
    type = db.Column(db.Integer, nullable=False)
    job_params = db.Column(db.Text)
    progress = db.Column(db.Integer, nullable=False, default=0)
    status = db.Column(db.Integer, nullable=False)
    explain = db.Column(db.String(512), nullable=False, default='')

    created_time = db.Column(db.DateTime, nullable=False)
    start_time = db.Column(db.DateTime)
    finish_time = db.Column(db.DateTime)

    sub_jobs = db.relationship('SubJob', backref="job", lazy='dynamic')

    def __init__(self,
                 user=None,
                 type=None,
                 name='',
                 job_params=None,
                 status=None,
                 explain='',
                 created_time=None,
                 start_time=None,
                 finish_time=None,
                 progress=0,
                 id=None,
                 user_id=None,
                 from_cache=False,
                 *args,
                 **kwargs):
        self.id = id
        self.name = name
        self.user_id = user.id if user else user_id
        self.type = type
        job_params = {} if job_params is None else job_params
        self.job_params = json.dumps(job_params) if isinstance(
            job_params, (dict, )) else job_params
        self.progress = progress
        self.status = self.STATUS_WATING if status is None else status
        self.explain = explain
        self.created_time = timezone.now(
        ) if created_time is None else created_time
        self.start_time = start_time
        self.finish_time = finish_time
        self.from_cache = from_cache

    @property
    def job_params_object(self):
        try:
            return json.loads(self.job_params)
        except BaseException as e:
            return {}

    @classmethod
    def create(cls, user, type, name, job_params):
        job = cls(user=user, type=type, name=name, job_params=job_params)
        db.session.add(job)
        db.session.commit()
        redis.hmset(REDIS_KEYS['JOB_CONTENT'].format(id=job.id),
                    job.as_dict_string())
        redis.lpush(REDIS_KEYS['JOB_PREPROCESS'], job.id)
        return job

    def preprocess(self):
        job = self
        if getattr(self, 'from_cache', False):
            job = self.get_by_key(self.id)
        job.status = self.STATUS_PREPROCESS
        job.progress = self.PROGRESS_PREPROCESS
        db.session.add(job)
        db.session.commit()
        redis.hmset(REDIS_KEYS['JOB_CONTENT'].format(id=self.id), {
            'status': self.STATUS_PREPROCESS,
            'progress': self.PROGRESS_PREPROCESS
        })
        redis.lpush(REDIS_KEYS['JOB_DOING'], self.id)

    def start(self):
        job = self
        if getattr(self, 'from_cache', False):
            job = self.get_by_key(self.id)
        if job.start_time is None:
            job.start_time = timezone.now()
        job.status = self.STATUS_DOING
        redis_content = {'status': self.STATUS_DOING}
        if job.progress < self.PROGRESS_DOING:
            job.progress = self.PROGRESS_DOING
            redis_content['progress'] = self.PROGRESS_DOING
        db.session.add(job)
        db.session.commit()
        redis.hmset(REDIS_KEYS['JOB_CONTENT'].format(id=self.id), {
            'status': self.STATUS_DOING,
            'progress': self.PROGRESS_DOING
        })

    @classmethod
    def get_by_key(cls, value, key='id'):
        return db.session.query(cls).filter_by(**{key: value}).first()

    @classmethod
    def get_by_cache(cls, id):
        bjob = redis.hgetall(REDIS_KEYS['JOB_CONTENT'].format(id=id))
        if bjob:
            job = {'from_cache': True}
            for k, v in bjob.items():
                job[k] = int(v) if k in ('id', 'user_id', 'type', 'progress',
                                         'status') else v
            return cls(**job)
        return None

    def finish(self, status=None, explain=''):
        job = self
        if getattr(self, 'from_cache', False):
            job = self.get_by_key(self.id)

        job.finish_time = timezone.now()
        job.status = self.STATUS_SUCCESS if status is None else self.STATUS_FAILURE
        job.progress = self.PROGRESS_COMPLATE
        job.explain = explain
        db.session.add(job)
        db.session.commit()
        redis.delete(REDIS_KEYS['JOB_CONTENT'].format(id=self.id))
        redis.lrem(REDIS_KEYS['JOB_DOING'], 0, self.id)
        for _, key in JobType.KEYS.items():
            redis.delete(REDIS_KEYS['JOB_QUEUE'].format(type=key, id=self.id))

        return True

    def cancel(self):
        job = self
        if getattr(self, 'from_cache', False):
            job = self.get_by_key(self.id)
        job.status = self.STATUS_CANCEL
        job.progress = self.PROGRESS_COMPLATE
        SubJob.cancel(job)
        db.session.add(job)
        db.session.commit()
        redis.delete(REDIS_KEYS['JOB_CONTENT'].format(id=self.id))
        redis.lrem(REDIS_KEYS['JOB_DOING'], 0, self.id)
        for _, key in JobType.KEYS.items():
            redis.delete(REDIS_KEYS['JOB_QUEUE'].format(type=key, id=self.id))

        return True

    def delete(self):
        job = self
        if getattr(self, 'from_cache', False):
            job = self.get_by_key(self.id)
        job.status = self.STATUS_DELETE
        job.progress = self.PROGRESS_COMPLATE
        SubJob.delete(job)
        db.session.add(job)
        db.session.commit()
        redis.delete(REDIS_KEYS['JOB_CONTENT'].format(id=self.id))
        redis.lrem(REDIS_KEYS['JOB_DOING'], 0, self.id)
        for _, key in JobType.KEYS.items():
            redis.delete(REDIS_KEYS['JOB_QUEUE'].format(type=key, id=self.id))

        return True

    def doing(self, progress=None):
        job = self
        if getattr(self, 'from_cache', False):
            job = self.get_by_key(self.id)

        job.status = self.STATUS_DOING
        if progress is not None:
            job.progress = progress
        db.session.add(job)
        db.session.commit()