mirror of
https://github.com/kevingruesser/bootstrap-vz.git
synced 2025-08-22 09:50:37 +00:00
logging
This commit is contained in:
parent
d18fa6dd6b
commit
a8364fe04d
10 changed files with 99 additions and 10 deletions
67
base/main.py
67
base/main.py
|
@ -1,15 +1,45 @@
|
|||
import logging
|
||||
|
||||
|
||||
def main():
|
||||
args = get_args()
|
||||
setup_logger(args)
|
||||
run(args)
|
||||
|
||||
|
||||
def get_args():
|
||||
from argparse import ArgumentParser
|
||||
parser = ArgumentParser(description='Bootstrap Debian for the cloud.')
|
||||
parser.add_argument('--debug', action='store_true',
|
||||
help='Print debugging information')
|
||||
parser.add_argument('manifest', help='Manifest file to use for bootstrapping', metavar='MANIFEST')
|
||||
parser.set_defaults(func=run)
|
||||
return parser.parse_args()
|
||||
|
||||
args = parser.parse_args()
|
||||
args.func(args)
|
||||
|
||||
def setup_logger(args):
|
||||
import sys
|
||||
import os.path
|
||||
from datetime import datetime
|
||||
root = logging.getLogger()
|
||||
root.setLevel(logging.NOTSET)
|
||||
|
||||
manifest_basename = os.path.basename(args.manifest)
|
||||
manifest_name, _ = os.path.splitext(manifest_basename)
|
||||
timestamp = datetime.now().strftime('%Y%m%d%H%M%S')
|
||||
filename = "{timestamp}_{name}.log".format(timestamp=timestamp, name=manifest_name)
|
||||
path=os.path.normpath(os.path.join(os.path.dirname(__file__), '../logs', filename))
|
||||
file_handler = logging.FileHandler(path)
|
||||
file_handler.setFormatter(FileFormatter('[%(asctime)s] %(message)s'))
|
||||
file_handler.setLevel(logging.DEBUG)
|
||||
root.addHandler(file_handler)
|
||||
|
||||
console_handler = logging.StreamHandler(sys.stderr)
|
||||
console_handler.setFormatter(ConsoleFormatter())
|
||||
if args.debug:
|
||||
console_handler.setLevel(logging.DEBUG)
|
||||
else:
|
||||
console_handler.setLevel(logging.INFO)
|
||||
root.addHandler(console_handler)
|
||||
|
||||
|
||||
def run(args):
|
||||
|
@ -30,3 +60,34 @@ def run(args):
|
|||
from bootstrapinfo import BootstrapInformation
|
||||
bootstrap_info = BootstrapInformation(manifest=manifest, debug=args.debug)
|
||||
tasklist.run(bootstrap_info)
|
||||
|
||||
|
||||
class ConsoleFormatter(logging.Formatter):
|
||||
|
||||
def format(self, record):
|
||||
from task import Task
|
||||
if(isinstance(record.msg, Task)):
|
||||
task = record.msg
|
||||
if(task.description is not None):
|
||||
return '\033[0;34m{description}\033[0m'.format(description=task.description)
|
||||
else:
|
||||
return '\033[0;34mRunning {task}\033[0m'.format(task=task)
|
||||
return super(ConsoleFormatter, self).format(record)
|
||||
|
||||
|
||||
class FileFormatter(logging.Formatter):
|
||||
|
||||
def format(self, record):
|
||||
from task import Task
|
||||
from datetime import datetime
|
||||
if(isinstance(record.msg, Task)):
|
||||
task = record.msg
|
||||
if(task.description is not None):
|
||||
record.msg = '{description} (running {task})'.format(task=task, description=task.description)
|
||||
else:
|
||||
record.msg = 'Running {task}'.format(task=task)
|
||||
message = super(FileFormatter, self).format(record)
|
||||
record.msg = task
|
||||
else:
|
||||
message = super(FileFormatter, self).format(record)
|
||||
return message
|
||||
|
|
|
@ -1,3 +1,5 @@
|
|||
import logging
|
||||
log = logging.getLogger(__name__)
|
||||
|
||||
|
||||
def load_manifest(path):
|
||||
|
@ -28,5 +30,7 @@ class Manifest(object):
|
|||
self.loaded_plugins = []
|
||||
for modname in self.plugins.keys():
|
||||
if self.plugins[modname]['enabled']:
|
||||
plugin = __import__('plugins.{module}'.format(module=modname), fromlist=['plugins'])
|
||||
plugin_name = 'plugins.{module}'.format(module=modname)
|
||||
plugin = __import__(plugin_name, fromlist=['plugins'])
|
||||
log.debug('Loaded plugin %s', plugin_name)
|
||||
self.loaded_plugins.append(plugin)
|
||||
|
|
|
@ -1,7 +1,7 @@
|
|||
|
||||
|
||||
class Task(object):
|
||||
name = None
|
||||
description = None
|
||||
|
||||
def __init__(self):
|
||||
pass
|
||||
|
@ -10,7 +10,4 @@ class Task(object):
|
|||
pass
|
||||
|
||||
def __str__(self):
|
||||
if self.name is None:
|
||||
return '{module}.{task}'.format(module=self.__module__, task=self.__class__.__name__)
|
||||
else:
|
||||
return self.name
|
||||
return '{module}.{task}'.format(module=self.__module__, task=self.__class__.__name__)
|
||||
|
|
|
@ -1,3 +1,5 @@
|
|||
import logging
|
||||
log = logging.getLogger(__name__)
|
||||
|
||||
|
||||
class TaskList(list):
|
||||
|
@ -8,18 +10,25 @@ class TaskList(list):
|
|||
|
||||
def run(self, bootstrap_info):
|
||||
for task in self:
|
||||
print('Running {taskname}'.format(taskname=task))
|
||||
log.info(task)
|
||||
task.run(bootstrap_info)
|
||||
|
||||
def before(self, ref, task):
|
||||
log.debug('Inserting %s before %s.%s', task, ref.__module__, ref.__name__)
|
||||
i = next(i for i, task in enumerate(self) if type(task) is ref)
|
||||
self.insert(i, task)
|
||||
|
||||
def replace(self, ref, task):
|
||||
log.debug('Replacing %s.%s with %s', ref.__module__, ref.__name__, task)
|
||||
i = next(i for i, task in enumerate(self) if type(task) is ref)
|
||||
self.pop(i)
|
||||
self.insert(i, task)
|
||||
|
||||
def after(self, ref, task):
|
||||
log.debug('Inserting %s after %s.%s', task, ref.__module__, ref.__name__)
|
||||
i = next(i for i, task in enumerate(self) if type(task) is ref)
|
||||
self.insert(i+1, task)
|
||||
|
||||
def append(self, task):
|
||||
super(TaskList, self).append(task)
|
||||
log.debug('Appending %s', task)
|
||||
|
|
2
logs/.gitignore
vendored
Normal file
2
logs/.gitignore
vendored
Normal file
|
@ -0,0 +1,2 @@
|
|||
*
|
||||
!.gitignore
|
|
@ -2,6 +2,8 @@ from base import Task
|
|||
|
||||
|
||||
class AddSudoPackage(Task):
|
||||
description = 'Adding ``sudo\'\' to the image packages'
|
||||
|
||||
def run(self, info):
|
||||
super(AddSudoPackage, self).run(info)
|
||||
info.img_packages[0].add('sudo')
|
||||
|
|
|
@ -2,6 +2,8 @@ from base import Task
|
|||
|
||||
|
||||
class PrintInfo(Task):
|
||||
description = 'Printing `info\' to the console'
|
||||
|
||||
def run(self, info):
|
||||
super(PrintInfo, self).run(info)
|
||||
print('info')
|
||||
|
|
|
@ -2,6 +2,8 @@ from base import Task
|
|||
|
||||
|
||||
class GetCredentials(Task):
|
||||
description = 'Getting AWS credentials'
|
||||
|
||||
def run(self, info):
|
||||
super(GetCredentials, self).run(info)
|
||||
info.ec2_credentials = self.get_ec2_credentials(info.manifest)
|
||||
|
@ -25,6 +27,8 @@ class GetCredentials(Task):
|
|||
|
||||
|
||||
class Connect(Task):
|
||||
description = 'Connecting to EC2'
|
||||
|
||||
def run(self, info):
|
||||
super(Connect, self).run(info)
|
||||
# import boto.ec2
|
||||
|
|
|
@ -2,6 +2,8 @@ from base import Task
|
|||
|
||||
|
||||
class GetInfo(Task):
|
||||
description = 'Retrieving host information'
|
||||
|
||||
def run(self, info):
|
||||
super(GetInfo, self).run(info)
|
||||
# import urllib2
|
||||
|
@ -12,6 +14,8 @@ class GetInfo(Task):
|
|||
|
||||
|
||||
class InstallPackages(Task):
|
||||
description = 'Installing host packages'
|
||||
|
||||
def run(self, info):
|
||||
# Check if packages are installed with
|
||||
# /usr/bin/dpkg -s ${name} | grep -q 'Status: install'
|
||||
|
|
|
@ -2,6 +2,8 @@ from base import Task
|
|||
|
||||
|
||||
class HostPackages(Task):
|
||||
description = 'Determining required host packages'
|
||||
|
||||
def run(self, info):
|
||||
super(HostPackages, self).run(info)
|
||||
packages = set(['debootstrap',
|
||||
|
@ -15,6 +17,8 @@ class HostPackages(Task):
|
|||
|
||||
|
||||
class ImagePackages(Task):
|
||||
description = 'Determining required image packages'
|
||||
|
||||
def run(self, info):
|
||||
super(ImagePackages, self).run(info)
|
||||
manifest = info.manifest
|
||||
|
|
Loading…
Add table
Reference in a new issue