forked from nacx/kahuna
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Configuration and logging refactoring
* Main config file now in ~/.hahuna directory * Main configuration is now a singleton * Loggign level configured in the main configuration file * Refactored plugins to output debug logs
- Loading branch information
Ignasi Barrera
committed
Jan 27, 2012
1 parent
5e4246e
commit 28fa291
Showing
9 changed files
with
68 additions
and
34 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
# Main configuration file fot Kahuna | ||
|
||
[connection] | ||
address = 10.60.1.222 | ||
user = admin | ||
pass = xabiquo | ||
|
||
[logging] | ||
level = INFO | ||
|
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,10 @@ | ||
import logging | ||
from config import Config | ||
|
||
FORMAT = '%(asctime)-15s %(levelname)s %(message)s' | ||
logging.basicConfig(format=FORMAT) | ||
logger = logging.getLogger("kahuna") | ||
logger.setLevel(logging.DEBUG) | ||
|
||
# Load the singleton configuration instance to initialize | ||
# the 'kahuna' logger | ||
Config() | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,31 +1,47 @@ | ||
#!/usr/bin/env jython | ||
|
||
import os | ||
import ConfigParser | ||
import logging | ||
import shutil | ||
import ConfigParser | ||
from utils.singleton import singleton | ||
|
||
log = logging.getLogger('kahuna') | ||
log.setLevel(logging.INFO) | ||
|
||
@singleton | ||
class Config: | ||
""" Main configuration. """ | ||
def __init__(self): | ||
config = ConfigParser.SafeConfigParser() | ||
configFound = os.environ['HOME'] + "/tmp/kahuna.conf" | ||
# User config has precedence, then system then /usr/local | ||
files = [os.environ['HOME'] + '/.kahuna.conf', '/etc/kahuna.conf', '/usr/local/etc/kahuna.conf'] | ||
config = ConfigParser.SafeConfigParser() | ||
user_dir = os.environ['HOME'] + "/.kahuna" | ||
user_config = user_dir + "/kahuna.conf" | ||
config_found = user_config | ||
# User config has precedence, then system | ||
files = [user_config, '/etc/kahuna.conf'] | ||
for file in files: | ||
if os.path.exists(file): | ||
#some debugging | ||
log.debug("Config found in %s" % file) | ||
configFound = file | ||
config_found = file | ||
break | ||
|
||
if not os.path.exists(configFound): | ||
log.error("Kahuna config file not found.") | ||
raise IOError("Configuration file not found. " + | ||
"Please, make sure that $HOME/.kahuna.conf or /etc/kahuna.conf exists"); | ||
# If no config file exists, copy the default one to the user config | ||
if not os.path.exists(config_found): | ||
log.warn("Kahuna config file not found. Creating the default one to %s" % user_config) | ||
if not os.path.isdir(user_dir): | ||
os.makedirs(user_dir) | ||
shutil.copy('config/kahuna.conf', user_config) | ||
config_found = user_config | ||
|
||
config.read(file) | ||
config.read(config_found) | ||
self.address = config.get("connection", "address") | ||
self.user = config.get("connection", "user") | ||
self.password = config.get("connection", "pass") | ||
|
||
try: | ||
self.loglevel = config.get("logging", "level") | ||
level = logging._levelNames[self.loglevel] | ||
log.setLevel(level) | ||
except ConfigParser.NoOptionError: | ||
pass | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,3 @@ | ||
#!/usr/bin/env jython | ||
|
||
import os | ||
|
||
# Automatically set the __all__ variable with all | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
#!/usr/bin/env jython | ||
|
||
def singleton(cls): | ||
""" Singleton decorator. """ | ||
instances = {} | ||
def instance(): | ||
if cls not in instances: | ||
instances[cls] = cls() | ||
return instances[cls] | ||
return instance | ||
|