Skip to content

Commit 097d594

Browse files
committed
Add cmd-remote-build
- cmd-remote-build allows cosa builds using podman remote Signed-off-by: Renata Ravanelli <[email protected]>
1 parent 0890f5f commit 097d594

File tree

1 file changed

+130
-0
lines changed

1 file changed

+130
-0
lines changed

src/cmd-remote-build

Lines changed: 130 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,130 @@
1+
#!/usr/bin/python3 -u
2+
3+
import sys
4+
import argparse
5+
from os import environ
6+
from subprocess import check_call, check_output, CalledProcessError
7+
8+
def run_cmd(command, output=None):
9+
'''
10+
Run the given command using check_call/check_output and verify its return code.
11+
@param str command command to be executed
12+
'''
13+
if output:
14+
try:
15+
output = check_output(command.split())
16+
except CalledProcessError as e:
17+
raise SystemExit(f"Failed to invoke command: {e}")
18+
return output
19+
else:
20+
try:
21+
check_call(command.split())
22+
except CalledProcessError as e:
23+
raise SystemExit(f"Failed to invoke command: {e}")
24+
25+
def build(expire, gitURL, gitRef, repo, tag):
26+
'''
27+
Build the image using podman remote and push to the registry
28+
@param repo str registry repository
29+
@param tag str image tag
30+
@param time str expiration time for Quay images
31+
'''
32+
if expire:
33+
run_cmd(f"podman --remote build {gitURL}#{gitRef} --tag {repo}:{tag} --label=quay.expires-after={expire}")
34+
else:
35+
run_cmd(f"podman --remote build {gitURL}#{gitRef} --tag {repo}:{tag}")
36+
37+
def push(repo, tag, force=None):
38+
'''
39+
Push image to registry
40+
@param repo str registry repository
41+
@param tag str image tag
42+
'''
43+
if (search(repo, tag) is True and force is True) or search(repo, tag) is False:
44+
run_cmd(f"podman --remote push {repo}:{tag}")
45+
# Even though check_call only returns after the cmd completion,
46+
# Quay seems to take more time to publish images using --LABEL after it.
47+
run_cmd("sleep 50")
48+
if search(repo, tag) is True:
49+
print(f"Build and Push done successfully via tag: {tag}")
50+
else:
51+
raise SystemExit(f"Registry doesn't seem to have tag:{tag}")
52+
else:
53+
raise SystemExit(f"Registry tag:{tag} found in {repo}. If you want to overwride it use --force")
54+
55+
def search(repo, tag):
56+
'''
57+
Search for a tag in the registry
58+
@param force str ignore if image is found
59+
@param repo str registry repository
60+
@param tag str image tag
61+
'''
62+
# Podman remote doesn't allow push using digestfile. That's why the tag check is done
63+
tags = run_cmd(f"podman search --list-tags {repo}", True)
64+
if (tag in str(tags)):
65+
return True
66+
return False
67+
68+
def main():
69+
parse_args()
70+
71+
def parse_args():
72+
parser = argparse.ArgumentParser(
73+
prog="CoreOS Assembler Remote Build",
74+
description="Build coreos-assembler remotely",
75+
usage="""
76+
Run multi-arch builds using podman remote.
77+
In order to get cmd-remote-build working the CONTAINER_SSHKEY and CONTAINER_HOST environment variables
78+
must be defined
79+
80+
Examples:
81+
$ cmd-remote-build \
82+
--arch aarch64 \
83+
--expire 4d \
84+
--git-ref main \
85+
--git-url https://github.com/coreos/coreos-assembler.git \
86+
--repo quay.io/coreos/coreos-assembler-staging \
87+
--push-to-registry """)
88+
89+
parser.add_argument(
90+
'--arch', required=True,
91+
help='Build Architecture')
92+
parser.add_argument(
93+
'--expire', required=False,
94+
help='Expiration time for Quay images')
95+
parser.add_argument(
96+
'--force', required=False, action='store_true',
97+
help='Force image overwrite')
98+
parser.add_argument(
99+
'--git-ref', required=True,
100+
help='Git branch or tag')
101+
parser.add_argument(
102+
'--git-url', required=True,
103+
help='Git URL')
104+
parser.add_argument(
105+
'--push-to-registry', required=False, action='store_true',
106+
help='Push image to registry. You must be logged in before pushing images')
107+
parser.add_argument(
108+
'--repo', required=True,
109+
help='Registry repository')
110+
parser.add_argument(
111+
'--tag', required=False,
112+
help='Force image tag. The default is arch-commit')
113+
114+
args, extra_args = parser.parse_known_args()
115+
args = parser.parse_args(namespace=args)
116+
117+
if environ.get('CONTAINER_HOST') is not None and environ.get('CONTAINER_SSHKEY') is not None:
118+
if not args.tag:
119+
commit = run_cmd(f"git ls-remote {args.git_url} {args.git_ref}", 'True')[0:6].decode("utf-8")
120+
tag = f"{args.arch}-{commit}"
121+
else:
122+
tag = args.tag
123+
build(args.expire, args.git_url, args.git_ref, args.repo, tag)
124+
if args.push_to_registry:
125+
push(args.repo, tag, args.force)
126+
else:
127+
sys.exit('You must have CONTAINER_HOST and CONTAINER_SSHKEY environment variables setup')
128+
129+
if __name__ == '__main__':
130+
sys.exit(main())

0 commit comments

Comments
 (0)