From 04224dc0b688abfbb67f796fd9fa89330bc5bf49 Mon Sep 17 00:00:00 2001 From: Lars Wirzenius Date: Fri, 7 Jan 2022 18:44:18 +0200 Subject: feat: hide output unless --verbose is used but do explicitly say OK if all went well Sponsored-by: author --- v-i | 44 +++++++++++++++++++++++++++----------------- 1 file changed, 27 insertions(+), 17 deletions(-) (limited to 'v-i') diff --git a/v-i b/v-i index 49721e4..ce26933 100755 --- a/v-i +++ b/v-i @@ -10,8 +10,12 @@ import yaml from subprocess import run +verbose = False + + def log(msg): - print("INSTALLER:", msg) + if verbose: + print("INSTALLER:", msg) def physical_volumes(): @@ -89,22 +93,22 @@ def clean_up_disks(): for lv in lvs: for m in find_mount_points(mounts, lv["path"]): log(f"unmount {m['mount']}") - run(["umount", m["mount"]]) + run(["umount", m["mount"]], capture_output=True) for vg in vgs: log(f"remove volume group {vg}") - run(["vgremove", "--yes", vg], check=True) + run(["vgremove", "--yes", vg], check=True, capture_output=True) for pv in pvs: log(f"remove physical volume {pv}") - run(["pvremove", pv["pv"]], check=True) + run(["pvremove", pv["pv"]], check=True, capture_output=True) if is_luks(pv["pv"]): - run(["cryptsetup", "close", pv["pv"]], check=True) + run(["cryptsetup", "close", pv["pv"]], check=True, capture_output=True) for mapping in glob.glob("/dev/mapper/*"): if not mapping.endswith("/control"): log(f"open LUKS volume {mapping} (just in case it is one)") - run(["cryptsetup", "close", mapping], check=False) + run(["cryptsetup", "close", mapping], check=False, capture_output=True) def vmdb_spec(cryptsetup_password, playbook, extra_vars): @@ -279,6 +283,10 @@ def main(): p.add_argument("device") args = p.parse_args() + if args.verbose: + global verbose + verbose = args.verbose + extra_vars = {} if args.vars: with open(args.vars) as f: @@ -299,22 +307,24 @@ def main(): env["ANSIBLE_STDOUT_CALLBACK"] = "yaml" env["ANSIBLE_NOCOWS"] = "1" env["ANSIBLE_LOG_PATH"] = "ansible.log" - run( - [ - "vmdb2", - "--verbose", - f"--rootfs-tarball={args.cache}", - f"--log={args.log}", - f"--image={args.device}", - specfile, - ], - check=True, - ) + + argv = [ + "vmdb2", + f"--rootfs-tarball={args.cache}", + f"--log={args.log}", + f"--image={args.device}", + specfile, + ] + if verbose: + argv.append("--verbose") + + run(argv, check=True, capture_output=True) log("cleanup") shutil.rmtree(tmp) log("OK, done") + print("OK, done") main() -- cgit v1.2.1