-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathcontroller-manager.ts
318 lines (291 loc) · 8.03 KB
/
controller-manager.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
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
import { Config, ux } from '@oclif/core'
import {execSync} from 'node:child_process'
import fs from 'node:fs'
import fspath from "node:path";
import {AppSettings} from './app-settings.js'
import DockerComposeConfigBare from './interfaces/docker-compose-config-bare-interface.js'
import IPManager from './ip-manager.js'
import YamlParser from './yaml-parser.js'
export default class ControllerManager {
/**
* @member
* Config from oclif.
*/
private readonly config: Config
/**
* @member
* Compose file path for our controller definition.
*/
private readonly controllerComposeFile: string
/**
* @member
* Docker executable path.
*/
private readonly dockerBin: string = 'docker'
/**
* @member
* Docker compose executable path.
*/
private readonly dockerComposeBin: string = 'docker compose'
/**
* @member
* Mkcert executable path.
*/
private readonly mkcertBin: string = 'mkcert'
/**
* @member
* Compose file path for our network definition.
*/
private readonly networkComposeFile: string
/**
* @member
* Development mode or not.
*/
protected developmentMode: boolean = false
public constructor(config: Config, dockerBin: string, dockerComposeBin: string, mkcertBin: string) {
this.dockerBin = dockerBin
this.dockerComposeBin = dockerComposeBin
this.mkcertBin = mkcertBin
this.config = config
this.controllerComposeFile = fspath.join(
this.config.dataDir,
'docker-compose.controller.yml',
)
this.networkComposeFile = fspath.join(
this.config.dataDir,
'docker-compose.network.yml',
)
if ((typeof process.env.NODE_ENV !== 'undefined') && (process.env.NODE_ENV === 'development')) {
this.developmentMode = true;
}
}
/**
* Check if our controller is up and running.
*
* @returns
* Whether controller already exists and is up.
*/
public controllerExists(): boolean {
const existing = execSync(
this.dockerBin + ' ps | grep -w ce_dev_controller | wc -l',
)
.toString()
.trim()
if (existing === '0') {
return false
}
return true
}
/**
* Start our controller.
*
* @return void
*/
public controllerStart(): void {
YamlParser.writeYaml(this.controllerComposeFile, this.getControllerConfig())
execSync(
this.dockerComposeBin +
' -f ' +
this.controllerComposeFile +
' -p ce_dev_controller up -d',
{cwd: this.config.dataDir, stdio: 'inherit'},
)
// Ensure uid/gid.
ux.action.start('Ensure user UID match those on the host')
let uid = 1000
let gid = 1000
if (process.getuid) {
uid = process.getuid()
}
if (process.getgid && process.getegid && process.getgid() > 1000) {
gid = process.getegid()
}
execSync(
this.dockerBin +
' exec ce_dev_controller /bin/sh /opt/ce-dev-ownership.sh ' +
uid.toString() +
' ' +
gid.toString(),
{stdio: 'inherit'},
)
ux.action.stop()
// Regenerate SSH keys.
ux.action.start('Regenerate containers SSH key')
execSync(
this.dockerBin + ' exec ce_dev_controller /bin/sh /opt/ce-dev-ssh.sh',
)
ux.action.stop()
// Ensure file perms.
ux.action.start('Ensure file ownership')
execSync(
this.dockerBin + ' exec ce_dev_controller chown -R ce-dev:ce-dev /home/ce-dev',
)
ux.action.stop()
// Ensure CA.
ux.action.start('Generate/renew CA certificate for SSL')
execSync(
this.dockerBin + ' exec --user ce-dev ce_dev_controller /usr/local/bin/mkcert -install',
{stdio: ['ignore', 'ignore', 'pipe']},
)
ux.action.stop()
}
/**
* Stop our controller.
*
* @return void
*/
public controllerStop(): void {
const existing = execSync(
this.dockerBin + ' ps | grep -w ce_dev_controller | wc -l',
)
.toString()
.trim()
if (existing !== '0') {
execSync(this.dockerBin + ' stop ce_dev_controller')
}
}
/**
* Generate an SSL certificate.
*
* @param domain
* Domain/host name.
*
* @return void
*/
public generateCertificate(domain: string): void {
execSync(
this.dockerBin + ' exec --workdir /home/ce-dev/.local/share/mkcert --user ce-dev ce_dev_controller /usr/local/bin/mkcert ' + domain,
{stdio: ['ignore', 'ignore', 'pipe']},
)
}
/**
* Installs CA for mkcerts on the host.
*
* @return void
*/
public installCertificateAuth(): void {
const currentCert = fspath.resolve(this.config.cacheDir + '/rootCA.pem')
const existingCert = fspath.resolve(this.config.dataDir + '/rootCA.pem')
// Copy new (or possibly new) cert.
execSync(
this.dockerBin + ' cp ce_dev_controller:/home/ce-dev/.local/share/mkcert/rootCA.pem ' + currentCert,
)
// Check if we have a new certs.
if (fs.existsSync(existingCert)) {
const currentCertBuffer = fs.readFileSync(currentCert)
const existingCertBuffer = fs.readFileSync(existingCert)
if (currentCertBuffer.equals(existingCertBuffer)) {
return
}
}
// New/changed certs.
ux.action.start('Install mkcert CA on the host.')
fs.renameSync(currentCert, existingCert)
process.env.CAROOT = this.config.dataDir
execSync(
this.mkcertBin + ' -install', {env: process.env},
)
ux.action.stop()
}
/**
* Check if our network is up and running.
*
* @returns
* True if network exists, else false.
*/
public networkExists(): boolean {
const existing = execSync(
this.dockerBin + ' network ls | grep -w ce_dev | wc -l',
).toString().trim();
if (existing === '0') {
return false
}
return true
}
/**
* Start our network.
*
* @return void
*/
public networkStart(): void {
const ipManager = new IPManager(this.config, this.dockerBin)
const base = ipManager.getNetBase()
const gw = base + '.1'
const subnet = base + '.0/24'
execSync(this.dockerBin + ' network create ce_dev --attachable --gateway ' + gw + ' --subnet ' + subnet, {
cwd: this.config.dataDir,
stdio: 'inherit',
})
}
/**
* Pull our controller image.
*
* @return void
*/
public pullImage(): void {
// If in development mode, we use devel version.
let version = AppSettings.ceDevVersion + '.x';
if (this.developmentMode) {
version = 'devel-' + version
}
execSync(this.dockerBin + ' pull codeenigma/ce-dev-controller:' + version, {
stdio: 'inherit',
})
}
private getControllerConfig(): DockerComposeConfigBare {
const ipManager = new IPManager(this.config, this.dockerBin)
let version = AppSettings.ceDevVersion + '.x'
if (this.developmentMode) {
version = 'devel-' + version
}
return {
networks: {
ce_dev: {
external: true,
name: 'ce_dev',
},
},
services: {
ce_dev_controller: {
cgroup: 'host',
container_name: 'ce_dev_controller',
hostname: 'ce_dev_controller',
image: 'codeenigma/ce-dev-controller:' + version,
networks: {
ce_dev: {
ipv4_address: ipManager.getControllerIP(),
},
},
platform: 'linux/amd64',
volumes: [
'ce_dev_ssh:/home/ce-dev/.ssh',
'ce_dev_mkcert:/home/ce-dev/.local/share/mkcert',
'ce_dev_apt_cache:/var/cache/apt/archives',
'ce_dev_composer_cache:/home/ce-dev/.composer/cache',
'ce_dev_nvm_node:/home/ce-dev/.nvm/versions/node',
'/sys/fs/cgroup:/sys/fs/cgroup:rw',
this.config.cacheDir + ':/home/ce-dev/.ce-dev-cache',
],
},
},
version: '3.7',
volumes: {
ce_dev_apt_cache: {
name: 'ce_dev_apt_cache',
},
ce_dev_composer_cache: {
name: 'ce_dev_composer_cache',
},
ce_dev_mkcert: {
name: 'ce_dev_mkcert',
},
ce_dev_nvm_node: {
name: 'ce_dev_nvm_node',
},
ce_dev_ssh: {
name: 'ce_dev_ssh',
},
},
}
}
}