diff options
author | Radostin Stoyanov <rstoyanov1@gmail.com> | 2017-06-29 18:44:32 +0100 |
---|---|---|
committer | Cédric Bosdonnat <cbosdonnat@suse.com> | 2017-07-01 07:42:49 +0200 |
commit | 1dd194f36079fd822f667419e030fbd07a8480b9 (patch) | |
tree | d6f77ef711135c4489cbcceb2b31f9269a3fc9c0 /src | |
parent | 96109db4bd08ffc1e1dcca6f87994761642ce413 (diff) | |
download | virt-bootstrap.git-1dd194f36079fd822f667419e030fbd07a8480b9.tar.gz virt-bootstrap.git-1dd194f36079fd822f667419e030fbd07a8480b9.tar.xz virt-bootstrap.git-1dd194f36079fd822f667419e030fbd07a8480b9.zip |
Set logger name/format/level
This patch aims to set logger name for the virtBootstrap module.
Set logging format to: %(levelname)s: %(message)s
and use an elegant way of setting log level.
Diffstat (limited to 'src')
-rw-r--r-- | src/virtBootstrap/sources.py | 43 | ||||
-rwxr-xr-x | src/virtBootstrap/virt_bootstrap.py | 45 |
2 files changed, 60 insertions, 28 deletions
diff --git a/src/virtBootstrap/sources.py b/src/virtBootstrap/sources.py index 650e45c..617adc6 100644 --- a/src/virtBootstrap/sources.py +++ b/src/virtBootstrap/sources.py @@ -29,6 +29,11 @@ import os import logging from subprocess import CalledProcessError, PIPE, Popen + +# pylint: disable=invalid-name +# Create logger +logger = logging.getLogger(__name__) + # Default virtual size of qcow2 image DEF_QCOW2_SIZE = '5G' if os.geteuid() == 0: @@ -61,15 +66,15 @@ def execute(cmd): Execute command and log debug message. """ cmd_str = ' '.join(cmd) - logging.debug("Call command:\n%s", cmd_str) + logger.debug("Call command:\n%s", cmd_str) proc = Popen(cmd, stdout=PIPE, stderr=PIPE) output, err = proc.communicate() if output: - logging.debug("Stdout:\n%s", output) + logger.debug("Stdout:\n%s", output) if err: - logging.debug("Stderr:\n%s", err) + logger.debug("Stderr:\n%s", err) if proc.returncode != 0: raise CalledProcessError(proc.returncode, cmd_str) @@ -103,11 +108,11 @@ def untar_layers(layers_list, image_dir, dest_dir): Untar each of layers from container image. """ for index, layer in enumerate(layers_list): - logging.info("Extracting layer (%s/%s)", index+1, len(layers_list)) + logger.info("Extracting layer (%s/%s)", index+1, len(layers_list)) sum_type, sum_value, layer_file = get_layer_info(layer['digest'], image_dir) - logging.debug('Untar layer file: (%s) %s', sum_type, layer_file) + logger.debug('Untar layer file: (%s) %s', sum_type, layer_file) # Verify the checksum if not checksum(layer_file, sum_type, sum_value): @@ -132,10 +137,10 @@ def create_qcow2(tar_file, layer_file, backing_file=None, size=DEF_QCOW2_SIZE): qemu_img_cmd = ["qemu-img", "create", "-f", "qcow2", layer_file, size] if not backing_file: - logging.info("Creating base qcow2 image") + logger.info("Creating base qcow2 image") execute(qemu_img_cmd) - logging.info("Formatting qcow2 image") + logger.info("Formatting qcow2 image") execute(['virt-format', '--format=qcow2', '--partition=none', @@ -146,12 +151,12 @@ def create_qcow2(tar_file, layer_file, backing_file=None, size=DEF_QCOW2_SIZE): qemu_img_cmd.insert(2, "-b") qemu_img_cmd.insert(3, backing_file) - logging.info("Creating qcow2 image with backing chain") + logger.info("Creating qcow2 image with backing chain") execute(qemu_img_cmd) # Get mime type of archive mime_tar_file = get_mime_type(tar_file) - logging.debug("Detected mime type of archive: %s", mime_tar_file) + logger.debug("Detected mime type of archive: %s", mime_tar_file) # Extract tarball using "tar-in" command from libguestfs tar_in_cmd = ["guestfish", @@ -182,13 +187,13 @@ def extract_layers_in_qcow2(layers_list, image_dir, dest_dir): qcow2_backing_file = None for index, layer in enumerate(layers_list): - logging.info("Extracting layer (%s/%s)", index+1, len(layers_list)) + logger.info("Extracting layer (%s/%s)", index+1, len(layers_list)) # Get layer file information sum_type, sum_value, tar_file = get_layer_info(layer['digest'], image_dir) - logging.debug('Untar layer file: (%s) %s', sum_type, tar_file) + logger.debug('Untar layer file: (%s) %s', sum_type, tar_file) # Verify the checksum if not checksum(tar_file, sum_type, sum_value): @@ -221,7 +226,7 @@ class FileSource(object): raise Exception('Invalid file source "%s"' % self.path) if self.output_format == 'dir': - logging.info("Extracting files into destination directory") + logger.info("Extracting files into destination directory") safe_untar(self.path, dest) elif self.output_format == 'qcow2': @@ -230,13 +235,13 @@ class FileSource(object): qcow2_file = os.path.realpath('{}/{}.qcow2'.format(dest, file_name)) - logging.info("Extracting files into qcow2 image") + logger.info("Extracting files into qcow2 image") create_qcow2(self.path, qcow2_file) else: raise Exception("Unknown format:" + self.output_format) - logging.info("Extraction completed successfully!") - logging.info("Files are stored in: " + dest) + logger.info("Extraction completed successfully!") + logger.info("Files are stored in: " + dest) class DockerSource(object): @@ -309,10 +314,10 @@ class DockerSource(object): # Reference: # https://github.com/containers/image/blob/master/image/oci.go#L100 if self.output_format == 'dir': - logging.info("Extracting container layers") + logger.info("Extracting container layers") untar_layers(manifest['layers'], images_dir, dest) elif self.output_format == 'qcow2': - logging.info("Extracting container layers into qcow2 images") + logger.info("Extracting container layers into qcow2 images") extract_layers_in_qcow2(manifest['layers'], images_dir, dest) else: raise Exception("Unknown format:" + self.output_format) @@ -321,8 +326,8 @@ class DockerSource(object): raise else: - logging.info("Download and extract completed!") - logging.info("Files are stored in: " + dest) + logger.info("Download and extract completed!") + logger.info("Files are stored in: " + dest) finally: # Clean up diff --git a/src/virtBootstrap/virt_bootstrap.py b/src/virtBootstrap/virt_bootstrap.py index 90379cc..681e50d 100755 --- a/src/virtBootstrap/virt_bootstrap.py +++ b/src/virtBootstrap/virt_bootstrap.py @@ -23,10 +23,10 @@ and calls corresponding methods on appropriate object. import argparse import gettext +import logging import sys import os from textwrap import dedent -from logging import getLogger, DEBUG, INFO, WARNING, error from subprocess import CalledProcessError, Popen, PIPE try: from urlparse import urlparse @@ -51,6 +51,10 @@ except IOError: import builtin builtin.__dict__['_'] = str +# pylint: disable=invalid-name +# Create logger +logger = logging.getLogger(__name__) + def get_source(args): """ @@ -86,18 +90,16 @@ def bootstrap(args): """ Get source object and call unpack method """ - # Set log level - logger = getLogger() - logger.setLevel(DEBUG if args.debug else WARNING if args.quiet else INFO) source = get_source(args) if not os.path.exists(args.dest): os.makedirs(args.dest) elif not os.path.isdir(args.dest): # Show error if not directory - error("Destination path '%s' is not directory.", args.dest) + logger.error("Destination path '%s' is not directory.", args.dest) sys.exit(1) elif not os.access(args.dest, os.W_OK): # Check write permissions - error("No write permissions on destination path '%s'", args.dest) + logger.error("No write permissions on destination path '%s'", + args.dest) sys.exit(1) source.unpack(args.dest) @@ -106,6 +108,27 @@ def bootstrap(args): set_root_password(args.dest, args.root_password) +def set_logging_conf(loglevel=None): + """ + Set format and logging level + """ + # Get logger + module_logger = logging.getLogger('virtBootstrap') + + # Create console handler + console_handler = logging.StreamHandler() + + # Set logging format + log_format = ('%(levelname)-8s: %(message)s') + console_handler.setFormatter(logging.Formatter(log_format)) + + # Add the handlers to logger + module_logger.addHandler(console_handler) + + # Set logging level + module_logger.setLevel(loglevel or logging.INFO) + + def main(): parser = argparse.ArgumentParser( description=_("Container bootstrapping tool"), @@ -140,9 +163,10 @@ def main(): parser.add_argument("-f", "--format", default='dir', choices=['dir', 'qcow2'], help=_("Format to be used for the root filesystem")) - parser.add_argument("-d", "--debug", action="store_true", - help=_("Show debug messages")) - parser.add_argument("-q", "--quiet", action="store_true", + parser.add_argument("-d", "--debug", action="store_const", dest="loglevel", + const=logging.DEBUG, help=_("Show debug messages")) + parser.add_argument("-q", "--quiet", action="store_const", dest="loglevel", + const=logging.WARNING, help=_("Suppresses messages notifying about" "current state or actions of virt-bootstrap")) # TODO add UID / GID mapping parameters @@ -150,6 +174,9 @@ def main(): try: args = parser.parse_args() + # Configure logging lovel/format + set_logging_conf(args.loglevel) + # do the job here! bootstrap(args) |