def main(): log.startLogging(sys.stderr) ssh_factory = ssh.create_factory( public_keys={'ssh-rsa': keys.Key.fromString(data=publicKey)}, private_keys={'ssh-rsa': keys.Key.fromString(data=privateKey)}, authnz=TestAuthnz(), git_configuration=TestGitConfiguration() ) reactor.listenTCP(5522, ssh_factory()) reactor.run()
def run_server(): import logging import logging.handlers from optparse import OptionParser parser = OptionParser() parser.add_option('-v', '--verbose', action='store_true', dest='verbose', default=False) parser.add_option('-d', '--daemonize', action='store_true', dest='daemonize', default=False) parser.add_option('-i', '--pidfile', action='store', dest='pidfile', default=None) parser.add_option('-l', '--logfile', action='store', dest='logfile', default=None) parser.add_option('-u', '--user', action='store', dest='user', default=None) parser.add_option('-g', '--group', action='store', dest='group', default=None) (options, args) = parser.parse_args() # configure logging formatter = logging.Formatter( '[%(asctime)s] %(levelname)s: <%(name)s@%(filename)s:%(lineno)d> %(message)s' ) if options.verbose: loglevel = logging.DEBUG else: loglevel = logging.ERROR if options.logfile: handler = logging.handlers.TimedRotatingFileHandler(options.logfile, when='midnight', backupCount=7, encoding='utf-8') else: handler = logging.StreamHandler() handler.setFormatter(formatter) logger = logging.getLogger() logger.addHandler(handler) logger.setLevel(loglevel) observer = log.PythonLoggingObserver() observer.start() port = 2222 if len(args) > 0: port = int(args[0]) cyd = cydra.Cydra() helper = CydraHelper(cyd) config = cyd.config.get_component_config( 'cydraplugins.twistedgit.TwistedGit', {}) keyfilename = config.get('server_key') if keyfilename is None: # try to find one anyways for location in ['cydra', '/etc/cydra']: if os.path.exists(location + '.key') and os.path.exists(location + '.pub'): keyfilename = location break if keyfilename is None: raise Exception("Failed to find SSH keypair") ssh_factory = ssh.create_factory( public_keys={'ssh-rsa': keys.Key.fromFile(keyfilename + '.pub')}, private_keys={'ssh-rsa': keys.Key.fromFile(keyfilename + '.key')}, authnz=helper, git_configuration=helper) # daemonize if requested if options.daemonize: if not options.logfile: print "Error: Cannot log to stderr, please specify a log file" return import daemon, grp, pwd files = [handler.stream] if options.pidfile: pidf = open(options.pidfile, 'w') files.append(pidf) args = dict(files_preserve=files) if options.group: if options.group.isdigit(): args['gid'] = int(options.group) else: args['gid'] = grp.getgrnam(options.group).gr_gid elif os.getgid() == 0: args['gid'] = grp.getgrnam('www-data').gr_gid if options.user: if options.user.isdigit(): args['uid'] = int(options.user) else: args['uid'] = pwd.getpwnam(options.user).pw_uid elif os.getuid() == 0: args['uid'] = pwd.getpwnam('www-data').pw_uid context = daemon.DaemonContext(**args) context.open() # save pid to file if options.pidfile: pidf.write(str(os.getpid())) pidf.close() import traceback, signal def dump_stack(sig, frame): logger.debug("Dumping Stack: \n" + ''.join(traceback.format_stack(frame))) signal.signal(signal.SIGUSR1, dump_stack) reactor.listenTCP(port, ssh_factory()) reactor.run()
def run_server(): import logging import logging.handlers from optparse import OptionParser parser = OptionParser() parser.add_option('-v', '--verbose', action='store_true', dest='verbose', default=False) parser.add_option('-d', '--daemonize', action='store_true', dest='daemonize', default=False) parser.add_option('-i', '--pidfile', action='store', dest='pidfile', default=None) parser.add_option('-l', '--logfile', action='store', dest='logfile', default=None) parser.add_option('-u', '--user', action='store', dest='user', default=None) parser.add_option('-g', '--group', action='store', dest='group', default=None) (options, args) = parser.parse_args() # configure logging formatter = logging.Formatter('[%(asctime)s] %(levelname)s: <%(name)s@%(filename)s:%(lineno)d> %(message)s') if options.verbose: loglevel = logging.DEBUG else: loglevel = logging.ERROR if options.logfile: handler = logging.handlers.TimedRotatingFileHandler(options.logfile, when='midnight', backupCount=7, encoding='utf-8') else: handler = logging.StreamHandler() handler.setFormatter(formatter) logger = logging.getLogger() logger.addHandler(handler) logger.setLevel(loglevel) observer = log.PythonLoggingObserver() observer.start() port = 2222 if len(args) > 0: port = int(args[0]) cyd = cydra.Cydra() helper = CydraHelper(cyd) config = cyd.config.get_component_config('cydraplugins.twistedgit.TwistedGit', {}) keyfilename = config.get('server_key') if keyfilename is None: # try to find one anyways for location in ['cydra', '/etc/cydra']: if os.path.exists(location + '.key') and os.path.exists(location + '.pub'): keyfilename = location break if keyfilename is None: raise Exception("Failed to find SSH keypair") ssh_factory = ssh.create_factory( public_keys={'ssh-rsa': keys.Key.fromFile(keyfilename + '.pub')}, private_keys={'ssh-rsa': keys.Key.fromFile(keyfilename + '.key')}, authnz=helper, git_configuration=helper ) # daemonize if requested if options.daemonize: if not options.logfile: print "Error: Cannot log to stderr, please specify a log file" return import daemon, grp, pwd files = [handler.stream] if options.pidfile: pidf = open(options.pidfile, 'w') files.append(pidf) args = dict(files_preserve=files) if options.group: if options.group.isdigit(): args['gid'] = int(options.group) else: args['gid'] = grp.getgrnam(options.group).gr_gid elif os.getgid() == 0: args['gid'] = grp.getgrnam('www-data').gr_gid if options.user: if options.user.isdigit(): args['uid'] = int(options.user) else: args['uid'] = pwd.getpwnam(options.user).pw_uid elif os.getuid() == 0: args['uid'] = pwd.getpwnam('www-data').pw_uid context = daemon.DaemonContext(**args) context.open() # save pid to file if options.pidfile: pidf.write(str(os.getpid())) pidf.close() import traceback, signal def dump_stack(sig, frame): logger.debug("Dumping Stack: \n" + ''.join(traceback.format_stack(frame))) signal.signal(signal.SIGUSR1, dump_stack) reactor.listenTCP(port, ssh_factory()) reactor.run()