def _connect(self): logger.debug("Opening ssh connection ... ") logger.debug("Trying '%s' key ... " % self.private_key ) private_key_path = expanduser(self.private_key) if ( (not exists( private_key_path)) and ('PRIVATE KEY' not in self.private_key) ): output = "'%s'key does not exist" % private_key_path raise ComException(output) logger.debug( "Connecting to '%s' as user '%s' port '%s' ..." % ( self.frontend, self.username, self.port ) ) if ':' in self.frontend: self.frontend, self.port = self.frontend.split( ':' ) try: self._conn = fabric.Connection( host=self.frontend, user=self.username, connect_kwargs={ "key_filename": private_key_path, }, connect_timeout=300 ) except Exception as err: logger.warning( "Error connecting '%s': %s" % (self.frontend, str(err)) )
def rmDirectory(self, url): to_dir = self._set_dir(urlparse(url).path) out, err = self.execCommand("rm -rf %s" % to_dir ) if err: output = "Could not remove %s directory: %s " % ( to_dir , ' '.join( err.split( '\n' ) ) ) logger.error( output ) raise ComException( output )
def copy(self, source_url, destination_url, execution_mode): with self._lock: if 'file://' in source_url: from_dir = urlparse(source_url).path to_dir = self._set_dir(urlparse(destination_url).path) else: from_dir = self._set_dir(urlparse(source_url).path) to_dir = urlparse(destination_url).path out, err = self.execCommand("cp -r %s %s" % (from_dir,to_dir)) if err: output = "Could not copy from %s to %s : %s" % ( from_dir, to_dir , ' '.join( err.split( '\n' ) ) ) logger.error( output ) raise ComException( output ) if execution_mode == 'X': os.chmod(to_dir, execution_permissions )
def rmDirectory(self, url): to_dir = self._set_dir(urlparse(url).path) stdout, stderr = self.execCommand("rm -rf %s" % to_dir) if stderr: raise ComException("Could not remove %s directory on '%s': %s" % (to_dir, self.frontend , stderr ))
def mkDirectory(self, url): to_dir = self._set_dir(urlparse(url).path) stdout, stderr = self.execCommand("mkdir -p %s" % to_dir) if stderr: raise ComException("Could not create %s directory on '%s': %s" % (to_dir, self.frontend , stderr ))
def connect(self): try: with self._lock: if not self._trans or not self._trans.is_authenticated(): logger.debug("Opening ssh connection ... ") keys = None logger.debug("Trying ssh-agent ... ") drm4g_agent = drm4g.commands.Agent() drm4g_agent.start() drm4g_agent.update_agent_env() # paramiko agent agent = Agent() keys = agent.get_keys() if not keys: logger.debug("Error trying to connect to '%s'" % self.frontend) logger.debug( "Impossible to load '%s' key from the ssh-agent" % self.private_key) try: status_ssh_agent = agent._conn except Exception as err: logger.warning( "Probably you are using paramiko version <= 1.7.7.2 : %s " % err) status_ssh_agent = agent.conn if not status_ssh_agent: logger.warning("'ssh-agent' is not running") else: if agent.get_keys(): logger.warning( "ssh-agent is running but none of the keys have been accepted" "by remote frontend %s." % self.frontend) else: logger.debug( "'ssh-agent' is running but without any keys" ) if self.private_key: logger.debug("Trying '%s' key ... " % self.private_key) private_key_path = expanduser(self.private_key) if (not exists(private_key_path)) and ( not 'PRIVATE KEY' in self.private_key): output = "'%s'key does not exist" % private_key_path raise ComException(output) for pkey_class in (RSAKey, DSSKey): try: if 'PRIVATE KEY' in self.private_key: import StringIO key = pkey_class.from_private_key( StringIO.StringIO( self.private_key.strip("'"))) else: key = pkey_class.from_private_key_file( private_key_path) keys = keys + (key, ) except Exception: pass if not keys: output = "Impossible to load any keys" logger.error(output) raise ComException(output) for key in keys: try: sock = socket.socket() try: sock.settimeout(SSH_CONNECT_TIMEOUT) except: output = "Timeout trying to connect to '%s'" % self.frontend raise ComException(output) logger.debug( "Connecting to '%s' as user '%s' port '%s' ..." % (self.frontend, self.username, self.port)) if ':' in self.frontend: self.frontend, self.port = self.frontend.split( ':') sock.connect((self.frontend, self.port)) self._trans = Transport(sock) self._trans.connect(username=self.username, pkey=key) if self._trans.is_authenticated(): break except socket.gaierror: output = "Could not resolve hostname '%s' " % self.frontend raise ComException(output) except Exception as err: logger.warning("Error connecting '%s': %s" % (self.frontend, str(err))) if not self._trans: output = "Authentication failed for '%s'. Try to execute `ssh -vvv -p %d %s@%s` and see the response." % ( self.frontend, self.port, self.username, self.frontend) raise ComException(output) except ComException: raise except Exception as err: if "No handlers could be found for logger" in str(err): raise Exception( "The connect function is the one causing problems : %s" % str(err)) else: raise