def __init__(self, remoteType="", **kwargs): self.cmd=False # pick requested remote connection try: self.remoteType = getattr(self.RPHType, remoteType.upper()) self.cmd = self.RPHTemplate[self.remoteType]["command"] self.copy = self.RPHTemplate[self.remoteType]["copy"] self.path = self.RPHTemplate[self.remoteType]["path"] self.argFormat = self.RPHTemplate[self.remoteType]["argFormat"] except Exception: raise ConfigError("Request to initialize RemoteProcessHandler of unknown type: %s" % remoteType) # destination should be of type: [user@]host if self.remoteType==self.RPHType.SSH or self.remoteType==self.RPHType.GSISSH: try: self.cmd = self.cmd % { "rhost" : kwargs["host"] } self.copy = self.copy % { "rhost" : kwargs["host"] } self.host = kwargs["host"] except Exception: raise ConfigError("Request to initialize RemoteProcessHandler of type %s without remote host." % self.RPHType.enumList[self.remoteType]) # add default arguments for all commands self.cmd = self.cmd % { "cmdargs" : kwargs.get("cmdargs",""), "args" : kwargs.get("args","") } self.copy = self.copy % { "cpargs" : kwargs.get("cpargs",""), "args" : kwargs.get("args","") } # test connection once proc = LoggedProcess(self.cmd % { "cmd" : "exit"}) ret, out, err = proc.getAll() if ret!=0: raise CondorProcessError('Validation of remote connection failed!', proc) vprint('Remote interface initialized:\n Cmd: %s\n Cp : %s' % (self.cmd,self.copy), level=2)
def __init__(self, remoteType="", **kwargs): self.cmd=False # pick requested remote connection try: self.remoteType = getattr(self.RPHType, remoteType.upper()) self.cmd = self.RPHTemplate[self.remoteType]["command"] self.copy = self.RPHTemplate[self.remoteType]["copy"] self.path = self.RPHTemplate[self.remoteType]["path"] self.argFormat = self.RPHTemplate[self.remoteType]["argFormat"] except Exception: raise ConfigError("Request to initialize RemoteProcessHandler of unknown type: %s" % remoteType) # destination should be of type: [user@]host if self.remoteType==self.RPHType.SSH or self.remoteType==self.RPHType.GSISSH: try: self.cmd = self.cmd % { "rhost" : kwargs["host"] } self.copy = self.copy % { "rhost" : kwargs["host"] } self.host = kwargs["host"] except Exception: raise ConfigError("Request to initialize RemoteProcessHandler of type %s without remote host." % self.RPHType.enumList[self.remoteType]) # add default arguments for all commands self.cmd = self.cmd % { "cmdargs" : kwargs.get("cmdargs",""), "args" : kwargs.get("args","") } self.copy = self.copy % { "cpargs" : kwargs.get("cpargs",""), "args" : kwargs.get("args","") } # test connection once proc = LoggedProcess(self.cmd % { "cmd" : "exit"}) ret = proc.getAll()[0] if ret != 0: raise CondorProcessError('Validation of remote connection failed!', proc) vprint('Remote interface initialized:\n Cmd: %s\n Cp : %s' % (self.cmd,self.copy), level=2)
def LoggedSocket(self, command="", args='', niceCmd=None, niceArgs=None): return LoggedProcess( self._socketWrapper.cmd, args=self._socketWrapper.args(command=command, args=args), niceCmd=self._socketWrapper.niceCmd(command=(niceCmd or command)), niceArgs=self._exeWrapper.niceArg(args=(niceArgs or args)), shell=False, )
def LoggedDelete(self, target): return LoggedProcess( self._delete.cmd, self._delete.args({"target": target}), niceCmd=self._delete.niceCmd(), niceArgs=self._delete.niceArg({"target": target}), shell=False, )
def LoggedCopyFromRemote(self, source, dest): return LoggedProcess( self.copy % { "source": self.path % { "host": self.host, "path": source }, "dest": dest })
def LoggedPut(self, source, destination): return LoggedProcess( self._copy.cmd, self._copy.args(source=source, destination=self.getGlobalAbsPath(destination)), niceCmd=self._copy.niceCmd(), niceArgs=self._copy.niceArg( source=source, destination=self.getGlobalAbsPath(destination)), shell=False, )
def _CreateSocket(self, duration = 60): args = [self.cmd, self.defaultArgs, "-o ControlMaster=yes", self.socketArgsDef, self.remoteHost, self._argFormat("sleep %d" % duration)] self.__ControlMaster = LoggedProcess(" ".join(args)) timeout = 0 while not os.path.exists(self.sshLink): time.sleep(0.5) timeout += 0.5 if timeout == 5: vprint("SSH socket still not available after 5 seconds...\n%s" % self.sshLink, level=1) vprint('Socket process: %s' % (self.__ControlMaster.cmd), level=2) if timeout == 10: return False
def LoggedCopy(self, source, dest, remoteKey="<remote>"): if source.startswith(remoteKey): source = self.path % { "host": self.host, "path": source[len(remoteKey):] } if dest.startswith(remoteKey): dest = self.path % { "host": self.host, "path": dest[len(remoteKey):] } return LoggedProcess(self.copy % { "source": "%s:%s" % (self.host, source), "dest": dest })
def LoggedCopyFromRemote(self, source, dest, **kwargs): return LoggedProcess( self.cpy, " ".join([source, dest]) )
def LoggedExecute(self, cmd, args = '', **kwargs): return LoggedProcess( cmd , args )
def LoggedExecute(self, cmd, args = '', argFormat=defaultArg): if argFormat is defaultArg: argFormat=self.argFormat return LoggedProcess( self.cmd % { "cmd" : argFormat(self, "%s %s" % ( cmd, args )) } )
def LoggedCopyFromRemote(self, source, dest, **kwargs): self._socketHandler() return LoggedProcess( " ".join([self.cpy, self.defaultArgs, self.socketArgs, self._remotePath(source), dest]) )
def LoggedExecute(self, cmd, args = '', **kwargs): self._socketHandler() return LoggedProcess( " ".join([self.cmd, self.defaultArgs, self.socketArgs, self.remoteHost, self._argFormat(cmd + " " + args)]) )
def LoggedDelete(self, target): return LoggedProcess(self._delete.cmd, self._delete.args(target=target), niceCmd=self._delete.niceCmd())
def LoggedGet(self, source, destination): return LoggedProcess(self._copy.cmd, self._copy.args(source=source, destination=destination), niceCmd=self._copy.niceCmd())
def LoggedExecute(self, command, args='', niceCmd=None, niceArgs=None): return LoggedProcess(command, args=args, niceCmd=niceCmd or command, niceArgs=niceArgs or args)