|
2 | 2 | import sys |
3 | 3 | import subprocess |
4 | 4 |
|
5 | | -def build_sqlancer(): |
6 | | - subprocess.run(["docker", "build", "-t", "sqlancer:latest", "./sqlancer"], check=True) |
| 5 | +def run_command(cmd, **kwargs): |
| 6 | + print("[CMD]", " ".join(cmd)) |
| 7 | + subprocess.run(cmd, check=True, **kwargs) |
7 | 8 |
|
8 | | -def build_db(dbms, version): |
9 | | - tag = f"{dbms}:{version}" |
10 | | - subprocess.run(["docker", "pull", tag], check=True) |
| 9 | +def build_sqlancer_image(force_rebuild=False): |
| 10 | + if force_rebuild: |
| 11 | + print("[INFO] Rebuilding SQLancer image due to --cache not specified...") |
| 12 | + run_command(["docker", "build", "--no-cache", "-t", "sqlancer:latest", "./sqlancer"]) |
| 13 | + return |
11 | 14 |
|
12 | | -if __name__ == "__main__": |
13 | | - cmd = sys.argv[1] |
14 | | - if cmd == "sqlancer": |
15 | | - build_sqlancer() |
16 | | - elif cmd == "db": |
17 | | - dbms = sys.argv[2] |
18 | | - version = sys.argv[4] |
19 | | - build_db(dbms, version) |
| 15 | + result = subprocess.run( |
| 16 | + ["docker", "images", "--format", "{{.Repository}}:{{.Tag}}"], |
| 17 | + capture_output=True, text=True |
| 18 | + ) |
| 19 | + images = result.stdout.strip().splitlines() |
| 20 | + if "sqlancer:latest" in images: |
| 21 | + print("[INFO] SQLancer image already exists: sqlancer:latest") |
| 22 | + else: |
| 23 | + print("[INFO] SQLancer image not found. Building from ./sqlancer...") |
| 24 | + run_command(["docker", "build", "-t", "sqlancer:latest", "./sqlancer"]) |
| 25 | + |
| 26 | +def build_network(network_name="sqlancer-net"): |
| 27 | + try: |
| 28 | + output = subprocess.check_output([ |
| 29 | + "docker", "network", "ls", |
| 30 | + "--filter", f"name={network_name}", |
| 31 | + "--format", "{{.Name}}" |
| 32 | + ]) |
| 33 | + networks = output.decode().splitlines() |
| 34 | + if network_name not in networks: |
| 35 | + print(f"[INFO] Creating docker network: {network_name}") |
| 36 | + subprocess.run(["docker", "network", "create", network_name], check=True) |
| 37 | + else: |
| 38 | + print(f"[INFO] Docker network '{network_name}' already exists.") |
| 39 | + except Exception as e: |
| 40 | + print(f"[ERROR] Failed to check/create Docker network: {e}") |
| 41 | + sys.exit(1) |
| 42 | + |
| 43 | +def build_db_image(cfg, use_cache, custom=False, dockerfile_path=""): |
| 44 | + if not use_cache and not custom: |
| 45 | + image = cfg["image"] |
| 46 | + print(f"[INFO] Pulling image {image}") |
| 47 | + run_command(["docker", "pull", image]) |
| 48 | + |
| 49 | + if custom: |
| 50 | + build_cmd = ["docker", "build", "-t", cfg["image"], os.path.dirname(dockerfile_path)] |
| 51 | + if not use_cache: |
| 52 | + build_cmd.insert(2, "--no-cache") |
| 53 | + run_command(build_cmd) |
| 54 | + |
| 55 | +def build_environment(cfg, use_cache, custom=False, dockerfile_path=""): |
| 56 | + build_network() |
| 57 | + build_sqlancer_image(force_rebuild=not use_cache) |
| 58 | + build_db_image(cfg, use_cache, custom, dockerfile_path) |
| 59 | + |
0 commit comments