From d63409acce1750db32781146d8366a13923262d0 Mon Sep 17 00:00:00 2001 From: Mike Crute Date: Thu, 21 May 2020 10:52:06 -0700 Subject: [PATCH] Convert make-amis to python This is paving the way for identity broker improvements for opt-in regions. Eventually we'll need to hook some region logic into these scripts so having them written in python will be helpful. --- Makefile | 4 +-- scripts/make-amis | 39 ----------------------- scripts/make-amis.py.in | 68 +++++++++++++++++++++++++++++++++++++++++ 3 files changed, 70 insertions(+), 41 deletions(-) delete mode 100755 scripts/make-amis create mode 100644 scripts/make-amis.py.in diff --git a/Makefile b/Makefile index 851ed7e..bc9b842 100644 --- a/Makefile +++ b/Makefile @@ -24,9 +24,9 @@ __check_defined = \ .PHONY: amis prune release-readme clean -amis: build build/packer.json build/profile/$(PROFILE) build/update-release.py +amis: build build/packer.json build/profile/$(PROFILE) build/update-release.py build/make-amis.py @:$(call check_defined, PROFILE, target profile name) - build/make-amis $(PROFILE) $(BUILDS) + build/make-amis.py $(PROFILE) $(BUILDS) prune: build build/prune-amis.py @:$(call check_defined, LEVEL, pruning level) diff --git a/scripts/make-amis b/scripts/make-amis deleted file mode 100755 index 45bccfd..0000000 --- a/scripts/make-amis +++ /dev/null @@ -1,39 +0,0 @@ -#!/bin/sh -# vim: set ts=4 et: - -export PACKER=${PACKER:-packer} - -cd build || exit 1 - -# we need a profile, at least -if [ $# -eq 0 ]; then - echo "Usage: $(basename "$0") [ ... ]" >&2 - exit 1 -fi - -PROFILE=$1; shift - -# no build(s) specified? do all the builds! -[ $# -gt 0 ] && BUILDS="$*" || BUILDS=$(ls "profile/$PROFILE") - -for BUILD in $BUILDS -do - printf "\n*** Building %s/%s ***\n\n" "$PROFILE" "$BUILD" - BUILD_DIR="profile/$PROFILE/$BUILD" - - # execute packer, capture output and exit code - ( - "$PACKER" build -var-file="$BUILD_DIR/vars.json" packer.json - echo $? >"$BUILD_DIR/exit" - ) | tee "$BUILD_DIR/output" - EXIT=$(cat "$BUILD_DIR/exit") - - if [ "$EXIT" -eq 0 ]; then - ./update-release.py "$PROFILE" "$BUILD" - else - # unless AMI revision already exists, exit - grep -q 'is used by an existing AMI' "$BUILD_DIR/output" || exit "$EXIT" - fi -done - -echo "\n=== DONE ===\n" diff --git a/scripts/make-amis.py.in b/scripts/make-amis.py.in new file mode 100644 index 0000000..c7f9f98 --- /dev/null +++ b/scripts/make-amis.py.in @@ -0,0 +1,68 @@ +@PYTHON@ +# vim: set ts=4 et: + +import os +import io +import sys +import argparse +import subprocess + + +def find_repo_root(): + path = os.getcwd() + + while ".git" not in set(os.listdir(path)) and path != "/": + path = os.path.dirname(path) + + if path == "/": + raise Exception("No repo found, stopping at /") + + return path + + +def main(args): + parser = argparse.ArgumentParser(description="Build Packer JSON variable " + "files from HOCON build profiles") + parser.add_argument("profile", help="name of profile to build") + parser.add_argument("builds", nargs="*", + help="name of builds within a profile to build") + args = parser.parse_args() + + os.chdir(os.path.join(find_repo_root(), "build")) + + builds = args.builds or os.listdir(os.path.join("profile", args.profile)) + for build in builds: + print(f"\n*** Building {args.profile}/{build} ***\n\n") + + build_dir = os.path.join("profile", args.profile, build) + if not os.path.exists(build_dir): + print(f"Build dir '{build_dir}' does not exist") + break + + out = io.StringIO() + + res = subprocess.Popen([ + os.environ.get("PACKER", "packer"), + "build", + f"-var-file={build_dir}/vars.json", + "packer.json" + ], stdout=subprocess.PIPE, encoding="utf-8") + + while res.poll() is None: + text = res.stdout.readline() + out.write(text) + print(text, end="") + + if res.returncode == 0: + subprocess.run(["./update-release.py", args.profile, build]) + else: + if "is used by an existing AMI" in out.getvalue(): + continue + else: + sys.exit(res.returncode) + + print("\n=== DONE ===\n") + + +if __name__ == "__main__": + main(sys.argv)