def test_report_ready(self): """Tests reporting service readyness.""" cwd = os.getcwd() tmpdir = self.root os.chdir(tmpdir) utils.report_ready() self.assertFalse(os.write.called) self.assertFalse(os.close.called) utils.report_ready(100) os.write.assert_called_with(100, mock.ANY) os.close.assert_called_with(100) os.write.reset() os.close.reset() with io.open('notification-fd', 'w') as f: f.write('300') utils.report_ready() os.write.assert_called_with(300, mock.ANY) os.close.assert_called_with(300) os.write.reset() os.close.reset() with io.open('notification-fd', 'w') as f: f.write('300\n') utils.report_ready() os.write.assert_called_with(300, mock.ANY) os.close.assert_called_with(300) os.chdir(cwd)
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)
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)