From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mails.dpdk.org (mails.dpdk.org [217.70.189.124]) by inbox.dpdk.org (Postfix) with ESMTP id DE43EA0542 for ; Mon, 24 Oct 2022 19:44:05 +0200 (CEST) Received: from [217.70.189.124] (localhost [127.0.0.1]) by mails.dpdk.org (Postfix) with ESMTP id 76B8842BD6; Mon, 24 Oct 2022 19:44:05 +0200 (CEST) Received: from mail-oi1-f225.google.com (mail-oi1-f225.google.com [209.85.167.225]) by mails.dpdk.org (Postfix) with ESMTP id 6423641143 for ; Mon, 24 Oct 2022 19:44:03 +0200 (CEST) Received: by mail-oi1-f225.google.com with SMTP id p127so11587295oih.9 for ; Mon, 24 Oct 2022 10:44:03 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=iol.unh.edu; s=unh-iol; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=QyrE7JAVFQjMBW3L1Nng9z+aX7HY/xo1u86qRSMm7yo=; b=MmtoAUiYoalOTOvWNUJPbGUtiwSHSUyZRoylz9cJchrynBUKDd1MUBtd6My73kwI5c UyTi6otj1DILUG3qeJ4qDghq8sEvKI9Udy55/91CPRspUOi6BUqm9ywG2GDoAVwRXNHb rVnUPeIDhNeJf513JIQ/Vyvl2xpLWKaNE8/98= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=QyrE7JAVFQjMBW3L1Nng9z+aX7HY/xo1u86qRSMm7yo=; b=SoxBb+F1o30HWOztRBfTlY4wraQMLmggSS0El1UaRoyvzPRoXnN8RGxZDkwoyKBFoL 5Avcn6L6xQRRzGI/Vbt4/ay1a0/cZi7cNFk698R1+tJH4VYR3laHA4AjK5jEnHD34kBy XyrJ+C2QXj+uyRz7D8+vY+HQbUMcM/zFpdMHayFFYTeF02YEsZMGlMCwM0iEgw9UX3H2 eQ9oFedIGltvGv3y8omVNoj49r/44BhDxvCIKU1GU9Bjql7LR3ozxMPvo/FB68I//nOx c506kvXTiDGW7P0323d2K02u3E5ea5XfJCmu92KdTiMWvAJ+SBV3eA10hvskKMIR58uw EC6w== X-Gm-Message-State: ACrzQf1yr24Op9COEGzUGbvcvr6ddpGVQAr9KPX3RU/DPwtEhNvQ59AP okKKfal3PT1HzsKJRGgfeuc+8EVxTWd4Rt4p68CfSko0aXtg6HmrcgiYhaipdDlshz+P8i4lCQ4 qOCagy5C6HvMt4sDndNW/SOkGUDmD2o7u4DVRoWMFxWN6hpaOzb6ZZd/bgSRTEgKvml+fnDwx6i n+aVz2rvGO3Pau X-Google-Smtp-Source: AMsMyM6qreqGIhVq4vbd0pmN2gP4lZw62/VZLdJyIkyGpPrB1Z67Vdq4O4E46/GU9CujohB6ndJkF6imXj/O X-Received: by 2002:a05:6808:16a1:b0:353:b8d9:c277 with SMTP id bb33-20020a05680816a100b00353b8d9c277mr31949628oib.204.1666633442760; Mon, 24 Oct 2022 10:44:02 -0700 (PDT) Received: from postal.iol.unh.edu (postal.iol.unh.edu. [2606:4100:3880:1234::84]) by smtp-relay.gmail.com with ESMTPS id u5-20020a4a6145000000b004808506c4f9sm35760ooe.3.2022.10.24.10.44.02 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Mon, 24 Oct 2022 10:44:02 -0700 (PDT) X-Relaying-Domain: iol.unh.edu Received: from iol.unh.edu (unknown [IPv6:2606:4100:3880:1220:579a:6d00:3172:e0f9]) by postal.iol.unh.edu (Postfix) with ESMTP id 217F5605CF44; Mon, 24 Oct 2022 13:44:01 -0400 (EDT) From: ohilyard@iol.unh.edu To: ci@dpdk.org Cc: aconole@redhat.com, alialnu@nvidia.com, Owen Hilyard Subject: [PATCH v3 3/6] containers/builder: Dockerfile creation script Date: Mon, 24 Oct 2022 13:43:44 -0400 Message-Id: <20221024174345.15402-4-ohilyard@iol.unh.edu> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20221024174345.15402-1-ohilyard@iol.unh.edu> References: <20221024174345.15402-1-ohilyard@iol.unh.edu> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-BeenThere: ci@dpdk.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: DPDK CI discussions List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: ci-bounces@dpdk.org From: Owen Hilyard This script will template out all of the Dockerfiles based on the definitions provided in the inventory using the jinja2 templating library. Signed-off-by: Owen Hilyard --- containers/template_engine/make_dockerfile.py | 240 ++++++++++++++++++ 1 file changed, 240 insertions(+) create mode 100755 containers/template_engine/make_dockerfile.py diff --git a/containers/template_engine/make_dockerfile.py b/containers/template_engine/make_dockerfile.py new file mode 100755 index 0000000..9a3c19b --- /dev/null +++ b/containers/template_engine/make_dockerfile.py @@ -0,0 +1,240 @@ +#!/usr/bin/env python3 +# SPDX-License-Identifier: BSD-3-Clause +# Copyright (c) 2022 University of New Hampshire +import argparse +import json +import logging +import os +from dataclasses import dataclass +from datetime import datetime +from typing import Any, Dict, List, Optional + +import jsonschema +import yaml +from jinja2 import Environment, FileSystemLoader, select_autoescape + + +@dataclass(frozen=True) +class Options: + on_rhel: bool + fail_on_unbuildable: bool + build_libabigail: bool + build_abi: bool + output_dir: str + registry_hostname: str + + +def _get_arg_parser() -> argparse.ArgumentParser: + parser = argparse.ArgumentParser(description="Makes the dockerfile") + parser.add_argument("--output-dir", required=True) + parser.add_argument( + "--rhel", + action="store_true", + help="Overwrite the check for running on RHEL", + default=False, + ) + parser.add_argument( + "--fail-on-unbuildable", + action="store_true", + help="If any container would not be possible to build, fail and exit with a non-zero exit code.", + default=False, + ) + parser.add_argument( + "--build-abi", + action="store_true", + help="Whether to build the ABI references into the image. Disabled by \ + default due to producing 10+ GB images. \ + Implies '--build-libabigail'.", + ) + parser.add_argument( + "--build-libabigail", + action="store_true", + help="Whether to build libabigail from source for distros that do not \ + package it. Implied by '--build-abi'", + ) + return parser + + +def parse_args() -> Options: + parser = _get_arg_parser() + args = parser.parse_args() + + registry_hostname = ( + os.environ.get("DPDK_CI_CONTAINERS_REGISTRY_HOSTNAME") or "localhost" + ) + + # In order to to build the ABIs, libabigail must be built from source on + # some platforms + build_libabigail: bool = args.build_libabigail or args.build_abi + + opts = Options( + on_rhel=args.rhel, + fail_on_unbuildable=args.fail_on_unbuildable, + build_libabigail=build_libabigail, + build_abi=args.build_abi, + output_dir=args.output_dir, + registry_hostname=registry_hostname, + ) + logging.info(f"make_dockerfile.py options: {opts}") + return opts + + +def running_on_RHEL(options: Options) -> bool: + """ + RHEL containers can only be built on RHEL, so disable them and emit a + warning if not on RHEL. + """ + redhat_release_path = "/etc/redhat-release" + + if os.path.exists(redhat_release_path): + with open(redhat_release_path) as f: + first_line = f.readline() + on_rhel = "Red Hat Enterprise Linux" in first_line + if on_rhel: + logging.info("Running on RHEL, allowing RHEL containers") + return True + + logging.warning("Not on RHEL, disabling RHEL containers") + assert options is not None, "Internal state error, OPTIONS should not be None" + + if options.on_rhel: + logging.info("Override enabled, enabling RHEL containers") + + return options.on_rhel + + +def get_path_to_parent_directory() -> str: + return os.path.dirname(__file__) + + +def get_raw_inventory(): + parent_dir = get_path_to_parent_directory() + + schema_path = os.path.join(parent_dir, "inventory_schema.json") + inventory_path = os.path.join(parent_dir, "inventory.yaml") + + inventory: Dict[str, Any] + with open(inventory_path, "r") as f: + inventory = yaml.safe_load(f) + + schema: Dict[str, Any] + with open(schema_path, "r") as f: + schema = json.load(f) + + jsonschema.validate(instance=inventory, schema=schema) + return inventory + + +def apply_group_config_to_target( + target: Dict[str, Any], + raw_inventory: Dict[str, Any], + on_rhel: bool, + fail_on_unbuildable: bool, +) -> Optional[Dict[str, Any]]: + groups_for_target: List[Dict[str, Any]] = [] + groups: List[Dict[str, Any]] = raw_inventory["dockerfiles"]["groups"] + group = groups[target["group"]] + + target_primary_group = target["group"] + + assert isinstance(target_primary_group, str), "Target group name was not a string" + + requires_rhel = "rhel" in target_primary_group.lower() + + if requires_rhel and not on_rhel: + logging.warning( + f"Disabling target {target['name']}, because it must be built on RHEL." + ) + if fail_on_unbuildable: + raise AssertionError( + f"Not on RHEL and target {target['name']} must be built on RHEL" + ) + + return None + + while group["parent"] != "NONE": + groups_for_target.append(group) + group = groups[group["parent"]] + + groups_for_target.append(group) # add the "all" group + groups_for_target.reverse() # reverse it so overrides work + + target_packages: List[str] = target.get("packages") or [] + + for group in groups_for_target: + target_packages = [*target_packages, *(group.get("packages") or [])] + target = dict(target, **group) + + target["packages"] = target_packages + + return target + + +def get_processed_inventory(options: Options) -> Dict[str, Any]: + raw_inventory: Dict[str, Any] = get_raw_inventory() + on_rhel = running_on_RHEL(options) + targets = raw_inventory["dockerfiles"]["targets"] + targets = [ + apply_group_config_to_target( + target, raw_inventory, on_rhel, options.fail_on_unbuildable + ) + for target in targets + ] + # remove disabled options + targets = [target for target in targets if target is not None] + raw_inventory["dockerfiles"]["targets"] = targets + + return raw_inventory + + +def main(): + options: Options = parse_args() + + env = Environment( + loader=FileSystemLoader("templates"), + ) + + inventory = get_processed_inventory(options) + + timestamp = datetime.now().strftime("%Y-%m-%d") + + for target in inventory["dockerfiles"]["targets"]: + template = env.get_template(f"containers/{target['group']}.dockerfile.j2") + dockerfile_location = os.path.join( + options.output_dir, target["name"] + ".dockerfile" + ) + + tags: list[str] = target.get("extra_tags") or [] + tags.insert(0, "$R/$N:latest") + tags.insert(1, "$R/$N:$T") + + target["tags"] = tags + + rendered_dockerfile = template.render( + timestamp=timestamp, + target=target, + build_libabigail=options.build_libabigail, + build_abi=options.build_abi, + registry_hostname=options.registry_hostname, + **inventory, + ) + with open(dockerfile_location, "w") as output_file: + output_file.write(rendered_dockerfile) + + makefile_template = env.get_template(f"containers.makefile.j2") + rendered_makefile = makefile_template.render( + timestamp=timestamp, + build_libabigail=options.build_libabigail, + build_abi=options.build_abi, + registry_hostname=options.registry_hostname, + **inventory, + ) + makefile_output_path = os.path.join(options.output_dir, "Makefile") + with open(makefile_output_path, "w") as f: + f.write(rendered_makefile) + + +if __name__ == "__main__": + logging.basicConfig() + logging.root.setLevel(0) # log everything + main() -- 2.34.1