2009-03-04 01:42:32 +08:00
|
|
|
"""
|
|
|
|
instantiating, managing and rsyncing to hosts
|
|
|
|
|
|
|
|
"""
|
2009-03-21 00:28:14 +08:00
|
|
|
|
2009-03-04 01:42:32 +08:00
|
|
|
import py
|
|
|
|
import sys, os
|
2009-03-18 06:41:56 +08:00
|
|
|
from py.__.execnet.channel import RemoteError
|
2009-03-04 01:42:32 +08:00
|
|
|
|
2009-03-18 06:41:56 +08:00
|
|
|
NO_ENDMARKER_WANTED = object()
|
2009-03-04 01:42:32 +08:00
|
|
|
|
|
|
|
class GatewayManager:
|
2009-03-18 06:41:56 +08:00
|
|
|
RemoteError = RemoteError
|
|
|
|
|
2009-03-21 01:14:45 +08:00
|
|
|
def __init__(self, specs, defaultchdir="pyexecnetcache"):
|
2009-03-17 19:53:09 +08:00
|
|
|
self.gateways = []
|
2009-03-21 01:14:45 +08:00
|
|
|
self.specs = []
|
|
|
|
for spec in specs:
|
|
|
|
if not isinstance(spec, py.execnet.XSpec):
|
|
|
|
spec = py.execnet.XSpec(spec)
|
|
|
|
if not spec.chdir and not spec.popen:
|
|
|
|
spec.chdir = defaultchdir
|
|
|
|
self.specs.append(spec)
|
2009-04-09 01:50:14 +08:00
|
|
|
self.api = py._com.PluginAPI(py.execnet._API)
|
2009-03-04 01:42:32 +08:00
|
|
|
|
|
|
|
def makegateways(self):
|
2009-03-17 19:53:09 +08:00
|
|
|
assert not self.gateways
|
|
|
|
for spec in self.specs:
|
2009-03-21 00:28:14 +08:00
|
|
|
gw = py.execnet.makegateway(spec)
|
2009-03-20 02:25:13 +08:00
|
|
|
self.gateways.append(gw)
|
|
|
|
gw.id = "[%s]" % len(self.gateways)
|
2009-04-09 01:50:14 +08:00
|
|
|
self.api.pyexecnet_gwmanage_newgateway(
|
|
|
|
gateway=gw, platinfo=gw._rinfo())
|
2009-03-17 19:53:09 +08:00
|
|
|
|
|
|
|
def getgateways(self, remote=True, inplacelocal=True):
|
2009-03-18 06:41:56 +08:00
|
|
|
if not self.gateways and self.specs:
|
|
|
|
self.makegateways()
|
2009-03-17 19:53:09 +08:00
|
|
|
l = []
|
|
|
|
for gw in self.gateways:
|
2009-03-21 00:28:14 +08:00
|
|
|
if gw.spec._samefilesystem():
|
2009-03-17 19:53:09 +08:00
|
|
|
if inplacelocal:
|
|
|
|
l.append(gw)
|
|
|
|
else:
|
|
|
|
if remote:
|
|
|
|
l.append(gw)
|
2009-03-20 01:05:41 +08:00
|
|
|
return py.execnet.MultiGateway(gateways=l)
|
2009-03-04 01:42:32 +08:00
|
|
|
|
|
|
|
def multi_exec(self, source, inplacelocal=True):
|
2009-03-17 05:17:14 +08:00
|
|
|
""" remote execute code on all gateways.
|
|
|
|
@param inplacelocal=False: don't send code to inplacelocal hosts.
|
|
|
|
"""
|
2009-03-17 19:53:09 +08:00
|
|
|
multigw = self.getgateways(inplacelocal=inplacelocal)
|
|
|
|
return multigw.remote_exec(source)
|
2009-03-04 01:42:32 +08:00
|
|
|
|
|
|
|
def multi_chdir(self, basename, inplacelocal=True):
|
2009-03-17 05:17:14 +08:00
|
|
|
""" perform a remote chdir to the given path, may be relative.
|
|
|
|
@param inplacelocal=False: don't send code to inplacelocal hosts.
|
|
|
|
"""
|
2009-03-04 01:42:32 +08:00
|
|
|
self.multi_exec("import os ; os.chdir(%r)" % basename,
|
2009-03-17 05:17:14 +08:00
|
|
|
inplacelocal=inplacelocal).waitclose()
|
2009-03-04 01:42:32 +08:00
|
|
|
|
|
|
|
def rsync(self, source, notify=None, verbose=False, ignores=None):
|
2009-03-17 05:17:14 +08:00
|
|
|
""" perform rsync to all remote hosts.
|
|
|
|
"""
|
2009-03-04 01:42:32 +08:00
|
|
|
rsync = HostRSync(source, verbose=verbose, ignores=ignores)
|
2009-03-20 08:34:59 +08:00
|
|
|
seen = {}
|
2009-03-17 19:53:09 +08:00
|
|
|
for gateway in self.gateways:
|
|
|
|
spec = gateway.spec
|
2009-03-21 00:28:14 +08:00
|
|
|
if not spec._samefilesystem():
|
|
|
|
if spec in seen:
|
2009-03-20 08:34:59 +08:00
|
|
|
continue
|
2009-03-04 01:42:32 +08:00
|
|
|
def finished():
|
|
|
|
if notify:
|
|
|
|
notify("rsyncrootready", spec, source)
|
|
|
|
rsync.add_target_host(gateway, finished=finished)
|
2009-03-21 00:28:14 +08:00
|
|
|
seen[spec] = gateway
|
2009-03-20 08:34:59 +08:00
|
|
|
if seen:
|
2009-04-09 01:50:14 +08:00
|
|
|
self.api.pyexecnet_gwmanage_rsyncstart(
|
|
|
|
source=source,
|
|
|
|
gateways=seen.values(),
|
|
|
|
)
|
2009-03-04 01:42:32 +08:00
|
|
|
rsync.send()
|
2009-04-09 01:50:14 +08:00
|
|
|
self.api.pyexecnet_gwmanage_rsyncfinish(
|
|
|
|
source=source,
|
|
|
|
gateways=seen.values()
|
|
|
|
)
|
2009-03-04 01:42:32 +08:00
|
|
|
|
|
|
|
def exit(self):
|
2009-03-17 19:53:09 +08:00
|
|
|
while self.gateways:
|
|
|
|
gw = self.gateways.pop()
|
2009-03-04 01:42:32 +08:00
|
|
|
gw.exit()
|
|
|
|
|
|
|
|
class HostRSync(py.execnet.RSync):
|
|
|
|
""" RSyncer that filters out common files
|
|
|
|
"""
|
|
|
|
def __init__(self, sourcedir, *args, **kwargs):
|
|
|
|
self._synced = {}
|
|
|
|
ignores= None
|
|
|
|
if 'ignores' in kwargs:
|
|
|
|
ignores = kwargs.pop('ignores')
|
|
|
|
self._ignores = ignores or []
|
|
|
|
super(HostRSync, self).__init__(sourcedir=sourcedir, **kwargs)
|
|
|
|
|
|
|
|
def filter(self, path):
|
|
|
|
path = py.path.local(path)
|
|
|
|
if not path.ext in ('.pyc', '.pyo'):
|
|
|
|
if not path.basename.endswith('~'):
|
|
|
|
if path.check(dotfile=0):
|
|
|
|
for x in self._ignores:
|
|
|
|
if path == x:
|
|
|
|
break
|
|
|
|
else:
|
|
|
|
return True
|
|
|
|
|
|
|
|
def add_target_host(self, gateway, finished=None):
|
|
|
|
remotepath = os.path.basename(self._sourcedir)
|
|
|
|
super(HostRSync, self).add_target(gateway, remotepath,
|
|
|
|
finishedcallback=finished,
|
|
|
|
delete=True,)
|
|
|
|
|
|
|
|
def _report_send_file(self, gateway, modified_rel_path):
|
|
|
|
if self._verbose:
|
|
|
|
path = os.path.basename(self._sourcedir) + "/" + modified_rel_path
|
2009-03-21 00:28:14 +08:00
|
|
|
remotepath = gateway.spec.chdir
|
2009-03-04 01:42:32 +08:00
|
|
|
print '%s:%s <= %s' % (gateway.remoteaddress, remotepath, path)
|