|
| 1 | +import toml |
| 2 | +import os |
| 3 | +import subprocess |
| 4 | +import argparse |
| 5 | + |
| 6 | + |
| 7 | +def get_cargo_version(cargo_toml_path): |
| 8 | + if not os.path.exists(cargo_toml_path): |
| 9 | + raise ValueError(f"No such file: {cargo_toml_path}") |
| 10 | + |
| 11 | + # Read the Cargo.toml file |
| 12 | + config = toml.load(cargo_toml_path) |
| 13 | + |
| 14 | + # Get the version |
| 15 | + version = config.get('package', {}).get('version') |
| 16 | + |
| 17 | + if version is None: |
| 18 | + raise ValueError(f"No version specified in {cargo_toml_path}") |
| 19 | + |
| 20 | + return version |
| 21 | + |
| 22 | + |
| 23 | +def build_docker_image(dockerfile_path, image_name, version, num_jobs=None): |
| 24 | + # Docker build command |
| 25 | + command = f"docker build -t {image_name}:{version} -f {dockerfile_path}" |
| 26 | + if num_jobs: |
| 27 | + command += f" --build-arg NUM_JOBS={num_jobs}" |
| 28 | + command += " ." |
| 29 | + |
| 30 | + try: |
| 31 | + # Run the command |
| 32 | + subprocess.check_call(command, shell=True) |
| 33 | + print(f"Built {image_name}:{version} successfully.") |
| 34 | + catch: |
| 35 | + print(f"Error occurred: {error}") |
| 36 | + exit(1) # stop the build |
| 37 | + |
| 38 | + |
| 39 | +def push_docker_image(image_name, version, latest=False): |
| 40 | + # Docker tag command |
| 41 | + full_image_name = f"{image_name}:{version}" |
| 42 | + if latest: |
| 43 | + latest_image_name = f"{image_name}:latest" |
| 44 | + tag_command = f"docker tag {full_image_name} {latest_image_name}" |
| 45 | + subprocess.check_call(tag_command, shell=True) |
| 46 | + |
| 47 | + # Docker push command |
| 48 | + push_command = f"docker push {full_image_name}" |
| 49 | + subprocess.check_call(push_command, shell=True) |
| 50 | + |
| 51 | + # if latest flag is true, push the image with 'latest' tag |
| 52 | + if latest: |
| 53 | + push_command_latest = f"docker push {latest_image_name}" |
| 54 | + subprocess.check_call(push_command_latest, shell=True) |
| 55 | + |
| 56 | + print(f"Pushed {full_image_name} successfully.") |
| 57 | + if latest: |
| 58 | + print(f"Pushed {latest_image_name} successfully.") |
| 59 | + |
| 60 | + |
| 61 | +def delete_docker_image(image_name, version): |
| 62 | + # Full image name |
| 63 | + full_image_name = f"{image_name}:{version}" |
| 64 | + |
| 65 | + # Docker rmi command |
| 66 | + command = f"docker rmi {full_image_name}" |
| 67 | + |
| 68 | + # Run the command |
| 69 | + try: |
| 70 | + subprocess.check_call(command, shell=True) |
| 71 | + print(f"Deleted {full_image_name} successfully.") |
| 72 | + except subprocess.CalledProcessError: |
| 73 | + print(f"Failed to delete {full_image_name}.") |
| 74 | + |
| 75 | + |
| 76 | +def build_instances(version, num_jobs=None): |
| 77 | + build_docker_image("build-tools/docker/Dockerfile.builder", "mintlayer-builder", "latest", num_jobs) |
| 78 | + build_docker_image("build-tools/docker/Dockerfile.node-daemon", "mintlayer/node-daemon", version) |
| 79 | + build_docker_image("build-tools/docker/Dockerfile.node-gui", "mintlayer/node-gui", version) |
| 80 | + build_docker_image("build-tools/docker/Dockerfile.wallet-cli", "mintlayer/wallet-cli", version) |
| 81 | + delete_docker_image("mintlayer-builder", "latest") |
| 82 | + |
| 83 | + |
| 84 | +def push_instances(version, latest): |
| 85 | + push_docker_image("mintlayer/node-daemon",version , latest) |
| 86 | + push_docker_image("mintlayer/node-gui",version , latest) |
| 87 | + push_docker_image("mintlayer/wallet-cli",version , latest) |
| 88 | + |
| 89 | + |
| 90 | +def main(): |
| 91 | + parser = argparse.ArgumentParser() |
| 92 | + parser.add_argument('--push', action='store_true', help='Push the Docker image to Docker Hub') |
| 93 | + parser.add_argument('--latest', action='store_true', help='Tag the Docker image as latest while pushing') |
| 94 | + parser.add_argument('--build', type=lambda x: (str(x).lower() == 'true'), default=True, help="Set to false avoid the build") |
| 95 | + parser.add_argument('--version', help='Override version number', default=None) |
| 96 | + parser.add_argument('--num_jobs', help='Number of parallel jobs, defaults to # of CPUs', default=None) |
| 97 | + args = parser.parse_args() |
| 98 | + |
| 99 | + version = args.version if args.version else get_cargo_version("Cargo.toml") |
| 100 | + |
| 101 | + if args.build: |
| 102 | + build_instances(version, num_jobs) |
| 103 | + |
| 104 | + # Only push the image if the --push flag is provided |
| 105 | + if args.push: |
| 106 | + latest = args.latest |
| 107 | + push_instances(version, latest) |
| 108 | + |
| 109 | + |
| 110 | +if __name__ == "__main__": |
| 111 | + main() |
0 commit comments