diff --git a/openxcap b/openxcap index 68e94bf..f514fa7 100644 --- a/openxcap +++ b/openxcap @@ -1,80 +1,83 @@ #!/usr/bin/env python -# Copyright (C) 2007-2015 AG Projects. -# - -"""OpenXCAP""" - - -MEMORY_DEBUG = False - if __name__ == '__main__': import sys import xcap from application import log from application.process import process, ProcessError - from optparse import OptionParser + from argparse import ArgumentParser name = 'openxcap' fullname = 'OpenXCAP' description = 'An open source XCAP Server' - config_directory = '/etc/openxcap' - runtime_directory = '/var/run/openxcap' - default_pid = "%s/%s.pid" % (runtime_directory, name) + process.configuration.user_directory = None + process.configuration.subdirectory = 'openxcap' - parser = OptionParser(version='%%prog %s' % xcap.__version__) - parser.add_option("--no-fork", action="store_false", dest="fork", default=1, - help="run the process in the foreground (for debugging)") - parser.add_option("--pid", dest="pidfile", default=default_pid, - help="pid file (%s)" % default_pid, - metavar="File") + parser = ArgumentParser(usage='%(prog)s [options]') + parser.add_argument('--version', action='version', version='%(prog)s {}'.format(xcap.__version__)) + parser.add_argument('--systemd', action='store_true', help='run as a systemd simple service and log to journal') + parser.add_argument('--no-fork', action='store_false', dest='fork', help='run in the foreground and log to the terminal') + parser.add_argument('--config-dir', dest='config_directory', default=None, help='the configuration directory ({})'.format(process.configuration.system_directory), metavar='PATH') + parser.add_argument('--runtime-dir', dest='runtime_directory', default=None, help='the runtime directory ({})'.format(process.runtime.directory), metavar='PATH') + parser.add_argument('--debug', action='store_true', help='enable verbose logging') + parser.add_argument('--debug-memory', action='store_true', help='enable memory debugging') - (options, args) = parser.parse_args() + options = parser.parse_args() try: from xcap.logutil import web_logger except Exception as e: - log.critical('Failed to create %s: %s' % (fullname, e)) + log.critical('Cannot start %s: %s' % (fullname, e)) if not isinstance(e, (RuntimeError, OSError, IOError)): log.exception() sys.exit(1) if web_logger.filename is None: # access log is reported along with the rest of the applications's logging log.Formatter.prefix_format = '{record.levelname:<8s} [{record.name}] ' else: log.Formatter.prefix_format = '{record.levelname:<8s} ' - pidfile = options.pidfile - process.system_config_directory = config_directory + if options.config_directory is not None: + process.configuration.local_directory = options.config_directory + if options.runtime_directory is not None: + process.runtime.directory = options.runtime_directory - if not options.fork: - process._runtime_directory = None - else: + if options.systemd: + from systemd.journal import JournalHandler + log.set_handler(JournalHandler(SYSLOG_IDENTIFIER=name)) + log.capture_output() + elif options.fork: try: - process.runtime_directory = runtime_directory - process.daemonize(pidfile) - except ProcessError, e: - log.critical('Fatal error: %s' % e) + process.daemonize(pidfile='{}.pid'.format(name)) + except ProcessError as e: + log.critical('Cannot start %s: %s' % (fullname, e)) sys.exit(1) log.use_syslog(name) - log.info('Starting %s' % fullname) + log.info('Starting %s %s' % (fullname, xcap.__version__)) + + try: + process.wait_for_network(wait_time=10, wait_message='Waiting for network to become available...') + except KeyboardInterrupt: + sys.exit(0) + except RuntimeError as e: + log.critical('Cannot start %s: %s' % (fullname, e)) + sys.exit(1) try: - if not options.fork and MEMORY_DEBUG: - from application.debug.memory import memory_dump from xcap.server import XCAPServer + if options.debug: + log.level.current = log.level.DEBUG + if options.debug_memory: + from application.debug.memory import memory_dump server = XCAPServer() server.start() - except Exception, e: - log.critical("Failed to create %s: %s" % (fullname, e)) + except Exception as e: + log.critical('Failed to create %s: %s' % (fullname, e)) if type(e) is not RuntimeError: log.exception() sys.exit(1) - if not options.fork and MEMORY_DEBUG: - print "------------------" + if options.debug_memory: memory_dump() - print "------------------" - diff --git a/xcap/tls.py b/xcap/tls.py index 7538549..77d42a6 100644 --- a/xcap/tls.py +++ b/xcap/tls.py @@ -1,54 +1,54 @@ """TLS helper classes""" __all__ = ['Certificate', 'PrivateKey'] from gnutls.crypto import X509Certificate, X509PrivateKey from application import log from application.process import process class _FileError(Exception): pass def file_content(file): - path = process.config_file(file) + path = process.configuration.file(filename) if path is None: raise _FileError("File '%s' does not exist" % file) try: f = open(path, 'rt') except Exception: raise _FileError("File '%s' could not be open" % file) try: return f.read() finally: f.close() class Certificate(object): """Configuration data type. Used to create a gnutls.crypto.X509Certificate object from a file given in the configuration file.""" def __new__(cls, value): if isinstance(value, str): try: return X509Certificate(file_content(value)) except Exception, e: log.warning('Certificate file %r could not be loaded: %s' % (value, e)) return None else: raise TypeError('value should be a string') class PrivateKey(object): """Configuration data type. Used to create a gnutls.crypto.X509PrivateKey object from a file given in the configuration file.""" def __new__(cls, value): if isinstance(value, str): try: return X509PrivateKey(file_content(value)) except Exception, e: log.warning('Private key file %r could not be loaded: %s' % (value, e)) return None else: raise TypeError('value should be a string')