def run(argv): # /usr/bin/neubot module ... del argv[0] module = argv[0] if module == "help": sys.stdout.write("Neubot help -- prints available commands\n") commands = " ".join(sorted(MODULES.keys())) lines = textwrap.wrap(commands, 60) sys.stdout.write("Commands: " + lines[0] + "\n") for s in lines[1:]: sys.stdout.write(" " + s + "\n") sys.stdout.write("Try `neubot CMD --help` for more help on CMD.\n") sys.exit(0) utils_modules.modprobe(None, "load_subcommand", MODULES) if not module in MODULES: sys.stderr.write("Invalid module: %s\n" % module) sys.stderr.write("Try `neubot help` to list the available modules\n") sys.exit(1) # Dinamically load the selected module's main() at runtime module = MODULES[module] __import__(module) MAIN = sys.modules[module].main # neubot module ... argv[0] = "neubot " + argv[0] try: MAIN(argv) except KeyboardInterrupt: pass except SystemExit: raise except: logging.error('Exception', exc_info=1) sys.exit(1) sys.exit(0)
def run(self, test, deferred, auto_discover=True, ctx=None): ''' Run test and deferred when done ''' if ( test != "rendezvous" and test != "speedtest" and test != "bittorrent" and test != "dload" and test != "raw" and test != "mlab-ns" and test not in self.dynamic_tests ): utils_modules.modprobe("mod_" + test, "register_test", self.dynamic_tests) if auto_discover: logging.info('runner_core: Need to auto-discover first...') deferred2 = Deferred() deferred2.add_callback(lambda param: None) if test == 'raw': self.queue.append(('mlab-ns', deferred2, {'policy': 'random'})) elif test == "bittorrent" or test == "speedtest": self.queue.append(('rendezvous', deferred2, None)) else: try: test_rec = self.dynamic_tests[test] self.queue.append((test_rec["discover_method"], deferred2, {"policy": test_rec["discover_policy"]})) except (KeyboardInterrupt, SystemExit): raise except: logging.warning("runner: internal error", exc_info=1) self.queue.append((test, deferred, ctx)) self.run_queue()
def main(subcommand, args): ''' Run a subcommand's main() ''' utils_modules.modprobe(None, "load_subcommand", SUBCOMMANDS) # # Map subcommand to module. The possiblity of running internal subcommands # by prefixing 'neubot.' to the module name is (for now) undocumented, but # is here because it is helpful for debugging. # if subcommand.startswith('neubot.'): module = subcommand elif not subcommand in SUBCOMMANDS: sys.stderr.write('Invalid subcommand: %s\n' % subcommand) print_subcommands(sys.stderr) sys.exit(1) else: module = SUBCOMMANDS[subcommand] # Dinamically load the selected subcommand's main() at runtime __import__(module) mainfunc = sys.modules[module].main # Fix args[0] args[0] = 'neubot ' + subcommand # Run main() try: mainfunc(args) except KeyboardInterrupt: sys.exit(1) except SystemExit: raise except: logging.error('Exception', exc_info=1) sys.exit(1)
def main(args): """ Starts the server module """ if not system.has_enough_privs(): sys.exit('FATAL: you must be root') try: options, arguments = getopt.getopt(args[1:], 'A:b:D:dv') except getopt.error: sys.exit(USAGE) if arguments: sys.exit(USAGE) address = ':: 0.0.0.0' backend = 'mlab' for name, value in options: if name == '-A': address = value elif name == '-b': backend = value elif name == '-D': name, value = value.split('=', 1) if name not in VALID_MACROS: sys.exit(USAGE) if name != 'server.datadir': # XXX value = int(value) SETTINGS[name] = value elif name == '-d': SETTINGS['server.daemonize'] = 0 elif name == '-v': CONFIG['verbose'] = 1 logging.debug('server: using backend: %s... in progress', backend) if backend == 'mlab': BACKEND.datadir_init(None, SETTINGS['server.datadir']) BACKEND.use_backend('mlab') elif backend == 'neubot': DATABASE.connect() BACKEND.use_backend('neubot') elif backend == 'volatile': BACKEND.use_backend('volatile') else: BACKEND.use_backend('null') logging.debug('server: using backend: %s... complete', backend) for name, value in SETTINGS.items(): CONFIG[name] = value conf = CONFIG.copy() # # Configure our global HTTP server and make # sure that we don't provide filesystem access # even by mistake. # conf["http.server.rootdir"] = "" HTTP_SERVER.configure(conf) # # New-new style: don't bother with abstraction and start the f*****g # server by invoking its listen() method. # if CONFIG['server.raw']: logging.debug('server: starting raw server... in progress') RAW_SERVER_EX.listen((address, 12345), CONFIG['prefer_ipv6'], 0, '') logging.debug('server: starting raw server... complete') if conf['server.skype']: logging.debug('server: starting skype server... in progress') SKYPE_SERVER_EX.listen((":: 0.0.0.0", 45678), CONFIG['prefer_ipv6'], 0, '') logging.debug('server: starting skype server... complete') # # New-style modules are started just setting a # bunch of conf[] variables and then invoking # their run() method in order to kick them off. # This is now depricated in favor of the new- # new style described above. # if conf["server.negotiate"]: negotiate.run(POLLER, conf) if conf["server.bittorrent"]: conf["bittorrent.address"] = address conf["bittorrent.listen"] = True conf["bittorrent.negotiate"] = True bittorrent.run(POLLER, conf) if conf['server.speedtest']: #conf['speedtest.listen'] = 1 # Not yet #conf['speedtest.negotiate'] = 1 # Not yet neubot.speedtest.wrapper.run(POLLER, conf) # Migrating from old style to new style if conf["server.rendezvous"]: #conf["rendezvous.listen"] = True # Not yet neubot.rendezvous.server.run() # # Historically Neubot runs on port 9773 and # 8080 but we would like to switch to port 80 # in the long term period, because it's rare # that they filter it. # OTOH it looks like it's not possible to # do that easily w/ M-Lab because the port # is already taken. # ports = (80, 8080, 9773) for port in ports: HTTP_SERVER.listen((address, port)) # # Start server-side API for Nagios plugin # to query the state of the server. # functionalities. # if conf["server.sapi"]: server = ServerSideAPI(POLLER) server.configure(conf) HTTP_SERVER.register_child(server, "/sapi") # # Create localhost-only debug server # if CONFIG['server.debug']: logging.info('server: Starting debug server at {127.0.0.1,::1}:9774') server = DebugAPI(POLLER) server.configure(conf) server.listen(('127.0.0.1 ::1', 9774)) # Probe existing modules and ask them to attach to us utils_modules.modprobe(None, "server", { "http_server": HTTP_SERVER, "negotiate_server": NEGOTIATE_SERVER, }) # # Go background and drop privileges, # then enter into the main loop. # if conf["server.daemonize"]: LOG.redirect() system.go_background() sigterm_handler = lambda signo, frame: POLLER.break_loop() signal.signal(signal.SIGTERM, sigterm_handler) logging.info('Neubot server -- starting up') system.drop_privileges() POLLER.loop() logging.info('Neubot server -- shutting down') utils_posix.remove_pidfile('/var/run/neubot.pid')