Skip to content

Commit

Permalink
Start logging service in each subsystem.
Browse files Browse the repository at this point in the history
  • Loading branch information
Erik Nolte committed Aug 4, 2011
1 parent 7a19e3e commit b205fca
Show file tree
Hide file tree
Showing 8 changed files with 187 additions and 0 deletions.
33 changes: 33 additions & 0 deletions butter/kvm/__init__.py
Expand Up @@ -2,6 +2,7 @@
Initialize interactions with the butter kvm subsytem
'''
# Import Python libs
import logging
import optparse
import os
import subprocess
Expand All @@ -16,6 +17,9 @@
import butter.kvm.create
import butter.kvm.migrate
import butter.kvm.overlay
import butter.log

log = logging.getLogger(__name__)

def domain():
'''
Expand All @@ -26,6 +30,18 @@ def domain():
shell=True,
stdout=subprocess.PIPE).communicate()[0].strip()

def verify_env(dirs):
'''
Verify that the named directories are in place and that the environment
can shake the salt
'''
for dir_ in dirs:
if not os.path.isdir(dir_):
try:
os.makedirs(dir_)
except OSError, e:
print 'Failed to create directory path "%s" - %s' % (dir_, e)

class KVM(object):
'''
The KVM class is used to wrap the functionality of all butter kvm calls
Expand All @@ -36,6 +52,9 @@ def __init__(self):
'''
self.opts = self.__parse()

for name, level in self.opts['log_granular_levels'].iteritems():
butter.log.set_logger_level(name, level)

def __parse(self):
'''
Parse the butter command line options
Expand Down Expand Up @@ -165,10 +184,21 @@ def __parse(self):
help='Pass in an alternative path for the butter kvm'\
+ ' configuration file; default: /etc/butter/kvm')

parser.add_option('-l',
'--log-level',
dest='log_level',
default='warning',
choices=butter.log.LOG_LEVELS.keys(),
help='Console log level. One of %s. For the logfile settings '
'see the config file. Default: \'%%default\'.' %
', '.join([repr(l) for l in butter.log.LOG_LEVELS.keys()]))

options, args = parser.parse_args()

cli = {}

butter.log.setup_console_logger(options.log_level)

cli['create'] = options.create
cli['destroy'] = options.destroy
cli['purge'] = options.purge
Expand Down Expand Up @@ -254,6 +284,9 @@ def run(self):
'''
Execute the logic required to act on the passed state data
'''
verify_env([os.path.dirname(self.opts['log_file'])])
butter.log.setup_logfile_logger(self.opts['log_file'], self.opts['log_level'])

# Each sequence should be a function in the class so that the
# capabilities can be manipulated in a more api centric way
if self.opts['create']:
Expand Down
17 changes: 17 additions & 0 deletions butter/kvm/config.py
Expand Up @@ -9,6 +9,15 @@
import os
import yaml

def prepend_root_dir(opts, path_options):
'''
Prepends the options that represent filesystem paths with value of the
'root_dir' option.
'''
for path_option in path_options:
opts[path_option] = os.path.normpath(
os.sep.join([opts['root_dir'], opts[path_option]]))

def config(path='/etc/butter/kvm'):
'''
Load up the configuration for butter kvm
Expand All @@ -17,6 +26,11 @@ def config(path='/etc/butter/kvm'):
'instances': '/srv/vm/instances',
'local_path': '/mnt/local/vm',
'salt_pki': '/etc/salt/pki',
# Log options
'root_dir': '/',
'log_file' : '/var/log/butter/kvm',
'log_level' : 'warning',
'log_granular_levels': {},
# Global vm generation options
'storage_type': 'local', # Can be 'local', 'shared', 'choose'
'distro': 'arch', # The default distribution to use
Expand All @@ -35,4 +49,7 @@ def config(path='/etc/butter/kvm'):
except:
pass

# Prepend root_dir to other paths
prepend_root_dir(opts, ['log_file'])

return opts
15 changes: 15 additions & 0 deletions butter/kvmd/__init__.py
Expand Up @@ -2,6 +2,7 @@
Initialize interactions with the butter kvm subsytem
'''
# Import Python libs
import logging
import optparse
import os
import subprocess
Expand All @@ -13,6 +14,9 @@

# Import butter libs
import butter.kvmd.daemon
import butter.log

log = logging.getLogger(__name__)

class KVMD(object):
'''
Expand Down Expand Up @@ -41,8 +45,19 @@ def __parse_cli(self):
dest='config',
help='Pass in an alternative configuration file')

parser.add_option('-l',
'--log-level',
dest='log_level',
default='warning',
choices=butter.log.LOG_LEVELS.keys(),
help='Console log level. One of %s. For the logfile settings '
'see the config file. Default: \'%%default\'.' %
', '.join([repr(l) for l in butter.log.LOG_LEVELS.keys()]))

options, args = parser.parse_args()

butter.log.setup_console_logger(options.log_level)

return {'foreground': options.foreground,
'config': options.config}

Expand Down
32 changes: 32 additions & 0 deletions butter/statd/__init__.py
Expand Up @@ -3,6 +3,7 @@
that uses salt for statistics system monitoring.
'''
# Import Python modules
import logging
import multiprocessing
import optparse
import os
Expand All @@ -16,6 +17,20 @@
import butter.statd.monitor
import butter.utils

log = logging.getLogger(__name__)

def verify_env(dirs):
'''
Verify that the named directories are in place and that the environment
can shake the salt
'''
for dir_ in dirs:
if not os.path.isdir(dir_):
try:
os.makedirs(dir_)
except OSError, e:
print 'Failed to create directory path "%s" - %s' % (dir_, e)

class StatD(object):
'''
The StatD object is used to initialize the stats monitoring subsytem
Expand Down Expand Up @@ -44,19 +59,36 @@ def __parse(self):
help='Choose an alternative config file for the statd '
'daemon; default /etc/butter/statd')

parser.add_option('-l',
'--log-level',
dest='log_level',
default='warning',
choices=butter.log.LOG_LEVELS.keys(),
help='Console log level. One of %s. For the logfile settings '
'see the config file. Default: \'%%default\'.' %
', '.join([repr(l) for l in butter.log.LOG_LEVELS.keys()]))

options, args = parser.parse_args()

opts = butter.statd.config.config(options.config)

opts['daemon'] = options.daemon

for name, level in opts['log_granular_levels'].iteritems():
butter.log.set_logger_level(name, level)

butter.log.setup_console_logger(options.log_level)

return opts

def run(self):
'''
Create the multiprocessing/threading interfaces for butter statd
and start them.
'''
verify_env([os.path.dirname(self.opts['log_file'])])
butter.log.setup_logfile_logger(self.opts['log_file'], self.opts['log_level'])

if self.opts['daemon']:
butter.utils.daemonize()
if self.opts['stats']:
Expand Down
18 changes: 18 additions & 0 deletions butter/statd/config.py
Expand Up @@ -9,6 +9,15 @@
import os
import yaml

def prepend_root_dir(opts, path_options):
'''
Prepends the options that represent filesystem paths with value of the
'root_dir' option.
'''
for path_option in path_options:
opts[path_option] = os.path.normpath(
os.sep.join([opts['root_dir'], opts[path_option]]))

def config(path='/etc/butter/statd'):
'''
Load up the configuration for butter kvm
Expand All @@ -28,6 +37,12 @@ def config(path='/etc/butter/statd'):
'target': '.*',
'target_type': 'pcre',
'stats': {},

# Log options
'root_dir': '/',
'log_file' : '/var/log/butter/statd',
'log_level' : 'warning',
'log_granular_levels': {},
}

if os.path.isfile(path):
Expand All @@ -36,4 +51,7 @@ def config(path='/etc/butter/statd'):
except:
pass

# Prepend root_dir to other paths
prepend_root_dir(opts, ['log_file'])

return opts
24 changes: 24 additions & 0 deletions conf/kvm
@@ -1,3 +1,27 @@
##### KVM configuration settings #####
##########################################
# The root directory prepended to these options: log_file
#root_dir: /

##### Logging settings #####
##########################################
# The location of the master log file
#log_file: /var/log/butter/kvm
# The level of messages to send to the log file.
# One of 'info', 'quiet', 'critical', 'error', 'debug', 'warning'.
# Default: 'warning'
#log_level: warning
#
# Logger levels can be used to tweak specific loggers logging levels.
# Imagine you want to have the butter library at the 'warning' level,
# but you still wish to have 'butter.modules' at the 'debug' level:
# log_granular_levels: {
# 'butter': 'warning',
# 'butter.modules': 'debug'
# }
#
#log_granular_levels: {}

#images: /srv/vm/images
#instances: /srv/vm/instances
#local_path: /mnt/local/vm
Expand Down
24 changes: 24 additions & 0 deletions conf/kvmd
@@ -1,3 +1,27 @@
##### KVMD configuration settings #####
##########################################
# The root directory prepended to these options: log_file
#root_dir: /

##### Logging settings #####
##########################################
# The location of the master log file
#log_file: /var/log/butter/kvmd
# The level of messages to send to the log file.
# One of 'info', 'quiet', 'critical', 'error', 'debug', 'warning'.
# Default: 'warning'
#log_level: warning
#
# Logger levels can be used to tweak specific loggers logging levels.
# Imagine you want to have the butter library at the 'warning' level,
# but you still wish to have 'butter.modules' at the 'debug' level:
# log_granular_levels: {
# 'butter': 'warning',
# 'butter.modules': 'debug'
# }
#
#log_granular_levels: {}

# Configuration for the kvm daemon
#images: /srv/vm/images
#pool_size: 5
Expand Down
24 changes: 24 additions & 0 deletions conf/statd
@@ -1,3 +1,27 @@
##### STATD configuration settings #####
##########################################
# The root directory prepended to these options: log_file
#root_dir: /

##### Logging settings #####
##########################################
# The location of the master log file
#log_file: /var/log/butter/statd
# The level of messages to send to the log file.
# One of 'info', 'quiet', 'critical', 'error', 'debug', 'warning'.
# Default: 'warning'
#log_level: warning
#
# Logger levels can be used to tweak specific loggers logging levels.
# Imagine you want to have the butter library at the 'warning' level,
# but you still wish to have 'butter.modules' at the 'debug' level:
# log_granular_levels: {
# 'butter': 'warning',
# 'butter.modules': 'debug'
# }
#
#log_granular_levels: {}

#### Global Stats Settings #####
#########################################
# Number of seconds between data queries
Expand Down

0 comments on commit b205fca

Please sign in to comment.