find_swarm_master.py 522 Bytes
Newer Older
1
#!/usr/bin/python3
2

3 4 5 6
"""
Find the Swarm manager by querying ZooKeeper.
"""

7 8 9 10
import sys

from kazoo.client import KazooClient

11

12
def zookeeper_swarm(zk_server_list, path='/swarm'):
13 14
    """Query ZooKeeper."""
    path += '/docker/swarm/leader'
15 16
    zk = KazooClient(hosts=zk_server_list)
    zk.start()
17
    master, stat_ = zk.get(path)
18 19 20 21 22 23 24 25
    zk.stop()
    return master.decode('utf-8')

if __name__ == "__main__":
    if len(sys.argv) != 2:
        print("Provide zookeeper server list")
    print(zookeeper_swarm(sys.argv[1]))