How to use the kazoo.client function in kazoo

To help you get started, we’ve selected a few kazoo examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github wglass / lighthouse / lighthouse / zookeeper.py View on Github external
def connect(self):
        """
        Creates a new KazooClient and establishes a connection.

        Passes the client the `handle_connection_change` method as a callback
        to fire when the Zookeeper connection changes state.
        """
        self.client = client.KazooClient(hosts=",".join(self.hosts))

        self.client.add_listener(self.handle_connection_change)
        self.client.start_async()
github Morgan-Stanley / treadmill / lib / python / treadmill / versionmgr.py View on Github external
"""Verifies that version info is up to date."""
    not_up_to_date = []
    for server in servers:
        if not zkclient.exists(z.path.server(server)):
            continue

        version_path = z.path.version(server)
        try:
            version_info = zkutils.get(zkclient, version_path)
            if version_info.get('digest') != expected:
                _LOGGER.debug('not up to date: %s', server)
                not_up_to_date.append(server)
            else:
                _LOGGER.debug('ok: %s', server)

        except kazoo.client.NoNodeError:
            _LOGGER.debug('version info does not exist: %s', server)
            not_up_to_date.append(server)

    return not_up_to_date
github Morgan-Stanley / treadmill / lib / python / treadmill / plugins / zookeeper.py View on Github external
def connect(zkurl, connargs):
    """Connect to zookeeper
    """
    if not connargs.get('hosts'):
        connargs['hosts'] = zkurl[len(_ZK_PREFIX):]

    if not connargs.get('sasl_data'):
        connargs['sasl_data'] = {
            'service': 'zookeeper',
            'mechanisms': ['GSSAPI']
        }

    return kazoo.client.KazooClient(**connargs)
github Morgan-Stanley / treadmill / lib / python / treadmill / cli / admin / blackout.py View on Github external
def _list_server_blackouts(zkclient, fmt):
    """List server blackouts."""

    with_partition = '%p' in fmt
    with_version = '%v' in fmt

    blackouts = []
    for node in zkclient.get_children(z.BLACKEDOUT_SERVERS):
        try:
            node_path = z.path.blackedout_server(node)
            data, metadata = zkutils.get_with_metadata(zkclient, node_path)
        except kazoo.client.NoNodeError:
            continue

        partition, version = None, None

        if with_partition:
            server_data = zkutils.get_default(
                zkclient, z.path.server(node)
            )
            if server_data and server_data.get('partition'):
                partition = server_data['partition']

        if with_version:
            version_data = zkutils.get_default(
                zkclient, z.path.version(node)
            )
            if version_data and version_data.get('codepath'):
github AppScale / appscale / AppDB / appscale / datastore / scripts / datastore.py View on Github external
parser.add_argument('-v', '--verbose', action='store_true',
                      help='Output debug-level logging')
  args = parser.parse_args()

  if args.verbose:
    logging.getLogger('appscale').setLevel(logging.DEBUG)

  options.define('private_ip', appscale_info.get_private_ip())
  options.define('port', args.port)
  taskqueue_locations = get_load_balancer_ips()

  server_node = '{}/{}:{}'.format(DATASTORE_SERVERS_NODE, options.private_ip,
                                  options.port)

  retry_policy = KazooRetry(max_tries=5)
  zk_client = kazoo.client.KazooClient(
    hosts=zookeeper_locations, connection_retry=ZK_PERSISTENT_RECONNECTS,
    command_retry=retry_policy)
  zk_client.start()

  if args.type == 'cassandra':
    datastore_batch = DatastoreFactory.getDatastore(
      args.type, log_level=logger.getEffectiveLevel())
    zookeeper = zktransaction.ZKTransaction(
      zk_client=zk_client, db_access=datastore_batch,
      log_level=logger.getEffectiveLevel())
    transaction_manager = TransactionManager(zk_client)
    datastore_access = DatastoreDistributed(
      datastore_batch, transaction_manager, zookeeper=zookeeper,
      log_level=logger.getEffectiveLevel(),
      taskqueue_locations=taskqueue_locations)
  else:
github douban / dpark / dpark / pymesos / detector.py View on Github external
def adjust_zk_logging_level():
        import logging
        import kazoo
        kazoo.client.log.setLevel(logging.WARNING)
        kazoo.protocol.connection.log.setLevel(logging.WARNING)
github Yelp / paasta / paasta_tools / mesos / zookeeper.py View on Github external
# 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 contextlib

import kazoo.client
import kazoo.exceptions
import kazoo.handlers.threading


TIMEOUT = 1

# Helper for testing
client_class = kazoo.client.KazooClient


@contextlib.contextmanager
def client(*args, **kwargs):
    zk = client_class(*args, **kwargs)
    zk.start(timeout=TIMEOUT)
    try:
        yield zk
    finally:
        zk.stop()
        zk.close()
github Morgan-Stanley / treadmill / lib / python / treadmill / zkutils.py View on Github external
# Default acl assumes world readable data, safe to log the payload. If
    # default acl is not specified, do not log the payload as it may be
    # private.
    if default_acl:
        realacl = zkclient.make_default_acl(acl)
        _LOGGER.debug('put (default_acl=%s): %s acl=%s seq=%s', default_acl,
                      path, realacl, sequence)
    else:
        realacl = acl
        _LOGGER.debug('put %s *** acl=%s seq=%s', path, realacl, sequence)

    try:
        return zkclient.create(path, payload, makepath=True, acl=realacl,
                               sequence=sequence, ephemeral=ephemeral)
    except kazoo.client.NodeExistsError:
        # This will never happen for sequence node, so requestor knows the
        # path.
        #
        # If there is not change, return None to indicate update was not done.
        if check_content:
            current, _metadata = zkclient.get(path)
            if current == payload:
                _LOGGER.debug('%s is up to date', path)
                return None

        zkclient.set(path, payload)
        _LOGGER.debug('Setting ACL on %s to %r', path, realacl)
        zkclient.set_acls(path, realacl)
        return path
github saltstack / salt / salt / modules / zookeeper.py View on Github external
if username is not None and password is not None and scheme is None:
        scheme = 'digest'

    auth_data = None
    if scheme and username and password:
        auth_data = [(scheme, ':'.join([username, password]))]

    if default_acl is not None:
        if isinstance(default_acl, list):
            default_acl = [make_digest_acl(**acl) for acl in default_acl]
        else:
            default_acl = [make_digest_acl(**default_acl)]

    __context__.setdefault('zkconnection', {}).setdefault(profile or hosts,
                                                          kazoo.client.KazooClient(hosts=hosts,
                                                                                   default_acl=default_acl,
                                                                                   auth_data=auth_data))

    if not __context__['zkconnection'][profile or hosts].connected:
        __context__['zkconnection'][profile or hosts].start()

    return __context__['zkconnection'][profile or hosts]
github Morgan-Stanley / treadmill / lib / python / treadmill / keytabs2 / locker.py View on Github external
raise keytabs2.KeytabLockerError(
                'princ "{}" not accepted'.format(princ)
            )

        hostname = princ[len('host/'):princ.rfind('@')]

        if not self.zkclient.exists(z.path.placement(hostname, appname)):
            _LOGGER.error('App %s not scheduled on node %s', appname, hostname)
            return []

        try:
            appnode = z.path.scheduled(appname)
            app = zkutils.with_retry(zkutils.get, self.zkclient, appnode)

            return app.get('keytabs', [])
        except kazoo.client.NoNodeError:
            _LOGGER.info('App does not exist: %s', appname)
            return []