|
| 1 | +#!/usr/bin/env python |
| 2 | + |
| 3 | +import argparse |
| 4 | +import glob |
| 5 | +import json |
| 6 | +import os |
| 7 | +import subprocess |
| 8 | + |
| 9 | + |
| 10 | +def pull_image(image_url, destination_image_file): |
| 11 | + """ |
| 12 | + Pull the image file from external server |
| 13 | + """ |
| 14 | + apptainer_pull_cmd = [ |
| 15 | + "apptainer", |
| 16 | + "pull", |
| 17 | + destination_image_file, |
| 18 | + image_url, |
| 19 | + ] |
| 20 | + subprocess.run(apptainer_pull_cmd) |
| 21 | + |
| 22 | + |
| 23 | +def push_image(source_image_file, image_url): |
| 24 | + """ |
| 25 | + Push apptainer image to destination image repository |
| 26 | + """ |
| 27 | + |
| 28 | + apptainer_push_cmd = [ |
| 29 | + "apptainer", |
| 30 | + "push", |
| 31 | + source_image_file, |
| 32 | + image_url, |
| 33 | + ] |
| 34 | + subprocess.run(apptainer_push_cmd) |
| 35 | + |
| 36 | + |
| 37 | +def main(args): |
| 38 | + repo_owner = os.environ['GITHUB_REPOSITORY_OWNER'].lower() |
| 39 | + |
| 40 | + wave_jsons = glob.glob(os.path.join(args.wave_jsons_dir, "*.json")) |
| 41 | + for wave_json in wave_jsons: |
| 42 | + with open(wave_json, 'r') as f: |
| 43 | + w = json.load(f) |
| 44 | + pull_image_url = w['containerImage'] |
| 45 | + image_name_with_version = pull_image_url.split('/')[-1] |
| 46 | + image_name, image_version = image_name_with_version.split(':') |
| 47 | + pull_destination = os.path.join(args.images_dir, f"{image_name}--{image_version}.img") |
| 48 | + pull_image(pull_image_url, pull_destination) |
| 49 | + |
| 50 | + push_image_url = f"oras://ghcr.io/{repo_owner}/{image_name}:{image_version}" |
| 51 | + push_image(pull_destination, push_image_url) |
| 52 | + |
| 53 | + |
| 54 | +if __name__ == '__main__': |
| 55 | + parser = argparse.ArgumentParser() |
| 56 | + parser.add_argument('--wave-jsons-dir') |
| 57 | + parser.add_argument('--images-dir') |
| 58 | + args = parser.parse_args() |
| 59 | + main(args) |
0 commit comments