Skip to content

Commit d769d87

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

File tree

2 files changed

+134
-1
lines changed

2 files changed

+134
-1
lines changed

src/cmd-remote-build-container

Lines changed: 133 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,133 @@
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(label, 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 label:
33+
run_cmd(f"podman --remote build {gitURL}#{gitRef} --tag {repo}:{tag} --label={label}")
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+
tag_exists_in_repo = search(repo, tag)
44+
if force or not tag_exists_in_repo:
45+
run_cmd(f"podman --remote push {repo}:{tag}")
46+
# Even though check_call only returns after the cmd completion,
47+
# Quay seems to take more time to publish images in some occasions
48+
run_cmd("sleep 50")
49+
if search(repo, tag) is True:
50+
print(f"Build and Push done successfully via tag: {tag}")
51+
else:
52+
raise SystemExit(f"Image pushed but not viewable in registry: tag:{tag}")
53+
else:
54+
raise SystemExit(f"Registry tag:{tag} found in {repo}. If you want to overwride it use --force")
55+
56+
def search(repo, tag):
57+
'''
58+
Search for a tag in the registry
59+
@param force str ignore if image is found
60+
@param repo str registry repository
61+
@param tag str image tag
62+
'''
63+
# Podman remote doesn't allow push using digestfile. That's why the tag check is done
64+
tags = run_cmd(f"podman search --list-tags {repo}", True)
65+
if (tag in str(tags)):
66+
return True
67+
return False
68+
69+
def main():
70+
parse_args()
71+
72+
def parse_args():
73+
parser = argparse.ArgumentParser(
74+
prog="CoreOS Assembler Remote Build",
75+
description="Build coreos-assembler remotely",
76+
usage="""
77+
Run multi-arch builds using podman remote.
78+
In order to get cmd-remote-build-container working the CONTAINER_SSHKEY and CONTAINER_HOST environment variables
79+
must be defined
80+
81+
Examples:
82+
$ cosa remote-build-container \
83+
--arch aarch64 \
84+
--label quay.labels-after=4d \
85+
--git-ref main \
86+
--git-url https://github.com/coreos/coreos-assembler.git \
87+
--repo quay.io/coreos/coreos-assembler-staging \
88+
--push-to-registry """)
89+
90+
parser.add_argument(
91+
'--arch', required=True,
92+
help='Build Architecture')
93+
parser.add_argument(
94+
'--label', required=False,
95+
help='Add image label')
96+
parser.add_argument(
97+
'--force', required=False, action='store_true',
98+
help='Force image overwrite')
99+
parser.add_argument(
100+
'--git-ref', required=True,
101+
help='Git branch or tag')
102+
parser.add_argument(
103+
'--git-url', required=True,
104+
help='Git URL')
105+
parser.add_argument(
106+
'--push-to-registry', required=False, action='store_true',
107+
help='Push image to registry. You must be logged in before pushing images')
108+
parser.add_argument(
109+
'--repo', required=True,
110+
help='Registry repository')
111+
parser.add_argument(
112+
'--tag', required=False,
113+
help='Force image tag. The default is arch-commit')
114+
115+
args, extra_args = parser.parse_known_args()
116+
args = parser.parse_args(namespace=args)
117+
118+
if environ.get('CONTAINER_HOST') is None or environ.get('CONTAINER_SSHKEY') is None:
119+
sys.exit('You must have CONTAINER_HOST and CONTAINER_SSHKEY environment variables setup')
120+
121+
if not args.tag:
122+
# If a tag wasn't passed then use the git
123+
# short commit hash
124+
commit = run_cmd(f"git ls-remote {args.git_url} {args.git_ref}", 'True')[0:6].decode("utf-8")
125+
tag = f"{args.arch}-{commit}"
126+
else:
127+
tag = args.tag
128+
build(args.label, args.git_url, args.git_ref, args.repo, tag)
129+
if args.push_to_registry:
130+
push(args.repo, tag, args.force)
131+
132+
if __name__ == '__main__':
133+
sys.exit(main())

src/coreos-assembler

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ build_commands="init fetch build run prune clean list"
4545
# commands more likely to be used in a prod pipeline only
4646
advanced_build_commands="buildfetch buildupload oc-adm-release push-container upload-oscontainer"
4747
buildextend_commands="aliyun aws azure digitalocean exoscale gcp ibmcloud kubevirt live metal metal4k nutanix openstack qemu virtualbox vmware vultr"
48-
utility_commands="aws-replicate compress generate-hashlist koji-upload kola remote-prune sign tag"
48+
utility_commands="aws-replicate compress generate-hashlist koji-upload kola remote-build-container remote-prune sign tag"
4949
other_commands="shell meta"
5050
if [ -z "${cmd}" ]; then
5151
echo Usage: "coreos-assembler CMD ..."

0 commit comments

Comments
 (0)