Skip to content
Browse files

Updated config handling

  • Loading branch information...
1 parent fe9732d commit 969ea9f62ee352be3f58071ab1b2af1a1e244738 @jbohman committed Jul 9, 2010
View
3 README
@@ -48,3 +48,6 @@ The keyspace definition might be obsolete in future releases of Cassandra (0.7+)
Usage
-----
+
+Configuration
+-------------
View
39 logsandra-httpd.py
@@ -2,7 +2,6 @@
"""
Logsandra http daemon
"""
-# Global imports
import sys
import os
import optparse
@@ -11,50 +10,62 @@
from cherrypy import wsgiserver
from paste.deploy import loadapp
-# Local imports
from logsandra import utils
from logsandra.utils.daemon import Daemon
+from logsandra.utils import config
class Application(Daemon):
def run(self):
- logger = logging.getLogger('logsandra.httpd')
-
- wsgi_app = loadapp('config:%s' % self.settings.config_file, relative_to=self.settings.working_directory)
- wsgi_app.config['logsandra'] = {'cassandra_address': 'localhost', 'cassandra_port': 9160, 'cassandra_timeout': 5}
- server = wsgiserver.CherryPyWSGIServer((self.settings.host, self.settings.port), wsgiserver.WSGIPathInfoDispatcher({'/': wsgi_app}))
+ wsgi_app = loadapp('config:%s' % self.config['httpd_config'], relative_to=self.config['httpd_working_directory'], global_conf={
+ 'cassandra_address': self.config['cassandra_address'],
+ 'cassandra_port': self.config['cassandra_port'],
+ 'cassandra_timeout': self.config['cassandra_timeout'],
+ 'ident': self.config['ident']})
+ server = wsgiserver.CherryPyWSGIServer((self.config['httpd_host'], int(self.config['httpd_port'])), wsgiserver.WSGIPathInfoDispatcher({'/': wsgi_app}))
try:
- logger.debug('Starting server')
+ self.logger.debug('Starting httpd server at %s:%s' % (self.config['httpd_host'], self.config['httpd_port']))
server.start()
except socket.error:
- logger.error('Address already in use')
+ self.logger.error('Address already in use: %s:%s' % (self.config['httpd_host'], self.config['httpd_port']))
+ server.stop()
except KeyboardInterrupt:
- logger.debug('Stopping server')
+ self.logger.error('Httpd server stopped by KeyboardInterrupt')
server.stop()
if __name__ == '__main__':
default_working_directory = os.curdir
+ default_config_file = os.path.join(default_working_directory, 'logsandra.yaml')
usage = 'usage: %prog [options] start|stop|restart'
parser = optparse.OptionParser(usage=usage)
- parser.add_option('--config', dest='config_file', metavar='FILE', default='development.ini')
+ parser.add_option('--config', dest='config_file', metavar='FILE', default=default_config_file)
parser.add_option('--working-directory', dest='working_directory', metavar='DIRECTORY', default=default_working_directory)
parser.add_option('--pid', dest='pid_file', metavar='FILE', default=os.path.join(default_working_directory, 'logsandra-httpd.pid'))
parser.add_option('--application-data-directory', dest='application_data_directory', default=utils.application_data_directory('logsandra'))
- parser.add_option('--host', dest='host', metavar='HOST', default='0.0.0.0')
- parser.add_option('--port', dest='port', metavar='PORT', type='int', default=5000)
+ parser.add_option('--host', dest='host', metavar='HOST')
+ parser.add_option('--port', dest='port', metavar='PORT', type='int')
(options, args) = parser.parse_args()
if not os.path.isdir(options.application_data_directory):
os.makedirs(options.application_data_directory)
logfile = os.path.join(options.application_data_directory, 'logsandra.log')
logging.basicConfig(filename=logfile, level=logging.DEBUG, format="%(asctime)s %(levelname)-5.5s [%(name)s] [%(threadName)s] %(message)s")
+ logger = logging.getLogger('logsandra.httpd')
application = Application(options.pid_file, working_directory=options.working_directory, stdout=logfile, stderr=logfile)
- application.settings = options
+ application.config = config.parse(options.config_file)
+ application.config['httpd_working_directory'] = options.working_directory
+ application.logger = logger
+
+ if options.host:
+ application.config['httpd_host'] = options.host
+
+ if options.port:
+ application.config['httpd_port'] = options.port
if len(args) == 1:
if args[0] == 'start':
View
17 logsandra-monitord.py
@@ -2,31 +2,28 @@
"""
Logsandra monitor daemon
"""
-# Global imports
import sys
import os
import optparse
import logging
-# Local imports
from logsandra import utils
from logsandra.utils.daemon import Daemon
-from logsandra.monitor import config, monitor
+from logsandra.utils import config
+from logsandra.monitor import monitor
class Application(Daemon):
def run(self):
- logger = logging.getLogger('logsandra.monitord')
- settings = config.parse(self.settings.config_file)
- m = monitor.Monitor(settings, False)
- logger.debug('Starting monitor daemon')
+ self.logger.debug('Starting monitor daemon')
+ m = monitor.Monitor(self.config, False)
m.run()
if __name__ == '__main__':
default_working_directory = os.curdir
- default_config_file = os.path.join(default_working_directory, 'config.yaml')
+ default_config_file = os.path.join(default_working_directory, 'logsandra.yaml')
usage = 'usage: %prog [options] start|stop|restart'
parser = optparse.OptionParser(usage=usage)
@@ -41,9 +38,11 @@ def run(self):
logfile = os.path.join(options.application_data_directory, 'logsandra.log')
logging.basicConfig(filename=logfile, level=logging.DEBUG, format="%(asctime)s %(levelname)-5.5s [%(name)s] [%(threadName)s] %(message)s")
+ logger = logging.getLogger('logsandra.monitord')
application = Application(options.pid_file, working_directory=options.working_directory, stdout=logfile, stderr=logfile)
- application.settings = options
+ application.config = config.parse(options.config_file)
+ application.logger = logger
if len(args) == 1:
if args[0] == 'start':
View
13 config.yaml → logsandra.yaml
@@ -2,15 +2,14 @@
ident: 'Logsandra Server 1'
# Web service
-web_enabled: True
-web_address: "0.0.0.0"
-web_port: 5000
-web_config: 'development.ini'
+httpd_host: '0.0.0.0'
+httpd_port: '5000'
+httpd_config: 'development.ini'
# Cassandra cluster to connect to
-cassandra_address: 'localhost'
-cassandra_port: 9160
-cassandra_timeout: 5
+cassandra_host: 'localhost'
+cassandra_port: '9160'
+cassandra_timeout: '5'
# List of paths (files and directories) to monitor
paths:
View
2 logsandra/config/environment.py
@@ -39,5 +39,5 @@ def load_environment(global_conf, app_conf):
# CONFIGURATION OPTIONS HERE (note: all config options will override
# any Pylons config options)
-
+
return config
View
1 logsandra/controllers/log.py
@@ -57,6 +57,7 @@ def view(self):
# and make sure it uses the config file
client = Cassandra('', 'localhost', 9160, 5)
+
if current_next:
entries, last, first = client.get_entries_by_keyword(keyword,
date_from, date_to, action_next=current_next)
View
3 logsandra/lib/app_globals.py
@@ -3,6 +3,8 @@
from beaker.cache import CacheManager
from beaker.util import parse_cache_config_options
+from logsandra.utils.model import Cassandra
+
class Globals(object):
"""Globals acts as a container for objects available throughout the
life of the application
@@ -16,3 +18,4 @@ def __init__(self, config):
"""
self.cache = CacheManager(**parse_cache_config_options(config))
+ #self.cassandra = Cassandra(config['ident'], config['cassandra_host'], config['cassandra_port'], config['cassandra_timeout'])
View
6 logsandra/monitor/config.py
@@ -1,6 +0,0 @@
-# Global import
-import yaml
-
-def parse(config_file):
- file_handler = open(config_file)
- return yaml.load(file_handler.read())
View
30 logsandra/utils/config.py
@@ -0,0 +1,30 @@
+import yaml
+
+def parse(config_file):
+ file_handler = open(config_file)
+ config = yaml.load(file_handler.read())
+
+ if 'ident' not in config:
+ raise Exception('Config %s must contain an ident' % config_file)
+
+
+ if 'httpd_address' not in config:
+ config['httpd_address'] = '0.0.0.0'
+
+ if 'httpd_port' not in config:
+ config['httpd_port'] = 5000
+
+ if 'httpd_config' not in config:
+ config['httpd_config'] = 'development.ini'
+
+
+ if 'cassandra_address' not in config:
+ config['cassandra_address'] = 'localhost'
+
+ if 'cassandra_port' not in config:
+ config['cassandra_port'] = '9160'
+
+ if 'cassandra_timeout' not in config:
+ config['cassandra_timeout'] = '5'
+
+ return config

0 comments on commit 969ea9f

Please sign in to comment.
Something went wrong with that request. Please try again.