zulip-puppet-apply: Factor out the --noop returncode logic.

This commit is contained in:
Alex Vandiver 2022-03-25 16:15:40 -07:00 committed by Tim Abbott
parent b15d8e0118
commit c91725bfb5
1 changed files with 14 additions and 5 deletions

View File

@ -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] ")