def connect(self): """ don't use at INIT because it hangs all application""" logger = logging.getLogger(__name__) logger.error( 'Connecting SFTP %s:%s (%s, %s)' % (settings.SFTP_DATASTORE_HOSTNAME, settings.SFTP_DATASTORE_PORT, settings.SFTP_DATASTORE_USER, settings.SFTP_DATASTORE_PASSWORD)) # TODO: Remove con = sftp.Connection(host=settings.SFTP_DATASTORE_HOSTNAME, port=settings.SFTP_DATASTORE_PORT, username=settings.SFTP_DATASTORE_USER, password=settings.SFTP_DATASTORE_PASSWORD, log=True) self.connection = con # self.ssh_client.connect(settings.SFTP_DATASTORE_HOSTNAME, port=settings.SFTP_DATASTORE_PORT, username=settings.SFTP_DATASTORE_USER, password=settings.SFTP_DATASTORE_PASSWORD) self.sftp = self.ssh_client.open_sftp() # list all buckets (folders) try: self.buckets = self.sftp.listdir(path=self.base_folder) logger.error('Buckets: %s' % str(self.buckets)) except Exception, e: logger.error('Error Connecting SFTP %s' % str(e)) self.sftp.close()
import sftp c = sftp.Connection( host='10.8.15.82', port=22022, username='******', password='******', ) print('c.listdir()=', c.listdir())
def main(args): global errors, servername, username, targetdir if "REPREPRO_OUT_DIR" in os.environ or "REPREPRO_LOG_DIR" in os.environ: # assume being called by reprepro if one of those variable # is set, so they all should be set: options.outdir = os.environ["REPREPRO_OUT_DIR"] options.logdir = os.environ["REPREPRO_LOG_DIR"] options.confdir = os.environ["REPREPRO_CONFIG_DIR"] else: parseoptions(args) assert (options.outdir and (options.ignorepending or options.logdir) and options.confdir) conffilename = options.confdir + "/outsftphook.conf" if os.path.exists(conffilename): conffile = open(conffilename, "r") for line in conffile: line = line.strip().split(None, 1) if len(line) == 0 or line[0].startswith("#"): continue if line[0] == "servername": servername = line[1] elif line[0] == "username": username = line[1] elif line[0] == "targetdir": targetdir = line[1] elif line[0] == "debug": if options.debugsftp is None: try: options.debugsftp = int(line[1]) except Exception: raise CriticalError( ("Cannot parse %s: " + "unparseable number %s") % (repr(conffilename), repr(line[1]))) elif line[0] == "verbose": if line[1].lower() in {'yes', 'on', '1', 'true'}: if options.verbose is None: options.verbose = True elif line[1].lower() in {'no', 'off', '0', 'false'}: if options.verbose is None: options.verbose = False else: raise CriticalError( ("Cannot parse %s: " + "unparseable truth value %s") % (repr(conffilename), repr(line[1]))) elif line[0] == "autoretry": if line[1].lower() in {'yes', 'on', '1', 'true'}: if options.autoretry is None: options.autoretry = True elif line[1].lower() in {'no', 'off', '0', 'false'}: if options.autoretry is None: options.autoretry = False else: raise CriticalError( ("Cannot parse %s: " + "unparseable truth value %s") % (repr(conffilename), repr(line[1]))) else: raise CriticalError("Cannot parse %s: unknown option %s" % (repr(conffilename), repr(line[0]))) conffile.close() if options.debugsftp is None: options.debugsftp = 0 if targetdir and not targetdir.endswith("/"): targetdir = targetdir + "/" if not servername: raise CriticalError("No servername configured!") if not username: raise CriticalError("No username configured!") if len(args) <= 0: if not options.pending: raise CriticalError("No .outlog files given at command line!") else: if options.pending: raise CriticalError( "--pending might not be combined with arguments!") if options.ignorepending: pendinglogs = set() else: pendinglogs = set(name for name in os.listdir(options.logdir) if name.endswith(".outlog")) maxbasename = None for f in args: if len(f) < 8 or f[-7:] != ".outlog": raise CriticalError( "command line argument '%s' does not look like a .outlog file!" % f) bn = os.path.basename(f) pendinglogs.discard(bn) if maxbasename: if maxbasename < bn: maxbasename = bn elif not options.forceorder: raise CriticalError( "The arguments are not in order (%s <= %s). Applying in this order might not be safe. (use --force-order to proceed in this order anyway)" % (bn, maxbasename)) else: maxbasename = bn if options.pending: pendinglogs = sorted(pendinglogs) else: pendinglogs = sorted(filter(lambda bn: bn < maxbasename, pendinglogs)) if pendinglogs and not options.autoretry: raise CriticalError( "Unprocessed earlier outlogs found: %s\nYou need to process them first (or use --autoretry or autoretry true in outsftphook.conf to automatically process them)" % repr(pendinglogs)) if pendinglogs and len(args) > 1: raise CriticalError( "autoretry does not work with multiple log files given (yet).") args = list(map(lambda bn: options.logdir + "/" + bn, pendinglogs)) + args outlogfiles = [] for f in args: donefile = f[:-7] + ".done" if options.verbose: print("Parsing '%s'" % f) try: outlogfiles.append(LogFile(f, donefile)) except ParseError as e: raise CriticalError("Error parsing %s: %s" % (f, str(e))) todo = {} distdirs = {} workpending = False for o in reversed(outlogfiles): workpending |= o.queue(todo, distdirs) if not workpending: if options.verbose: print("Nothing to do") raise SystemExit(0) s = sftp.Connection(servername=servername, username=username, debug=options.debugsftp) doround(s, Round.POOLFILES, todo) if errors: raise SystemExit(1) for d in distdirs.values(): for t in d.finalizeifready(): s.start(t) doround(s, Round.DISTFILES, todo) if errors: raise SystemExit(1) doround(s, Round.DELETES, todo) if errors: raise SystemExit(1)