2019-05-02 18:21:35 +00:00
|
|
|
|
|
|
|
__all__ = [
|
|
|
|
"CLINodeAPI",
|
|
|
|
"Expect",
|
|
|
|
"on_stdout",
|
2019-05-03 12:55:35 +00:00
|
|
|
"on_stdout_and_stderr",
|
2019-05-03 15:36:11 +00:00
|
|
|
"on_different",
|
2019-05-02 18:21:35 +00:00
|
|
|
"wait_for_exit",
|
|
|
|
]
|
|
|
|
|
|
|
|
import os
|
|
|
|
import sys
|
2019-05-03 12:55:35 +00:00
|
|
|
from errno import ENOENT
|
2019-05-02 18:21:35 +00:00
|
|
|
|
|
|
|
import attr
|
|
|
|
|
|
|
|
from twisted.internet.error import (
|
|
|
|
ProcessDone,
|
2019-05-03 12:55:35 +00:00
|
|
|
ProcessTerminated,
|
2019-05-13 13:31:46 +00:00
|
|
|
ProcessExitedAlready,
|
2019-05-02 18:21:35 +00:00
|
|
|
)
|
2019-05-03 15:36:11 +00:00
|
|
|
from twisted.internet.interfaces import (
|
|
|
|
IProcessProtocol,
|
|
|
|
)
|
2019-05-02 18:21:35 +00:00
|
|
|
from twisted.python.filepath import (
|
|
|
|
FilePath,
|
|
|
|
)
|
2019-05-13 13:35:31 +00:00
|
|
|
from twisted.python.runtime import (
|
|
|
|
platform,
|
|
|
|
)
|
2019-05-02 18:21:35 +00:00
|
|
|
from twisted.internet.protocol import (
|
|
|
|
Protocol,
|
|
|
|
ProcessProtocol,
|
|
|
|
)
|
|
|
|
from twisted.internet.defer import (
|
|
|
|
Deferred,
|
|
|
|
succeed,
|
|
|
|
)
|
2019-05-13 13:31:46 +00:00
|
|
|
from twisted.internet.task import (
|
|
|
|
deferLater,
|
|
|
|
)
|
2019-05-03 11:27:58 +00:00
|
|
|
from ..client import (
|
|
|
|
_Client,
|
|
|
|
)
|
|
|
|
from ..scripts.tahoe_stop import (
|
|
|
|
COULD_NOT_STOP,
|
|
|
|
)
|
2019-05-13 13:31:46 +00:00
|
|
|
from ..util.eliotutil import (
|
|
|
|
inline_callbacks,
|
|
|
|
)
|
2019-05-02 18:21:35 +00:00
|
|
|
|
2019-08-13 20:55:40 +00:00
|
|
|
class Expect(Protocol, object):
|
2019-05-02 18:21:35 +00:00
|
|
|
def __init__(self):
|
|
|
|
self._expectations = []
|
|
|
|
|
2019-05-03 12:55:35 +00:00
|
|
|
def get_buffered_output(self):
|
|
|
|
return self._buffer
|
|
|
|
|
2019-05-02 18:21:35 +00:00
|
|
|
def expect(self, expectation):
|
|
|
|
if expectation in self._buffer:
|
|
|
|
return succeed(None)
|
|
|
|
d = Deferred()
|
|
|
|
self._expectations.append((expectation, d))
|
|
|
|
return d
|
|
|
|
|
|
|
|
def connectionMade(self):
|
|
|
|
self._buffer = b""
|
|
|
|
|
|
|
|
def dataReceived(self, data):
|
|
|
|
self._buffer += data
|
|
|
|
for i in range(len(self._expectations) - 1, -1, -1):
|
|
|
|
expectation, d = self._expectations[i]
|
|
|
|
if expectation in self._buffer:
|
|
|
|
del self._expectations[i]
|
|
|
|
d.callback(None)
|
|
|
|
|
2019-05-03 12:55:35 +00:00
|
|
|
def connectionLost(self, reason):
|
|
|
|
for ignored, d in self._expectations:
|
|
|
|
d.errback(reason)
|
|
|
|
|
2019-05-02 18:21:35 +00:00
|
|
|
|
2019-08-13 20:55:40 +00:00
|
|
|
class _ProcessProtocolAdapter(ProcessProtocol, object):
|
2019-05-03 15:36:11 +00:00
|
|
|
def __init__(self, fds):
|
2019-05-03 12:55:35 +00:00
|
|
|
self._fds = fds
|
2019-05-02 18:21:35 +00:00
|
|
|
|
|
|
|
def connectionMade(self):
|
2019-05-03 15:36:11 +00:00
|
|
|
for proto in self._fds.values():
|
|
|
|
proto.makeConnection(self.transport)
|
2019-05-02 18:21:35 +00:00
|
|
|
|
2019-05-03 12:55:35 +00:00
|
|
|
def childDataReceived(self, childFD, data):
|
2019-05-03 15:36:11 +00:00
|
|
|
try:
|
|
|
|
proto = self._fds[childFD]
|
|
|
|
except KeyError:
|
|
|
|
pass
|
|
|
|
else:
|
|
|
|
proto.dataReceived(data)
|
2019-05-02 18:21:35 +00:00
|
|
|
|
|
|
|
def processEnded(self, reason):
|
2019-05-03 15:36:11 +00:00
|
|
|
notified = set()
|
|
|
|
for proto in self._fds.values():
|
|
|
|
if proto not in notified:
|
|
|
|
proto.connectionLost(reason)
|
|
|
|
notified.add(proto)
|
2019-05-02 18:21:35 +00:00
|
|
|
|
|
|
|
|
|
|
|
def on_stdout(protocol):
|
2019-05-03 15:36:11 +00:00
|
|
|
return _ProcessProtocolAdapter({1: protocol})
|
2019-05-03 12:55:35 +00:00
|
|
|
|
|
|
|
def on_stdout_and_stderr(protocol):
|
2019-05-03 15:36:11 +00:00
|
|
|
return _ProcessProtocolAdapter({1: protocol, 2: protocol})
|
2019-05-02 18:21:35 +00:00
|
|
|
|
2019-05-03 15:36:11 +00:00
|
|
|
def on_different(fd_mapping):
|
|
|
|
return _ProcessProtocolAdapter(fd_mapping)
|
2019-05-02 18:21:35 +00:00
|
|
|
|
|
|
|
@attr.s
|
|
|
|
class CLINodeAPI(object):
|
|
|
|
reactor = attr.ib()
|
|
|
|
basedir = attr.ib(type=FilePath)
|
2019-05-13 13:31:46 +00:00
|
|
|
process = attr.ib(default=None)
|
2019-05-02 18:21:35 +00:00
|
|
|
|
|
|
|
@property
|
|
|
|
def twistd_pid_file(self):
|
|
|
|
return self.basedir.child(u"twistd.pid")
|
|
|
|
|
|
|
|
@property
|
|
|
|
def node_url_file(self):
|
|
|
|
return self.basedir.child(u"node.url")
|
|
|
|
|
|
|
|
@property
|
|
|
|
def storage_furl_file(self):
|
|
|
|
return self.basedir.child(u"private").child(u"storage.furl")
|
|
|
|
|
2019-05-03 15:36:11 +00:00
|
|
|
@property
|
|
|
|
def introducer_furl_file(self):
|
|
|
|
return self.basedir.child(u"private").child(u"introducer.furl")
|
|
|
|
|
2019-05-02 18:21:35 +00:00
|
|
|
@property
|
|
|
|
def config_file(self):
|
|
|
|
return self.basedir.child(u"tahoe.cfg")
|
|
|
|
|
|
|
|
@property
|
|
|
|
def exit_trigger_file(self):
|
|
|
|
return self.basedir.child(_Client.EXIT_TRIGGER_FILE)
|
|
|
|
|
|
|
|
def _execute(self, process_protocol, argv):
|
|
|
|
exe = sys.executable
|
|
|
|
argv = [
|
|
|
|
exe,
|
|
|
|
u"-m",
|
|
|
|
u"allmydata.scripts.runner",
|
|
|
|
] + argv
|
|
|
|
return self.reactor.spawnProcess(
|
|
|
|
processProtocol=process_protocol,
|
|
|
|
executable=exe,
|
|
|
|
args=argv,
|
|
|
|
env=os.environ,
|
|
|
|
)
|
|
|
|
|
2019-05-03 15:36:11 +00:00
|
|
|
def run(self, protocol, extra_tahoe_args=()):
|
2019-05-02 18:21:35 +00:00
|
|
|
"""
|
|
|
|
Start the node running.
|
|
|
|
|
2019-05-03 15:36:11 +00:00
|
|
|
:param IProcessProtocol protocol: This protocol will be hooked up to
|
2019-05-02 18:21:35 +00:00
|
|
|
the node process and can handle output or generate input.
|
|
|
|
"""
|
2019-05-03 15:36:11 +00:00
|
|
|
if not IProcessProtocol.providedBy(protocol):
|
|
|
|
raise TypeError("run requires process protocol, got {}".format(protocol))
|
2019-05-02 18:21:35 +00:00
|
|
|
self.process = self._execute(
|
|
|
|
protocol,
|
2019-05-03 15:36:11 +00:00
|
|
|
list(extra_tahoe_args) + [u"run", self.basedir.asTextMode().path],
|
2019-05-02 18:21:35 +00:00
|
|
|
)
|
|
|
|
# Don't let the process run away forever.
|
2019-05-03 12:55:35 +00:00
|
|
|
try:
|
|
|
|
self.active()
|
|
|
|
except OSError as e:
|
|
|
|
if ENOENT != e.errno:
|
|
|
|
raise
|
2019-05-02 18:21:35 +00:00
|
|
|
|
|
|
|
def stop(self, protocol):
|
|
|
|
self._execute(
|
|
|
|
protocol,
|
|
|
|
[u"stop", self.basedir.asTextMode().path],
|
|
|
|
)
|
|
|
|
|
2019-05-13 13:31:46 +00:00
|
|
|
@inline_callbacks
|
2019-05-03 12:55:35 +00:00
|
|
|
def stop_and_wait(self):
|
2019-05-13 13:31:46 +00:00
|
|
|
if platform.isWindows():
|
|
|
|
# On Windows there is no PID file and no "tahoe stop".
|
|
|
|
if self.process is not None:
|
|
|
|
while True:
|
|
|
|
try:
|
|
|
|
self.process.signalProcess("TERM")
|
|
|
|
except ProcessExitedAlready:
|
|
|
|
break
|
|
|
|
else:
|
|
|
|
yield deferLater(self.reactor, 0.1, lambda: None)
|
|
|
|
else:
|
|
|
|
protocol, ended = wait_for_exit()
|
|
|
|
self.stop(protocol)
|
|
|
|
yield ended
|
2019-05-03 12:55:35 +00:00
|
|
|
|
2019-05-02 18:21:35 +00:00
|
|
|
def active(self):
|
|
|
|
# By writing this file, we get two minutes before the client will
|
|
|
|
# exit. This ensures that even if the 'stop' command doesn't work (and
|
|
|
|
# the test fails), the client should still terminate.
|
|
|
|
self.exit_trigger_file.touch()
|
|
|
|
|
2019-05-03 12:55:35 +00:00
|
|
|
def _check_cleanup_reason(self, reason):
|
|
|
|
# Let it fail because the process has already exited.
|
|
|
|
reason.trap(ProcessTerminated)
|
|
|
|
if reason.value.exitCode != COULD_NOT_STOP:
|
|
|
|
return reason
|
|
|
|
return None
|
|
|
|
|
2019-05-03 11:27:58 +00:00
|
|
|
def cleanup(self):
|
2019-05-03 12:55:35 +00:00
|
|
|
stopping = self.stop_and_wait()
|
|
|
|
stopping.addErrback(self._check_cleanup_reason)
|
2019-05-03 11:27:58 +00:00
|
|
|
return stopping
|
|
|
|
|
2019-05-02 18:21:35 +00:00
|
|
|
|
2019-08-13 20:55:40 +00:00
|
|
|
class _WaitForEnd(ProcessProtocol, object):
|
2019-05-02 18:21:35 +00:00
|
|
|
def __init__(self, ended):
|
|
|
|
self._ended = ended
|
|
|
|
|
|
|
|
def processEnded(self, reason):
|
|
|
|
if reason.check(ProcessDone):
|
|
|
|
self._ended.callback(None)
|
|
|
|
else:
|
|
|
|
self._ended.errback(reason)
|
|
|
|
|
|
|
|
|
|
|
|
def wait_for_exit():
|
|
|
|
ended = Deferred()
|
|
|
|
protocol = _WaitForEnd(ended)
|
|
|
|
return protocol, ended
|