-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathdockerize.py
More file actions
289 lines (244 loc) · 7.98 KB
/
dockerize.py
File metadata and controls
289 lines (244 loc) · 7.98 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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
#!/usr/bin/env python3
"""
Generate docker-compose.yaml for running Rust distbench nodes.
This script reads a distbench configuration file and generates a docker-compose.yaml
that runs each node as a separate container in network mode.
"""
import argparse
import sys
from pathlib import Path
from typing import Any, Optional
import yaml
def load_config(config_path: Path) -> dict[str, Any]:
"""Load the YAML configuration file.
Args:
config_path: Path to the configuration file
Returns:
Dictionary containing the configuration
"""
with open(config_path, "r") as f:
config = yaml.safe_load(f)
# Filter out template keys (starting with _)
return {k: v for k, v in config.items() if not k.startswith("_")}
def generate_compose_config(
config: dict[str, Any],
config_path: Path,
algorithm: str,
format_type: str = "json",
timeout: int = 30,
verbosity: int = 0,
latency: str = "0-0",
startup_delay: int = 0,
report_dir: Optional[str] = None,
port: int = 8000,
) -> dict[str, Any]:
"""Generate docker-compose configuration for Rust nodes.
Args:
config: The loaded distbench configuration
config_path: Path to the config file (for mounting)
algorithm: Name of the algorithm to run
format_type: Serialization format ('json' or 'bincode')
timeout: Timeout in seconds
verbosity: Verbosity level (0-2)
latency: Latency range in milliseconds (e.g., "0-0" or "10-50")
startup_delay: Startup delay in milliseconds
report_dir: Optional directory for reports
port: Port number to use for all nodes (default: 8000)
Returns:
Dictionary representing the docker-compose configuration
"""
if format_type not in ["json", "bincode"]:
raise ValueError(
f"Rust supports 'json' or 'bincode' format, got '{format_type}'"
)
compose_config: dict[str, Any] = {
"services": {},
"networks": {"distbench-net": {"driver": "bridge"}},
}
image_name = "distbench-rust"
dockerfile = "Dockerfile"
# Build verbosity flag
verbose_flag = ""
if verbosity > 0:
verbose_flag = "-" + "v" * min(verbosity, 3)
# Get absolute path to config file
config_abs_path = config_path.resolve()
# Create a service for each node
for node_id in config.keys():
cmd = [
"--config",
"/config/config.yaml",
"--algorithm",
algorithm,
"--mode",
"network",
"--id",
node_id,
"--format",
format_type,
"--timeout",
str(timeout),
"--port-base",
str(port),
"--latency",
latency,
"--startup-delay",
str(startup_delay),
]
if verbose_flag:
cmd.append(verbose_flag)
if report_dir:
cmd.extend(["--report-folder", "/reports"])
# Create service configuration
service_config: dict[str, Any] = {
"image": f"{image_name}:latest",
"container_name": f"distbench-{node_id}",
"networks": ["distbench-net"],
"command": cmd,
"volumes": [
f"{config_abs_path}:/config/config.yaml:ro",
],
"build": {
"context": ".",
"dockerfile": dockerfile,
},
}
# Add report directory volume if specified
if report_dir:
report_abs_path = Path(report_dir).resolve()
service_config["volumes"].append(f"{report_abs_path}:/reports")
# Add environment variable for the node's info (informational)
service_config["environment"] = [
f"NODE_ID={node_id}",
f"PORT={port}",
]
compose_config["services"][node_id] = service_config
return compose_config
def main() -> None:
"""Main entry point for the compose generator."""
parser = argparse.ArgumentParser(
description="Generate docker-compose.yaml for Rust distbench",
formatter_class=argparse.RawDescriptionHelpFormatter,
epilog="""
Examples:
# Generate compose file
python dockerize.py -c configs/echo.yaml -a Echo -o docker-compose.yaml
# Generate with custom timeout and latency
python dockerize.py -c configs/echo.yaml -a Echo --timeout 60 --latency 10-50
""",
)
parser.add_argument(
"-c",
"--config",
type=Path,
required=True,
help="Path to the distbench configuration YAML file",
)
parser.add_argument(
"-a",
"--algorithm",
type=str,
required=True,
help="Name of the algorithm to run",
)
parser.add_argument(
"-o",
"--output",
type=Path,
default=Path("docker-compose.yaml"),
help="Output path for the generated docker-compose file (default: docker-compose.yaml)",
)
parser.add_argument(
"-f",
"--format",
type=str,
default="json",
help="Serialization format (json or bincode, default: json)",
)
parser.add_argument(
"-t",
"--timeout",
type=int,
default=30,
help="Timeout in seconds (default: 30)",
)
parser.add_argument(
"-v",
"--verbose",
action="count",
default=0,
help="Increase verbosity (-v, -vv, -vvv)",
)
parser.add_argument(
"--latency",
type=str,
default="0-0",
help="Network latency simulation in milliseconds (e.g., '10-50' for range, default: '0-0')",
)
parser.add_argument(
"--startup-delay",
type=int,
default=600,
help="Startup delay in milliseconds (default: 0)",
)
parser.add_argument(
"--report-dir",
type=str,
help="Directory for storing node reports (will be mounted as volume)",
)
parser.add_argument(
"--port",
type=int,
default=8000,
help="Port number for all nodes (default: 8000)",
)
args = parser.parse_args()
# Validate config file exists
if not args.config.exists():
print(f"Error: Config file '{args.config}' not found", file=sys.stderr)
sys.exit(1)
try:
# Load configuration
config = load_config(args.config)
# Generate docker-compose configuration
compose_config = generate_compose_config(
config=config,
config_path=args.config,
algorithm=args.algorithm,
format_type=args.format,
timeout=args.timeout,
verbosity=args.verbose,
latency=args.latency,
startup_delay=args.startup_delay,
report_dir=args.report_dir,
port=args.port,
)
# Write to output file
with open(args.output, "w") as f:
yaml.dump(compose_config, f, default_flow_style=False, sort_keys=False)
print(f"✓ Generated docker-compose configuration: {args.output}")
print(f" - Algorithm: {args.algorithm}")
print(f" - Nodes: {len(config)}")
print(f" - Port: {args.port}")
print(f" - Format: {args.format}")
print(f" - Timeout: {args.timeout}s")
if args.latency != "0-0":
print(f" - Latency: {args.latency}ms")
if args.startup_delay > 0:
print(f" - Startup delay: {args.startup_delay}ms")
if args.report_dir:
print(f" - Reports: {args.report_dir}")
print()
print("To start the distributed system, run:")
print(f" sudo docker-compose -f {args.output} up --build")
print()
print("To stop and clean up:")
print(f" sudo docker-compose -f {args.output} down")
except Exception as e:
print(f"Error: {e}", file=sys.stderr)
if args.verbose > 0:
import traceback
traceback.print_exc()
sys.exit(1)
if __name__ == "__main__":
main()