示例#1
0
class DBDNSServer(db.Model):
    __tablename__ = 'dns_server'
    id = db.Column(db.Integer, primary_key=True)
    host = db.Column(db.String(64), index=True)
    ip = db.Column(db.String(64))
    env = db.Column(db.String(64))
    dns_server_type = db.Column(db.String(64))
    status = db.Column(db.String(64), default='INITING')
    zb_process_itemid = db.Column(db.String(64))
    zb_port_itemid = db.Column(db.String(64))
    zb_resolve_itemid = db.Column(db.String(64))
    zb_resolve_rate_itemid = db.Column(db.String(64))
    server_log = db.Column(db.Text())
    gmt_create = db.Column(db.DateTime(), default=datetime.now)
    gmt_modified = db.Column(db.DateTime(), default=datetime.now)

    @property
    def can_update(self):
        return g.current_user.can_do(
                    Operation.UPDATE, ResourceType.SERVER, self.id)

    @property
    def can_delete(self):
        return g.current_user.can_do(
                    Operation.DELETE, ResourceType.SERVER, self.id)

    def to_json(self):
        json_server = {
            'id': self.id,
            'host': self.host,
            'ip': self.ip,
            'env': self.env,
            'dns_type': self.dns_type,
            'status': self.status,
            'logs': self.logs
        }
        return json_server

    def get_server_status(self):        
        zb = ZBapi(self)
        server_status = zb.get_server_status()
        return server_status

    def get_resolve_rate(self, start_time, end_time):
        zb = ZBapi(self)
        resolve_rate = zb.get_resolve_rate(start_time, end_time)
        # resolve_rates.append({dns_server.host: resolve_rate})
        return resolve_rate

    def get_content_str(self, prefix=None):
        #'修改前内容:'
        content = 'id: ' + str(self.id) + '\n' \
        + '主机名: ' + str(self.host) + '\n' \
        + 'IP地址: ' + str(self.ip) + '\n' \
        + '环境: ' + str(self.env) + '\n' \
        + 'DNS类型: ' + str(self.dns_server_type) + '\n'
        if prefix:
            content = prefix + '\n' + content
        return content
示例#2
0
class DBOperationLog(db.Model):
    __tablename__ = 'dns_operation_log'
    id = db.Column(db.Integer, primary_key=True)
    operation_time = db.Column(db.DateTime(), default=datetime.now)
    operation_type = db.Column(db.String(64))
    operator = db.Column(db.String(64))
    target_type = db.Column(db.String(64))
    target_name = db.Column(db.String(64))
    target_id = db.Column(db.String(64))
    target_detail = db.Column(db.Text())
    gmt_create = db.Column(db.DateTime(), default=datetime.now)
    gmt_modified = db.Column(db.DateTime(), default=datetime.now)
示例#3
0
class DBView(db.Model):
    __tablename__ = 'dns_view'
    id = db.Column(db.Integer, primary_key=True)
    name = db.Column(db.String(64), index=True)
    acl = db.Column(db.Text())
    gmt_create = db.Column(db.DateTime(), default=datetime.now)
    gmt_modified = db.Column(db.DateTime(), default=datetime.now)

    def __init__(self, **kwargs):
        super(DBView, self).__init__(**kwargs)

    def __repr__(self):
        return '<DBView %r>' % self.name

    @property
    def zones(self):
        related_zones = db.session.query(DBZone).join(
            DBViewZone, and_(DBViewZone.zone_id == DBZone.id)) \
            .join(DBView, and_(DBView.id == DBViewZone.view_id)) \
            .filter(DBView.id == self.id).all()
        if not related_zones:
            return []
        return related_zones

    @property
    def zone_name_list(self):
        return [v.name for v in self.zones]


    def get_content_str(self, prefix=None):
        content = 'id: ' + str(self.id) + '\n' \
        + 'View名称: ' + str(self.name) + '\n' \
        + 'ACL: ' + str(self.acl) + '\n'

        if prefix:
            content = prefix + '\n' + content
        return content

    @property
    def can_update(self):
        return g.current_user.can_do(
                Operation.UPDATE, ResourceType.VIEW, self.id)

    @property
    def can_delete(self):
        return g.current_user.can_do(
                Operation.DELETE, ResourceType.VIEW, self.id)

    def make_view(self, action, view_list):
        prevExist = True
        if action == 'create':
            prevExist = False
        etcd_client = getETCDclient()
        if action == 'del':
            view_base_dir = current_app.config.get('ETCD_BASE_DIR') + self.name
            etcd_client.delete(view_base_dir, recursive=True)
            time.sleep(0.2)
            return
        if action == 'create':
            view_zone_conf = current_app.config.get('ETCD_BASE_DIR') + self.name + '/view.conf'
            view_zone_conf_content = Template(
                        current_app.config.get('VIEW_TEMPLATE')).render(view_name=self.name)
            etcd_client.write(view_zone_conf, view_zone_conf_content, prevExist=prevExist)
            time.sleep(0.2)   #连续几个提交速度过快,etcd server检测不到提交

            view_define_conf_content = Template(
                current_app.config.get('VIEW_DEFINE_TEMPLATE')).render(view_list=view_list)
            # print(current_app.config.get('VIEW_DEFINE_CONF'))
            # print(view_define_conf_content)
            etcd_client.write(
                current_app.config.get('VIEW_DEFINE_CONF'), 
                view_define_conf_content, 
                prevExist=True)
            time.sleep(0.2)

        acl_conf = current_app.config.get('ETCD_BASE_DIR') + self.name + '/acl.conf'
        acl_conf_content = Template(current_app.config.get('ACL_TEMPLATE')).render(
            view_name=self.name, ip_list=self.acl.split())
        etcd_client.write(acl_conf, acl_conf_content, prevExist=prevExist)
        time.sleep(0.2)