-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.ts
244 lines (230 loc) · 7.44 KB
/
server.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
import { ChildProcess, exec, execSync } from 'child_process'
import { defaultConfig } from './config'
export type ServerOptions = {
/** @default 24080 */
port?: number
/** @default 'easynmt/api:2.0-cpu' */
image?: string
/** @default false */
debug?: boolean
/** @default 'docker-easynmt'' */
container?: string
}
function expandOptions(options?: ServerOptions): Required<ServerOptions> {
let port = options?.port || defaultConfig.port
let image = options?.image || defaultConfig.image
let debug = options?.debug || false
let container = options?.container || defaultConfig.container
return {
port,
image,
debug,
container,
}
}
function parse_docker_ps_line(line: string) {
// e.g. "CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES"
if (line.startsWith('CONTAINER ID') && line.endsWith('NAMES')) {
return
}
// e.g. "06ef13feb536 easynmt/api:2.0-cpu "/start.sh" 9 minutes ago Exited (137) 4 seconds ago docker-easynmt2"
// e.g. "06ef13feb536 easynmt/api:2.0-cpu "/start.sh" 8 minutes ago Up 44 seconds 0.0.0.0:24080->80/tcp, :::24080->80/tcp docker-easynmt2"
let parts = line.split(' ').filter(part => part.length > 0)
let id = parts[0]
let image = parts[1]
let name = parts[parts.length - 1]
if (!id || !image || !name) return
let running = !parts.includes('Exited') && parts.includes('Up')
function parsePortFromLine() {
return parts
.map(part => {
// e.g. "0.0.0.0:24080->80/tcp,"
// e.g. ":::24080->80/tcp"
let parts = part.split('->')
if (parts.length != 2) return
let port = parts[0].split(':').pop()
if (!port) return
return +port
})
.find(port => Number.isInteger(port))
}
function parsePortFromInspect() {
// e.g. "map[80/tcp:[{ 24080}]]"
let text = execSync(
`docker inspect ${name} --format '{{ .HostConfig.PortBindings }}'`,
).toString()
let matches = text.match(/{ *\d+}/g)
if (!matches) return
return matches
.map(part => {
// e.g. "{ 24080}"
let match = part.match(/\d+/)
if (!match) return
return +match[0]
})
.find(port => Number.isInteger(port))
}
let port = running ? parsePortFromLine() : parsePortFromInspect()
return { id, image, name, port, running }
}
/** @description scan existing (running) docker container */
export function scanServer(options?: ServerOptions): string[] {
let { port, image, debug } = expandOptions(options)
let ids = execSync('docker ps')
.toString()
.split('\n')
.map(line => parse_docker_ps_line(line))
.filter(
container =>
container && (container.port == port || container.image == image),
)
.map(container => container!.id)
return ids
}
/** @description stop the docker container */
export function stopServer(options?: ServerOptions) {
let { port, image, container: containerName, debug } = expandOptions(options)
let ids = execSync('docker ps -a')
.toString()
.split('\n')
.map(line => parse_docker_ps_line(line))
.filter(
container =>
container &&
(container.image == image ||
container.name == containerName ||
container.port == port),
)
.map(container => container!.id)
if (ids.length > 0) {
if (debug) {
console.error('stop docker containers:', ids)
}
execSync(`docker stop ${ids.join(' ')}`)
}
}
function resumeContainer(options: { name: string; debug: boolean }) {
let { name, debug } = options
execSync(`docker start ${name}`)
let childProcess: ChildProcess = null as any
let ready = new Promise<void>((resolve, reject) => {
childProcess = exec(`docker logs --follow ${name}`)
let timer: ReturnType<typeof setTimeout> | null = null
let onData = (data: string) => {
if (debug) {
process.stderr.write(data)
}
if (timer) {
clearTimeout(timer)
}
timer = setTimeout(() => {
if (data.toLowerCase().replaceAll('errorlog', '').includes('error')) {
reject(new Error(data))
} else if (data.includes('Application startup complete')) {
resolve()
} else {
return
}
if (!debug) {
childProcess.stdout!.off('data', onData)
childProcess.stderr!.off('data', onData)
}
}, 1000)
}
childProcess.stdout!.on('data', onData)
childProcess.stderr!.on('data', onData)
})
return { childProcess, ready }
}
function startContainer(options: {
image: string
name: string
port: number
debug: boolean
}) {
let { image, name, port, debug } = options
let childProcess: ChildProcess = null as any
let ready = new Promise<void>((resolve, reject) => {
childProcess = exec(`docker run --name ${name} -p ${port}:80 ${image}`)
let onData = (data: string) => {
if (debug) {
process.stderr.write(data)
}
if (data.toLowerCase().includes('error')) {
reject(new Error(data))
} else if (data.includes('Application startup complete')) {
resolve()
} else {
return
}
if (!debug) {
childProcess.stdout!.off('data', onData)
childProcess.stderr!.off('data', onData)
}
}
childProcess.stdout!.on('data', onData)
childProcess.stderr!.on('data', onData)
})
return { childProcess, ready }
}
function scanContainer(options: { image: string; name: string; port: number }) {
let { image, name, port } = options
let lines = execSync('docker ps -a').toString().split('\n')
for (let line of lines) {
let container = parse_docker_ps_line(line)
if (!container) continue
if (container.port == port && container.name != name && container.running) {
throw new Error(
`port ${port} is used by another container "${container.name}"`,
)
}
if (
container.port == port &&
container.image != image &&
container.running
) {
throw new Error(
`port ${port} is used by another image "${container.name}"`,
)
}
if (container.name == name && container.image != image) {
throw new Error(
`container name "${name}" is used by another image "${container.image}"`,
)
}
if (container.name == name && container.port != port) {
throw new Error(
`container "${name}" is using another port "${container.port}"`,
)
}
if (container.image == image && container.port == port) {
return container
}
}
}
/** @description start a docker container */
export function startServer(options?: ServerOptions): {
childProcess?: ChildProcess
ready: Promise<void>
} {
let { port, image, debug, container: name } = expandOptions(options)
let container = scanContainer({ image, name, port })
if (container && container.running) {
console.log('[DEV] container already running?', container)
return { ready: Promise.resolve() }
}
if (container && !container.running) {
console.log('[DEV] container to be resume:', container)
return resumeContainer({ name, debug })
}
console.log('[DEV] start container...')
return startContainer({ image, name, port, debug })
}
/** @description start a docker container if not already running */
export async function autoStartServer(options?: ServerOptions) {
let ids = scanServer(options)
if (ids.length == 0) {
let server = startServer()
await server.ready
}
}