-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgenerate_docker.js
More file actions
executable file
·462 lines (382 loc) · 17.2 KB
/
generate_docker.js
File metadata and controls
executable file
·462 lines (382 loc) · 17.2 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
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
#!/usr/bin/env node
const fs = require('fs');
const path = require('path');
function loadConfig() {
const configPath = path.join(__dirname, 'config.json');
const configData = fs.readFileSync(configPath, 'utf8');
return JSON.parse(configData);
}
const config = loadConfig();
const PROJECT_CONFIGS = config.projects;
const INTERMEDIATE_TEMPLATES = config.intermediate_templates;
/**
* Generate cache warming commands for a project
* @param {Object} cacheWarm - The cache_warm configuration object
* @returns {string} Dockerfile RUN commands for cache warming
*/
function generateCacheWarmCommands(cacheWarm) {
if (!cacheWarm) return '';
const commands = [];
// NPM cache warming
if (cacheWarm.npm && cacheWarm.npm.length > 0) {
commands.push(`# Pre-warm npm cache with project-specific packages`);
commands.push(`RUN npm cache add ${cacheWarm.npm.join(' ')} || true`);
}
// Cargo cache warming (fetch crates without building)
if (cacheWarm.cargo && cacheWarm.cargo.length > 0) {
// Create a temporary Cargo.toml to fetch dependencies
const deps = cacheWarm.cargo.map(crate => {
const [name, version] = crate.split('@');
return `${name} = "${version || '*'}"`;
}).join('\\n');
commands.push(`# Pre-warm cargo cache with project-specific crates`);
commands.push(`RUN mkdir -p /tmp/cargo-warm && \\`);
commands.push(` printf '[package]\\nname = "warm"\\nversion = "0.0.0"\\nedition = "2021"\\n\\n[dependencies]\\n${deps}\\n' > /tmp/cargo-warm/Cargo.toml && \\`);
commands.push(` mkdir -p /tmp/cargo-warm/src && echo 'fn main() {}' > /tmp/cargo-warm/src/main.rs && \\`);
commands.push(` cd /tmp/cargo-warm && cargo fetch && \\`);
commands.push(` rm -rf /tmp/cargo-warm`);
commands.push(`\nUSER root`);
commands.push(`RUN chmod -R a+w $CARGO_HOME`);
commands.push(`USER agent`);
}
// pip cache warming
if (cacheWarm.pip && cacheWarm.pip.length > 0) {
commands.push(`# Pre-warm pip cache with project-specific packages`);
commands.push(`RUN pip download --dest /tmp/pip-warm ${cacheWarm.pip.join(' ')} && rm -rf /tmp/pip-warm`);
}
return commands.length > 0 ? '\n' + commands.join('\n') + '\n' : '';
}
function formatEnvValue(value) {
const stringValue = String(value);
return /\s/.test(stringValue) ? JSON.stringify(stringValue) : stringValue;
}
function normalizeRootCommand(command) {
if (typeof command !== 'string') {
return command;
}
if (!command.includes('apt-get install')) {
return command;
}
if (command.includes('DEBIAN_FRONTEND=noninteractive apt-get install')) {
return command;
}
return command.replace(/apt-get\s+install/g, 'DEBIAN_FRONTEND=noninteractive apt-get install');
}
function buildCargoInstallCommand(pkg) {
if (typeof pkg === 'string') {
if (pkg.includes('@')) {
const [name, version] = pkg.split('@');
return `cargo install ${name} --version ${version}`;
}
return `cargo install ${pkg}`;
}
if (!pkg || typeof pkg !== 'object') {
throw new Error(`Invalid cargo package spec: ${pkg}`);
}
const crate = pkg.name || pkg.package || pkg.crate;
if (!crate) {
throw new Error(`Cargo package object must include one of: name, package, crate`);
}
const commandParts = ['cargo install'];
if (pkg.git) commandParts.push(`--git ${pkg.git}`);
if (pkg.branch) commandParts.push(`--branch ${pkg.branch}`);
if (pkg.tag) commandParts.push(`--tag ${pkg.tag}`);
if (pkg.rev) commandParts.push(`--rev ${pkg.rev}`);
if (pkg.version && !pkg.git) commandParts.push(`--version ${pkg.version}`);
if (pkg.locked) commandParts.push(`--locked`);
if (pkg.force) commandParts.push(`--force`);
if (Array.isArray(pkg.features) && pkg.features.length > 0) {
commandParts.push(`--features ${pkg.features.join(',')}`);
}
commandParts.push(crate);
return commandParts.join(' ');
}
function dedupeCargoSpecs(cargoSpecs) {
const seen = new Set();
const uniqueSpecs = [];
for (const pkg of cargoSpecs) {
const key = typeof pkg === 'string'
? `str:${pkg}`
: `obj:${JSON.stringify(pkg, Object.keys(pkg).sort())}`;
if (!seen.has(key)) {
seen.add(key);
uniqueSpecs.push(pkg);
}
}
return uniqueSpecs;
}
function generateIntermediateDockerfile(base, outputDir) {
if (!(base in INTERMEDIATE_TEMPLATES)) {
throw new Error(`Unknown base: ${base}`);
}
const filepath = path.join(outputDir, `${base}.Dockerfile`);
fs.writeFileSync(filepath, INTERMEDIATE_TEMPLATES[base]);
console.log(`Generated intermediate: ${filepath}`);
}
function generateInfraDockerfile(project, config, outputDir) {
const base = config.base;
const packages = config.packages;
const customInstall = config.custom_install;
const dockerfileLines = [`FROM ${base}:latest\n`];
if (customInstall && customInstall.env) {
dockerfileLines.push('\n');
const envVars = Object.entries(customInstall.env)
.map(([key, value]) => ` ${key}=${formatEnvValue(value)}`)
.join(' \\\n');
dockerfileLines.push(`ENV ${envVars}\n`);
}
if (customInstall && customInstall.apt_packages && customInstall.apt_packages.length > 0) {
const aptPackages = customInstall.apt_packages.join(' ');
dockerfileLines.push(`\nUSER root\n`);
dockerfileLines.push(`RUN apt-get update && \\\n`);
dockerfileLines.push(` DEBIAN_FRONTEND=noninteractive apt-get install -y --no-install-recommends \\\n`);
dockerfileLines.push(` ${aptPackages} && \\\n`);
dockerfileLines.push(` rm -rf /var/lib/apt/lists/*\n`);
dockerfileLines.push(`\nUSER agent\n`);
}
if (customInstall && customInstall.root_commands && customInstall.root_commands.length > 0) {
dockerfileLines.push(`\nUSER root\n`);
dockerfileLines.push(`RUN export HOME=/root && \\\n `);
const commands = customInstall.root_commands
.map(normalizeRootCommand)
.join(' && \\\n ');
dockerfileLines.push(`${commands}\n`);
dockerfileLines.push(`\nUSER agent\n`);
}
if (customInstall && customInstall.commands && customInstall.commands.length > 0) {
dockerfileLines.push(`\n`);
const commands = customInstall.commands.join(' && \\\n ');
dockerfileLines.push(`RUN ${commands}\n`);
}
if (packages.npm && packages.npm.length > 0) {
const npmPackages = packages.npm.join(' ');
dockerfileLines.push(`\nUSER root\n`);
dockerfileLines.push(`RUN npm install -g ${npmPackages} && chown -R agent:agent /tmp/.npm-cache 2>/dev/null || true\n`);
dockerfileLines.push(`USER agent\n`);
}
if (packages.cargo && packages.cargo.length > 0) {
for (const pkg of packages.cargo) {
const cargoCmd = buildCargoInstallCommand(pkg);
dockerfileLines.push(`\nRUN ${cargoCmd}\n`);
}
}
// Cache warming (pre-fetch packages without installing)
if (config.cache_warm) {
dockerfileLines.push(generateCacheWarmCommands(config.cache_warm));
}
dockerfileLines.push(`\nLABEL description="${project} infrastructure layer"\n`);
const filepath = path.join(outputDir, `${project}.Dockerfile`);
fs.writeFileSync(filepath, dockerfileLines.join(''));
console.log(`Generated infra: ${filepath}`);
}
function generateCombinedDockerfile(projectNames, outputDir) {
const sortedProjectNames = [...projectNames].sort();
if (JSON.stringify(projectNames) !== JSON.stringify(sortedProjectNames)) {
console.log(`Sorting projects alphabetically: ${sortedProjectNames.join('_')}`);
}
const configs = [];
let base = null;
for (const name of sortedProjectNames) {
if (!(name in PROJECT_CONFIGS)) {
console.log(`Error: Unknown project '${name}'`);
return false;
}
const config = PROJECT_CONFIGS[name];
const projectBase = config.base;
if (base === null) {
base = projectBase;
} else if (base !== projectBase) {
console.log(`Error: Cannot combine projects with different base images`);
console.log(` ${sortedProjectNames[0]} uses '${base}'`);
console.log(` ${name} uses '${projectBase}'`);
return false;
}
configs.push([name, config]);
}
const combinedName = sortedProjectNames.join('_');
const dockerfileLines = [`FROM ${base}:latest\n`];
const allNpmPackages = [];
const allCargoPackages = [];
const allEnvVars = {};
const allAptPackages = [];
const allRootCommands = [];
const allCommands = [];
const allCacheWarm = { npm: [], cargo: [], pip: [] };
for (const [name, config] of configs) {
const packages = config.packages;
if (packages.npm && packages.npm.length > 0) {
allNpmPackages.push(...packages.npm);
}
if (packages.cargo && packages.cargo.length > 0) {
allCargoPackages.push(...packages.cargo);
}
if (config.custom_install) {
if (config.custom_install.env) {
Object.assign(allEnvVars, config.custom_install.env);
}
if (config.custom_install.apt_packages) {
allAptPackages.push(...config.custom_install.apt_packages);
}
if (config.custom_install.root_commands) {
allRootCommands.push(...config.custom_install.root_commands);
}
if (config.custom_install.commands) {
allCommands.push(...config.custom_install.commands);
}
}
// Collect cache_warm configs
if (config.cache_warm) {
if (config.cache_warm.npm) allCacheWarm.npm.push(...config.cache_warm.npm);
if (config.cache_warm.cargo) allCacheWarm.cargo.push(...config.cache_warm.cargo);
if (config.cache_warm.pip) allCacheWarm.pip.push(...config.cache_warm.pip);
}
}
const uniqueNpmPackages = [...new Set(allNpmPackages)];
const uniqueCargoPackages = dedupeCargoSpecs(allCargoPackages);
const uniqueAptPackages = [...new Set(allAptPackages)];
if (Object.keys(allEnvVars).length > 0) {
dockerfileLines.push('\n');
const envVars = Object.entries(allEnvVars)
.map(([key, value]) => ` ${key}=${formatEnvValue(value)}`)
.join(' \\\n');
dockerfileLines.push(`ENV ${envVars}\n`);
}
if (uniqueAptPackages.length > 0) {
const aptPackages = uniqueAptPackages.join(' ');
dockerfileLines.push(`\nUSER root\n`);
dockerfileLines.push(`RUN apt-get update && \\\n`);
dockerfileLines.push(` DEBIAN_FRONTEND=noninteractive apt-get install -y --no-install-recommends \\\n`);
dockerfileLines.push(` ${aptPackages} && \\\n`);
dockerfileLines.push(` rm -rf /var/lib/apt/lists/*\n`);
dockerfileLines.push(`\nUSER agent\n`);
}
if (allRootCommands.length > 0) {
dockerfileLines.push(`\nUSER root\n`);
dockerfileLines.push(`RUN export HOME=/root && \\\n `);
const commands = allRootCommands
.map(normalizeRootCommand)
.join(' && \\\n ');
dockerfileLines.push(`${commands}\n`);
dockerfileLines.push(`\nUSER agent\n`);
}
if (allCommands.length > 0) {
dockerfileLines.push(`\n`);
const commands = allCommands.join(' && \\\n ');
dockerfileLines.push(`RUN ${commands}\n`);
}
if (uniqueNpmPackages.length > 0) {
const npmPackages = uniqueNpmPackages.join(' ');
dockerfileLines.push(`\nUSER root\n`);
dockerfileLines.push(`RUN npm install -g ${npmPackages} && chown -R agent:agent /tmp/.npm-cache 2>/dev/null || true\n`);
dockerfileLines.push(`USER agent\n`);
}
if (uniqueCargoPackages.length > 0) {
for (const pkg of uniqueCargoPackages) {
const cargoCmd = buildCargoInstallCommand(pkg);
dockerfileLines.push(`\nRUN ${cargoCmd}\n`);
}
}
// Combined cache warming (deduplicated)
const mergedCacheWarm = {
npm: [...new Set(allCacheWarm.npm)],
cargo: [...new Set(allCacheWarm.cargo)],
pip: [...new Set(allCacheWarm.pip)]
};
const hasCacheWarm = mergedCacheWarm.npm.length > 0 || mergedCacheWarm.cargo.length > 0 || mergedCacheWarm.pip.length > 0;
if (hasCacheWarm) {
dockerfileLines.push(generateCacheWarmCommands(mergedCacheWarm));
}
const projectsDesc = sortedProjectNames.join(', ');
dockerfileLines.push(`\nLABEL description="Combined: ${projectsDesc}"\n`);
const filepath = path.join(outputDir, `${combinedName}.Dockerfile`);
fs.writeFileSync(filepath, dockerfileLines.join(''));
console.log(`Generated combined infra: ${filepath}`);
return true;
}
function main() {
if (process.argv.length < 3) {
console.log('Usage: node generate_docker.js <project_name> [project_name2 ...]');
console.log(' node generate_docker.js <project1_project2_...>');
console.log('\nAvailable projects:');
for (const project of Object.keys(PROJECT_CONFIGS).sort()) {
console.log(` - ${project}`);
}
console.log('\nExamples:');
console.log(' node generate_docker.js coinbase');
console.log(' node generate_docker.js coinbase_mongodb_postgresql');
console.log(' node generate_docker.js ethereum solana');
console.log('\nNote: Combined projects are automatically sorted alphabetically.');
console.log(' ethereum_polygon_zksync and zksync_polygon_ethereum generate the same file.');
process.exit(1);
}
const rootDir = __dirname;
const intermediateDir = path.join(rootDir, 'intermediate');
const infraDir = path.join(rootDir, 'infra');
if (!fs.existsSync(intermediateDir)) {
fs.mkdirSync(intermediateDir, { recursive: true });
}
if (!fs.existsSync(infraDir)) {
fs.mkdirSync(infraDir, { recursive: true });
}
const projects = process.argv.slice(2);
for (const projectSpec of projects) {
const normalizedSpec = projectSpec.toLowerCase();
if (normalizedSpec.includes('_')) {
const projectNames = normalizedSpec.split('_').map(p => p.trim());
if (projectNames.length === 0) {
console.log(`Error: Invalid project specification '${projectSpec}'`);
continue;
}
if (projectNames.length === 1) {
const project = projectNames[0];
if (!(project in PROJECT_CONFIGS)) {
console.log(`Error: Unknown project '${project}'`);
continue;
}
const config = PROJECT_CONFIGS[project];
const base = config.base;
if (base !== 'base-system') {
const intermediateFile = path.join(intermediateDir, `${base}.Dockerfile`);
if (!fs.existsSync(intermediateFile) || fs.statSync(intermediateFile).size === 0) {
generateIntermediateDockerfile(base, intermediateDir);
}
}
generateInfraDockerfile(project, config, infraDir);
} else {
const firstProject = projectNames[0];
if (firstProject in PROJECT_CONFIGS) {
const base = PROJECT_CONFIGS[firstProject].base;
if (base !== 'base-system') {
const intermediateFile = path.join(intermediateDir, `${base}.Dockerfile`);
if (!fs.existsSync(intermediateFile) || fs.statSync(intermediateFile).size === 0) {
generateIntermediateDockerfile(base, intermediateDir);
}
}
}
generateCombinedDockerfile(projectNames, infraDir);
}
} else {
const project = normalizedSpec;
if (!(project in PROJECT_CONFIGS)) {
console.log(`Error: Unknown project '${project}'`);
console.log('\nAvailable projects:');
for (const p of Object.keys(PROJECT_CONFIGS).sort()) {
console.log(` - ${p}`);
}
continue;
}
const config = PROJECT_CONFIGS[project];
const base = config.base;
if (base !== 'base-system') {
const intermediateFile = path.join(intermediateDir, `${base}.Dockerfile`);
if (!fs.existsSync(intermediateFile) || fs.statSync(intermediateFile).size === 0) {
generateIntermediateDockerfile(base, intermediateDir);
}
}
generateInfraDockerfile(project, config, infraDir);
}
}
}
if (require.main === module) {
main();
}