Exemplo n.º 1
0
 def test_connect_chroot(self):
     """Test connecting with chroot."""
     zkutils.connect('zookeeper://me@xxx:123,yyy:123,zzz:123/a/b/c')
     treadmill.zkutils.ZkClient.create.assert_has_calls([
         mock.call('/a', b'', makepath=True, acl=mock.ANY),
         mock.call('/a/b', b'', makepath=True, acl=mock.ANY),
         mock.call('/a/b/c', b'', makepath=True, acl=mock.ANY),
     ])
Exemplo n.º 2
0
def connect(zkurl, idpath=None, session_timeout=None):
    """Returns connection to Zookeeper.
    """
    return zkutils.connect(zkurl,
                           idpath=idpath,
                           listener=zkutils.exit_never,
                           session_timeout=session_timeout)
Exemplo n.º 3
0
    def presence(zkid):
        """Runs the presence service.
        """
        root_dir = local_ctx['root-dir']
        watchdogs_dir = local_ctx['watchdogs-dir']

        # Explicitely create global zk connection, so that zk session id is
        # preserved.
        context.GLOBAL.zk.conn = zkutils.connect(context.GLOBAL.zk.url,
                                                 idpath=zkid)

        def sigterm_handler(_signo, _stack_frame):
            """Handle sigterm.

            On sigterm, stop Zookeeper session and delete Zookeeper session
            id file.
            """
            _LOGGER.info('Got SIGTERM, closing zk session and rm: %s', zkid)
            fs.rm_safe(zkid)
            context.GLOBAL.zk.conn.stop()

        signal.signal(utils.term_signal(), sigterm_handler)

        svc = services.ResourceService(
            service_dir=os.path.join(root_dir, 'presence_svc'),
            impl='presence',
        )

        svc.run(watchdogs_dir=os.path.join(root_dir, watchdogs_dir))
Exemplo n.º 4
0
    def conn(self):
        """Lazily creates Zookeeper client."""
        if self._conn is None:
            _LOGGER.debug('Connecting to Zookeeper %s', self.url)
            if self.url is None:
                if self._resolve:
                    self._resolve()
                if self.url is None:
                    raise ContextError('Zookeeper url not set.')

            self.proid, _ = self.url[len('zookeeper://'):].split('@')
            self._conn = zkutils.connect(self.url, listener=zkutils.exit_never)

        return self._conn
Exemplo n.º 5
0
 def setUp(self):
     super(VersionMgrTest, self).setUp()
     self.zkclient = zkutils.connect('zk')
     self.root = tempfile.mkdtemp()
Exemplo n.º 6
0
    def top(ctx, exit_on_fail, zkid, notification_fd, approot, runtime):
        """Run treadmill init process."""
        _LOGGER.info('Initializing Treadmill: %s (%s)', approot, runtime)

        tm_env = appenv.AppEnvironment(approot)
        stop_on_lost = functools.partial(_stop_on_lost, tm_env)
        zkclient = zkutils.connect(context.GLOBAL.zk.url,
                                   idpath=zkid,
                                   listener=stop_on_lost)

        while not zkclient.exists(z.SERVER_PRESENCE):
            _LOGGER.warning('namespace not ready.')
            time.sleep(30)

        hostname = sysinfo.hostname()

        zk_blackout_path = z.path.blackedout_server(hostname)
        zk_server_path = z.path.server(hostname)
        zk_presence_path = z.path.server_presence(hostname)

        while not zkclient.exists(zk_server_path):
            _LOGGER.warning('server %s not defined in the cell.', hostname)
            time.sleep(30)

        _LOGGER.info('Checking blackout list.')
        blacklisted = bool(zkclient.exists(zk_blackout_path))

        root_cgroup = ctx.obj['ROOT_CGROUP']
        os_args = {}
        if os.name == 'posix':
            os_args['cgroup_prefix'] = root_cgroup

        if not blacklisted:
            # Node startup.
            _node_start(tm_env, runtime, zkclient, hostname, zk_server_path,
                        zk_presence_path, os_args)

            utils.report_ready(notification_fd)

            _init_network()

            _start_init1(tm_env)
            _LOGGER.info('Ready.')

            down_reason = _main_loop(tm_env, zkclient, zk_presence_path)

            if down_reason is not None:
                _LOGGER.warning('Shutting down: %s', down_reason)
                # Blackout the server.
                zkutils.ensure_exists(
                    zkclient,
                    zk_blackout_path,
                    acl=[zkclient.make_host_acl(hostname, 'rwcda')],
                    data=down_reason)
                trigger_postmortem = True
            else:
                # Blacked out manually
                trigger_postmortem = bool(zkclient.exists(zk_blackout_path))

            if trigger_postmortem:
                postmortem.run(approot, root_cgroup)

        else:
            # Node was already blacked out.
            _LOGGER.warning('Shutting down blacked out node.')

        # This is the shutdown phase.

        # Delete the node
        if zk_presence_path:
            zkutils.ensure_deleted(zkclient, zk_presence_path)
        zkclient.remove_listener(stop_on_lost)
        zkclient.stop()
        zkclient.close()

        _cleanup_network()

        # to ternminate all the running apps
        _blackout_terminate(tm_env)

        if exit_on_fail:
            utils.sys_exit(-1)
        else:
            # Sit forever in a broken state
            while True:
                time.sleep(1000000)
Exemplo n.º 7
0
    def top(exit_on_fail, zkid, approot):
        """Run treadmill init process."""
        _LOGGER.info('Initializing Treadmill: %s', approot)

        tm_env = appenv.AppEnvironment(approot)
        zkclient = zkutils.connect(context.GLOBAL.zk.url,
                                   idpath=zkid,
                                   listener=_exit_clear_watchdog_on_lost)

        utils.report_ready()

        while not zkclient.exists(z.SERVER_PRESENCE):
            _LOGGER.warn('namespace not ready.')
            time.sleep(30)

        hostname = sysinfo.hostname()

        zk_blackout_path = z.path.blackedout_server(hostname)
        zk_presence_path = z.path.server_presence(hostname)
        zk_server_path = z.path.server(hostname)

        while not zkclient.exists(zk_server_path):
            _LOGGER.warn('server %s not defined in the cell.', hostname)
            time.sleep(30)

        _LOGGER.info('Checking blackout list.')
        blacklisted = bool(zkclient.exists(zk_blackout_path))

        if not blacklisted:
            # Node startup.
            _node_start(tm_env, zkclient, hostname, zk_server_path,
                        zk_presence_path)

            # Cleanup the watchdog directory
            tm_env.watchdogs.initialize()

            _init_network()

            _LOGGER.info('Ready.')

            down_reason = _main_loop(tm_env, zkclient, zk_presence_path)

            if down_reason is not None:
                _LOGGER.warning('Shutting down: %s', down_reason)

                # Blackout the server.
                zkutils.ensure_exists(
                    zkclient,
                    zk_blackout_path,
                    acl=[zkutils.make_host_acl(hostname, 'rwcda')],
                    data=down_reason)

        else:
            # Node was already blacked out.
            _LOGGER.warning('Shutting down blacked out node.')

        # This is the shutdown phase.

        # Delete the node
        zkutils.ensure_deleted(zkclient, zk_presence_path)
        zkclient.remove_listener(_exit_clear_watchdog_on_lost)
        zkclient.stop()
        zkclient.close()

        _cleanup_network()

        # to ternminate all the running apps
        _blackout_terminate(tm_env)

        if exit_on_fail:
            utils.sys_exit(-1)
        else:
            # Sit forever in a broken state
            while True:
                time.sleep(1000000)
Exemplo n.º 8
0
def connect(zkurl):
    """Returns connection to Zookeeper."""
    return zkutils.connect(zkurl, listener=zkutils.exit_never)