def delete_znodes(cluster_name, path, recursive=False, del_snapshots=True): """delete znodes' meta info in mysql """ del_znode_query = del_snapshot_query = None if recursive: # monkey patch for delete znodes recursively target_path = path.rstrip("/") + "/" del_znode_query = ZdZnode.delete().where( (ZdZnode.cluster_name == cluster_name) & ((ZdZnode.path.startswith(target_path)) | (ZdSnapshot.path == path)) ) del_snapshot_query = ZdSnapshot.delete().where( (ZdSnapshot.cluster_name == cluster_name) & ((ZdSnapshot.path.startswith(target_path)) | (ZdSnapshot.path == path)) ) else: del_znode_query = ZdZnode.delete().where( (ZdZnode.cluster_name == cluster_name) & (ZdZnode.path == path) ) del_snapshot_query = ZdSnapshot.delete().where( (ZdSnapshot.cluster_name == cluster_name) & (ZdSnapshot.path == path) ) del_znode_query.execute() if del_snapshots: del_snapshot_query.execute()
def make_snapshot(cluster_name, path, data=None): """生成快照,包括快照树结构信息(zd_snapshot_tree)和快照数据(zd_snapshot) """ if data is None: data = ZookeeperService.get(cluster_name, path) # 验证节点路径是否已经存在,不存在才创建相应树结构节点 if not _is_tree_node_exists(cluster_name, path): if path.strip() == "/": # 增加根节点 _add_tree_root(cluster_name=cluster_name, node_path=path) else: # 增加子节点 parent_path = _extract_parent_path(path) parent_node = ZdSnapshotTree.one(cluster_name=cluster_name, node_path=parent_path) if not parent_node: raise MakeSnapshotError("Parent node does not exists, could not build tree!") _add_tree_node(cluster_name=cluster_name, parent_node=parent_node, node_path=path) # 检验快照是否重复生成 commit_md5 = hashlib.md5(data).hexdigest() if is_snapshot_redundant(cluster_name, path, commit_md5): log.warn("Snapshot already exists for znode in cluster: %s, path: %s", cluster_name, path) return # 保存快照信息 snapshot = ZdSnapshot( cluster_name=cluster_name, path=path, data=data, create_time=datetime.now(), commit=commit_md5 ) snapshot.save()
def make_snapshot(cluster_name, path, data=None): """生成快照,包括快照树结构信息(zd_snapshot_tree)和快照数据(zd_snapshot) """ if data is None: data = ZookeeperService.get(cluster_name, path) # 验证节点路径是否已经存在,不存在才创建相应树结构节点 if not _is_tree_node_exists(cluster_name, path): if path.strip() == "/": # 增加根节点 _add_tree_root(cluster_name=cluster_name, node_path=path) else: # 增加子节点 parent_path = _extract_parent_path(path) parent_node = ZdSnapshotTree.one(cluster_name=cluster_name, node_path=parent_path) if not parent_node: raise MakeSnapshotError("Parent node does not exists, could not build tree!") _add_tree_node(cluster_name=cluster_name, parent_node=parent_node, node_path=path) # 检验快照是否重复生成 commit_md5 = hashlib.md5(data).hexdigest() if is_snapshot_redundant(cluster_name, path, commit_md5): log.warn("Snapshot already exists for znode in cluster: %s, path: %s", cluster_name, path) return # 保存快照信息 snapshot = ZdSnapshot(cluster_name=cluster_name, path=path, data=data, create_time=datetime.now(), commit=commit_md5) snapshot.save()
def response(self): '''delete ''' if self.info_ids: id_li = self.info_ids.split(',') for user_id in id_li: ZdSnapshot.one(id=user_id).delete_instance() return self.ajax_ok(close_current=True)
def response(self): """delete """ if self.info_ids: id_li = self.info_ids.split(",") for user_id in id_li: ZdSnapshot.one(id=user_id).delete_instance() return self.ajax_ok(close_current=True)
def response(self): """delete """ try: ZdSnapshot.one(id=self.id).delete_instance() return self.ajax_ok(close_current=True) except Exception as exc: log.error("error occurred while delete snapshot, id: %s\n%s", self.id, str(exc)) return self.ajax_popup(code=300, msg="删除快照出错啦!")
def response(self): '''delete ''' try: ZdSnapshot.one(id=self.id).delete_instance() return self.ajax_ok(close_current=True) except Exception as exc: log.error("error occurred while delete snapshot, id: %s\n%s", self.id, str(exc)) return self.ajax_popup(code=300, msg="删除快照出错啦!")
def delete_snapshots(cluster_name, path, recursive='0'): """删除快照数据信息 """ if recursive == "1": # monkey patch for delete snapshots recursively target_path = path.rstrip("/") + "/" del_snapshot_query = ZdSnapshot.delete().where( (ZdSnapshot.cluster_name == cluster_name) & ((ZdSnapshot.path.startswith(target_path)) | (ZdSnapshot.path == path))) else: del_snapshot_query = ZdSnapshot.delete().where( (ZdSnapshot.cluster_name == cluster_name) & (ZdSnapshot.path == path)) del_snapshot_query.execute()
def delete_snapshots(cluster_name, path, recursive="0"): """删除快照数据信息 """ if recursive == "1": # monkey patch for delete snapshots recursively target_path = path.rstrip("/") + "/" del_snapshot_query = ZdSnapshot.delete().where( (ZdSnapshot.cluster_name == cluster_name) & ((ZdSnapshot.path.startswith(target_path)) | (ZdSnapshot.path == path)) ) else: del_snapshot_query = ZdSnapshot.delete().where( (ZdSnapshot.cluster_name == cluster_name) & (ZdSnapshot.path == path) ) del_snapshot_query.execute()
def is_snapshot_redundant(cluster_name, path, commit_md5): """ 检验快照是否重复生成 """ return ZdSnapshot.one(cluster_name=cluster_name, path=path, commit=commit_md5, deleted="0")
def response(self): '''zookeeper上znode快照的查看 ''' status_mapping = {"0": "备份中", "1": "最近使用"} snapshots = ZdSnapshot.select().where( (ZdSnapshot.cluster_name == self.cluster_name) & (ZdSnapshot.path == self.path) & (ZdSnapshot.deleted == "0")).order_by(ZdSnapshot.create_time) return self.render('config/snapshot/view.html', status_mapping=status_mapping, path=self.path, snapshots=snapshots)
def rollback_snapshot(cluster_name, snapshot): """快照回滚 """ # 更新所有正在使用的快照为备份中, 0代表备份中,1代表使用中 query = ZdSnapshot.update(status="0").where( (ZdSnapshot.cluster_name == cluster_name) & (ZdSnapshot.path == snapshot.path) & (ZdSnapshot.status == "1") ) query.execute() # 根据快照数据更新zookeeper上znode的data ZookeeperService.set_or_create(cluster_name, snapshot.path, str(snapshot.data)) # 更新回滚的快照状态为正在使用 snapshot.status = "1" snapshot.save()
def delete_znodes(cluster_name, path, recursive=False, del_snapshots=True): """delete znodes' meta info in mysql """ del_znode_query = del_snapshot_query = None if recursive: # monkey patch for delete znodes recursively target_path = path.rstrip("/") + "/" del_znode_query = ZdZnode.delete().where( (ZdZnode.cluster_name == cluster_name) & ((ZdZnode.path.startswith(target_path)) | (ZdSnapshot.path == path))) del_snapshot_query = ZdSnapshot.delete().where( (ZdSnapshot.cluster_name == cluster_name) & ((ZdSnapshot.path.startswith(target_path)) | (ZdSnapshot.path == path))) else: del_znode_query = ZdZnode.delete().where( (ZdZnode.cluster_name == cluster_name) & (ZdZnode.path == path)) del_snapshot_query = ZdSnapshot.delete().where( (ZdSnapshot.cluster_name == cluster_name) & (ZdSnapshot.path == path)) del_znode_query.execute() if del_snapshots: del_snapshot_query.execute()
def response(self): """zookeeper上znode快照的查看 """ status_mapping = {"0": "备份中", "1": "最近使用"} snapshots = ( ZdSnapshot.select() .where( (ZdSnapshot.cluster_name == self.cluster_name) & (ZdSnapshot.path == self.path) & (ZdSnapshot.deleted == "0") ) .order_by(ZdSnapshot.create_time) ) return self.render( "config/snapshot/view.html", status_mapping=status_mapping, path=self.path, snapshots=snapshots )
def response(self): """rollback """ if self.snapshot_id: snapshot = ZdSnapshot.one(id=self.snapshot_id) else: snapshot = SnapshotService.last_snapshot(self.cluster_name, self.path) # 检查快照是否存在 if not snapshot: return self.ajax_popup(code=300, msg="快照未找到,请手动查看一下") if self.recursive == "1": # 递归回滚快照 SnapshotService.rollback_snapshots_recursively(self.cluster_name, snapshot) else: SnapshotService.rollback_snapshot(self.cluster_name, snapshot) return self.ajax_ok(close_current=False)
def last_snapshot(cluster_name, path): """获取上次保存的快照 如有快照正在使用,选取的是离它时间最近的快照, 否则为最新保存的快照 """ snapshots = ZdSnapshot.select().where( (ZdSnapshot.cluster_name == cluster_name) & (ZdSnapshot.path == path) & (ZdSnapshot.deleted == "0")).order_by(ZdSnapshot.create_time) last_shot = None for snapshot in snapshots: if last_shot is None: last_shot = snapshot # status为1代表正在使用,0代表已备份 if snapshot.status == "1": break last_shot = snapshot return last_shot
def last_snapshot(cluster_name, path): """获取上次保存的快照 如有快照正在使用,选取的是离它时间最近的快照, 否则为最新保存的快照 """ snapshots = ZdSnapshot.select().where( (ZdSnapshot.cluster_name == cluster_name) & (ZdSnapshot.path == path) & (ZdSnapshot.deleted == "0") ).order_by(ZdSnapshot.create_time) last_shot = snapshots[0] for snapshot in snapshots: # status为1代表正在使用,0代表备份中 if snapshot.status == "1": break last_shot = snapshot return last_shot
def response(self): """rollback """ if self.snapshot_id: snapshot = ZdSnapshot.one(id=self.snapshot_id) else: snapshot = SnapshotService.last_snapshot(self.cluster_name, self.path) # 检查快照是否存在 if not snapshot: return self.ajax_popup(code=300, msg="快照未找到,请手动查看一下") if self.recursive == "1": # 递归回滚快照 SnapshotService.rollback_snapshots_recursively( self.cluster_name, snapshot) else: SnapshotService.rollback_snapshot(self.cluster_name, snapshot) return self.ajax_ok(close_current=False)