def response(self): """返回指定zookeeper集群的znode信息, 响应ajax请求 """ nodes = [] normalized_path = normalize_path(self.path) if USE_QCONF: ZnodeService.get_znode_tree_from_qconf(self.cluster_name, normalized_path, nodes) else: zoo_client = ZookeeperService.get_zoo_client(self.cluster_name) if not zoo_client: return self.ajax_popup(code=300, msg="连接zookeeper出错!") ZnodeService.get_znode_tree(zoo_client, normalized_path, nodes) if normalized_path != "/" and len(nodes) <= 1: return self.ajax_popup(code=300, msg="对不起,该节点路径下(%s)无数据!" % self.path) for node in nodes: zk_node = ZdZnode.one(path=node["path"], cluster_name=self.cluster_name) if zk_node: node['type'] = zk_node.type node['business'] = zk_node.business node['data'] = ZookeeperService.get(self.cluster_name, node["path"]) znodes_data = json.dumps(nodes) return self.render('config/znode/displaytree.html', cluster_name=self.cluster_name, znodes_data=znodes_data)
def get_znode_tree_from_qconf(cluster_name, path, nodes, current_id='1', parent_id='0'): """get zookeeper nodes from qconf recursively, format as ztree data """ from lib.zyqconf import qconf_py # 节点名只取最末尾的名称 name = path if path == "/" else path.rsplit('/', 1)[-1] nodes.append({ "id": current_id, "pId": parent_id, "name": name, "path": path }) children = [] try: children = qconf_py.get_batch_keys(path, cluster_name) except qconf_py.Error as exc: # fix bugs for qconf's get_batch_keys error while path is root path("/") if exc.message == "Error parameter!": zoo_client = ZookeeperService.get_zoo_client(cluster_name) children = zoo_client.get_children(path) else: log.warning('Node does not exists on QConf Agent, path: %s', path) for idx, child in enumerate(children): child_path = os.path.join(path, str(child)) # 如果父节点ID为1,则它的子节点ID应为101, 102, 103(左填充0到数字, 避免树的广度过宽,id冲突错误, 01, 09...) child_id = "{0}{1:02d}".format(current_id, idx) get_znode_tree_from_qconf(cluster_name, child_path, nodes, child_id, current_id)
def response(self): '''watch ''' zoo_client = ZookeeperService.get_zoo_client(self.cluster_name) if not zoo_client: return self.ajax_popup(code=300, msg="连接zookeeper出错!") try: zk_agents = zoo_client.get_children(self.agent_register_prefix) except NoNodeError: return self.ajax_popup(code=300, msg="节点路径不存在!") records = ZdQconfAgent.select().where( (ZdQconfAgent.cluster_name == self.cluster_name) & (ZdQconfAgent.deleted == '0') ) mysql_agents = [record.hostname for record in records] # agent在mysql上的统计信息和在zookeeper上注册信息的对比 agents_stat = [] for diff_info in Differ().compare(mysql_agents, zk_agents): agent_name = diff_info[2:] if diff_info[0] == "+": cmp_res = ['无', agent_name] elif diff_info[0] == "-": cmp_res = [agent_name, '无'] else: cmp_res = [agent_name, agent_name] agents_stat.append(cmp_res) return agents_stat
def get_znode_tree_from_qconf(cluster_name, path, nodes, current_id='1', parent_id='0'): """get zookeeper nodes from qconf recursively, format as ztree data """ from lib.zyqconf import qconf_py # 节点名只取最末尾的名称 name = path if path == "/" else path.rsplit('/', 1)[-1] nodes.append({ "id": current_id, "pId": parent_id, "name": name, "path": path }) children = [] try: children = qconf_py.get_batch_keys(path, cluster_name) except qconf_py.Error as exc: # fix bug for qconf get_batch_keys from root path "/" if exc.message == "Error parameter!": zoo_client = ZookeeperService.get_zoo_client(cluster_name) children = zoo_client.get_children(path) else: log.warning('Node does not exists on QConf agent: %s', path) for idx, child in enumerate(children): # 左填充0到数字, 避免树的广度过宽,id冲突错误, 01, 09... idx = '{0:02d}'.format(idx) # parent_id as 1, then child_id should be 10, 11, 12... child_id = "{0}{1}".format(current_id, idx) child_path = os.path.join(path, str(child)) get_znode_tree_from_qconf(cluster_name, child_path, nodes, child_id, current_id)
def response(self): """返回指定zookeeper集群的znode信息, 响应ajax请求 """ nodes = [] normalized_path = normalize_path(self.path) zoo_client = ZookeeperService.get_zoo_client(self.cluster_name) if not zoo_client: return self.ajax_popup(code=300, msg="连接zookeeper出错!") ZnodeService.get_znode_tree(zoo_client, normalized_path, nodes) #if normalized_path != "/" and len(nodes) <= 1: # return self.ajax_popup( # code=300, msg="对不起,该节点路径下(%s)无数据!" % self.path) for node in nodes: zk_node = ZdZnode.one(path=node["path"], cluster_name=self.cluster_name) if zk_node: node['description'] = zk_node.description node['data'] = ZookeeperService.get(self.cluster_name, node["path"]) znodes_data = json.dumps(nodes) return self.render('config/znode/displaytree.html', cluster_name=self.cluster_name, service_name=self.service_name, znodes_data=znodes_data)
def make_snapshots_from_path(cluster_name, path): """根据节点路径批量生成快照 """ zoo_client = ZookeeperService.get_zoo_client(cluster_name) nodes = [] _get_recursively(zoo_client, path, nodes) for node in nodes: make_snapshot(cluster_name, node["path"], node["data"])
def delete_znodes_diff_with_keys(cluster_name, parent_path, keys): """删除给定路径下节点名不在传入keys的子节点 """ zoo_client = ZookeeperService.get_zoo_client(cluster_name) children = zoo_client.get_children(parent_path) diff_znodes = [child for child in children if child not in keys] for znode in diff_znodes: path = os.path.join(parent_path, znode) zoo_client.delete(path, version=-1, recursive=False) delete_znodes(cluster_name, path, recursive=False, del_snapshots=True)
def get_child_znodes(cluster_name, path): """get child znodes with extra info """ zoo_client = ZookeeperService.get_zoo_client(cluster_name) child_znodes = [] children = zoo_client.get_children(path) # iter child nodes and convert to dict with extra info for child in children: child_path = os.path.join(path, child) data, _ = zoo_client.get(child_path) # node node = {"path": child_path, "value": data} node["name"] = child_path.rsplit('/', 1)[-1] child_znodes.append(node) return child_znodes
def response(self): '''savecopy ''' # node_name中不可包含`/`特殊字符 if self.path and not ZnodeService.is_node_name_ok(self.path): return self.ajax_popup(code=300, msg="节点名不允许包含特殊字符'/'!") zk_path = "" if not self.path: # 新增节点需要进行存在检验 return self.ajax_popup(code=300, msg="节点名称不能为空!") else: zk_path = os.path.join(self.parent_path, self.path) if ZookeeperService.exists(self.cluster_name, zk_path): return self.ajax_popup(code=300, msg="节点已经存在!") normalized_path = normalize_path(self.old_path) nodes = [] if USE_QCONF: ZnodeService.get_znode_tree_from_qconf(self.cluster_name, normalized_path, nodes) else: zoo_client = ZookeeperService.get_zoo_client(self.cluster_name) if not zoo_client: return self.ajax_popup(code=300, msg="连接zookeeper出错!") ZnodeService.get_znode_tree(zoo_client, normalized_path, nodes) path = full_path(self.path,self.parent_path) #ZnodeService.save_znode_tree(self.cluster_name , normalized_path , path) index = self.old_path.rfind('/') old_node_name = self.old_path[index+1:] for node in nodes: new_path = node['path'].replace(old_node_name,self.path,1) ZnodeService.save_znode_tree(self.cluster_name , node['path'] ,new_path) #ret = ZnodeService.save_znode_tree(self.cluster_name ,self.parent_path,self.old_path,self.path) #if not ret: # return self.ajax_popup(code=300,msg='添加失败') return self.ajax_ok(close_current=True)