Ejemplo n.º 1
0
Archivo: core.py Proyecto: m1-key/tern
def execute_base(layer_obj, prereqs):
    """Given an ImageLayer object, find packages installed in the layer
    using the default method. The prereqisites required for this to work:
        prereqs.shell: the shell to use
        prereqs.binary: the binary to look up in the command library
        optional prerequisites:
        prereqs.envs: any environment variables to set before execution

        1. Use command_lib's base to look up the binary to see if there
           is a method to retrieve the metadata
        2. If there is, invoke the scripts in a chroot environment and
           process the results
        3. Add the results to the ImageLayer object

    It is assumed that the filesystem is prepped for execution by mounting
    the filesystem in the working directory and /proc, /sys and /dev device
    nodes are mounted"""
    # Add notices to this layer object
    origin_layer = 'Layer {}'.format(layer_obj.layer_index)
    # find the binary listing
    listing = command_lib.get_base_listing(prereqs.binary)
    if listing:
        # put info notice about what is going to be invoked
        snippet_msg = (formats.invoke_for_base + '\n' +
                       content.print_base_invoke(prereqs.binary))
        layer_obj.origins.add_notice_to_origins(origin_layer,
                                                Notice(snippet_msg, 'info'))
        # get list of metadata by invoking scripts in chroot
        logger.debug("Collecting metadata for image layer...")
        pkg_dict, invoke_msg, warnings = collect.collect_list_metadata(
            prereqs.shell, listing, layer_obj.get_layer_workdir(),
            prereqs.envs)
        # more processing for debian copyrights to get licenses
        if listing.get("pkg_format") == "deb":
            logger.debug("Processing Debian copyrights...")
            pkg_dict["pkg_licenses"] = com.get_deb_package_licenses(
                pkg_dict["copyrights"])
        # add any errors and warnings to the layer's origins object
        if invoke_msg:
            logger.error(
                "Script invocation error. Unable to collect some metadata")
            layer_obj.origins.add_notice_to_origins(
                origin_layer, Notice(invoke_msg, 'error'))
        if warnings:
            logger.warning("Some metadata may be missing")
            layer_obj.origins.add_notice_to_origins(
                origin_layer, Notice(warnings, 'warning'))
        # bundle the results into Package objects
        bundle.fill_pkg_results(layer_obj, pkg_dict)
        # remove extra FileData objects from the layer
        com.remove_duplicate_layer_files(layer_obj)
    # if there is no listing add a notice
    else:
        layer_obj.origins.add_notice_to_origins(
            origin_layer,
            Notice(
                errors.no_listing_for_base_key.format(
                    listing_key=prereqs.binary), 'error'))
Ejemplo n.º 2
0
def print_base_invoke(key):
    '''Given the key in the base library, return a string containing
    the command_lib/base.yml'''
    info = command_lib.get_base_listing(key)
    report = ''
    for item in command_lib.base_keys:
        if item in info.keys():
            report = report + print_invoke_list(info, item)
    report = report + '\n'
    return report
Ejemplo n.º 3
0
Archivo: run.py Proyecto: nishakm/tern
def fill_packages(layer, prereqs):
    """Collect package metadata and fill in the packages for the given layer
    object"""
    # Create an origin string to record notices
    origin_str = "Inventory Results"
    # For every indicator that exists on the filesystem, inventory the packages
    for binary in dcom.get_existing_bins(prereqs.host_path):
        set_layer_os(layer, origin_str, binary)
        prereqs.binary = binary
        listing = command_lib.get_base_listing(binary)
        pkg_dict, invoke_msg, warnings = collect.collect_list_metadata(
            listing, prereqs, True)
        # processing for debian copyrights
        if listing.get("pkg_format") == "deb":
            pkg_dict["pkg_licenses"] = com.get_deb_package_licenses(
                pkg_dict["copyrights"])
        if invoke_msg:
            logger.error("Script invocation error. Unable to collect some"
                         "metadata.")
        if warnings:
            logger.warning("Some metadata may be missing.")
        bundle.fill_pkg_results(layer, pkg_dict)
        com.remove_duplicate_layer_files(layer)