forked from apache/pulsar
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Watch-znode script for docker image (apache#1289)
* Watch-znode script for docker image Docker compose doesn't have any way to run initialization like k8s does, so you need to role your own solution. This script allows you to wait for a znode to be created in zookeeper, check if it exists and create it. It can be used to ensure that pulsar brokers and bookies do not come up before metadata format has run. * fixup license
- Loading branch information
Showing
2 changed files
with
97 additions
and
1 deletion.
There are no files selected for viewing
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
#!/usr/bin/env python | ||
# | ||
# Licensed to the Apache Software Foundation (ASF) under one | ||
# or more contributor license agreements. See the NOTICE file | ||
# distributed with this work for additional information | ||
# regarding copyright ownership. The ASF licenses this file | ||
# to you under the Apache License, Version 2.0 (the | ||
# "License"); you may not use this file except in compliance | ||
# with the License. You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, | ||
# software distributed under the License is distributed on an | ||
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
# KIND, either express or implied. See the License for the | ||
# specific language governing permissions and limitations | ||
# under the License. | ||
# | ||
|
||
import sys, getopt, time | ||
from kazoo.client import KazooClient | ||
|
||
def usage(): | ||
print >> sys.stderr, "\n%s -z <zookeeper> -p <path> [-w|-c|-e]" % (sys.argv[0]) | ||
print >> sys.stderr, "\nWait for, or create znode" | ||
print >> sys.stderr, "\n-z Specify zookeeper connect string" | ||
print >> sys.stderr, "\n-p Znode path to watch or create" | ||
print >> sys.stderr, "\n-w Watch for path creation" | ||
print >> sys.stderr, "\n-c Create path" | ||
print >> sys.stderr, "\n-e Check if znode exists" | ||
|
||
try: | ||
opts, args = getopt.getopt(sys.argv[1:], "z:p:cweh") | ||
except getopt.GetoptError as err: | ||
print >> sys.stderr, str(err) | ||
usage() | ||
sys.exit(2) | ||
|
||
zookeeper = None | ||
znode = None | ||
create = False | ||
watch = False | ||
exists = False | ||
|
||
for o, a in opts: | ||
if o in ("-h"): | ||
usage() | ||
sys.exit() | ||
elif o in ("-z"): | ||
zookeeper = a | ||
elif o in ("-p"): | ||
znode = a | ||
elif o in ("-w"): | ||
watch = True | ||
elif o in ("-c"): | ||
create = True | ||
elif o in ("-e"): | ||
exists = True | ||
else: | ||
assert False, "unhandled option" | ||
usage() | ||
sys.exit(2) | ||
|
||
if not zookeeper: | ||
print >> sys.stderr, "Zookeeper must be specified" | ||
usage() | ||
sys.exit(3) | ||
|
||
if not znode: | ||
print >> sys.stderr, "Znode must be specified" | ||
usage() | ||
sys.exit(4) | ||
|
||
if (not watch and not create and not exists): | ||
print >> sys.stderr, "Exactly one of watch (-w), create (-c) or exists (-e) must be specified" | ||
usage() | ||
sys.exit(5) | ||
|
||
zk = KazooClient(hosts=zookeeper) | ||
zk.start() | ||
|
||
if create: | ||
zk.create(znode) | ||
elif watch: | ||
while not zk.exists(znode): | ||
print "Waiting for %s" % znode | ||
time.sleep(1) | ||
elif exists: | ||
if zk.exists(znode): | ||
sys.exit(0) | ||
else: | ||
sys.exit(-1) | ||
|
||
zk.stop() |