-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathbuild.ts
114 lines (99 loc) · 3.45 KB
/
build.ts
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
import { Flags, ux } from '@oclif/core'
import {execSync} from 'node:child_process'
import fspath from "node:path";
import BaseCmd from '../abstracts/base-cmd-abstract.js'
import ComposeConfig from '../interfaces/docker-compose-config-interface.js'
import YamlParser from '../yaml-parser.js'
import {AppSettings} from "../app-settings.js";
export default class BuildCmd extends BaseCmd {
static description = 'Commit the existing containers as new docker images, and create a new docker compose file referencing them.'
static examples = [
'$ ce-dev build --template example.compose.yml',
]
static flags = {
destination: Flags.string({
char: 'd',
default: 'ce-dev.compose.prebuilt.yml',
description: 'Path to the output docker compose file, relative to the project ce-dev folder.',
}),
help: Flags.help({char: 'h'}),
registry: Flags.string({
char: 'r',
default: '',
description: 'Docker registry to use. This overrides the one defined in the source compose template.',
}),
template: Flags.string({
char: 't',
default: 'ce-dev.compose.yml',
description: 'Path to a docker compose template file, relative to the project ce-dev folder. WARNING: this must match the original one the project was constructed with.',
}),
}
/**
* @member
* Docker compose content parsed from yaml.
*/
private composeConfig: ComposeConfig
/**
* @member
* Absolute path to the Compose file output.
*/
private composeDest: string = ''
/**
* @member
* Absolute path to the Compose file template.
*/
private composeTemplate: string = ''
/**
* @inheritdoc
*/
async run(): Promise<void> {
const {flags} = await this.parse(BuildCmd)
this.composeTemplate = this.getPathFromRelative(flags.template)
// @todo normalize path for destination.
this.composeDest = fspath.join(this.ceDevDir, flags.destination)
this.composeConfig = this.loadComposeConfig(this.composeTemplate)
if (flags.registry.length > 0) {
this.dockerRegistry = flags.registry
}
this.commit()
this.generateCompose()
}
/**
* Commit containers as base images.
*
* @return void
*/
private commit(): void {
for (const name of Object.keys(this.composeConfig?.services)) {
let containerName = name;
if (this.composeConfig['x-ce_dev']) {
containerName = this.composeConfig['x-ce_dev'].project_name + '-' + name
}
let version = AppSettings.ceDevVersion + '.x'
if (this.developmentMode) {
version = 'devel-' + version
}
ux.action.start('Committing container ' + containerName + ' as a new image.')
execSync(this.dockerBin + ' commit ' + containerName + ' ' + this.dockerRegistry + '/' + containerName + ':' + version, {stdio: 'inherit'})
ux.action.stop()
}
}
/**
* Generate derivative compose file.
*
* @return void
*/
private generateCompose(): void {
let version = AppSettings.ceDevVersion + '.x'
if (this.developmentMode) {
version = 'devel-' + version
}
ux.action.start('Generating new compose file ' + this.composeDest + '.')
for (const [name, service] of Object.entries(this.composeConfig.services)) {
const containerName = this.composeConfig['x-ce_dev'].project_name + '-' + name
service.image = this.dockerRegistry + '/' + containerName + ':' + version
}
YamlParser.writeYaml(this.composeDest, this.composeConfig)
ux.action.stop()
}
}