Created
September 15, 2013 23:13
-
-
Save dpsoft/6575115 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
import argparse | |
import sys | |
from clint.textui import progress | |
from clint.textui import colored | |
from kazoo.client import KazooClient, KazooState | |
def processArguments(): | |
parser = argparse.ArgumentParser() | |
parser.add_argument("hosts", help="Zookeeper server hosts") | |
parser.add_argument("path", help="Path to Remove") | |
return parser.parse_args() | |
def main(): | |
args = processArguments() | |
zk = KazooClient(hosts=args.hosts) | |
zk.add_listener(stateListener) | |
zk.start() | |
pathToRemove = "/{path}".format(path=args.path) | |
nodesToRemove = [] | |
print(colored.green("Trying to get children from path {path}".format(path=pathToRemove))) | |
children = zk.get_children(pathToRemove) | |
for name in children: | |
childrenPath = '{pathToRemove}/{name}'.format(pathToRemove=pathToRemove, name=name) | |
stat = zk.exists(childrenPath) | |
if None != stat and 0 == stat.children_count: | |
nodesToRemove.append(childrenPath) | |
if not nodesToRemove: | |
print(colored.green("Nothing to do")) | |
sys.exit() | |
choice = getInput(nodesToRemove) | |
if choice: | |
for node in progress.bar(label="Removing empty ZkNodes", it=nodesToRemove): | |
zk.delete(node) | |
print(colored.green("All Nodes Were Removed")) | |
print(colored.green("Bye.")) | |
sys.exit() | |
def stateListener(state): | |
if state == KazooState.LOST: | |
print(colored.red("Connection lost.")) | |
elif state == KazooState.SUSPENDED: | |
print(colored.blue("Connection Suspended.")) | |
else: | |
print(colored.green("Connected to Zookeeper Server")) | |
def getInput(nodesToRemove): | |
return str2bool(raw_input(colored.blue("-> Will proceed to remove {nodes} nodes, are you sure? [yes - no]".format(nodes=len(nodesToRemove))))) | |
def str2bool(v): | |
return v.lower() in ("yes","y") | |
if __name__ == "__main__": | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment