From 35a20922f0a77ecb78b8f4420a4f8f9f82bb591f Mon Sep 17 00:00:00 2001 From: Christian Kellner Date: Fri, 24 Apr 2020 17:48:25 +0200 Subject: [PATCH] osbuild: validate pipeline options Validate the options of stages and assembler of the pipeline before running it. A validation failure will abort the run. Errors are printed in human readable unless `--json` is passed; For each error a human readable message together with a path to the object with the error is given. The syntax of the path is such it can be used via the `jq` command to select the item. --- osbuild/main_cli.py | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) diff --git a/osbuild/main_cli.py b/osbuild/main_cli.py index 260dbf2f..367e3da9 100644 --- a/osbuild/main_cli.py +++ b/osbuild/main_cli.py @@ -10,12 +10,15 @@ import argparse import json import os import sys + import osbuild +import osbuild.meta RESET = "\033[0m" BOLD = "\033[1m" RED = "\033[31m" +GREEN = "\033[32m" def mark_checkpoints(pipeline, checkpoints): @@ -47,6 +50,24 @@ def parse_manifest(path): return manifest +def show_validation(result, name): + if name == "-": + name = "" + + print(f"{BOLD}{name}{RESET} ", end='') + + if result: + print(f"is {BOLD}{GREEN}valid{RESET}") + return + + print(f"has {BOLD}{RED}errors{RESET}:") + print("") + + for error in result: + print(f"{BOLD}{error.id}{RESET}:") + print(f" {error.message}\n") + + def parse_arguments(sys_argv): parser = argparse.ArgumentParser(description="Build operating system images") @@ -77,6 +98,17 @@ def osbuild_cli(*, sys_argv=[]): args = parse_arguments(sys_argv) manifest = parse_manifest(args.manifest_path) + # first thing after parsing is validation of the input + index = osbuild.meta.Index(args.libdir) + res = osbuild.meta.validate(manifest, index) + if not res: + if not args.json: + show_validation(res, args.manifest_path) + else: + json.dump(res.as_dict(), sys.stdout) + sys.stdout.write("\n") + return 2 + pipeline = manifest.get("pipeline", {}) sources_options = manifest.get("sources", {})