def run_twistd_plugin(filename): from carbon.conf import get_parser from twisted.scripts.twistd import ServerOptions bin_dir = dirname(abspath(filename)) root_dir = dirname(bin_dir) os.environ.setdefault('GRAPHITE_ROOT', root_dir) program = basename(filename).split('.')[0] # First, parse command line options as the legacy carbon scripts used to # do. parser = get_parser(program) (options, args) = parser.parse_args() if not args: parser.print_usage() return # This isn't as evil as you might think __builtins__["instance"] = options.instance __builtins__["program"] = program # Then forward applicable options to either twistd or to the plugin itself. twistd_options = ["--no_save"] # If no reactor was selected yet, try to use the epoll reactor if # available. try: from twisted.internet import epollreactor twistd_options.append("--reactor=epoll") except: pass if options.debug: twistd_options.extend(["--nodaemon"]) if options.profile: twistd_options.append("--profile") if options.pidfile: twistd_options.extend(["--pidfile", options.pidfile]) # Now for the plugin-specific options. twistd_options.append(program) if options.debug: twistd_options.append("--debug") for option_name, option_value in vars(options).items(): if (option_value is not None and option_name not in ("debug", "profile", "pidfile")): twistd_options.extend(["--%s" % option_name.replace("_", "-"), option_value]) # Finally, append extra args so that twistd has a chance to process them. twistd_options.extend(args) config = ServerOptions() config.parseOptions(twistd_options) runApp(config)
def run(): """Load twistd which will then load the rest of knoboo """ twistd_options = preRun() config = twistd.ServerOptions() config.parseOptions(twistd_options) twistd.runApp(config)
def start(): twistd_args = [] flags = ['nodaemon', 'no_save'] options = ['pidfile', 'logfile', 'rundir', 'euid', 'gid', 'uid', 'umask'] for option in options: if config.main.get(option, None) is not None: twistd_args.append('--%s' % option) twistd_args.append(config.main.get(option)) for flag in flags: if config.main.get(flag, None) is True: twistd_args.append('--%s' % flag) twistd_args.append('StartOONIBackend') # Point to our backend plugin twistd_config = OONIBackendTwistdConfig() try: twistd_config.parseOptions(twistd_args) except usage.error: print "Usage error from twistd" sys.exit(7) twistd_config.loadedPlugins = { 'StartOONIBackend': StartOONIBackendPlugin() } twistd.runApp(twistd_config) return 0
def run_twistd_plugin(filename): from carbon.conf import get_parser from twisted.scripts.twistd import ServerOptions bin_dir = dirname(abspath(filename)) root_dir = dirname(bin_dir) os.environ.setdefault('GRAPHITE_ROOT', root_dir) program = basename(filename).split('.')[0] # First, parse command line options as the legacy carbon scripts used to # do. parser = get_parser(program) (options, args) = parser.parse_args() if not args: parser.print_usage() return # This isn't as evil as you might think __builtins__["instance"] = options.instance __builtins__["program"] = program # Then forward applicable options to either twistd or to the plugin itself. twistd_options = ["--no_save"] # If no reactor was selected yet, try to use the epoll reactor if # available. try: from twisted.internet import epollreactor twistd_options.append("--reactor=epoll") except: pass if options.debug: twistd_options.extend(["--nodaemon"]) if options.profile: twistd_options.append("--profile") if options.pidfile: twistd_options.extend(["--pidfile", options.pidfile]) # Now for the plugin-specific options. twistd_options.append(program) if options.debug: twistd_options.append("--debug") for option_name, option_value in vars(options).items(): if (option_value is not None and option_name not in ("debug", "profile", "pidfile")): twistd_options.extend( ["--%s" % option_name.replace("_", "-"), option_value]) # Finally, append extra args so that twistd has a chance to process them. twistd_options.extend(args) config = ServerOptions() config.parseOptions(twistd_options) runApp(config)
def runTAC(filename, options): """ Run the specified TAC C{filename}, which is expected to be in the C{fluiddb/scripts} directory. """ path = getScriptPath(filename) options['python'] = path options.parseOptions() runApp(options)
def runTAC(filename, options): """ Run the specified TAC C{filename}, which is expected to be in the C{fluiddb/scripts} directory. """ path = getScriptPath(filename) options['python'] = path options.parseOptions() runApp(options)
def _run_twistd_with_config(twistd_config): if twistd_config['D']: # replace the default failure object with one that performs no cleaning from twisted.python import failure from piped import util failure._OriginalFailure = failure.Failure failure.Failure = util.NonCleaningFailure sys.argv[0] = 'twistd' # pretend to be twistd, so that piped.log doesn't decide to bootstrap the logging system twistd.runApp(twistd_config)
def run(config): """ Runs a Tahoe-LAFS node in the foreground. Sets up the IService instance corresponding to the type of node that's starting and uses Twisted's twistd runner to disconnect our process from the terminal. """ out = config.stdout err = config.stderr basedir = config['basedir'] quoted_basedir = quote_local_unicode_path(basedir) print("'tahoe {}' in {}".format(config.subcommand_name, quoted_basedir), file=out) if not os.path.isdir(basedir): print("%s does not look like a directory at all" % quoted_basedir, file=err) return 1 nodetype = identify_node_type(basedir) if not nodetype: print("%s is not a recognizable node directory" % quoted_basedir, file=err) return 1 # Now prepare to turn into a twistd process. This os.chdir is the point # of no return. os.chdir(basedir) twistd_args = ["--nodaemon"] twistd_args.extend(config.twistd_args) twistd_args.append( "DaemonizeTahoeNode") # point at our DaemonizeTahoeNodePlugin twistd_config = MyTwistdConfig() twistd_config.stdout = out twistd_config.stderr = err try: twistd_config.parseOptions(twistd_args) except usage.error as ue: # these arguments were unsuitable for 'twistd' print(config, file=err) print("tahoe %s: usage error from twistd: %s\n" % (config.subcommand_name, ue), file=err) return 1 twistd_config.loadedPlugins = { "DaemonizeTahoeNode": DaemonizeTahoeNodePlugin(nodetype, basedir) } # handle invalid PID file (twistd might not start otherwise) pidfile = get_pidfile(basedir) if get_pid_from_pidfile(pidfile) == -1: print("found invalid PID file in %s - deleting it" % basedir, file=err) os.remove(pidfile) # We always pass --nodaemon so twistd.runApp does not daemonize. print("running node in %s" % (quoted_basedir, ), file=out) twistd.runApp(twistd_config) return 0
def serve(filename): logging.debug("Serving %s" % filename) name = os.path.basename(filename) OptionsClass = WrapTwistedOptions(TwistdServerOptions) options = OptionsClass() argv = options.parseOptions() logging.getLogger('amqplib').setLevel(logging.WARN) FLAGS.python = filename FLAGS.no_save = True if not FLAGS.pidfile: FLAGS.pidfile = '%s.pid' % name elif FLAGS.pidfile.endswith('twistd.pid'): FLAGS.pidfile = FLAGS.pidfile.replace('twistd.pid', '%s.pid' % name) if not FLAGS.logfile: FLAGS.logfile = '%s.log' % name action = 'start' if len(argv) > 1: action = argv.pop() if action == 'stop': stop(FLAGS.pidfile) sys.exit() elif action == 'restart': stop(FLAGS.pidfile) elif action == 'start': pass else: print 'usage: %s [options] [start|stop|restart]' % argv[0] sys.exit(1) formatter = logging.Formatter( name + '(%(name)s): %(levelname)s %(message)s') handler = logging.StreamHandler(log.StdioOnnaStick()) handler.setFormatter(formatter) logging.getLogger().addHandler(handler) if FLAGS.verbose: logging.getLogger().setLevel(logging.DEBUG) else: logging.getLogger().setLevel(logging.WARNING) if FLAGS.syslog: syslog = logging.handlers.SysLogHandler(address='/dev/log') syslog.setFormatter(formatter) logging.getLogger().addHandler(syslog) logging.debug("Full set of FLAGS:") for flag in FLAGS: logging.debug("%s : %s" % (flag, FLAGS.get(flag, None))) twistd.runApp(options)
def start_server(args): c = MyTwistdConfig() #twistd_args = tuple(args.twistd_args) + ("XYZ",) base_args = [] if args.no_daemon: base_args.append("--nodaemon") twistd_args = base_args + ["XYZ"] c.parseOptions(tuple(twistd_args)) c.loadedPlugins = {"XYZ": MyPlugin(args)} print("starting wormhole relay server") # this forks and never comes back. The parent calls os._exit(0) twistd.runApp(c)
def start_server(args): from twisted.python import usage from twisted.scripts import twistd class MyTwistdConfig(twistd.ServerOptions): subCommands = [("XYZ", None, usage.Options, "node")] c = MyTwistdConfig() #twistd_args = tuple(args.twistd_args) + ("XYZ",) twistd_args = ("XYZ",) # TODO: allow user to add twistd-specific args c.parseOptions(twistd_args) c.loadedPlugins = {"XYZ": MyPlugin(args)} print("starting wormhole relay server") # this forks and never comes back. The parent calls os._exit(0) twistd.runApp(c)
def start_server(args): from twisted.python import usage from twisted.scripts import twistd class MyTwistdConfig(twistd.ServerOptions): subCommands = [("XYZ", None, usage.Options, "node")] c = MyTwistdConfig() #twistd_args = tuple(args.twistd_args) + ("XYZ",) twistd_args = ("XYZ", ) # TODO: allow user to add twistd-specific args c.parseOptions(twistd_args) c.loadedPlugins = {"XYZ": MyPlugin(args)} print("starting wormhole relay server") # this forks and never comes back. The parent calls os._exit(0) twistd.runApp(c)
def run_twistd_plugin(filename): from twisted.scripts.twistd import runApp from twisted.scripts.twistd import ServerOptions from rurouni.conf import get_parser bin_dir = dirname(abspath(filename)) root_dir = dirname(bin_dir) os.environ.setdefault('GRAPHITE_ROOT', root_dir) program = splitext(basename(filename))[0] parser = get_parser() (options, args) = parser.parse_args() if not args: parser.print_usage() return twistd_options = [] try: from twisted.internet import epollreactor twistd_options.append('--reactor=epoll') except: pass if options.debug or options.nodaemon: twistd_options.append('--nodaemon') if options.pidfile: twistd_options.extend(['--pidfile', options.pidfile]) if options.umask: twistd_options.extend(['--umask', options.umask]) twistd_options.append(program) if options.debug: twistd_options.append('--debug') for name, value in vars(options).items(): if (value is not None and name not in ('debug', 'nodaemon', 'pidfile', 'umask')): twistd_options.extend(["--%s" % name.replace("_", '-'), value]) twistd_options.extend(args) config = ServerOptions() config.parseOptions(twistd_options) runApp(config)
def start_server(args): from twisted.python import usage from twisted.scripts import twistd class MyTwistdConfig(twistd.ServerOptions): subCommands = [("XYZ", None, usage.Options, "node")] c = MyTwistdConfig() #twistd_args = tuple(args.twistd_args) + ("XYZ",) base_args = [] if args.no_daemon: base_args.append("--nodaemon") twistd_args = base_args + ["XYZ"] c.parseOptions(tuple(twistd_args)) c.loadedPlugins = {"XYZ": MyPlugin(args)} print("starting wormhole relay server") # this forks and never comes back. The parent calls os._exit(0) twistd.runApp(c)
def start_server(args): from twisted.python import usage from twisted.scripts import twistd class MyTwistdConfig(twistd.ServerOptions): subCommands = [("XYZ", None, usage.Options, "node")] c = MyTwistdConfig() #twistd_args = tuple(args.twistd_args) + ("XYZ",) base_args = [] if args.no_daemon: base_args.append("--nodaemon") twistd_args = base_args + ["XYZ"] c.parseOptions(tuple(twistd_args)) c.loadedPlugins = {"XYZ": MyPlugin(args)} print("starting wormhole relay server") # this forks and never comes back. The parent calls os._exit(0) twistd.runApp(c)
def serve(filename): logging.debug(_("Serving %s") % filename) name = os.path.basename(filename) OptionsClass = WrapTwistedOptions(TwistdServerOptions) options = OptionsClass() argv = options.parseOptions() FLAGS.python = filename FLAGS.no_save = True if not FLAGS.pidfile: FLAGS.pidfile = '%s.pid' % name elif FLAGS.pidfile.endswith('twistd.pid'): FLAGS.pidfile = FLAGS.pidfile.replace('twistd.pid', '%s.pid' % name) if not FLAGS.prefix: FLAGS.prefix = name elif FLAGS.prefix.endswith('twisted'): FLAGS.prefix = FLAGS.prefix.replace('twisted', name) action = 'start' if len(argv) > 1: action = argv.pop() if action == 'stop': stop(FLAGS.pidfile) sys.exit() elif action == 'restart': stop(FLAGS.pidfile) elif action == 'start': pass else: print 'usage: %s [options] [start|stop|restart]' % argv[0] sys.exit(1) logging.basicConfig() logging.debug(_("Full set of FLAGS:")) for flag in FLAGS: logging.debug("%s : %s" % (flag, FLAGS.get(flag, None))) logging.audit(_("Starting %s"), name) twistd.runApp(options)
def serve(filename): logging.debug(_("Serving %s") % filename) name = os.path.basename(filename) OptionsClass = WrapTwistedOptions(TwistdServerOptions) options = OptionsClass() argv = options.parseOptions() FLAGS.python = filename FLAGS.no_save = True if not FLAGS.pidfile: FLAGS.pidfile = '%s.pid' % name elif FLAGS.pidfile.endswith('twistd.pid'): FLAGS.pidfile = FLAGS.pidfile.replace('twistd.pid', '%s.pid' % name) if not FLAGS.prefix: FLAGS.prefix = name elif FLAGS.prefix.endswith('twisted'): FLAGS.prefix = FLAGS.prefix.replace('twisted', name) action = 'start' if len(argv) > 1: action = argv.pop() if action == 'stop': stop(FLAGS.pidfile) sys.exit() elif action == 'restart': stop(FLAGS.pidfile) elif action == 'start': pass else: print 'usage: %s [options] [start|stop|restart]' % argv[0] sys.exit(1) logging.debug(_("Full set of FLAGS:")) for flag in FLAGS: logging.debug("%s : %s" % (flag, FLAGS.get(flag, None))) logging.audit(_("Starting %s"), name) twistd.runApp(options)
def start_agent(options=None): config.set_paths() config.initialize_ooni_home() config.read_config_file() os.chdir(config.running_path) # Since we are starting the logger below ourselves we make twistd log to # a null log observer twistd_args = ['--logger', 'ooni.utils.log.ooniloggerNull', '--umask', '022'] twistd_config = OoniprobeTwistdConfig() if options is not None: twistd_args.extend(options.twistd_args) twistd_args.append("StartOoniprobeAgent") try: twistd_config.parseOptions(twistd_args) except usage.error as ue: print("ooniprobe: usage error from twistd: {}\n".format(ue)) sys.exit(1) twistd_config.loadedPlugins = { "StartOoniprobeAgent": StartOoniprobeAgentPlugin() } try: get_running_pidfile() print("Stop ooniprobe-agent before attempting to start it") return 1 except NotRunning: pass print("Starting ooniprobe agent.") print("To view the GUI go to %s" % config.web_ui_url) log.start() twistd.runApp(twistd_config) return 0
def start_agent(options=None): config.set_paths() config.initialize_ooni_home() config.read_config_file() os.chdir(config.running_path) # Since we are starting the logger below ourselves we make twistd log to # a null log observer twistd_args = ['--logger', 'ooni.utils.log.ooniloggerNull', '--umask', '022'] twistd_config = OoniprobeTwistdConfig() if options is not None: twistd_args.extend(options.twistd_args) twistd_args.append("StartOoniprobeAgent") try: twistd_config.parseOptions(twistd_args) except usage.error as ue: print("ooniprobe: usage error from twistd: {}\n".format(ue)) sys.exit(1) twistd_config.loadedPlugins = { "StartOoniprobeAgent": StartOoniprobeAgentPlugin() } try: get_running_pidfile() print("Stop ooniprobe-agent before attempting to start it") return 1 except NotRunning: pass print("Starting ooniprobe agent.") print("To view the GUI go to %s" % config.web_ui_url) log.start() twistd.runApp(twistd_config) return 0
def start(): twistd_args = [] flags = ["nodaemon", "no_save"] options = ["pidfile", "logfile", "rundir", "euid", "gid", "uid", "umask"] for option in options: if config.main.get(option, None) is not None: twistd_args.append("--%s" % option) twistd_args.append(config.main.get(option)) for flag in flags: if config.main.get(flag, None) is True: twistd_args.append("--%s" % flag) twistd_args.append("StartOONIBackend") # Point to our backend plugin twistd_config = OONIBackendTwistdConfig() try: twistd_config.parseOptions(twistd_args) except usage.error: print "Usage error from twistd" sys.exit(7) twistd_config.loadedPlugins = {"StartOONIBackend": StartOONIBackendPlugin()} twistd.runApp(twistd_config) return 0
def execute(self, settings, id): print "Starting module from {0} with ID {1}".format(settings, id) conf.settings = conf.Settings.from_file(settings) # @todo: Install the chosen reactor (look at settings.reactor) #from twisted.internet import reactor from twisted.internet.selectreactor import install reactor = install() sys.path.insert(0, os.path.dirname(settings)) # The module should now be on the path, let's search for it. from smac.modules import base, utils try: # The get_class_instance function loads the 'implementation' # module by default. Search for an instance of base.ModuleBase # in the 'implementation' module. # The 'implementation module should be defined by the SMAC module # and thus contained in the directory which we just inserted in # the path. handler = utils.get_class(base.ModuleBase) except ImportError as e: print "The 'implementation' python module was not found." print e import traceback traceback.print_exc() sys.exit(1) if handler is None: print "No instances of 'smac.modules.base.ModuleBase' were ", \ "found in the 'implementation' python module." sys.exit(1) def amqp_service(interface): return interface.extends(AMQPService.Iface) def rpc_service(interface): return interface.extends(RPCService.Iface) amqp_interface = filter(amqp_service, list(implementedBy(handler)))[0] amqp_processor = utils.get_module_for_interface(amqp_interface) try: rpc_interface = filter(rpc_service, list(implementedBy(handler)))[0] except IndexError: pass else: rpc_processor = utils.get_module_for_interface(rpc_interface) startup_registry['rpc_processor'] = rpc_processor # Save the necessary variables in the global registry to be used by # the runner called by twisted.runApp() startup_registry['amqp_processor'] = amqp_processor startup_registry['handler'] = handler startup_registry['instance_id'] = id startup_registry['__name__'] = '__main__' runner = os.path.join(os.path.dirname(base.__file__), 'runner.py') # Initialize basic configuration of the twisted application runner # @TODO: Move this to the `smac run` command from twisted.scripts import twistd config = twistd.ServerOptions() options = ['-noy', runner] if runtime.platformType != "win32": options.append('--pidfile={0}.pid'.format(id)) config.parseOptions(options) # Set the terminal title print "\x1b]2;SMAC Module - {0} {1}\x07".format(handler.__name__, id) print "Initialization succeded, passing control to twistd\n" # Run the selected module twistd.runApp(config)
#!/usr/bin/python # -*- coding:utf-8 -*- from twisted.scripts.twistd import runApp from os.path import join, dirname from sys import argv from utils import orderServerOptions, orderServerOption from const import * import sys reload(sys) sys.setdefaultencoding('utf-8') if __name__ == '__main__': orderServerOption.parseOptions() if orderServerOption.get('env') == ENV_DEV: # argv[1:1] = ['-n', '-b', '-y', join(dirname(__file__), 'orderApp.py')] argv[1:1] = ['-y', join(dirname(__file__), 'orderApp.py')] else: argv[1:1] = ['-y', join(dirname(__file__), 'orderApp.py')] orderServerOption = orderServerOptions() orderServerOption.parseOptions() runApp(orderServerOption)
from twisted.python import usage from codenode.frontend.backend.fixtures.development import run as bootstrap_database from codenode.frontend.search import search def run(options): """ this reimplements twisted.application.app.run to use an option array, instead of argvs""" config = ServerOptions() try: config.parseOptions(options=options) except usage.error, ue: print config print "%s: %s" % (sys.argv[0], ue) else: runApp(config) class Command(NoArgsCommand): """ Run local desktop version of Codenode. """ def check_home_dir(self, path): """ check an environment directory contains a database and search index, etc.""" if not os.path.exists(path): os.mkdir(path) if not os.path.exists(settings.DATABASE_NAME):
# this spawns off a child process, and the parent calls os._exit(0), so # there's no way for us to get control afterwards, even with 'except # SystemExit'. So if we want to do anything with the running child, we # have two options: # * fork first, and have our child wait for the runApp() child to get # running. (note: just fork(). This is easier than fork+exec, since we # don't have to get PATH and PYTHONPATH set up, since we're not # starting a *different* process, just cloning a new instance of the # current process) # * or have the user run a separate command some time after this one # exits. print >> out, "starting node in %s" % basedir child_pid = os.fork() if child_pid == 0: # this is the child: turn int othe new app twistd.runApp(twistd_config) os._exit(1) # should never be called # CONTROL NEVER REACHES HERE # this is the parent print >> out, "child launched .. waiting for startup" from . import webwait baseurl = webwait.wait(basedir, err) print >> out, "child ready at %s" % baseurl def stop(so, out, err): basedir = so["basedir"] dbfile = os.path.join(basedir, "toolbed.db") if not (os.path.isdir(basedir) and os.path.exists(dbfile)): print >> err, "'%s' doesn't look like a toolbed basedir, quitting" % basedir return 1
def daemonize(config): """ Runs the 'tahoe daemonize' command. Sets up the IService instance corresponding to the type of node that's starting and uses Twisted's twistd runner to disconnect our process from the terminal. """ out = config.stdout err = config.stderr basedir = config['basedir'] quoted_basedir = quote_local_unicode_path(basedir) print("daemonizing in {}".format(quoted_basedir), file=out) if not os.path.isdir(basedir): print("%s does not look like a directory at all" % quoted_basedir, file=err) return 1 nodetype = identify_node_type(basedir) if not nodetype: print("%s is not a recognizable node directory" % quoted_basedir, file=err) return 1 # Now prepare to turn into a twistd process. This os.chdir is the point # of no return. os.chdir(basedir) twistd_args = [] if (nodetype in (u"client", u"introducer") and "--nodaemon" not in config.twistd_args and "--syslog" not in config.twistd_args and "--logfile" not in config.twistd_args): fileutil.make_dirs(os.path.join(basedir, u"logs")) twistd_args.extend(["--logfile", os.path.join("logs", "twistd.log")]) twistd_args.extend(config.twistd_args) twistd_args.append("DaemonizeTahoeNode") # point at our DaemonizeTahoeNodePlugin twistd_config = MyTwistdConfig() try: twistd_config.parseOptions(twistd_args) except usage.error as ue: # these arguments were unsuitable for 'twistd' print(config, file=err) print("tahoe %s: usage error from twistd: %s\n" % (config.subcommand_name, ue), file=err) return 1 twistd_config.loadedPlugins = {"DaemonizeTahoeNode": DaemonizeTahoeNodePlugin(nodetype, basedir)} # handle invalid PID file (twistd might not start otherwise) pidfile = get_pidfile(basedir) if get_pid_from_pidfile(pidfile) == -1: print("found invalid PID file in %s - deleting it" % basedir, file=err) os.remove(pidfile) # On Unix-like platforms: # Unless --nodaemon was provided, the twistd.runApp() below spawns off a # child process, and the parent calls os._exit(0), so there's no way for # us to get control afterwards, even with 'except SystemExit'. If # application setup fails (e.g. ImportError), runApp() will raise an # exception. # # So if we wanted to do anything with the running child, we'd have two # options: # # * fork first, and have our child wait for the runApp() child to get # running. (note: just fork(). This is easier than fork+exec, since we # don't have to get PATH and PYTHONPATH set up, since we're not # starting a *different* process, just cloning a new instance of the # current process) # * or have the user run a separate command some time after this one # exits. # # For Tahoe, we don't need to do anything with the child, so we can just # let it exit. # # On Windows: # twistd does not fork; it just runs in the current process whether or not # --nodaemon is specified. (As on Unix, --nodaemon does have the side effect # of causing us to log to stdout/stderr.) if "--nodaemon" in twistd_args or sys.platform == "win32": verb = "running" else: verb = "starting" print("%s node in %s" % (verb, quoted_basedir), file=out) twistd.runApp(twistd_config) # we should only reach here if --nodaemon or equivalent was used return 0
def daemonize(config): """ Runs the 'tahoe daemonize' command. Sets up the IService instance corresponding to the type of node that's starting and uses Twisted's twistd runner to disconnect our process from the terminal. """ out = config.stdout err = config.stderr basedir = config['basedir'] quoted_basedir = quote_local_unicode_path(basedir) print("daemonizing in {}".format(quoted_basedir), file=out) if not os.path.isdir(basedir): print("%s does not look like a directory at all" % quoted_basedir, file=err) return 1 nodetype = identify_node_type(basedir) if not nodetype: print("%s is not a recognizable node directory" % quoted_basedir, file=err) return 1 # Now prepare to turn into a twistd process. This os.chdir is the point # of no return. os.chdir(basedir) twistd_args = [] if (nodetype in (u"client", u"introducer") and "--nodaemon" not in config.twistd_args and "--syslog" not in config.twistd_args and "--logfile" not in config.twistd_args): fileutil.make_dirs(os.path.join(basedir, u"logs")) twistd_args.extend(["--logfile", os.path.join("logs", "twistd.log")]) twistd_args.extend(config.twistd_args) twistd_args.append( "DaemonizeTahoeNode") # point at our DaemonizeTahoeNodePlugin twistd_config = MyTwistdConfig() try: twistd_config.parseOptions(twistd_args) except usage.error as ue: # these arguments were unsuitable for 'twistd' print(config, file=err) print("tahoe %s: usage error from twistd: %s\n" % (config.subcommand_name, ue), file=err) return 1 twistd_config.loadedPlugins = { "DaemonizeTahoeNode": DaemonizeTahoeNodePlugin(nodetype, basedir) } # handle invalid PID file (twistd might not start otherwise) pidfile = get_pidfile(basedir) if get_pid_from_pidfile(pidfile) == -1: print("found invalid PID file in %s - deleting it" % basedir, file=err) os.remove(pidfile) # On Unix-like platforms: # Unless --nodaemon was provided, the twistd.runApp() below spawns off a # child process, and the parent calls os._exit(0), so there's no way for # us to get control afterwards, even with 'except SystemExit'. If # application setup fails (e.g. ImportError), runApp() will raise an # exception. # # So if we wanted to do anything with the running child, we'd have two # options: # # * fork first, and have our child wait for the runApp() child to get # running. (note: just fork(). This is easier than fork+exec, since we # don't have to get PATH and PYTHONPATH set up, since we're not # starting a *different* process, just cloning a new instance of the # current process) # * or have the user run a separate command some time after this one # exits. # # For Tahoe, we don't need to do anything with the child, so we can just # let it exit. # # On Windows: # twistd does not fork; it just runs in the current process whether or not # --nodaemon is specified. (As on Unix, --nodaemon does have the side effect # of causing us to log to stdout/stderr.) if "--nodaemon" in twistd_args or sys.platform == "win32": verb = "running" else: verb = "starting" print("%s node in %s" % (verb, quoted_basedir), file=out) twistd.runApp(twistd_config) # we should only reach here if --nodaemon or equivalent was used return 0
rtmpMode = "server" rtmpHost = "0.0.0.0" rtmpPort = 1935 httpHost = "0.0.0.0" httpPort = 8080 def run(): tdcmds = ["-no", "-y", __file__] tdoptions = td.ServerOptions() try: tdoptions.parseOptions(tdcmds) except usage.UsageError, errortext: print '%s' % (errortext) sys.exit(1) # run app with twistd td.runApp(tdoptions) if __name__ == '__main__': run() else: # Normal twistd startup. application = service.Application("Tape") # Create a MultiService, and hook up a the RTMP # and HTTP TCPServers as children. flashServices = service.MultiService() # HTTP/AMF server. remotingServer = RemotingServer(httpHost, httpPort) remotingServer.setServiceParent(flashServices)
# this spawns off a child process, and the parent calls os._exit(0), so # there's no way for us to get control afterwards, even with 'except # SystemExit'. So if we want to do anything with the running child, we # have two options: # * fork first, and have our child wait for the runApp() child to get # running. (note: just fork(). This is easier than fork+exec, since we # don't have to get PATH and PYTHONPATH set up, since we're not # starting a *different* process, just cloning a new instance of the # current process) # * or have the user run a separate command some time after this one # exits. print >>out, "starting node in %s" % basedir child_pid = os.fork() if child_pid == 0: # this is the child: turn int othe new app twistd.runApp(twistd_config) os._exit(1) # should never be called # CONTROL NEVER REACHES HERE # this is the parent print >>out, "child launched .. waiting for startup" from . import webwait baseurl = webwait.wait(basedir, err) print >>out, "child ready at %s" % baseurl def stop(so, out, err): basedir = so["basedir"] dbfile = os.path.join(basedir, "toolbed.db") if not (os.path.isdir(basedir) and os.path.exists(dbfile)): print >>err, "'%s' doesn't look like a toolbed basedir, quitting" % basedir return 1 print >>out, "STOPPING", basedir