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.
This commit is contained in:
Mike Crute 2020-05-21 10:52:06 -07:00
parent 1fd42af98d
commit d63409acce
3 changed files with 70 additions and 41 deletions

View File

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

View File

@ -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") <profile> [ <build> ... ]" >&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"

68
scripts/make-amis.py.in Normal file
View File

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