|
2 | 2 | """Command-line interface for Feel++ Docker factory.""" |
3 | 3 |
|
4 | 4 | import sys |
| 5 | +import subprocess |
5 | 6 | from pathlib import Path |
6 | 7 | from typing import Optional |
7 | 8 |
|
@@ -219,6 +220,118 @@ def matrix(ctx, variant: str, format: str): |
219 | 220 | sys.exit(1) |
220 | 221 |
|
221 | 222 |
|
| 223 | +@main.command() |
| 224 | +@click.argument('dist', required=True) |
| 225 | +@click.option('--variant', default='feelpp-env', help='Variant to build') |
| 226 | +@click.option('--tag', help='Docker tag (default: feelpp-env:DIST-VERSION)') |
| 227 | +@click.option('--platform', help='Platform to build for (e.g., linux/amd64)') |
| 228 | +@click.option('--no-cache', is_flag=True, help='Build without using cache') |
| 229 | +@click.option('--output-dir', type=click.Path(path_type=Path), |
| 230 | + default=Path.cwd(), |
| 231 | + help='Directory containing generated Dockerfiles') |
| 232 | +@click.pass_context |
| 233 | +def build(ctx, dist: str, variant: str, tag: Optional[str], |
| 234 | + platform: Optional[str], no_cache: bool, output_dir: Path): |
| 235 | + """Build Docker image locally for testing. |
| 236 | + |
| 237 | + DIST should be in format 'name:version' (e.g., ubuntu:24.04, debian:13) |
| 238 | + |
| 239 | + Examples: |
| 240 | + |
| 241 | + # Build Ubuntu 24.04 image |
| 242 | + ./factory.sh build ubuntu:24.04 |
| 243 | + |
| 244 | + # Build with custom tag |
| 245 | + ./factory.sh build debian:13 --tag test:debian-13 |
| 246 | + |
| 247 | + # Build for specific platform |
| 248 | + ./factory.sh build ubuntu:24.04 --platform linux/amd64 |
| 249 | + |
| 250 | + # Build without cache |
| 251 | + ./factory.sh build fedora:42 --no-cache |
| 252 | + """ |
| 253 | + config: Config = ctx.obj['config'] |
| 254 | + builder: ImageBuilder = ctx.obj['builder'] |
| 255 | + |
| 256 | + if variant not in config.variants: |
| 257 | + console.print(f"[red]Error:[/red] Unknown variant '{variant}'") |
| 258 | + console.print(f"Available variants: {', '.join(config.variants.keys())}") |
| 259 | + sys.exit(1) |
| 260 | + |
| 261 | + try: |
| 262 | + dist_name, version_tag = dist.split(':') |
| 263 | + except ValueError: |
| 264 | + console.print(f"[red]Error:[/red] DIST must be in format 'name:version' (got '{dist}')") |
| 265 | + console.print("Examples: ubuntu:24.04, debian:13, fedora:42") |
| 266 | + sys.exit(1) |
| 267 | + |
| 268 | + # Check if Dockerfile exists, generate if not |
| 269 | + dockerfile_dir = output_dir / f"{dist_name}-{version_tag}-clang++" |
| 270 | + dockerfile_path = dockerfile_dir / "Dockerfile" |
| 271 | + |
| 272 | + if not dockerfile_path.exists(): |
| 273 | + console.print(f"[yellow]Dockerfile not found, generating...[/yellow]") |
| 274 | + try: |
| 275 | + builder.generate_dockerfile(variant, dist_name, version_tag, dockerfile_dir) |
| 276 | + console.print(f"[green]✓[/green] Generated {dockerfile_path}") |
| 277 | + except Exception as e: |
| 278 | + console.print(f"[red]Error generating Dockerfile:[/red] {e}") |
| 279 | + sys.exit(1) |
| 280 | + |
| 281 | + # Prepare Docker build command |
| 282 | + if not tag: |
| 283 | + tag = f"feelpp-env:{dist_name}-{version_tag}" |
| 284 | + |
| 285 | + build_cmd = [ |
| 286 | + "docker", "build", |
| 287 | + "-t", tag, |
| 288 | + "-f", str(dockerfile_path), |
| 289 | + ] |
| 290 | + |
| 291 | + if platform: |
| 292 | + build_cmd.extend(["--platform", platform]) |
| 293 | + |
| 294 | + if no_cache: |
| 295 | + build_cmd.append("--no-cache") |
| 296 | + |
| 297 | + # Context is the directory containing the Dockerfile |
| 298 | + build_cmd.append(str(dockerfile_dir)) |
| 299 | + |
| 300 | + console.print(f"\n[blue]Building Docker image:[/blue]") |
| 301 | + console.print(f" Distribution: {dist}") |
| 302 | + console.print(f" Tag: {tag}") |
| 303 | + console.print(f" Dockerfile: {dockerfile_path}") |
| 304 | + if platform: |
| 305 | + console.print(f" Platform: {platform}") |
| 306 | + console.print() |
| 307 | + |
| 308 | + # Show the command |
| 309 | + console.print(f"[dim]$ {' '.join(build_cmd)}[/dim]\n") |
| 310 | + |
| 311 | + # Run Docker build |
| 312 | + try: |
| 313 | + result = subprocess.run( |
| 314 | + build_cmd, |
| 315 | + check=False, |
| 316 | + cwd=dockerfile_dir.parent |
| 317 | + ) |
| 318 | + |
| 319 | + if result.returncode == 0: |
| 320 | + console.print(f"\n[green]✓ Successfully built {tag}[/green]") |
| 321 | + console.print(f"\nTo run the image:") |
| 322 | + console.print(f" docker run -it --rm {tag}") |
| 323 | + else: |
| 324 | + console.print(f"\n[red]✗ Build failed with exit code {result.returncode}[/red]") |
| 325 | + sys.exit(result.returncode) |
| 326 | + |
| 327 | + except FileNotFoundError: |
| 328 | + console.print("[red]Error:[/red] Docker command not found. Is Docker installed?") |
| 329 | + sys.exit(1) |
| 330 | + except KeyboardInterrupt: |
| 331 | + console.print("\n[yellow]Build interrupted by user[/yellow]") |
| 332 | + sys.exit(130) |
| 333 | + |
| 334 | + |
222 | 335 | @main.command() |
223 | 336 | @click.option('--variant', default='feelpp-env', help='Variant to update CI for') |
224 | 337 | @click.option('--workflow-file', type=click.Path(path_type=Path), |
|
0 commit comments