|
| 1 | +import os |
| 2 | +import platform |
| 3 | +import subprocess |
| 4 | +import sys |
| 5 | +from pathlib import Path |
| 6 | +import shutil |
| 7 | +import tempfile |
| 8 | +import urllib.request |
| 9 | + |
| 10 | +def get_platform_binary(): |
| 11 | + """Determine the correct binary for the current platform.""" |
| 12 | + system = platform.system().lower() |
| 13 | + arch = platform.machine().lower() |
| 14 | + |
| 15 | + # Normalize architecture names |
| 16 | + if arch in ["x86_64", "amd64"]: |
| 17 | + arch = "amd64" |
| 18 | + elif arch in ["arm64", "aarch64"]: |
| 19 | + arch = "arm64" |
| 20 | + else: |
| 21 | + raise RuntimeError(f"Unsupported architecture: {arch}") |
| 22 | + |
| 23 | + if system == "darwin": |
| 24 | + return f"kubernetes-mcp-server-darwin-{arch}" |
| 25 | + elif system == "linux": |
| 26 | + return f"kubernetes-mcp-server-linux-{arch}" |
| 27 | + elif system == "windows": |
| 28 | + return f"kubernetes-mcp-server-windows-{arch}.exe" |
| 29 | + else: |
| 30 | + raise RuntimeError(f"Unsupported operating system: {system}") |
| 31 | + |
| 32 | +def download_binary(version="latest", destination=None): |
| 33 | + """Download the correct binary for the current platform.""" |
| 34 | + binary_name = get_platform_binary() |
| 35 | + if destination is None: |
| 36 | + destination = Path.home() / ".kubernetes-mcp-server" / "bin" |
| 37 | + |
| 38 | + destination = Path(destination) |
| 39 | + destination.mkdir(parents=True, exist_ok=True) |
| 40 | + binary_path = destination / binary_name |
| 41 | + |
| 42 | + if binary_path.exists(): |
| 43 | + return binary_path |
| 44 | + |
| 45 | + base_url = "https://github.com/manusa/kubernetes-mcp-server/releases" |
| 46 | + if version == "latest": |
| 47 | + release_url = f"{base_url}/latest/download/{binary_name}" |
| 48 | + else: |
| 49 | + release_url = f"{base_url}/download/{version}/{binary_name}" |
| 50 | + |
| 51 | + # Download the binary |
| 52 | + print(f"Downloading {binary_name} from {release_url}") |
| 53 | + with tempfile.NamedTemporaryFile(delete=False) as temp_file: |
| 54 | + try: |
| 55 | + with urllib.request.urlopen(release_url) as response: |
| 56 | + shutil.copyfileobj(response, temp_file) |
| 57 | + temp_file.close() |
| 58 | + |
| 59 | + # Move to destination and make executable |
| 60 | + shutil.move(temp_file.name, binary_path) |
| 61 | + binary_path.chmod(binary_path.stat().st_mode | 0o755) # Make executable |
| 62 | + |
| 63 | + return binary_path |
| 64 | + except Exception as e: |
| 65 | + os.unlink(temp_file.name) |
| 66 | + raise RuntimeError(f"Failed to download binary: {e}") |
| 67 | + |
| 68 | +def execute(args=None): |
| 69 | + """Download and execute the kubernetes-mcp-server binary.""" |
| 70 | + if args is None: |
| 71 | + args = [] |
| 72 | + |
| 73 | + try: |
| 74 | + binary_path = download_binary() |
| 75 | + cmd = [str(binary_path)] + args |
| 76 | + |
| 77 | + # Execute the binary with the provided arguments |
| 78 | + process = subprocess.run(cmd) |
| 79 | + return process.returncode |
| 80 | + except Exception as e: |
| 81 | + print(f"Error executing kubernetes-mcp-server: {e}", file=sys.stderr) |
| 82 | + return 1 |
| 83 | + |
| 84 | +if __name__ == "__main__": |
| 85 | + sys.exit(execute(sys.argv[1:])) |
| 86 | + |
| 87 | + |
| 88 | +def main(): |
| 89 | + """Main function to execute the kubernetes-mcp-server binary.""" |
| 90 | + args = sys.argv[1:] if len(sys.argv) > 1 else [] |
| 91 | + return execute(args) |
0 commit comments