def get_zk(zkconf, zkhosts, timeout, command_retry=None, connection_retry=None): ''' Initiate a zookeeper connection and add a listener ''' def parse_acl(s): parts = s.split(':') if len(parts) < 3: raise RuntimeError("invalid ACL spec: %s" % s) scheme, cred, perms = parts[0], parts[1:-1], parts[-1] return make_acl(scheme, ':'.join(cred), read='r' in perms, write='w' in perms, create='c' in perms, delete='d' in perms, admin='a' in perms, all='*' in perms) auth_data = None default_acl = None sasl_options = None if zkconf: parser = ConfigParser.ConfigParser() with open(zkconf, 'r') as fp: parser.readfp(fp, zkconf) if zkhosts == 'localhost:2181' and parser.has_option( 'zookeeper', 'hosts'): zkhosts = parser.get('zookeeper', 'hosts').split() if parser.has_option('zookeeper', 'auth'): auth_data = [ tuple(a.split(':', 1)) for a in parser.get('zookeeper', 'auth').split() ] if parser.has_option('zookeeper', 'default_acl'): default_acl = [ parse_acl(a) for a in parser.get('zookeeper', 'default_acl').split() ] if parser.has_section('sasl_auth'): sasl_options = dict(parser.items('sasl_auth')) conn = KazooClient(hosts=zkhosts, default_acl=default_acl, auth_data=auth_data, sasl_options=sasl_options, timeout=timeout, command_retry=command_retry, connection_retry=connection_retry) conn.add_listener(listener) try: conn.start() except KazooTimeoutError as exc: log.error(exc) sys.exit(1) return conn, zkhosts
def kill_job(job): ''' Kill a subprocess popen job and it's children ''' if job: try: kill(job.pid) except psutil.NoSuchProcess as exc: log.error("{0}. May have exited already".format(exc))
def listener(state): ''' Default listner to log events ''' if state == KazooState.LOST: log.error(state) os.kill(os.getpid(), signal.SIGTERM) sys.exit(1) elif state == KazooState.SUSPENDED: log.error(state) else: pass
def get_zk(zkhosts, timeout, command_retry=None, connection_retry=None): ''' Initiate a zookeeper connection and add a listener ''' conn = KazooClient(hosts=zkhosts, timeout=timeout, command_retry=command_retry, connection_retry=connection_retry) conn.add_listener(listener) try: conn.start() except KazooTimeoutError as exc: log.error(exc) sys.exit(1) return conn
def kill(pid): ''' Kill a pid ''' process = psutil.Process(pid) try: for proc in process.children(recursive=True) + [process]: try: log.info("Killing pid={0}".format(proc.pid)) proc.kill() time.sleep(0.1) proc.terminate() except psutil.NoSuchProcess as exc: log.error(exc) except AttributeError: log.info("Killing pid={0}".format(process.pid)) process.kill() time.sleep(0.1) try: proc.terminate() except UnboundLocalError: pass