tahoe-lafs/src/allmydata/scripts/runner.py

308 lines
10 KiB
Python
Raw Normal View History

from __future__ import print_function
import os, sys
from six.moves import StringIO
import six
from twisted.python import usage
from twisted.internet import defer, task, threads
from allmydata.scripts.common import get_default_nodedir
from allmydata.scripts import debug, create_node, cli, \
admin, tahoe_run, tahoe_invite
from allmydata.util.encodingutil import quote_output, quote_local_unicode_path, get_io_encoding
from allmydata.util.eliotutil import (
opt_eliot_destination,
opt_help_eliot_destinations,
2019-03-04 14:44:00 +00:00
eliot_logging_service,
)
from .. import (
__full_version__,
)
_default_nodedir = get_default_nodedir()
NODEDIR_HELP = ("Specify which Tahoe node directory should be used. The "
"directory should either contain a full Tahoe node, or a "
"file named node.url that points to some other Tahoe node. "
"It should also contain a file named '"
+ os.path.join('private', 'aliases') +
"' which contains the mapping from alias name to root "
"dirnode URI.")
if _default_nodedir:
NODEDIR_HELP += " [default for most commands: " + quote_local_unicode_path(_default_nodedir) + "]"
# XXX all this 'dispatch' stuff needs to be unified + fixed up
_control_node_dispatch = {
"run": tahoe_run.run,
}
process_control_commands = [
["run", None, tahoe_run.RunOptions, "run a node without daemonizing"],
]
class Options(usage.Options):
# unit tests can override these to point at StringIO instances
stdin = sys.stdin
stdout = sys.stdout
stderr = sys.stderr
subCommands = ( create_node.subCommands
+ admin.subCommands
+ process_control_commands
+ debug.subCommands
+ cli.subCommands
+ tahoe_invite.subCommands
)
optFlags = [
["quiet", "q", "Operate silently."],
["version", "V", "Display version numbers."],
["version-and-path", None, "Display version numbers and paths to their locations."],
]
optParameters = [
["node-directory", "d", None, NODEDIR_HELP],
["wormhole-server", None, u"ws://wormhole.tahoe-lafs.org:4000/v1", "The magic wormhole server to use.", six.text_type],
["wormhole-invite-appid", None, u"tahoe-lafs.org/invite", "The appid to use on the wormhole server.", six.text_type],
]
def opt_version(self):
print(__full_version__, file=self.stdout)
self.no_command_needed = True
opt_version_and_path = opt_version
opt_eliot_destination = opt_eliot_destination
opt_help_eliot_destinations = opt_help_eliot_destinations
def __str__(self):
return ("\nUsage: tahoe [global-options] <command> [command-options]\n"
+ self.getUsage())
synopsis = "\nUsage: tahoe [global-options]" # used only for subcommands
def getUsage(self, **kwargs):
t = usage.Options.getUsage(self, **kwargs)
t = t.replace("Options:", "\nGlobal options:", 1)
return t + "\nPlease run 'tahoe <command> --help' for more details on each command.\n"
def postOptions(self):
if not hasattr(self, 'subOptions'):
if not hasattr(self, 'no_command_needed'):
raise usage.UsageError("must specify a command")
sys.exit(0)
create_dispatch = {}
for module in (create_node,):
create_dispatch.update(module.dispatch)
def parse_options(argv, config=None):
if not config:
config = Options()
config.parseOptions(argv) # may raise usage.error
return config
def parse_or_exit_with_explanation_with_config(config, argv, stdout, stderr):
2020-12-17 14:16:05 +00:00
"""
Parse Tahoe-LAFS CLI arguments and return a configuration object if they
are valid.
If they are invalid, write an explanation to ``stdout`` and exit.
:param twisted.python.usage.Options config: An instance of the
argument-parsing class to use.
:param [str] argv: The argument list to parse, including the name of the
program being run as ``argv[0]``.
:param stdout: The file-like object to use as stdout.
:param stderr: The file-like object to use as stderr.
:raise SystemExit: If there is an argument-parsing problem.
:return: ``config``, after using it to parse the argument list.
"""
try:
parse_options(argv[1:], config=config)
except usage.error as e:
c = config
while hasattr(c, 'subOptions'):
c = c.subOptions
print(str(c), file=stdout)
try:
msg = e.args[0].decode(get_io_encoding())
except Exception:
msg = repr(e)
print("%s: %s\n" % (argv[0], quote_output(msg, quotemarks=False)), file=stdout)
sys.exit(1)
return config
def dispatch(config,
stdin=sys.stdin, stdout=sys.stdout, stderr=sys.stderr):
command = config.subCommand
so = config.subOptions
if config['quiet']:
stdout = StringIO()
so.stdout = stdout
so.stderr = stderr
so.stdin = stdin
if command in create_dispatch:
f = create_dispatch[command]
elif command in _control_node_dispatch:
f = _control_node_dispatch[command]
elif command in debug.dispatch:
f = debug.dispatch[command]
elif command in admin.dispatch:
f = admin.dispatch[command]
2007-07-11 02:37:37 +00:00
elif command in cli.dispatch:
# these are blocking, and must be run in a thread
f0 = cli.dispatch[command]
f = lambda so: threads.deferToThread(f0, so)
elif command in tahoe_invite.dispatch:
f = tahoe_invite.dispatch[command]
else:
raise usage.UsageError()
d = defer.maybeDeferred(f, so)
# the calling convention for CLI dispatch functions is that they either:
# 1: succeed and return rc=0
# 2: print explanation to stderr and return rc!=0
# 3: raise an exception that should just be printed normally
# 4: return a Deferred that does 1 or 2 or 3
def _raise_sys_exit(rc):
sys.exit(rc)
d.addCallback(_raise_sys_exit)
return d
2019-03-04 14:44:00 +00:00
def _maybe_enable_eliot_logging(options, reactor):
2019-03-04 15:08:46 +00:00
if options.get("destinations"):
2019-03-04 14:44:00 +00:00
service = eliot_logging_service(reactor, options["destinations"])
# There is no Twisted "Application" around to hang this on so start
# and stop it ourselves.
service.startService()
reactor.addSystemEventTrigger("after", "shutdown", service.stopService)
# Pass on the options so we can dispatch the subcommand.
return options
def run(configFactory=Options, argv=sys.argv, stdout=sys.stdout, stderr=sys.stderr):
2020-12-17 14:22:43 +00:00
"""
Run a Tahoe-LAFS node.
:param configFactory: A zero-argument callable which creates the config
object to use to parse the argument list.
:param [str] argv: The argument list to use to configure the run.
:param stdout: The file-like object to use for stdout.
:param stderr: The file-like object to use for stderr.
:raise SystemExit: Always raised after the run is complete.
"""
# TODO(3035): Remove tox-check when error becomes a warning
if 'TOX_ENV_NAME' not in os.environ:
assert sys.version_info < (3,), u"Tahoe-LAFS does not run under Python 3. Please use Python 2.7.x."
if sys.platform == "win32":
from allmydata.windows.fixups import initialize
initialize()
2019-03-04 14:44:00 +00:00
# doesn't return: calls sys.exit(rc)
task.react(
lambda reactor: _run_with_reactor(
reactor,
configFactory(),
argv,
stdout,
stderr,
),
)
2019-03-04 14:44:00 +00:00
2019-07-23 16:39:45 +00:00
def _setup_coverage(reactor, argv):
2019-07-23 16:39:45 +00:00
"""
Arrange for coverage to be collected if the 'coverage' package is
installed
"""
# can we put this _setup_coverage call after we hit
# argument-parsing?
if '--coverage' not in argv:
2019-07-23 16:39:45 +00:00
return
argv.remove('--coverage')
2019-07-23 16:39:45 +00:00
try:
import coverage
except ImportError:
2019-08-07 20:08:23 +00:00
raise RuntimeError(
"The 'coveage' package must be installed to use --coverage"
)
2019-07-23 16:39:45 +00:00
2019-08-07 18:39:29 +00:00
# this doesn't change the shell's notion of the environment, but
# it makes the test in process_startup() succeed, which is the
# goal here.
2019-07-23 16:39:45 +00:00
os.environ["COVERAGE_PROCESS_START"] = '.coveragerc'
2019-08-07 18:39:29 +00:00
2019-07-23 16:39:45 +00:00
# maybe-start the global coverage, unless it already got started
cov = coverage.process_startup()
if cov is None:
cov = coverage.process_startup.coverage
2019-08-07 18:39:29 +00:00
def write_coverage_data():
2019-07-23 16:39:45 +00:00
"""
Make sure that coverage has stopped; internally, it depends on
ataxit handlers running which doesn't always happen (Twisted's
shutdown hook also won't run if os._exit() is called, but it
runs more-often than atexit handlers).
"""
cov.stop()
cov.save()
reactor.addSystemEventTrigger('after', 'shutdown', write_coverage_data)
def _run_with_reactor(reactor, config, argv, stdout, stderr):
2020-12-17 14:22:43 +00:00
"""
Run a Tahoe-LAFS node using the given reactor.
:param reactor: The reactor to use. This implementation largely ignores
this and lets the rest of the implementation pick its own reactor.
Oops.
:param twisted.python.usage.Options config: The config object to use to
parse the argument list.
2019-07-23 16:39:45 +00:00
2020-12-17 14:22:43 +00:00
:param argv: See ``run``.
:param stdout: See ``run``.
:param stderr: See ``run``.
:return: A ``Deferred`` that fires when the run is complete.
"""
_setup_coverage(reactor, argv)
2019-07-23 16:39:45 +00:00
d = defer.maybeDeferred(
parse_or_exit_with_explanation_with_config,
config,
argv,
stdout,
stderr,
)
2019-03-04 14:44:00 +00:00
d.addCallback(_maybe_enable_eliot_logging, reactor)
d.addCallback(dispatch, stdout=stdout, stderr=stderr)
def _show_exception(f):
# when task.react() notices a non-SystemExit exception, it does
# log.err() with the failure and then exits with rc=1. We want this
# to actually print the exception to stderr, like it would do if we
# weren't using react().
if f.check(SystemExit):
return f # dispatch function handled it
f.printTraceback(file=stderr)
sys.exit(1)
d.addErrback(_show_exception)
2019-03-04 14:44:00 +00:00
return d
if __name__ == "__main__":
run()