forked from testcontainers/testcontainers-node
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpodman-container-client.ts
More file actions
132 lines (111 loc) · 4.29 KB
/
Copy pathpodman-container-client.ts
File metadata and controls
132 lines (111 loc) · 4.29 KB
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
import { Container, ExecCreateOptions } from "dockerode";
import { ExecOptions, ExecResult, ExecVerboseResult } from "./types";
import byline from "byline";
import { DockerContainerClient } from "./docker-container-client";
import { execLog, log } from "../../../common";
import { PassThrough, Readable } from "stream";
export class PodmanContainerClient extends DockerContainerClient {
override async exec(container: Container, command: string[], opts?: Partial<ExecOptions>): Promise<ExecResult> {
const execOptions: ExecCreateOptions = {
Cmd: command,
AttachStdout: true,
AttachStderr: true,
};
if (opts?.env !== undefined) {
execOptions.Env = Object.entries(opts.env).map(([key, value]) => `${key}=${value}`);
}
if (opts?.workingDir !== undefined) {
execOptions.WorkingDir = opts.workingDir;
}
if (opts?.user !== undefined) {
execOptions.User = opts.user;
}
const chunks: string[] = [];
try {
if (opts?.log) {
log.debug(`Execing container with command "${command.join(" ")}"...`, { containerId: container.id });
}
const exec = await container.exec(execOptions);
const stream = await this.demuxStream(container.id, await exec.start({ stdin: true, Detach: false, Tty: true }));
if (opts?.log && execLog.enabled()) {
byline(stream).on("data", (line) => execLog.trace(line, { containerId: container.id }));
}
await new Promise((res, rej) => {
stream.on("data", (chunk) => chunks.push(chunk));
stream.on("end", res);
stream.on("error", rej);
});
stream.destroy();
const inspectResult = await exec.inspect();
const exitCode = inspectResult.ExitCode ?? -1;
const output = chunks.join("");
return { output, exitCode };
} catch (err) {
log.error(`Failed to exec container with command "${command.join(" ")}": ${err}: ${chunks.join("")}`, {
containerId: container.id,
});
throw err;
}
}
override async execVerbose(
container: Container,
command: string[],
opts?: Partial<ExecOptions>
): Promise<ExecVerboseResult> {
const execOptions: ExecCreateOptions = {
Cmd: command,
AttachStdout: true,
AttachStderr: true,
};
if (opts?.env !== undefined) {
execOptions.Env = Object.entries(opts.env).map(([key, value]) => `${key}=${value}`);
}
if (opts?.workingDir !== undefined) {
execOptions.WorkingDir = opts.workingDir;
}
if (opts?.user !== undefined) {
execOptions.User = opts.user;
}
const stdoutChunks: string[] = [];
const stderrChunks: string[] = [];
try {
if (opts?.log) {
log.debug(`Execing container verbosely with command "${command.join(" ")}"...`, { containerId: container.id });
}
const exec = await container.exec(execOptions);
const stream = await exec.start({ stdin: true, Detach: false, Tty: false });
const stdoutStream = new PassThrough();
const stderrStream = new PassThrough();
// Podman may use the same demuxing approach as Docker
this.dockerode.modem.demuxStream(stream, stdoutStream, stderrStream);
const processStream = (stream: Readable, chunks: string[], label: "stdout" | "stderr") => {
stream.on("data", (chunk) => {
chunks.push(chunk.toString());
if (opts?.log && execLog.enabled()) {
execLog.trace(chunk.toString(), { containerId: container.id });
}
});
};
processStream(stdoutStream, stdoutChunks, "stdout");
processStream(stderrStream, stderrChunks, "stderr");
await new Promise((res, rej) => {
stream.on("end", res);
stream.on("error", rej);
});
stream.destroy();
const inspectResult = await exec.inspect();
const exitCode = inspectResult.ExitCode ?? -1;
const stdout = stdoutChunks.join("");
const stderr = stderrChunks.join("");
if (opts?.log) {
log.debug(`ExecVerbose completed with command "${command.join(" ")}"`, { containerId: container.id });
}
return { stdout, stderr, exitCode };
} catch (err) {
log.error(`Failed to exec container with command "${command.join(" ")}": ${err}: ${stderrChunks.join("")}`, {
containerId: container.id,
});
throw err;
}
}
}