-
Notifications
You must be signed in to change notification settings - Fork 3
/
build_tftask_image.py
executable file
·102 lines (93 loc) · 4.64 KB
/
build_tftask_image.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
#!/usr/bin/env python3
#
# Example Usage:
#
# ## Build a single manifest (a manifest consists of all images of the same version under a single tag)
# python3 build-tftask-image.py --dockerhubrepo isaaguilar/terraform-arm64 --org galleybyes --image tftaskv1 --tag 1.0.9
#
# ## Build all the images by not specifying a tag
# python3 build-tftask-image.py --dockerhubrepo isaaguilar/terraform-arm64 --org galleybyes --image tftaskv1
#
# ## Force rebuilding (this should not be used for production images)
# python3 build-tftask-image.py --dockerhubrepo isaaguilar/terraform-arm64 --org galleybyes --image tftaskv1 --tag 1.0.9 --nocache
#
import argparse
import builder
import requests
import base64
import os
ignore_versions = [
"0.11.0-beta1", "0.11.0", "0.11.1", "0.11.2", "0.11.3", "0.11.4", "0.11.5", "0.11.6",
"0.11.7", "0.11.8", "0.11.9", "0.11.10", "0.11.11", "0.11.12", "0.11.13"
]
def terraform_versions(url):
print(url)
versions = []
while True:
resp = requests.get(f"{url}")
data = resp.json()
for item in data["results"]:
if item["name"] not in versions:
versions.append(item["name"])
url = data.get("next")
if not url:
break
return versions
def unbuilt_versions(host, org, image, tags, already_built_tags):
versions = []
for tag in tags:
if builder.release_manifest_exists(host, org, image, tag, already_built_tags):
print(f"Release manifest {tag} already exists")
continue
versions.append(tag)
return versions
if __name__ == "__main__":
parser = argparse.ArgumentParser()
parser.add_argument("-H", "--host", required=False, default="ghcr.io", help="Container repo hostname")
parser.add_argument('-d', '--dockerhubrepo', required=False, help="Dockerhub owner/image (to tags)")
parser.add_argument('-o', '--org', required=True, help="Github organization owner of image")
parser.add_argument('-i', '--image', required=True, help="Container Image (no tags)")
parser.add_argument('-t', '--tag', required=False, help="Tag of container image")
parser.add_argument('-p', '--platform', required=False, help="Platform/architecture for the container image")
parser.add_argument('-b', '--skipbuild', required=False, default=False, action='store_true', help="Skip the builds")
parser.add_argument('-r', '--release', required=False, default=False, action='store_true', help="Release the manifest")
parser.add_argument('--deletelocal', required=False, default=False, action='store_true', help="Remove image on host")
parser.add_argument('--norebuild', required=False, default=False, action='store_true', help="Skip if tag exists. Only works if platform is defined.")
parser.add_argument('--nocache', required=False, default=False, action='store_true', help="Tag of container image")
args = parser.parse_args()
if args.dockerhubrepo:
supported_versions = terraform_versions(f"https://registry.hub.docker.com/v2/repositories/{args.dockerhubrepo}/tags/?page=1")
else:
supported_versions = builder.ghcr_scrape_tags("ghcr.io", "galleybytes", "terraform-arm64")
image = builder.image_name(args.image)
already_built_tags = builder.ghcr_scrape_tags(args.host, args.org, image)
if args.tag:
all_available_versions = [args.tag]
else:
all_available_versions = unbuilt_versions(args.host, args.org, image, supported_versions, already_built_tags)
versions_to_build = []
for version in all_available_versions:
if version in ignore_versions:
continue
versions_to_build.append(version)
print("the following versions need to be built in ghcr.io", versions_to_build)
builder.docker_login(args.org)
for version in versions_to_build:
if builder.release_manifest_exists(args.host, args.org, image, args.tag, already_built_tags):
print(f"Tag {args.tag} already exists")
continue
if not args.skipbuild:
if args.norebuild:
# Only works if a platform is defined
if args.platform:
arch = args.platform.split("/")[1]
if f"{version}-{arch}" in already_built_tags:
print(f"Tag {version}-{arch} already exists")
continue
built = builder.build(args.host, args.org, image, version, args.nocache, args.platform)
else:
built = True
if args.deletelocal:
builder.delete_local_image(args.host, args.org, image, version, args.platform)
if args.release and built:
builder.release_manifest(args.org, image, version)