-
Notifications
You must be signed in to change notification settings - Fork 273
/
Copy pathdeploy.ts
365 lines (316 loc) · 10.4 KB
/
deploy.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
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
/*
* Copyright (C) 2018-2023 Garden Technologies, Inc. <[email protected]>
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*/
import { mapValues } from "lodash"
import { join } from "path"
import split2 = require("split2")
import { PrimitiveMap } from "../../config/common"
import { dedent } from "../../util/string"
import { ExecOpts, sleep } from "../../util/util"
import { TimeoutError } from "../../exceptions"
import { Log } from "../../logger/log-entry"
import execa from "execa"
import chalk from "chalk"
import { renderMessageWithDivider } from "../../logger/util"
import { LogLevel } from "../../logger/logger"
import { createWriteStream } from "fs"
import { ensureFile, readFile, remove, writeFile } from "fs-extra"
import { Transform } from "stream"
import { ExecLogsFollower } from "./logs"
import { PluginContext } from "../../plugin-context"
import { ExecDeploy } from "./config"
import { DeployActionHandler } from "../../plugin/action-types"
import { deployStateToActionState, DeployStatus } from "../../plugin/handlers/Deploy/get-status"
import { Resolved } from "../../actions/types"
import { convertCommandSpec, execRun, getDefaultEnvVars } from "./common"
import { isRunning, killRecursive } from "../../process"
const persistentLocalProcRetryIntervalMs = 2500
export const getExecDeployStatus: DeployActionHandler<"getStatus", ExecDeploy> = async (params) => {
const { action, log, ctx } = params
const { env, statusCommand } = action.getSpec()
if (statusCommand) {
const result = await execRun({
command: statusCommand,
action,
ctx,
log,
env,
opts: { reject: false },
})
const state = result.exitCode === 0 ? "ready" : "outdated"
return {
state: deployStateToActionState(state),
detail: {
state,
version: action.versionString(),
detail: { statusCommandOutput: result.all },
},
outputs: {
log: result.all || "",
},
}
} else {
const state = "unknown"
return {
state: deployStateToActionState(state),
detail: { state, version: action.versionString(), detail: {} },
outputs: {
log: "",
},
}
}
}
export const getExecDeployLogs: DeployActionHandler<"getLogs", ExecDeploy> = async (params) => {
const { action, stream, follow, ctx, log } = params
const logFilePath = getLogFilePath({ ctx, deployName: action.name })
const logsFollower = new ExecLogsFollower({ stream, log, logFilePath, deployName: action.name })
if (follow) {
ctx.events.on("abort", () => {
logsFollower.stop()
ctx.events.emit("done")
})
await logsFollower.streamLogs({ since: params.since, tail: params.tail, follow: true })
} else {
await logsFollower.streamLogs({ since: params.since, tail: params.tail, follow: false })
}
return {}
}
export const deployExec: DeployActionHandler<"deploy", ExecDeploy> = async (params) => {
const { action, log, ctx } = params
const spec = action.getSpec()
const env = spec.env
if (spec.deployCommand.length === 0) {
log.info("No deploy command found. Skipping.")
return { state: "ready", detail: { state: "ready", detail: { skipped: true } }, outputs: {} }
} else if (spec.persistent) {
return deployPersistentExecService({ action, log, ctx, env, deployName: action.name })
} else {
const result = await execRun({
command: spec.deployCommand,
action,
ctx,
log,
env,
opts: { reject: true },
})
const outputLog = (result.stdout + result.stderr).trim()
if (outputLog) {
const prefix = `Finished deploying service ${chalk.white(action.name)}. Here is the output:`
log.verbose(renderMessageWithDivider({
prefix,
msg: outputLog,
isError: false,
color: chalk.gray
}))
}
return {
state: "ready",
detail: { state: "ready", detail: { deployCommandOutput: result.all } },
outputs: {},
}
}
}
export async function deployPersistentExecService({
ctx,
deployName,
log,
action,
env,
}: {
ctx: PluginContext
deployName: string
log: Log
action: Resolved<ExecDeploy>
env: { [key: string]: string }
}): Promise<DeployStatus> {
const logFilePath = getLogFilePath({ ctx, deployName })
const pidFilePath = getPidFilePath({ ctx, deployName })
try {
await resetLogFile(logFilePath)
} catch (err) {
log.debug(`Failed resetting log file for service ${deployName} at path ${logFilePath}: ${err.message}`)
}
await killProcess(log, pidFilePath, deployName)
const proc = runPersistent({
action,
log,
deployName,
logFilePath,
env,
opts: { reject: true },
})
const pid = proc.pid
if (pid) {
await writeFile(pidFilePath, "" + pid)
}
const startedAt = new Date()
const spec = action.getSpec()
if (spec.statusCommand) {
let ready = false
let lastStatusResult: execa.ExecaReturnBase<string> | undefined
while (!ready) {
await sleep(persistentLocalProcRetryIntervalMs)
const now = new Date()
const timeElapsedSec = (now.getTime() - startedAt.getTime()) / 1000
if (timeElapsedSec > spec.statusTimeout) {
let lastResultDescription = ""
if (lastStatusResult) {
lastResultDescription = dedent`\n\nThe last exit code was ${lastStatusResult.exitCode}.\n\n`
if (lastStatusResult.stderr) {
lastResultDescription += `Command error output:\n${lastStatusResult.stderr}\n\n`
}
if (lastStatusResult.stdout) {
lastResultDescription += `Command output:\n${lastStatusResult.stdout}\n\n`
}
}
throw new TimeoutError(
dedent`Timed out waiting for local service ${deployName} to be ready.
Garden timed out waiting for the command ${chalk.gray(spec.statusCommand)}
to return status code 0 (success) after waiting for ${spec.statusTimeout} seconds.
${lastResultDescription}
Possible next steps:
Find out why the configured status command fails.
In case the service just needs more time to become ready, you can adjust the ${chalk.gray("timeout")} value
in your service definition to a value that is greater than the time needed for your service to become ready.
`,
{
deployName,
statusCommand: spec.statusCommand,
pid: proc.pid,
statusTimeout: spec.statusTimeout,
}
)
}
const result = await execRun({
command: spec.statusCommand,
action,
ctx,
log,
env,
opts: { reject: false },
})
lastStatusResult = result
ready = result.exitCode === 0
}
}
return {
state: "ready",
detail: { state: "ready", detail: { persistent: true, pid: proc.pid } },
outputs: {},
}
}
export const deleteExecDeploy: DeployActionHandler<"delete", ExecDeploy> = async (params) => {
const { action, log, ctx } = params
const { cleanupCommand, env } = action.getSpec()
const pidFilePath = getPidFilePath({ ctx, deployName: action.name })
await killProcess(log, pidFilePath, action.name)
if (cleanupCommand) {
const result = await execRun({
command: cleanupCommand,
action,
ctx,
log,
env,
opts: { reject: true },
})
return {
state: "not-ready",
detail: { state: "missing", detail: { cleanupCommandOutput: result.all } },
outputs: {},
}
} else {
log.warn(`Missing cleanupCommand, unable to clean up service`)
return { state: "unknown", detail: { state: "unknown", detail: {} }, outputs: {} }
}
}
function getExecMetadataPath(ctx: PluginContext) {
return join(ctx.gardenDirPath, "exec")
}
export function getLogFilePath({ ctx, deployName }: { ctx: PluginContext; deployName: string }) {
return join(getExecMetadataPath(ctx), `${deployName}.jsonl`)
}
function getPidFilePath({ ctx, deployName }: { ctx: PluginContext; deployName: string }) {
return join(getExecMetadataPath(ctx), `${deployName}.pid`)
}
async function killProcess(log: Log, pidFilePath: string, deployName: string) {
try {
const pidString = (await readFile(pidFilePath)).toString()
if (pidString) {
const oldPid = parseInt(pidString, 10)
if (isRunning(oldPid)) {
try {
await killRecursive("INT", oldPid)
log.debug(`Sent SIGINT to existing ${deployName} process (PID ${oldPid})`)
} catch (err) {
// This most likely means that the process had already been terminated, which is fine for our purposes here.
log.debug(`An error occurred while deleting existing ${deployName} process (PID ${oldPid}): ${err.message}`)
}
}
}
} catch (err) {
// This is normal, there may not be an existing pidfile
}
}
/**
* Truncate the log file by deleting it and recreating as an empty file.
* This ensures that the handlers streaming logs can respond to the file change event.
*/
async function resetLogFile(logFilePath: string) {
await remove(logFilePath)
await ensureFile(logFilePath)
}
function runPersistent({
action,
env,
deployName,
logFilePath,
opts = {},
}: {
action: Resolved<ExecDeploy>
log: Log
deployName: string
logFilePath: string
env?: PrimitiveMap
opts?: ExecOpts
}) {
const toLogEntry = (level: LogLevel) =>
new Transform({
transform(chunk, _encoding, cb) {
const line = chunk.toString().trim()
if (!line) {
cb(null)
return
}
const entry = {
timestamp: new Date(),
name: deployName,
msg: line,
level,
}
const entryStr = JSON.stringify(entry) + "\n"
cb(null, entryStr)
},
})
const shell = !!action.getSpec().shell
const { cmd, args } = convertCommandSpec(action.getSpec("deployCommand"), shell)
const proc = execa(cmd, args, {
cwd: action.getBuildPath(),
env: {
...getDefaultEnvVars(action),
...(env ? mapValues(env, (v) => v + "") : {}),
},
shell,
cleanup: true,
...opts,
detached: true, // Detach
windowsHide: true, // Avoid a console window popping up on Windows
stdio: ["ignore", "pipe", "pipe"],
})
proc.stdout?.pipe(split2()).pipe(toLogEntry(LogLevel.info)).pipe(createWriteStream(logFilePath))
proc.stderr?.pipe(split2()).pipe(toLogEntry(LogLevel.error)).pipe(createWriteStream(logFilePath))
return proc
}