aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMike Crute <mike@crute.us>2020-05-21 10:52:06 -0700
committerMike Crute <mike@crute.us>2020-05-22 18:23:32 -0700
commitd63409acce1750db32781146d8366a13923262d0 (patch)
tree8bf509eed3aba76b48efe76292fa83951f3870aa
parent1fd42af98dac79011496a7956a8cf592b596e2cf (diff)
downloadalpine-ec2-ami-d63409acce1750db32781146d8366a13923262d0.tar.bz2
alpine-ec2-ami-d63409acce1750db32781146d8366a13923262d0.tar.xz
alpine-ec2-ami-d63409acce1750db32781146d8366a13923262d0.zip
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.
-rw-r--r--Makefile4
-rwxr-xr-xscripts/make-amis39
-rw-r--r--scripts/make-amis.py.in68
3 files changed, 70 insertions, 41 deletions
diff --git a/Makefile b/Makefile
index 851ed7e..bc9b842 100644
--- a/Makefile
+++ b/Makefile
@@ -24,9 +24,9 @@ __check_defined = \
24 24
25.PHONY: amis prune release-readme clean 25.PHONY: amis prune release-readme clean
26 26
27amis: build build/packer.json build/profile/$(PROFILE) build/update-release.py 27amis: build build/packer.json build/profile/$(PROFILE) build/update-release.py build/make-amis.py
28 @:$(call check_defined, PROFILE, target profile name) 28 @:$(call check_defined, PROFILE, target profile name)
29 build/make-amis $(PROFILE) $(BUILDS) 29 build/make-amis.py $(PROFILE) $(BUILDS)
30 30
31prune: build build/prune-amis.py 31prune: build build/prune-amis.py
32 @:$(call check_defined, LEVEL, pruning level) 32 @:$(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 @@
1#!/bin/sh
2# vim: set ts=4 et:
3
4export PACKER=${PACKER:-packer}
5
6cd build || exit 1
7
8# we need a profile, at least
9if [ $# -eq 0 ]; then
10 echo "Usage: $(basename "$0") <profile> [ <build> ... ]" >&2
11 exit 1
12fi
13
14PROFILE=$1; shift
15
16# no build(s) specified? do all the builds!
17[ $# -gt 0 ] && BUILDS="$*" || BUILDS=$(ls "profile/$PROFILE")
18
19for BUILD in $BUILDS
20do
21 printf "\n*** Building %s/%s ***\n\n" "$PROFILE" "$BUILD"
22 BUILD_DIR="profile/$PROFILE/$BUILD"
23
24 # execute packer, capture output and exit code
25 (
26 "$PACKER" build -var-file="$BUILD_DIR/vars.json" packer.json
27 echo $? >"$BUILD_DIR/exit"
28 ) | tee "$BUILD_DIR/output"
29 EXIT=$(cat "$BUILD_DIR/exit")
30
31 if [ "$EXIT" -eq 0 ]; then
32 ./update-release.py "$PROFILE" "$BUILD"
33 else
34 # unless AMI revision already exists, exit
35 grep -q 'is used by an existing AMI' "$BUILD_DIR/output" || exit "$EXIT"
36 fi
37done
38
39echo "\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 @@
1@PYTHON@
2# vim: set ts=4 et:
3
4import os
5import io
6import sys
7import argparse
8import subprocess
9
10
11def find_repo_root():
12 path = os.getcwd()
13
14 while ".git" not in set(os.listdir(path)) and path != "/":
15 path = os.path.dirname(path)
16
17 if path == "/":
18 raise Exception("No repo found, stopping at /")
19
20 return path
21
22
23def main(args):
24 parser = argparse.ArgumentParser(description="Build Packer JSON variable "
25 "files from HOCON build profiles")
26 parser.add_argument("profile", help="name of profile to build")
27 parser.add_argument("builds", nargs="*",
28 help="name of builds within a profile to build")
29 args = parser.parse_args()
30
31 os.chdir(os.path.join(find_repo_root(), "build"))
32
33 builds = args.builds or os.listdir(os.path.join("profile", args.profile))
34 for build in builds:
35 print(f"\n*** Building {args.profile}/{build} ***\n\n")
36
37 build_dir = os.path.join("profile", args.profile, build)
38 if not os.path.exists(build_dir):
39 print(f"Build dir '{build_dir}' does not exist")
40 break
41
42 out = io.StringIO()
43
44 res = subprocess.Popen([
45 os.environ.get("PACKER", "packer"),
46 "build",
47 f"-var-file={build_dir}/vars.json",
48 "packer.json"
49 ], stdout=subprocess.PIPE, encoding="utf-8")
50
51 while res.poll() is None:
52 text = res.stdout.readline()
53 out.write(text)
54 print(text, end="")
55
56 if res.returncode == 0:
57 subprocess.run(["./update-release.py", args.profile, build])
58 else:
59 if "is used by an existing AMI" in out.getvalue():
60 continue
61 else:
62 sys.exit(res.returncode)
63
64 print("\n=== DONE ===\n")
65
66
67if __name__ == "__main__":
68 main(sys.argv)