def start(): root = static.File(os.environ['JPD_HTDOCS_PATH']) root.processors = {'.rpy': script.ResourceScript} root = rewrite.RewriterResource( root, rewrite.alias('cgi-bin/get_icon.py', 'get_icon.rpy')) site = JolicloudWSSite(root) site.addHandler('/jolicloud/', JolicloudWSHandler) # Setting up the log file path if os.environ.get('JPD_SYSTEM', '0') == '1': if os.getuid(): log.err('You must be root to run this daemon in system mode.') exit() log_path = '/var/log' else: try: import xdg.BaseDirectory log_path = xdg.BaseDirectory.save_data_path( 'Jolicloud', 'jolicloud-daemon') except ImportError: log_path = os.path.join(os.getenv('HOME'), '.local', 'share', 'Jolicloud', 'jolicloud-daemon') port = int( os.environ.get('JPD_PORT', 804 if os.environ.get('JPD_SYSTEM', None) else 8004)) # http://twistedmatrix.com/documents/9.0.0/web/howto/using-twistedweb.html#auto5 if os.environ.get('JPD_DEBUG', '0') == '1': log.startLogging(sys.stdout) log.startLogging( LogFile('jolicloud-daemon.log', log_path, maxRotatedFiles=2)) reactor.listenTCP(port, site) else: log.startLogging( LogFile('jolicloud-daemon.log', log_path, maxRotatedFiles=2)) reactor.listenTCP(port, site, interface='127.0.0.1') # TODO, use random port for session daemon # We load the plugins: if os.environ.get('JPD_SYSTEM', '0') == '1': log.msg('We load the system plugins.') plugins = getPlugins(ijolidaemon.ISystemManager, managers) else: log.msg('We load the session plugins.') plugins = getPlugins(ijolidaemon.ISessionManager, managers) for plugin in plugins: log.msg(plugin.__class__.__name__) reactor.run()
def startService(self): self.logfile = LogFile( self.worker_id, self.path, rotateLength=self.rotate, maxRotatedFiles=self.max_files) self.log_observer = JunebugLogObserver(self.logfile, self.worker_id) self.logger.addObserver(self.log_observer) return super(JunebugLoggerService, self).startService()
def get_file(): path.parent().makedirs(ignoreExistingDirectory=True) return LogFile( path.basename(), path.dirname(), rotateLength=rotate_length, maxRotatedFiles=max_rotated_files, )
def get_json_file_observer(name=DEFAULT_JSON_LOG_FILENAME, path=USER_LOG_DIR): _ensure_dir_exists(path) logfile = LogFile(name=name, directory=path, rotateLength=MAXIMUM_LOG_SIZE, maxRotatedFiles=MAX_LOG_FILES) observer = jsonFileLogObserver(outFile=logfile) return observer
def get_text_file_observer(name=DEFAULT_LOG_FILENAME, path=USER_LOG_DIR): _ensure_dir_exists(path) logfile = LogFile(name=name, directory=path, rotateLength=MAXIMUM_LOG_SIZE, maxRotatedFiles=MAX_LOG_FILES) observer = FileLogObserver(formatEvent=formatEventAsClassicLogText, outFile=logfile) return observer
def startService(self): if not os.path.exists(self.path): os.makedirs(self.path, 0755) self.logfile = LogFile( self.worker_id, self.path, rotateLength=self.rotate, maxRotatedFiles=self.max_files) self.log_observer = JunebugLogObserver(self.logfile, self.worker_id) self.logger.addObserver(self.log_observer) return super(JunebugLoggerService, self).startService()
def __init__(self, worker_id, directory, rotateLength, maxRotatedFiles): self.worker_id = worker_id self.directory = directory self.rotateLength = rotateLength self.maxRotatedFiles = maxRotatedFiles self.closed_count = 0 self.logfile = LogFile(worker_id, directory, rotateLength=rotateLength, maxRotatedFiles=maxRotatedFiles) self.path = self.logfile.path
def _parse_file(self, kind, args): if args == "-": get_file = lambda: stdout else: path = FilePath(args) get_file = lambda: LogFile( path.basename(), path.dirname(), rotateLength=1024 * 1024 * 1024, maxRotatedFiles=10, ) return lambda reactor: FileDestination(get_file())
def create_log_file(filename, directory, max_size, max_files, twistd_user, log_group): """Helper function to create twisted LogFiles and set file permissions Change the log file permissions to match our service user if one is defined. This is needed so that the service can rotate the log files. """ log_file = LogFile(filename, directory, rotateLength=max_size, maxRotatedFiles=max_files, defaultMode=0o640, data_type_text=True) if twistd_user is not None: uid, gid = _parse_user(twistd_user) if log_group: gid = _parse_group(log_group) os.chown(os.path.join(directory, filename), uid, gid) return log_file
def start(self, logfile=None, application_name="ooniprobe"): from ooni.settings import config if not logfile: logfile = os.path.expanduser(config.basic.logfile) log_folder = os.path.dirname(logfile) if (not os.access(log_folder, os.W_OK) or (os.path.exists(logfile) and not os.access(logfile, os.W_OK))): # If we don't have permissions to write to the log_folder or # logfile. log_folder = config.running_path logfile = os.path.join(log_folder, "ooniprobe.log") self.log_filepath = logfile mkdir_p(log_folder) log_filename = os.path.basename(logfile) file_log_level = levels.get(config.basic.loglevel, levels['INFO']) stdout_log_level = levels['INFO'] if config.advanced.debug: stdout_log_level = levels['DEBUG'] if config.basic.rotate == 'daily': logfile = MyDailyLogFile(log_filename, log_folder) elif config.basic.rotate == 'length': logfile = LogFile(log_filename, log_folder, rotateLength=int( human_size_to_bytes( config.basic.rotate_length)), maxRotatedFiles=config.basic.max_rotated_files) else: logfile = open(os.path.join(log_folder, log_filename), 'a') self.fileObserver = MsecLogObserver(logfile, log_level=file_log_level) self.stdoutObserver = StdoutStderrObserver(sys.stdout, log_level=stdout_log_level) tw_log.startLoggingWithObserver(self.fileObserver.emit) tw_log.addObserver(self.stdoutObserver.emit) tw_log.msg("Starting %s on %s (%s UTC)" % (application_name, otime.prettyDateNow(), otime.prettyDateNowUTC()))
def run(inst, port=None, logdir=None): """Start the ``twisted`` event loop, with an HTTP server. :param inst: a :py:class:`~smap.core.SmapInstance` which you want to server. :param int port: port to run on :rtype: none; this function does not return """ if not port: port = int(smapconf.SERVER.get('port', 8085)) if not logdir: logdir = smapconf.SERVER.get('logdir', os.getcwd()) if not os.path.exists(logdir): os.makedirs(logdir) print "Logging to", logdir print "Starting server on port", port # Allow 50 1MB files observer = log.FileLogObserver(LogFile('sMAP.log', logdir, rotateLength=1000000, maxRotatedFiles=50)) log.startLogging(observer) # Start server inst.start() reactor.listenTCP(port, getSite(inst)) reactor.run()
def _parse_file(self, kind, arg_text): # Reserve the possibility of an escape character in the future. \ is # the standard choice but it's the path separator on Windows which # pretty much ruins it in this context. Most other symbols already # have some shell-assigned meaning which makes them treacherous to use # in a CLI interface. Eliminating all such dangerous symbols leaves # approximately @. if u"@" in arg_text: raise ValueError( u"Unsupported escape character (@) in destination text ({!r})." .format(arg_text), ) arg_list = arg_text.split(u",") path_name = arg_list.pop(0) if path_name == "-": get_file = lambda: stdout else: path = FilePath(path_name) rotate_length = int( self._get_arg( u"rotate_length", 1024 * 1024 * 1024, arg_list, )) max_rotated_files = int( self._get_arg( u"max_rotated_files", 10, arg_list, )) get_file = lambda: LogFile( path.basename(), path.dirname(), rotateLength=rotate_length, maxRotatedFiles=max_rotated_files, ) return lambda reactor: FileDestination(get_file())
#conn.create_tags(['r-75625877'], {'Name':'aaaaaaaaa'}) def request_instances(): ret = conn.run_instances( "ami-a579efa4", min_count=1, max_count=1, #key_name='favbuykey', #security_groups=['sg-5d0b7d5c'], security_group_ids=['sg-5d0b7d5c'], #instance_profile_name = "aa", instance_type="t1.micro", #user_data=get_init_script(*(NUMS.get(itype, (10, 10))),burst=burst) ) return ret if __name__ == "__main__": log.startLogging(sys.stdout) reactor.callWhenRunning(start) reactor.run() if __name__ == "__builtin__": from twisted.python.log import ILogObserver, FileLogObserver from twisted.python.logfile import DailyLogFile, LogFile reactor.callWhenRunning(start) application = service.Application('ec2_schd') logfile = LogFile("ec2_schd.log", "/var/log/", rotateLength=100000000000) application.setComponent(ILogObserver, FileLogObserver(logfile).emit)
syslog_conf = (syslog_conf[0], int(syslog_conf[1])) else: syslog_conf = syslog_conf[0] init_logger( syslog_conf ) ''' from server import Server from datetime import datetime server = Server() reactor.addSystemEventTrigger('before', 'shutdown', server.cleanup) application = service.Application(SERVER_NAME) log_path = config.log_path log_rotate = int(config.log_rotate_interval) logfile = LogFile('message.log', log_path, rotateLength=log_rotate) logOb = FileLogObserver(logfile) logOb.formatTime = lambda when: datetime.fromtimestamp(when).strftime( '%m/%d %T.%f') application.setComponent(ILogObserver, logOb.emit) internet.TCPServer(config.port, server, interface=config.interface).setServiceParent( service.IServiceCollection(application)) internet.TCPServer(config.adminport, ShellFactory(), interface=config.interface).setServiceParent( service.IServiceCollection(application))
sys.setdefaultencoding('utf-8') from os.path import abspath, dirname, join, normpath PREFIX = normpath(dirname(abspath(__file__))) for path in (PREFIX, normpath(join(PREFIX, '../lib'))): if path not in sys.path: sys.path = [path] + sys.path import setting SERVER_NAME = 'GAMESERVER' import log log.init(setting.LOG_THRESHOLD) import redis redis.init(setting.REDIS_CONF) from server import Server from datetime import datetime server = Server() reactor.addSystemEventTrigger('before', 'shutdown', server.cleanup) application = service.Application(SERVER_NAME) logfile = LogFile('game.log', join(PREFIX, '../logs'), rotateLength=setting.LOG_ROTATE_INTERVAL) logOb = FileLogObserver(logfile) logOb.formatTime = lambda when : datetime.fromtimestamp(when).strftime('%m/%d %T.%f') application.setComponent( ILogObserver, logOb.emit ) internet.TCPServer(setting.GAMESERVER['port'], server, interface=setting.GAMESERVER['hostname']).setServiceParent(service.IServiceCollection(application)) internet.TCPServer(setting.GAMESERVER['adminport'], ShellFactory(), interface=setting.GAMESERVER['hostname']).setServiceParent(service.IServiceCollection(application))
class Readiness(resource.Resource): isLeaf = True def render_GET(self, request): data = {"msg": "Readiness checking"} request.setResponseCode(200) return json.dumps(data) def getAppService(port, interface): root = resource.Resource() root.putChild("liveness", Liveness()) root.putChild("readiness", Readiness()) site = server.Site(root) return internet.TCPServer(port, site, interface=interface) application = service.Application('app') log_file = LogFile('daemon.log', './', rotateLength=1024 * 1024 * 1024, maxRotatedFiles=2) log_observer = FilteringLogObserver(textFileLogObserver(log_file), predicates=[ LogLevelFilterPredicate( LogLevel.debug), ]) application.setComponent(ILogObserver, log_observer) app_service = getAppService(5000, '0.0.0.0') app_service.setServiceParent(application)
from server import Server from web.server import process from datetime import datetime server = Server() httpserver = http.HTTPFactory() httpserver.protocol().requestFactory.process = process #httpserver.protocol.requestFactory.process = process reactor.addSystemEventTrigger('before', 'shutdown', server.cleanup) application = service.Application(SERVER_NAME) log_path = config.log_path log_rotate = int(config.log_rotate_interval) logfile = LogFile('gateway.log', log_path, rotateLength=log_rotate) logOb = FileLogObserver(logfile) logOb.formatTime = lambda when: datetime.fromtimestamp(when).strftime( '%m/%d %T.%f') application.setComponent(ILogObserver, logOb.emit) internet.TCPServer(config.port, server, backlog=500).setServiceParent( service.IServiceCollection(application)) internet.TCPServer(config.httport, httpserver).setServiceParent( service.IServiceCollection(application)) internet.TCPServer(config.adminport, ShellFactory(), interface='127.0.0.1').setServiceParent( service.IServiceCollection(application))
def startServer(options): """ Configures and starts the twisted event loop to provide archiver services """ from twisted.python import log from twisted.internet import protocol, reactor, task # record some info about how the server was started info = ServerInfo() info.startedAt = time.time() info.pid = os.getpid() info.user = getpass.getuser() info.host = socket.getfqdn() info.commandLine = ' '.join(sys.argv) # create a unique temporary path to hold our logging and buffers options.tmpPath = getEnvPath(options.tmpPath) options.listenPath = getEnvPath(options.listenPath) options.cmdPath = getEnvPath(options.cmdPath) print('Running', __file__, 'as PID', info.pid) if 'PID' in options.tmpPath: options.tmpPath = options.tmpPath.replace('PID', '%d') % info.pid assert not os.path.exists(options.tmpPath) os.makedirs(options.tmpPath) print('Starting archive server with output to', options.tmpPath) # start logging all stdout and stderr traffic if options.interactive: log.startLogging(sys.stdout) else: f = LogFile("server.log", options.tmpPath + '/', rotateLength=1000000000) logger = LevelFileLogObserver(f, logging.DEBUG) log.addObserver(logger.emit) # find our product dir info.productDir = os.getenv('ICS_ARCHIVER_DIR') if not info.productDir: print('ICS_ARCHIVER_DIR is not defined. Will try to use working dir.') info.productDir = os.getcwd() # startup the database archiver.protocol.MessageReceiver.options = options archiver.database.init(options) # define a periodic timer interrupt handler def ping(): archiver.database.ping(options) pinger = task.LoopingCall(ping) if options.pingInterval > 0: pinger.start(options.pingInterval) # configure and start the reactor event loop try: # listen for clients sending reply messages replyFactory = protocol.Factory() replyFactory.protocol = archiver.protocol.ReplyReceiver if options.listenPort > 0: print('Listening for replies on TCP port %d' % options.listenPort) reactor.listenTCP(options.listenPort, replyFactory) if options.listenPath: print('Listening for replies on UNIX path %s' % options.listenPath) reactor.listenUNIX(options.listenPath, replyFactory) # connect to the hub's reply message stream if options.hubHost and options.hubPort > 0: print('Looking for the hub at %s:%d' % (options.hubHost, options.hubPort)) class HubFactory(protocol.ReconnectingClientFactory): initialDelay = options.hubInitialDelay factor = options.hubDelayFactor maxDelay = options.hubMaxDelay * 3600.0 def buildProtocol(self, addr): print('Connected to the hub') handler = archiver.protocol.ReplyReceiver() handler.factory = self self.resetDelay() return handler reactor.connectTCP(options.hubHost, options.hubPort, HubFactory()) # listen for clients sending command messages cmdFactory = protocol.Factory() cmdFactory.protocol = archiver.protocol.CommandReceiver if options.cmdPort > 0: print('Listening for commands on TCP port %d' % options.cmdPort) reactor.listenTCP(options.cmdPort, cmdFactory) if options.cmdPath: print('Listening for commands on UNIX path %s' % options.cmdPath) reactor.listenUNIX(options.cmdPath, cmdFactory) # start up a web server if options.httpPort > 0: reactor.listenTCP(options.httpPort, archiver.web.ArchiverSite(info, options)) reactor.run() except Exception: print('Reactor started failed') # need to release unix socket cleanly here... if options.listenPath: os.unlink(options.listenPath) raise
import os import sys sys.path.append(os.path.abspath(os.path.dirname(__file__))) from twisted.logger import textFileLogObserver, ILogObserver from twisted.application import service from twisted.python.logfile import LogFile import config from lite_auth_ldap.service import LiteAutLDAPService application = service.Application("LiteAuth LDAP Application") if not config.DEBUG: f = LogFile('ldap.log', config.LOG_PATH, rotateLength=config.LOG_MAX_BYTES, maxRotatedFiles=config.LOG_BACKUP_COUNT) application.setComponent(ILogObserver, textFileLogObserver(f)) ip, port = config.LDAP_LISTEN.split(':') service = LiteAutLDAPService(int(port), ip) service.setServiceParent(application)
syslog_conf = (syslog_conf[0], int(syslog_conf[1])) else: syslog_conf = syslog_conf[0] init_logger( syslog_conf ) ''' from server import Server from datetime import datetime server = Server() reactor.addSystemEventTrigger('before', 'shutdown', server.cleanup) application = service.Application(SERVER_NAME) log_path = config.log_path log_rotate = int(config.log_rotate_interval) logfile = LogFile('alliance.log', log_path, rotateLength=log_rotate) logOb = FileLogObserver(logfile) logOb.formatTime = lambda when: datetime.fromtimestamp(when).strftime( '%m/%d %T.%f') application.setComponent(ILogObserver, logOb.emit) internet.TCPServer(config.port, server, interface=config.interface).setServiceParent( service.IServiceCollection(application)) internet.TCPServer(config.adminport, ShellFactory(), interface=config.interface).setServiceParent( service.IServiceCollection(application))
print "registered client " + client.peerstr self.clients.append(client) def unregister(self, client): if client in self.clients: print "unregistered client " + client.peerstr self.clients.remove(client) def start(): factory = StoreServerFactory("ws://localhost:9000", debugWamp=True) factory.protocol = RepeaterServerProtocol factory.setProtocolOptions(allowHixie76=True) listenWS(factory) if __name__ == "__main__": log.startLogging(sys.stdout) reactor.callWhenRunning(start) reactor.run() if __name__ == "__builtin__": from twisted.python.log import ILogObserver, FileLogObserver from twisted.python.logfile import DailyLogFile, LogFile reactor.callWhenRunning(start) application = service.Application('server_admin') logfile = LogFile("server_admin.log", "/var/log/", rotateLength=100000000000) application.setComponent(ILogObserver, FileLogObserver(logfile).emit)