2013-06-09 20:29:54 +02:00
|
|
|
import logging
|
2013-05-16 08:00:28 +02:00
|
|
|
|
|
|
|
|
|
|
|
def main():
|
2013-06-09 20:29:54 +02:00
|
|
|
args = get_args()
|
|
|
|
setup_logger(args)
|
|
|
|
run(args)
|
|
|
|
|
|
|
|
|
|
|
|
def get_args():
|
2013-05-16 08:00:28 +02:00
|
|
|
from argparse import ArgumentParser
|
|
|
|
parser = ArgumentParser(description='Bootstrap Debian for the cloud.')
|
|
|
|
parser.add_argument('--debug', action='store_true',
|
|
|
|
help='Print debugging information')
|
2013-06-09 15:50:00 +02:00
|
|
|
parser.add_argument('manifest', help='Manifest file to use for bootstrapping', metavar='MANIFEST')
|
2013-06-09 20:29:54 +02:00
|
|
|
return parser.parse_args()
|
|
|
|
|
2013-05-16 08:00:28 +02:00
|
|
|
|
2013-06-09 20:29:54 +02:00
|
|
|
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)
|
2013-05-16 08:00:28 +02:00
|
|
|
|
|
|
|
|
|
|
|
def run(args):
|
2013-06-09 15:50:00 +02:00
|
|
|
from manifest import load_manifest
|
2013-06-23 12:00:17 +02:00
|
|
|
(provider, manifest) = load_manifest(args.manifest)
|
2013-05-16 08:00:28 +02:00
|
|
|
|
2013-06-09 16:23:08 +02:00
|
|
|
from tasklist import TaskList
|
|
|
|
tasklist = TaskList()
|
2013-06-23 15:26:08 +02:00
|
|
|
provider.tasks(tasklist, manifest)
|
|
|
|
for plugin in manifest.loaded_plugins:
|
|
|
|
plugin.tasks(tasklist, manifest)
|
2013-05-16 08:00:28 +02:00
|
|
|
|
2013-06-09 16:15:23 +02:00
|
|
|
from bootstrapinfo import BootstrapInformation
|
2013-06-09 15:50:00 +02:00
|
|
|
bootstrap_info = BootstrapInformation(manifest=manifest, debug=args.debug)
|
2013-05-16 08:00:28 +02:00
|
|
|
tasklist.run(bootstrap_info)
|
2013-06-09 20:29:54 +02:00
|
|
|
|
|
|
|
|
|
|
|
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
|