forked from kuzzleio/kourou
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathspawn.ts
278 lines (248 loc) · 7.89 KB
/
spawn.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
import { flags } from '@oclif/command'
import chalk from 'chalk'
import { ChildProcess, spawn } from 'child_process'
import cli from 'cli-ux'
import { writeFileSync } from 'fs'
import Listr from 'listr'
import net from 'net'
import emoji from 'node-emoji'
import { Kommand } from '../../common'
import { execute } from '../../support/execute'
const MIN_MAX_MAP_COUNT = 262144
const MIN_DOCO_VERSION = '1.12.0'
const kuzzleStackV1 = (increment: number): string => `
version: '3'
services:
kuzzle:
image: kuzzleio/kuzzle:1
ports:
- "${7512 + increment}:7512"
- "${1883 + increment}:1883"
- "${9229 + increment}:9229"
cap_add:
- SYS_PTRACE
depends_on:
- redis
- elasticsearch
environment:
- kuzzle_services__db__client__host=http://elasticsearch:9200
- kuzzle_services__internalCache__node__host=redis
- kuzzle_services__memoryStorage__node__host=redis
- NODE_ENV=development
- DEBUG=kuzzle:*,-kuzzle:entry-point:protocols:websocket
redis:
image: redis:5
elasticsearch:
image: kuzzleio/elasticsearch:5.6.10
ports:
- "${9200 + increment}:9200"
ulimits:
nofile: 65536
environment:
- cluster.name=kuzzle
- "ES_JAVA_OPTS=-Xms1024m -Xmx1024m"
`
const kuzzleStackV2 = (increment: number): string => `
version: '3'
services:
kuzzle:
image: kuzzleio/kuzzle:2
ports:
- "${7512 + increment}:7512"
- "${1883 + increment}:1883"
- "${9229 + increment}:9229"
cap_add:
- SYS_PTRACE
depends_on:
- redis
- elasticsearch
environment:
- kuzzle_services__storageEngine__client__node=http://elasticsearch:9200
- kuzzle_services__internalCache__node__host=redis
- kuzzle_services__memoryStorage__node__host=redis
- kuzzle_server__protocols__mqtt__enabled=true
- kuzzle_server__protocols__mqtt__developmentMode=false
- kuzzle_limits__loginsPerSecond=50
- NODE_ENV=development
- DEBUG=kuzzle:*,-kuzzle:entry-point:protocols:websocket
redis:
image: redis:5
elasticsearch:
image: kuzzleio/elasticsearch:7
ports:
- "${9200 + increment}:9200"
ulimits:
nofile: 65536
`
export default class InstanceSpawn extends Kommand {
static initSdk = false
public static description = 'Spawn a new Kuzzle instance';
public static flags = {
help: flags.help(),
check: flags.boolean({
description: 'Check prerequisite before running Kuzzle',
default: false,
}),
version: flags.string({
char: 'v',
description: 'Core-version of the instance to spawn',
default: '2',
}),
};
async runSafe() {
const portIndex = await this.findAvailablePort()
const docoFilename = `/tmp/kuzzle-stack-${portIndex}.yml`
const successfullCheck = this.flags.check ?
await this.checkPrerequisites() :
true
if (this.flags.check && successfullCheck) {
this.log(
`\n${emoji.get('ok_hand')} Prerequisites are ${chalk.green.bold('OK')}!`)
} else if (this.flags.check && !successfullCheck) {
throw new Error(
`${emoji.get('shrug')} Your system doesn't satisfy all the prerequisites. Cannot run Kuzzle.`)
}
this.log(
chalk.grey(`\nWriting docker-compose file to ${docoFilename}...`),
)
writeFileSync(docoFilename, this.generateDocoFile(this.flags.version, portIndex))
// clean up
await execute('docker-compose', '-f', docoFilename, '-p', `stack-${portIndex}`, 'down')
const doco: ChildProcess = spawn(
'docker-compose',
['-f', docoFilename, '-p', `stack-${portIndex}`, 'up', '-d'])
cli.action.start(
` ${emoji.get('rocket')} Kuzzle version ${this.flags.version} is launching`,
undefined,
{
stdout: true,
},
)
doco.on('close', docoCode => {
if (docoCode === 0) {
cli.action.stop('done')
this.log(
`\n${emoji.get('thumbsup')} ${chalk.bold(
'Kuzzle is booting',
)} in the background right now.`)
this.log(chalk.grey('To watch the logs, run'))
this.log(
chalk.grey(` docker-compose -f ${docoFilename} -p stack-${portIndex} logs -f\n`),
)
this.log(` Kuzzle port: ${7512 + portIndex}`)
this.log(` MQTT port: ${1883 + portIndex}`)
this.log(` Node.js debugger port: ${9229 + portIndex}`)
this.log(` Elasticsearch port: ${9200 + portIndex}`)
} else {
cli.action.stop(
chalk.red(
` Something went wrong: docker-compose exited with ${docoCode}`,
),
)
this.log(
chalk.grey('If you want to investigate the problem, try running'),
)
this.log(chalk.grey(` docker-compose -f ${docoFilename} -p stack-${portIndex} up\n`))
throw new Error('docker-compose exited witn non-zero status')
}
})
}
public async checkPrerequisites(): Promise<boolean> {
this.log(chalk.grey('Checking prerequisites...'))
const checks: Listr = new Listr([
{
title: `docker-compose exists and the version is at least ${MIN_DOCO_VERSION}`,
task: async () => {
try {
const docov = await execute('docker-compose', '-v')
const matches = docov.stdout.match(/[^0-9.]*([0-9.]*).*/)
if (matches === null) {
throw new Error(
'Unable to read docker-compose verson. This is weird.',
)
}
const docoVersion = matches.length > 0 ? matches[1] : null
if (docoVersion === null) {
throw new Error(
'Unable to read docker-compose version. This is weird.',
)
}
try {
if (docoVersion < MIN_DOCO_VERSION) {
throw new Error(
`The detected version of docker-compose (${docoVersion}) is not recent enough (${MIN_DOCO_VERSION})`,
)
}
} catch (error) {
throw new Error(error)
}
} catch (error) {
throw new Error(
'No docker-compose found. Are you sure docker-compose is installed?',
)
}
},
},
{
title: `vm.max_map_count is greater than ${MIN_MAX_MAP_COUNT}`,
task: async () => {
try {
const sysctl = await execute('/sbin/sysctl', '-n', 'vm.max_map_count')
if (sysctl.exitCode !== 0) {
throw new Error('Something went wrong checking vm.max_map_count')
}
const value: number = parseInt(sysctl.stdout, 10)
if (value < MIN_MAX_MAP_COUNT) {
throw new Error(
`vm.max_map_count must be at least ${MIN_MAX_MAP_COUNT} (found ${value})`,
)
}
}
catch (error) {
throw new Error(`Something went wrong checking vm.max_map_count: ${error.message}`)
}
},
},
])
try {
await checks.run()
return true
} catch (error) {
this.logKo(error.message)
return false
}
}
private generateDocoFile(kuzzleMajor: string, portIndex: number): string {
if (kuzzleMajor === '1') {
return kuzzleStackV1(portIndex)
}
return kuzzleStackV2(portIndex)
}
private isPortAvailable(port: number): Promise<boolean> {
return new Promise((resolve, reject) => {
const tester = net.createServer()
.once('error', error => {
if (!error.message.match(/EADDRINUSE/)) {
reject(error)
}
resolve(false)
})
.once('listening', () => {
tester
.once('close', () => resolve(true))
.close()
})
.listen(port)
})
}
private async findAvailablePort(): Promise<number> {
let i = 0
// eslint-disable-next-line
while (true) {
if (await this.isPortAvailable(7512 + i)) {
return i
}
i++
}
}
}