diff --git a/application.py b/application.py index 4d84bdc..6b6d32f 100644 --- a/application.py +++ b/application.py @@ -2,18 +2,21 @@ import core # Nexus core import argparse, sys import logging -def run(args): +def run(args, name="nexus"): parser = argparse.ArgumentParser(description="Nexus daemon") parser.add_argument("-c", "--config", dest="config_file", metavar="PATH", help="specifies the configuration file to use", default="config.yaml") parser.add_argument("-d", "--debug", dest="debug_mode", action="store_true", help="enables debugging mode", default=False) arguments = parser.parse_args(args) if arguments.debug_mode == True: - logging.basicConfig(level=logging.DEBUG, format="%(asctime)s - %(levelname)s - %(message)s") + loglevel = logging.DEBUG else: - logging.basicConfig(level=logging.INFO, format="%(asctime)s - %(levelname)s - %(message)s") + loglevel = logging.INFO - logging.info("Application started") + logformat = "[%(appname)s] %(asctime)s - %(levelname)s - %(message)s" + logging.basicConfig(level=loglevel, format=logformat) + + logging.info("Application started", extra={"appname":name}) try: config = core.config.ConfigurationReader(arguments.config_file) @@ -21,42 +24,42 @@ def run(args): sys.stderr.write("Failed to read configuration file (%s).\nCreate a configuration file that Nexus can access, or specify a different location using the -c switch.\n" % e.strerror) exit(1) - logging.info("Read configuration file at %s" % arguments.config_file) + logging.info("Read configuration file at %s" % arguments.config_file, extra={"appname":name}) # Connect to node database database = core.db.Database(config.database) database.setup() node_table = database.get_memory_table("nodes") - logging.info("Connected to database at %s" % config.database) + logging.info("Connected to database at %s" % config.database, extra={"appname":name}) # Read bootstrap/override node data for uuid, node in config.nodes.iteritems(): existing_rows = [dbnode for rowid, dbnode in node_table.data.iteritems() if dbnode['uuid'] == uuid] - if node['override'] == True: - row = existing_rows[0] - row['uuid'] = uuid - row['host'] = node['host'] - row['port'] = node['port'] - row['pubkey'] = node['pubkey'] - row['presupplied'] = 1 - row['attributes'] = 0 - row.commit() - logging.info("Updated data in database for node using configuration file (%s:%s, %s)" % (node['host'], node['port'], uuid)) - else: - if len(existing_rows) == 0: - row = core.db.Row() + if len(existing_rows) > 0: + if node['override'] == True: + row = existing_rows[0] row['uuid'] = uuid row['host'] = node['host'] row['port'] = node['port'] row['pubkey'] = node['pubkey'] row['presupplied'] = 1 row['attributes'] = 0 - database['nodes'].append(row) - logging.info("Learned about new node from configuration file, inserted into database (%s:%s, %s)" % (node['host'], node['port'], uuid)) + row.commit() + logging.info("Updated data in database for node using configuration file (%s:%s, %s)" % (node['host'], node['port'], uuid), extra={"appname":name}) else: pass # Already exists and no override flag set, ignore + else: + row = core.db.Row() + row['uuid'] = uuid + row['host'] = node['host'] + row['port'] = node['port'] + row['pubkey'] = node['pubkey'] + row['presupplied'] = 1 + row['attributes'] = 0 + database['nodes'].append(row) + logging.info("Learned about new node from configuration file, inserted into database (%s:%s, %s)" % (node['host'], node['port'], uuid), extra={"appname":name}) if __name__ == "__main__": run(sys.argv[1:]) diff --git a/nexus-cli.py b/nexus-cli.py index 4e59ac2..4c1b6f1 100755 --- a/nexus-cli.py +++ b/nexus-cli.py @@ -11,7 +11,7 @@ if len(sys.argv) < 2: if sys.argv[1] == "start": import application - application.run(sys.argv[2:]) + application.run(sys.argv[2:], "blah") elif sys.argv[1] == "stop": pass else: diff --git a/test_dual.py b/test_dual.py new file mode 100644 index 0000000..2eac39e --- /dev/null +++ b/test_dual.py @@ -0,0 +1,37 @@ +import application +import threading, logging + +class ApplicationThread(threading.Thread): + def __init__(self, name, args, hook): + threading.Thread.__init__(self) + self._name = name + self._args = args + self._hook = hook + + def run(self): + application.run(self._args, self._hook) + +class Hook(object): + def write(self, data, *args, **kwargs): + lines = data.strip().split("\n") + for line in lines: + print "[%s] %s" % (self._name, line) + +def logger_hook(level, format_, target): + logger = logging.getLogger() + logger.setLevel(level) + + handler = logging.StreamHandler(stream=target) + handler.setLevel(level) + handler.setFormatter(logging.Formatter(format_)) + + logger.addHandler(handler) + +#hook = Hook() +#logger_hook(logging.DEBUG, "[%(name)s] %(asctime)s - %(levelname)s - %(message)s", hook) + +t1 = ApplicationThread("node1", ["--debug", "--config", "test/node1.yaml"], "node1") +t2 = ApplicationThread("node2", ["--debug", "--config", "test/node2.yaml"], "node2") + +t1.start() +t2.start()