|
| 1 | +""" |
| 2 | +Docker Image Mirror Tool |
| 3 | +Mirrors selected Docker images from source to destination registry using `skopeo`. |
| 4 | +""" |
| 5 | + |
| 6 | +import json |
| 7 | +import subprocess |
| 8 | +import sys |
| 9 | + |
| 10 | + |
| 11 | +def run_command(cmd, capture_output=True): |
| 12 | + """Run a shell command and return output.""" |
| 13 | + try: |
| 14 | + if capture_output: |
| 15 | + result = subprocess.run(cmd, capture_output=True, text=True, check=True) |
| 16 | + return result.stdout.strip() |
| 17 | + else: |
| 18 | + return subprocess.run(cmd, check=True) |
| 19 | + except subprocess.CalledProcessError as e: |
| 20 | + print(f"Error: {e.stderr if hasattr(e, 'stderr') else str(e)}") |
| 21 | + return None |
| 22 | + except FileNotFoundError: |
| 23 | + print("Error: 'skopeo' not found. Please install it first.") |
| 24 | + sys.exit(1) |
| 25 | + |
| 26 | + |
| 27 | +def check_skopeo_installed() -> bool: |
| 28 | + """Check if skopeo is installed.""" |
| 29 | + try: |
| 30 | + subprocess.run(["skopeo", "--version"], capture_output=True, check=True) |
| 31 | + return True |
| 32 | + except (subprocess.CalledProcessError, FileNotFoundError): |
| 33 | + return False |
| 34 | + |
| 35 | + |
| 36 | +def list_tags(image_path) -> list[str] | None: |
| 37 | + """List all tags for a given image.""" |
| 38 | + print(f"\nFetching tags from {image_path}...") |
| 39 | + |
| 40 | + output = run_command(["skopeo", "list-tags", f"docker://{image_path}"]) |
| 41 | + if not output: |
| 42 | + return None |
| 43 | + |
| 44 | + try: |
| 45 | + data = json.loads(output) |
| 46 | + return [t for t in data.get("Tags", []) if "sha256" not in t] |
| 47 | + except json.JSONDecodeError as e: |
| 48 | + print(f"Error parsing tags: {e}") |
| 49 | + return None |
| 50 | + |
| 51 | + |
| 52 | +def copy_image(source, destination, tag): |
| 53 | + """Copy a single image tag using skopeo.""" |
| 54 | + if ":" in source: |
| 55 | + source = source.split(":")[0] |
| 56 | + if ":" in destination: |
| 57 | + destination = destination.split(":")[0] |
| 58 | + source_full = f"docker://{source}:{tag}" |
| 59 | + dest_full = f"docker://{destination}:{tag}" |
| 60 | + |
| 61 | + print(f"\nCopying {tag}...") |
| 62 | + print(f" From: {source_full}") |
| 63 | + print(f" To: {dest_full}") |
| 64 | + |
| 65 | + cmd = ["skopeo", "copy", source_full, dest_full] |
| 66 | + return run_command(cmd, capture_output=False) is not None |
| 67 | + |
| 68 | + |
| 69 | +def main(): |
| 70 | + print("=" * 60) |
| 71 | + print("Docker Image Mirror Tool") |
| 72 | + print("=" * 60) |
| 73 | + |
| 74 | + # Check if skopeo is installed |
| 75 | + if not check_skopeo_installed(): |
| 76 | + print("\n❌ Error: skopeo is not installed.") |
| 77 | + print("\nInstallation instructions:") |
| 78 | + print(" Ubuntu/Debian: sudo apt-get install skopeo") |
| 79 | + print(" macOS: brew install skopeo") |
| 80 | + print(" Fedora/RHEL: sudo dnf install skopeo") |
| 81 | + sys.exit(1) |
| 82 | + |
| 83 | + print("✅ skopeo is installed") |
| 84 | + |
| 85 | + # Get source and destination |
| 86 | + source_image = input("\nEnter source image path (tag is optional): ").strip() |
| 87 | + if not source_image: |
| 88 | + print("Error: Source image cannot be empty") |
| 89 | + sys.exit(1) |
| 90 | + |
| 91 | + destination_image = input("Enter destination image path (exclude tag): ").strip() |
| 92 | + if not destination_image: |
| 93 | + destination_image = f"ghcr.io/embeddedllm/{source_image}" |
| 94 | + print(f'Defaulting to "{destination_image}"') |
| 95 | + |
| 96 | + # List available tags |
| 97 | + if ":" in source_image: |
| 98 | + src_tag = source_image.split(":")[-1] |
| 99 | + if ":" in destination_image and src_tag != destination_image.split(":")[-1]: |
| 100 | + print("Error: When specifying tags in source and destination, they must match") |
| 101 | + sys.exit(1) |
| 102 | + tags = [src_tag] |
| 103 | + else: |
| 104 | + tags = list_tags(source_image) |
| 105 | + if not tags: |
| 106 | + print("Error: No tags found") |
| 107 | + sys.exit(1) |
| 108 | + if len(tags) == 1: |
| 109 | + selected_tags = tags |
| 110 | + else: |
| 111 | + print(f"\nAvailable tags ({len(tags)} total):") |
| 112 | + for tag in tags: |
| 113 | + print(f" - {tag}") |
| 114 | + |
| 115 | + # Get user selection |
| 116 | + print("\nEnter tags to mirror (comma-separated):") |
| 117 | + print("Example: latest,v1.0.0,v1.1.0") |
| 118 | + selection = input("Tags: ").strip() |
| 119 | + |
| 120 | + if not selection: |
| 121 | + print("Error: No tags selected") |
| 122 | + sys.exit(1) |
| 123 | + |
| 124 | + selected_tags = [tag.strip() for tag in selection.split(",")] |
| 125 | + |
| 126 | + # Validate selected tags |
| 127 | + invalid_tags = [tag for tag in selected_tags if tag not in tags] |
| 128 | + if invalid_tags: |
| 129 | + print(f"\nError: Invalid tags: {', '.join(invalid_tags)}") |
| 130 | + sys.exit(1) |
| 131 | + |
| 132 | + # Confirm |
| 133 | + print(f"\nWill mirror {len(selected_tags)} tag(s):") |
| 134 | + for tag in selected_tags: |
| 135 | + print(f" - {tag}") |
| 136 | + |
| 137 | + confirm = input("\nProceed? (Y/n): ").strip().lower() |
| 138 | + if confirm not in ("", "y"): |
| 139 | + print("Cancelled") |
| 140 | + sys.exit(0) |
| 141 | + |
| 142 | + # Copy images |
| 143 | + print("\nStarting mirror process...") |
| 144 | + success_count = 0 |
| 145 | + failed_tags = [] |
| 146 | + |
| 147 | + for idx, tag in enumerate(selected_tags, 1): |
| 148 | + print(f"\n[{idx}/{len(selected_tags)}]") |
| 149 | + if copy_image(source_image, destination_image, tag): |
| 150 | + success_count += 1 |
| 151 | + print(f"✅ Success: {tag}") |
| 152 | + else: |
| 153 | + failed_tags.append(tag) |
| 154 | + print(f"❌ Failed: {tag}") |
| 155 | + |
| 156 | + # Summary |
| 157 | + print("\n" + "=" * 60) |
| 158 | + print(f"Summary: {success_count}/{len(selected_tags)} successful") |
| 159 | + if failed_tags: |
| 160 | + print(f"Failed tags: {', '.join(failed_tags)}") |
| 161 | + print("=" * 60) |
| 162 | + |
| 163 | + |
| 164 | +if __name__ == "__main__": |
| 165 | + try: |
| 166 | + main() |
| 167 | + except KeyboardInterrupt: |
| 168 | + print("\n\nCancelled by user") |
| 169 | + sys.exit(1) |
0 commit comments