forked from nasa-gcn/architect-plugin-search
-
Notifications
You must be signed in to change notification settings - Fork 0
/
runDocker.ts
85 lines (77 loc) · 2.14 KB
/
runDocker.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
/*!
* Copyright © 2023 United States Government as represented by the
* Administrator of the National Aeronautics and Space Administration.
* All Rights Reserved.
*
* SPDX-License-Identifier: Apache-2.0
*/
import Dockerode from 'dockerode'
import { fork } from 'node:child_process'
import { type SearchEngineLauncherFunction } from './run.js'
const [, , command, jsonifiedArgs] = process.argv
if (command === 'launch-docker-subprocess') {
const { dataDir, logsDir, engine, port, options } = JSON.parse(jsonifiedArgs)
const Image =
engine === 'elasticsearch'
? 'elastic/elasticsearch:8.6.2'
: 'opensearchproject/opensearch:2.11.0'
console.log('Launching Docker container', Image)
const docker = new Dockerode()
const container = await docker.createContainer({
Env: [...options, 'path.data=/var/lib/search', 'path.logs=/var/log/search'],
HostConfig: {
AutoRemove: true,
Mounts: [
{ Source: dataDir, Target: '/var/lib/search', Type: 'bind' },
{ Source: logsDir, Target: '/var/log/search', Type: 'bind' },
],
PortBindings: {
[`${port}/tcp`]: [{ HostIP: '127.0.0.1', HostPort: `${port}` }],
},
},
Image,
})
const stream = await container.attach({ stream: true, stderr: true })
stream.pipe(process.stderr)
await container.start()
const signals = ['message', 'SIGTERM', 'SIGINT']
signals.forEach((signal) => {
process.on(signal, async () => {
await container.kill()
})
})
await container.wait()
}
export const launchDocker: SearchEngineLauncherFunction = async ({
dataDir,
logsDir,
engine,
port,
options,
}) => {
const argv = {
dataDir,
logsDir,
engine,
port,
options,
}
const subprocess = fork(new URL(import.meta.url), [
'launch-docker-subprocess',
JSON.stringify(argv),
])
return {
async kill() {
console.log('Killing Docker container')
subprocess.send({ action: 'kill' })
},
async waitUntilStopped() {
return new Promise((resolve) => {
subprocess.on('exit', () => {
console.log('Docker container exited')
resolve()
})
})
},
}
}