#!/usr/bin/env python
# -*- coding: utf-8 -*-
#
##########################################################################
# creole.containers - management of LXC containers
# Copyright © 2012,2013 Pôle de compétences EOLE <eole@ac-dijon.fr>
#
# License CeCILL:
#  * in french: http://www.cecill.info/licences/Licence_CeCILL_V2-fr.html
#  * in english http://www.cecill.info/licences/Licence_CeCILL_V2-en.html
##########################################################################

"""Generate LXC containers

`gen_conteneurs` generate the base LXC chroot and instanciate basic
LXC configuration files.

"""

import sys
import argparse
import traceback

from pyeole import scriptargs
from pyeole.log import init_logging

from creole.reconfigure import containers


def parse_cmdline():
    """Parse commande line.
    """
    parser = argparse.ArgumentParser(description=u"Génération et configuration"
                                     u" des conteneurs LXC",
                                     parents=[scriptargs.container(),
                                              scriptargs.logging()])
    opts = parser.parse_args()

    if opts.verbose:
        opts.log_level = 'info'
    if opts.debug:
        opts.log_level = 'debug'

    return opts


def main():
    """Setup environnment and run LXC generation.
    """

    options = parse_cmdline()
    try:
        log = init_logging(level=options.log_level)
        containers()
    except StandardError, err:
        if options.debug:
            log.debug(traceback.format_exc())
        else:
            log.error(err)
        sys.exit(1)
    sys.exit(0)

if __name__ == '__main__':
    main()
