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)
def get(self): try: etcd_client = getETCDclient() bind_conf_content = etcd_client.read( current_app.config.get('BIND_CONF')).value return get_response(RequestCode.SUCCESS, '修改成功!', dict(bind_conf=bind_conf_content)) except Exception as e: return get_response(RequestCode.OTHER_FAILED, '获取数据失败!')
def post(self): try: req = self.post_reqparse.parse_args() bind_conf_content = req.get('bind_conf') etcd_client = getETCDclient() etcd_client.write(current_app.config.get('BIND_CONF'), bind_conf_content, prevExist=True) return get_response(RequestCode.SUCCESS, '修改成功') except Exception as e: return get_response(RequestCode.OTHER_FAILED, '提交数据失败 !')
def _make_record(self, zone_name, record_list): etcd_client = getETCDclient() zone_record_conf = current_app.config.get('ZONE_BASE_DIR') + \ self.view_name + '/zone.' + zone_name zone_record_conf_content = Template( current_app.config.get('RECORD_TEMPLATE')).render( zone_name=zone_name, record_list=record_list) etcd_client.write(zone_record_conf, zone_record_conf_content, prevExist=True) time.sleep(0.5)
def init_bind_config(app): client = getETCDclient() print('initing etcd data...') try: client.read(app.config.get('BIND_CONF')) except etcd.EtcdKeyNotFound: client.write(app.config.get('BIND_CONF'), app.config.get('DEFAULT_BIND_CONF_CONTENT'), prevExist=False) time.sleep(1) try: client.read(app.config.get('VIEW_DEFINE_CONF')) except etcd.EtcdKeyNotFound: client.write(app.config.get('VIEW_DEFINE_CONF'), '', prevExist=False) time.sleep(1)
def _make_zone(self, action, view_name, zone_list, record_list): etcd_client = getETCDclient() view_zone_conf = current_app.config.get( 'ETCD_BASE_DIR') + view_name + '/view.conf' bind_zones = [] if action == 'del': for zz in zone_list: if view_name in zz.view_name_list and zz.name != self.name: bind_zones.append(zz) else: for zz in zone_list: if view_name in zz.view_name_list: bind_zones.append(zz) # print(view_name) # print(bind_zones) view_zone_conf_content = Template( current_app.config.get('ZONE_TEMPLATE')).render( view_name=view_name, zone_list=bind_zones) etcd_client.write(view_zone_conf, view_zone_conf_content, prevExist=True) time.sleep(0.5) # view_zone_confiig 文件操作 # forward only类型的zone,不生成 zone.xx.xx 文件 # 修改zone不需要更改zone.xx.xx 文件 if self.zone_type != 'forward only': zone_record_conf = current_app.config.get('ZONE_BASE_DIR') + \ view_name + '/zone.' + self.name if action == 'create' or action == 'modify': zone_record_conf_content = Template( current_app.config.get('RECORD_TEMPLATE')).render( zone_name=self.name, record_list=record_list) try: etcd_client.write(zone_record_conf, zone_record_conf_content, prevExist=True) except etcd.EtcdKeyNotFound: zone_record_conf_content = Template( current_app.config.get('RECORD_TEMPLATE')).render( zone_name=self.name, record_list=[]) etcd_client.write(zone_record_conf, zone_record_conf_content, prevExist=False) time.sleep(0.5) elif action == 'del': # print(zone_record_conf) etcd_client.delete(zone_record_conf) time.sleep(0.5)