[svn r62994] introducing internal MultiGateway class

--HG--
branch : trunk
This commit is contained in:
hpk 2009-03-17 12:53:09 +01:00
parent 7ed26c2929
commit 8a8ae5fe5d
5 changed files with 43 additions and 34 deletions

View File

@ -94,31 +94,46 @@ class MultiChannel:
for ch in self._channels:
ch.waitclose()
class MultiGateway:
def __init__(self, gateways):
self.gateways = gateways
def remote_exec(self, source):
channels = []
for gw in self.gateways:
channels.append(gw.remote_exec(source))
return MultiChannel(channels)
class GatewayManager:
def __init__(self, specs):
self.spec2gateway = {}
for spec in specs:
self.spec2gateway[GatewaySpec(spec)] = None
self.specs = [GatewaySpec(spec) for spec in specs]
self.gateways = []
def trace(self, msg):
py._com.pyplugins.notify("trace", "gatewaymanage", msg)
def makegateways(self):
for spec, value in self.spec2gateway.items():
assert value is None
assert not self.gateways
for spec in self.specs:
self.trace("makegateway %s" %(spec))
self.spec2gateway[spec] = spec.makegateway()
self.gateways.append(spec.makegateway())
def getgateways(self, remote=True, inplacelocal=True):
l = []
for gw in self.gateways:
if gw.spec.inplacelocal():
if inplacelocal:
l.append(gw)
else:
if remote:
l.append(gw)
return MultiGateway(gateways=l)
def multi_exec(self, source, inplacelocal=True):
""" remote execute code on all gateways.
@param inplacelocal=False: don't send code to inplacelocal hosts.
"""
source = py.code.Source(source)
channels = []
for spec, gw in self.spec2gateway.items():
if inplacelocal or not spec.inplacelocal():
channels.append(gw.remote_exec(source))
return MultiChannel(channels)
multigw = self.getgateways(inplacelocal=inplacelocal)
return multigw.remote_exec(source)
def multi_chdir(self, basename, inplacelocal=True):
""" perform a remote chdir to the given path, may be relative.
@ -132,7 +147,8 @@ class GatewayManager:
"""
rsync = HostRSync(source, verbose=verbose, ignores=ignores)
added = False
for spec, gateway in self.spec2gateway.items():
for gateway in self.gateways:
spec = gateway.spec
if not spec.inplacelocal():
self.trace("add_target_host %r" %(gateway,))
def finished():
@ -148,8 +164,8 @@ class GatewayManager:
self.trace("rsync: nothing to do.")
def exit(self):
while self.spec2gateway:
spec, gw = self.spec2gateway.popitem()
while self.gateways:
gw = self.gateways.pop()
self.trace("exiting gateway %s" % gw)
gw.exit()

View File

@ -106,21 +106,21 @@ class TestGatewayManagerPopen:
def test_hostmanager_popen_makegateway(self):
hm = GatewayManager(["popen"] * 2)
hm.makegateways()
assert len(hm.spec2gateway) == 2
assert len(hm.gateways) == 2
hm.exit()
assert not len(hm.spec2gateway)
assert not len(hm.gateways)
def test_hostmanager_popens_rsync(self, source):
hm = GatewayManager(["popen"] * 2)
hm.makegateways()
assert len(hm.spec2gateway) == 2
for gw in hm.spec2gateway.values():
assert len(hm.gateways) == 2
for gw in hm.gateways:
gw.remote_exec = None
l = []
hm.rsync(source, notify=lambda *args: l.append(args))
assert not l
hm.exit()
assert not len(hm.spec2gateway)
assert not len(hm.gateways)
def test_hostmanager_rsync_popen_with_path(self, source, dest):
hm = GatewayManager(["popen:%s" %dest] * 1)
@ -129,7 +129,7 @@ class TestGatewayManagerPopen:
l = []
hm.rsync(source, notify=lambda *args: l.append(args))
assert len(l) == 1
assert l[0] == ("rsyncrootready", hm.spec2gateway.keys()[0], source)
assert l[0] == ("rsyncrootready", hm.gateways[0].spec, source)
hm.exit()
dest = dest.join(source.basename)
assert dest.join("dir1").check()

View File

@ -99,7 +99,8 @@ class HostManager(object):
self.trace_hoststatus()
for host, gateway in self.gwmanager.spec2gateway.items():
for gateway in self.gwmanager.gateways:
host = gateway.spec
host.node = MasterNode(host,
gateway,
self.config,

View File

@ -25,7 +25,7 @@ class TestHostManager:
config = py.test.config._reparse(args)
assert config.topdir == source
hm = HostManager(config)
assert hm.gwmanager.spec2gateway
assert hm.gwmanager.specs
return hm
def xxtest_hostmanager_custom_hosts(self, source, dest):
@ -114,7 +114,7 @@ class TestHostManager:
config = py.test.config._reparse([source])
hm = HostManager(config, hosts=hosts)
hm.rsync_roots()
for gwspec in hm.gwmanager.spec2gateway:
for gwspec in hm.gwmanager.specs:
assert gwspec.inplacelocal()
assert not gwspec.joinpath
@ -127,20 +127,12 @@ class TestHostManager:
hm = HostManager(config, hosts=hosts)
evrec = EventRecorder(config.bus, debug=True)
hm.setup_hosts(putevent=[].append)
for host in hm.gwmanager.spec2gateway:
for host in hm.gwmanager.specs:
l = evrec.getnamed("trace")
print evrec.events
assert l
hm.teardown_hosts()
def test_hostmanage_simple_ssh_test(self, testdir):
rp = testdir.mkdir('xyz123')
rp.ensure("__init__.py")
p = testdir.makepyfile("def test_123(): import xyz123")
result = testdir.runpytest(p, '-d', "--hosts=popen", '--rsyncdirs=' + str(rp))
assert result.ret == 0
assert result.stdout.str().find("1 passed") != -1
@py.test.mark.xfail("implement double-rsync test")
def test_ssh_rsync_samehost_twice(self):
option = py.test.config.option

View File

@ -36,7 +36,7 @@ class TmpTestdir:
def __init__(self, pyfuncitem):
self.pyfuncitem = pyfuncitem
# XXX remove duplication with tmpdir plugin
basetmp = py.test.ensuretemp("testdir")
basetmp = pyfuncitem._config.ensuretemp("testdir")
name = pyfuncitem.name
for i in range(100):
try: