Exemplo n.º 1
0
    def __init__(self, config_path=None, name=None):
        self.log = logger.getlogger()
        self.cfg = Config(config_path)

        self.cont_package_path = gen.get_container_package_path()
        self.cont_id_file = gen.get_container_id_file()
        self.cont_venv_path = gen.get_container_venv_path()
        self.cont_scripts_path = gen.get_container_scripts_path()
        self.cont_python_path = gen.get_container_python_path()
        self.cont_os_images_path = gen.get_container_os_images_path()
        self.cont_playbooks_path = gen.get_container_playbooks_path()
        self.depl_package_path = gen.get_package_path()
        self.depl_python_path = gen.get_python_path()
        self.depl_playbooks_path = gen.get_playbooks_path()

        if name is True or name is None:
            for vlan in self.cfg.yield_depl_netw_client_vlan('pxe'):
                break
            self.name = '{}-pxe{}'.format(self.DEFAULT_CONTAINER_NAME, vlan)
        else:
            self.name = name

        self.client = docker.from_env()

        try:
            self.image = self.client.images.get('power-up')
        except docker.errors.ImageNotFound:
            self.image = None

        try:
            self.cont = self.client.containers.get(self.name)
        except docker.errors.NotFound:
            self.cont = None
Exemplo n.º 2
0
 def build_image(self):
     self.log.info("Building Docker image "
                   f"'{self.DEFAULT_CONTAINER_NAME}'")
     try:
         self.image, build_logs = self.client.images.build(
             path=gen.get_package_path(),
             tag=self.DEFAULT_CONTAINER_NAME,
             rm=True)
     except docker.errors.APIError as exc:
         msg = ("Failed to create image "
                f"'{self.DEFAULT_CONTAINER_NAME}': {exc}")
         self.log.error(msg)
         raise UserException(msg)
     self.log.debug("Created image "
                    f"'{self.DEFAULT_CONTAINER_NAME}'")
Exemplo n.º 3
0
 def build_image(self):
     repo_name = self.DEFAULT_CONTAINER_NAME
     dockerfile_tag = sha1sum(self.depl_dockerfile_path)
     tag = f"{repo_name}:{dockerfile_tag}"
     try:
         self.client.images.get(tag)
         self.log.info(f"Using existing Docker image '{tag}'")
     except docker.errors.ImageNotFound:
         self.log.info(f"Building Docker image '{repo_name}'")
         try:
             self.image, build_logs = self.client.images.build(
                 path=gen.get_package_path(), tag=tag, rm=True)
         except docker.errors.APIError as exc:
             msg = ("Failed to create image "
                    f"'{self.DEFAULT_CONTAINER_NAME}': {exc}")
             self.log.error(msg)
             raise UserException(msg)
         self.log.debug("Created image " f"'{self.DEFAULT_CONTAINER_NAME}'")
     return tag
Exemplo n.º 4
0
    def __init__(self, config_path=None, name=None):
        self.log = logger.getlogger()
        self.cfg = Config(config_path)

        self.cont_package_path = gen.get_container_package_path()
        self.cont_id_file = gen.get_container_id_file()
        self.cont_venv_path = gen.get_container_venv_path()
        self.cont_scripts_path = gen.get_container_scripts_path()
        self.cont_python_path = gen.get_container_python_path()
        self.cont_os_images_path = gen.get_container_os_images_path()
        self.cont_playbooks_path = gen.get_container_playbooks_path()
        self.depl_package_path = gen.get_package_path()
        self.depl_python_path = gen.get_python_path()
        self.depl_playbooks_path = gen.get_playbooks_path()

        self.cont_ini = os.path.join(self.depl_package_path, 'container.ini')
        self.rootfs = self.ROOTFS

        # Check if architecture is supported
        arch = platform.machine()
        if arch not in self.ARCHITECTURE.keys():
            msg = "Unsupported architecture '{}'".format(arch)
            self.log.error(msg)
            raise UserException(msg)
        self.rootfs.arch = self.ARCHITECTURE[arch]

        if name is True or name is None:
            for vlan in self.cfg.yield_depl_netw_client_vlan('pxe'):
                break
            self.name = '{}-pxe{}'.format(self.DEFAULT_CONTAINER_NAME, vlan)
        else:
            self.name = name
        self.cont = lxc.Container(self.name)
        # Get a file descriptor for stdout
        self.fd = open(os.path.join(gen.GEN_LOGS_PATH,
                                    self.name + '.stdout.log'), 'w')
Exemplo n.º 5
0
from enum import Enum

import lib.genesis as gen


class LogLevel(Enum):
    NOLOG = 'nolog'
    DEBUG = 'debug'
    INFO = 'info'
    WARNING = 'warning'
    ERROR = 'error'
    CRITICAL = 'critical'


LOG_NAME = 'gen'
LOG_PATH = os.path.join(gen.get_package_path(), 'logs')
LOG_FILE = os.path.join(LOG_PATH, 'gen')

GEN_LOG_LEVEL_FILE = 'GEN_LOG_LEVEL_FILE'
GEN_LOG_LEVEL_PRINT = 'GEN_LOG_LEVEL_PRINT'

DEFAULT_LOG_LEVEL = getattr(logging, LogLevel.DEBUG.name)
DEFAULT_FILE_HANDLER_LEVEL = LogLevel.DEBUG.value
DEFAULT_STREAM_HANDLER_LEVEL = LogLevel.DEBUG.value

FORMAT_FILE = ('%(asctime)s' ' - %(levelname)s' ' - %(message)s')
FORMAT_STREAM = ('%(levelname)s' ' - %(message)s')
FORMAT_DEBUG = ('%(asctime)s'
                ' - %(filename)s|%(funcName)s|%(lineno)d'
                ' - %(levelname)s'
                ' - %(message)s')