-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathbuild.py
More file actions
95 lines (82 loc) · 3.83 KB
/
build.py
File metadata and controls
95 lines (82 loc) · 3.83 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
import os
import sys
import subprocess
import logging
from utils import run_command
def build_sqlancer_image(script_log, docker_log, embedded, dbms, force_rebuild=False):
context_dir = "./sqlancer"
if embedded == "yes":
dockerfile_path = f"./{dbms}/Dockerfile"
script_log.info(f"Using embedded DBMS Dockerfile: {dockerfile_path}")
script_log.info("Embedded mode: rebuilding SQLancer image unconditionally ...")
run_command(
["docker", "build", "--no-cache", "-f", dockerfile_path, "-t", "sqlancer:latest", context_dir],
docker_log
)
script_log.info("SQLancer image built: sqlancer:latest")
return
else:
dockerfile_path = "./sqlancer/Dockerfile"
context_dir = "./sqlancer"
script_log.info(f"Using default SQLancer Dockerfile: {dockerfile_path}")
if force_rebuild:
script_log.info("Rebuilding SQLancer image: sqlancer:latest ...")
run_command(
["docker", "build", "--no-cache", "-f", dockerfile_path, "-t", "sqlancer:latest", context_dir],
docker_log
)
script_log.info("SQLancer image built: sqlancer:latest")
return
result = subprocess.run(
["docker", "images", "--format", "{{.Repository}}:{{.Tag}}"],
capture_output=True, text=True
)
images = result.stdout.strip().splitlines()
if "sqlancer:latest" in images:
script_log.info("SQLancer image exists: sqlancer:latest")
else:
script_log.info("Building SQLancer image from cache: sqlancer:latest ...")
run_command(
["docker", "build", "-f", dockerfile_path, "-t", "sqlancer:latest", context_dir],
docker_log
)
script_log.info("SQLancer image built: sqlancer:latest")
def build_network(script_log, docker_log, network_name="sqlancer-net"):
try:
output = subprocess.check_output([
"docker", "network", "ls",
"--filter", f"name={network_name}",
"--format", "{{.Name}}"
])
networks = output.decode().splitlines()
if network_name not in networks:
script_log.info("Building network: %s ...", network_name)
run_command(["docker", "network", "create", network_name], docker_log)
script_log.info("Network built: %s", network_name)
else:
script_log.info("Network already exists: %s", network_name)
except Exception as e:
script_log.error("Network building failed: %s", network_name)
sys.exit(1)
def build_db_image(cfg, use_cache, script_log, docker_log, custom=False, dockerfile_path=""):
image = f"{cfg['image_name']}:{cfg['tag']}"
if not use_cache and not custom:
script_log.info("Pulling db image: %s ...", image)
run_command(["docker", "pull", image], docker_log)
script_log.info("DB image pulled: %s", image)
elif custom:
build_cmd = ["docker", "build", "-t", image, os.path.dirname(dockerfile_path)]
if not use_cache:
build_cmd.insert(2, "--no-cache")
script_log.info("Building db image: %s ...", image)
run_command(build_cmd, docker_log)
script_log.info("DB image built: %s ...", image)
else:
script_log.info("DB image already exists: %s", image)
def build_environment(cfg, use_cache, script_log, docker_log, custom=False, dockerfile_path=""):
script_log.info("==============================Building environment==============================")
build_network(script_log, docker_log)
build_sqlancer_image(script_log, docker_log, cfg["embedded"], cfg["dbms"], force_rebuild=False)
if cfg["embedded"] == "no":
build_db_image(cfg, use_cache, script_log, docker_log, custom, dockerfile_path)
script_log.info("==============================Building environment==============================")