From c91725bfb5e30675c7f67b7a0dee8791d9598bf8 Mon Sep 17 00:00:00 2001 From: Alex Vandiver Date: Fri, 25 Mar 2022 16:15:40 -0700 Subject: [PATCH] zulip-puppet-apply: Factor out the --noop returncode logic. --- scripts/zulip-puppet-apply | 19 ++++++++++++++----- 1 file changed, 14 insertions(+), 5 deletions(-) diff --git a/scripts/zulip-puppet-apply b/scripts/zulip-puppet-apply index d0b9de3622..4a5be9b4e3 100755 --- a/scripts/zulip-puppet-apply +++ b/scripts/zulip-puppet-apply @@ -6,6 +6,7 @@ import re import subprocess import sys import tempfile +from typing import List import yaml @@ -68,22 +69,30 @@ if (distro_info["ID"], distro_info["VERSION_ID"]) in [("ubuntu", "20.04")]: if (distro_info["ID"], distro_info["VERSION_ID"]) in [("ubuntu", "22.04")]: puppet_env["RUBYOPT"] = "-r " + os.path.join(scripts_path, "lib", "ruby3hack.rb") -if not args.noop and not args.force: - # --noop does not work with --detailed-exitcodes; see https://tickets.puppetlabs.com/browse/PUP-686 + +def noop_would_change(puppet_cmd: List[str]) -> bool: + # --noop does not work with --detailed-exitcodes; see + # https://tickets.puppetlabs.com/browse/PUP-686 try: lastrun_file = tempfile.NamedTemporaryFile() subprocess.check_call( - [*puppet_cmd, "--noop", "--show_diff", "--lastrunfile", lastrun_file.name], + # puppet_cmd may already contain --noop, but it is safe to + # supply twice + [*puppet_cmd, "--noop", "--lastrunfile", lastrun_file.name], env=puppet_env, ) with open(lastrun_file.name) as lastrun: lastrun_data = yaml.safe_load(lastrun) - if lastrun_data.get("resources", {}).get("out_of_sync", 0) == 0: - sys.exit(0) + return lastrun_data.get("resources", {}).get("out_of_sync", 0) != 0 finally: lastrun_file.close() + +if not args.noop and not args.force: + if not noop_would_change([*puppet_cmd, "--show_diff"]): + sys.exit(0) + do_apply = None while do_apply != "y": sys.stdout.write("Apply changes? [y/N] ")