def testIntegration(self): # CONFIG: config.beah_conf(args=[]) conf = config.get_conf('beah') srv = start_server(conf=conf) log = logging.getLogger('backend') class FakeTask(object): origin = {'signature': 'FakeTask'} task_id = 'no-id' def proc_cmd(self, cmd): log.debug("FakeTask.proc_cmd(%r)", cmd) class FakeBackend(object): def proc_evt(self, evt, **kwargs): log.debug("FakeBackend.proc_evt(%r, **%r)", evt, kwargs) t = FakeTask() reactor.callLater(2, srv.proc_evt, t, event.variable_set('say_hi', 'Hello World!')) #reactor.callLater(2.1, srv.proc_evt, t, event.variable_get('say_hi')) reactor.callLater(2.2, srv.proc_evt, t, event.variable_get('say_hi', dest='test.loop')) b = FakeBackend() reactor.callLater(3, srv.proc_cmd, b, command.kill()) forwarder.main(args=[])
def testIntegration(self): # CONFIG: config.beah_conf(args=[]) conf = config.get_conf('beah') srv = start_server(conf=conf) log = logging.getLogger('backend') class FakeTask(object): origin = {'signature':'FakeTask'} task_id = 'no-id' def proc_cmd(self, cmd): log.debug("FakeTask.proc_cmd(%r)", cmd) class FakeBackend(object): def proc_evt(self, evt, **kwargs): log.debug("FakeBackend.proc_evt(%r, **%r)", evt, kwargs) t = FakeTask() reactor.callLater(2, srv.proc_evt, t, event.variable_set('say_hi', 'Hello World!')) #reactor.callLater(2.1, srv.proc_evt, t, event.variable_get('say_hi')) reactor.callLater(2.2, srv.proc_evt, t, event.variable_get('say_hi', dest='test.loop')) b = FakeBackend() reactor.callLater(3, srv.proc_cmd, b, command.kill()) forwarder.main(args=[])
def main(): from beah.core import debug config.beah_conf() conf = config.get_conf('beah') debug.setup(os.getenv("BEAH_SRV_DEBUGGER"), conf.get('DEFAULT', 'NAME')) print main_srv.__doc__ main_srv(conf=conf) debug.runcall(reactor.run)
def _try_beah_conf(): config.beah_conf(overrides={'BEAH_CONF':'', 'TEST':'Test'}, args=[]) def dump_config(cfg): print "\n=== Dump:%s ===" % cfg.id print "files: %s + %s" % (cfg._conf_runtime(None), cfg._conf_files()) print "file: %s" % (cfg._get_conf_file({}), ) #config._get_config('beah').print_() config._Config._remove('beah')
def _try_beah_conf(): config.beah_conf(overrides={'BEAH_CONF': '', 'TEST': 'Test'}, args=[]) def dump_config(cfg): print "\n=== Dump:%s ===" % cfg.id print "files: %s + %s" % (cfg._conf_runtime(None), cfg._conf_files()) print "file: %s" % (cfg._get_conf_file({}), ) #config._get_config('beah').print_() config._Config._remove('beah')
def start_server(conf=None, backend_host=None, backend_port=None, backend_adaptor=BackendAdaptor_JSON, task_host=None, task_port=None, task_adaptor=TaskAdaptor_JSON, spawn=None): # CONFIG: if not conf: config.beah_conf() conf = config.get_conf('beah') # LOGGING: twisted_logging(log, level=logging.WARNING) log.setLevel(str2log_level(conf.get('CONTROLLER', 'LOG'))) # Create a directory for runtime # FIXME: should try to create a temp path if following fails: ensuredir(conf.get('CONTROLLER', 'VAR_ROOT')) # Create a directory for logging and check permissions lp = conf.get('CONTROLLER', 'LOG_PATH') make_log_handler(log, lp, conf.get('CONTROLLER', 'LOG_FILE_NAME'), syslog=True, console=conf.get('CONTROLLER', 'CONSOLE_LOG', False)) if parse_bool(config.get_conf('beah').get('CONTROLLER', 'DEVEL')): print_this = log_this(log.debug, True) make_class_verbose(Controller, print_this) make_class_verbose(MasterTask, print_this) # RUN: backend_host = backend_host or conf.get('BACKEND', 'INTERFACE') backend_port = backend_port or conf.get('BACKEND', 'PORT') task_host = task_host or conf.get('TASK', 'INTERFACE') task_port = task_port or int(conf.get('TASK', 'PORT')) if os.name == 'posix': if backend_port != '': backend_port = int(backend_port) backend_socket = conf.get('BACKEND', 'SOCKET') if task_port != '': task_port = int(task_port) task_socket = conf.get('TASK', 'SOCKET') else: backend_port = int(backend_port) backend_socket = '' task_port = int(task_port) task_socket = '' runtime = runtimes.ShelveRuntime(conf.get('CONTROLLER', 'RUNTIME_FILE_NAME')) runtime.vars = runtimes.TypeDict(runtime, 'vars') runtime.tasks = runtimes.TypeDict(runtime, 'tasks') controller = Controller(spawn or Spawn(task_host, task_port, socket=task_socket), runtime=runtime) def on_killed(): if not controller.backends: reactor.stop() return reactor.callLater(2, reactor.stop) controller.on_killed = on_killed log.info("################################") log.info("# Starting a Controller... #") log.info("################################") backend_listener = BackendListener(controller, backend_adaptor) if backend_port != '': if backend_host == 'localhost': listening = listen_loopback_tcp(backend_port, backend_listener) elif backend_host: listening = reactor.listenTCP(backend_port, backend_listener, interface=backend_host) else: listening = listen_all_tcp(backend_port, backend_listener, ipv6_disabled=parse_bool(conf.get('DEFAULT', 'IPV6_DISABLED'))) log.info("Controller: BackendListener listening on %s port %s", listening.getHost().host, listening.getHost().port) if backend_socket: if os.path.exists(backend_socket): # clean-up after e.g. system crash: log.warning("Controller: BackendListener cleaning %s", backend_socket) os.remove(backend_socket) log.info("Controller: BackendListener listening on %s", backend_socket) reactor.listenUNIX(backend_socket, backend_listener) task_listener = TaskListener(controller, task_adaptor) if task_port != '': if task_host == 'localhost': listening = listen_loopback_tcp(task_port, task_listener) elif task_host: listening = reactor.listenTCP(task_port, task_listener, interface=task_host) else: listening = listen_all_tcp(task_port, task_listener, ipv6_disabled=parse_bool(conf.get('DEFAULT', 'IPV6_DISABLED'))) log.info("Controller: TaskListener listening on %s port %s", listening.getHost().host, listening.getHost().port) if task_socket: if os.path.exists(task_socket): # clean-up after e.g. system crash: log.warning("Controller: TaskListener cleaning %s", task_socket) os.remove(task_socket) log.info("Controller: TaskListener listening on %s", task_socket) reactor.listenUNIX(task_socket, task_listener) return controller
def start_server(conf=None, backend_host=None, backend_port=None, backend_adaptor=BackendAdaptor_JSON, task_host=None, task_port=None, task_adaptor=TaskAdaptor_JSON, spawn=None): # CONFIG: if not conf: config.beah_conf() conf = config.get_conf('beah') # LOGGING: twisted_logging(log, level=logging.WARNING) log.setLevel(str2log_level(conf.get('CONTROLLER', 'LOG'))) # Create a directory for runtime # FIXME: should try to create a temp path if following fails: ensuredir(conf.get('CONTROLLER', 'VAR_ROOT')) # Create a directory for logging and check permissions lp = conf.get('CONTROLLER', 'LOG_PATH') make_log_handler(log, lp, conf.get('CONTROLLER', 'LOG_FILE_NAME'), syslog=True, console=conf.get('CONTROLLER', 'CONSOLE_LOG', False)) if parse_bool(config.get_conf('beah').get('CONTROLLER', 'DEVEL')): print_this = log_this(log.debug, True) make_class_verbose(Controller, print_this) make_class_verbose(MasterTask, print_this) # RUN: backend_host = backend_host or conf.get('BACKEND', 'INTERFACE') backend_port = backend_port or conf.get('BACKEND', 'PORT') task_host = task_host or conf.get('TASK', 'INTERFACE') task_port = task_port or int(conf.get('TASK', 'PORT')) if os.name == 'posix': if backend_port != '': backend_port = int(backend_port) backend_socket = conf.get('BACKEND', 'SOCKET') if task_port != '': task_port = int(task_port) task_socket = conf.get('TASK', 'SOCKET') else: backend_port = int(backend_port) backend_socket = '' task_port = int(task_port) task_socket = '' runtime = runtimes.ShelveRuntime( conf.get('CONTROLLER', 'RUNTIME_FILE_NAME')) runtime.vars = runtimes.TypeDict(runtime, 'vars') runtime.tasks = runtimes.TypeDict(runtime, 'tasks') controller = Controller(spawn or Spawn(task_host, task_port, socket=task_socket), runtime=runtime) def on_killed(): if not controller.backends: reactor.stop() return reactor.callLater(2, reactor.stop) controller.on_killed = on_killed log.info("################################") log.info("# Starting a Controller... #") log.info("################################") backend_listener = BackendListener(controller, backend_adaptor) if backend_port != '': if backend_host == 'localhost': listening = listen_loopback_tcp(backend_port, backend_listener) elif backend_host: listening = reactor.listenTCP(backend_port, backend_listener, interface=backend_host) else: listening = listen_all_tcp(backend_port, backend_listener, ipv6_disabled=parse_bool( conf.get('DEFAULT', 'IPV6_DISABLED'))) log.info("Controller: BackendListener listening on %s port %s", listening.getHost().host, listening.getHost().port) if backend_socket: if os.path.exists(backend_socket): # clean-up after e.g. system crash: log.warning("Controller: BackendListener cleaning %s", backend_socket) os.remove(backend_socket) log.info("Controller: BackendListener listening on %s", backend_socket) reactor.listenUNIX(backend_socket, backend_listener) task_listener = TaskListener(controller, task_adaptor) if task_port != '': if task_host == 'localhost': listening = listen_loopback_tcp(task_port, task_listener) elif task_host: listening = reactor.listenTCP(task_port, task_listener, interface=task_host) else: listening = listen_all_tcp(task_port, task_listener, ipv6_disabled=parse_bool( conf.get('DEFAULT', 'IPV6_DISABLED'))) log.info("Controller: TaskListener listening on %s port %s", listening.getHost().host, listening.getHost().port) if task_socket: if os.path.exists(task_socket): # clean-up after e.g. system crash: log.warning("Controller: TaskListener cleaning %s", task_socket) os.remove(task_socket) log.info("Controller: TaskListener listening on %s", task_socket) reactor.listenUNIX(task_socket, task_listener) return controller