2022-07-27 17:22:14 +02:00
|
|
|
/*
|
|
|
|
Copyright 2022 The Matrix.org Foundation C.I.C.
|
|
|
|
|
|
|
|
Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
you may not use this file except in compliance with the License.
|
|
|
|
You may obtain a copy of the License at
|
|
|
|
|
|
|
|
http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
|
|
|
Unless required by applicable law or agreed to in writing, software
|
|
|
|
distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
See the License for the specific language governing permissions and
|
|
|
|
limitations under the License.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/// <reference types="cypress" />
|
|
|
|
|
|
|
|
import * as os from "os";
|
2022-09-07 17:42:39 +02:00
|
|
|
import * as crypto from "crypto";
|
2022-07-27 17:22:14 +02:00
|
|
|
import * as childProcess from "child_process";
|
|
|
|
import * as fse from "fs-extra";
|
|
|
|
|
|
|
|
import PluginEvents = Cypress.PluginEvents;
|
|
|
|
import PluginConfigOptions = Cypress.PluginConfigOptions;
|
|
|
|
|
|
|
|
// A cypress plugin to run docker commands
|
|
|
|
|
2023-04-14 13:02:59 +02:00
|
|
|
export async function dockerRun(opts: {
|
2022-07-27 17:22:14 +02:00
|
|
|
image: string;
|
|
|
|
containerName: string;
|
|
|
|
params?: string[];
|
2023-01-11 00:29:56 +01:00
|
|
|
cmd?: string[];
|
2022-07-27 17:22:14 +02:00
|
|
|
}): Promise<string> {
|
|
|
|
const userInfo = os.userInfo();
|
2022-09-07 17:42:39 +02:00
|
|
|
const params = opts.params ?? [];
|
2022-07-27 17:22:14 +02:00
|
|
|
|
2022-09-07 17:42:39 +02:00
|
|
|
if (params?.includes("-v") && userInfo.uid >= 0) {
|
2023-05-03 15:28:35 +02:00
|
|
|
// Run the docker container as our uid:gid to prevent problems with permissions.
|
2023-04-14 13:02:59 +02:00
|
|
|
if (await isPodman()) {
|
2023-05-03 15:28:35 +02:00
|
|
|
// Note: this setup is for podman rootless containers.
|
|
|
|
|
|
|
|
// In podman, run as root in the container, which maps to the current
|
|
|
|
// user on the host. This is probably the default since Synapse's
|
|
|
|
// Dockerfile doesn't specify, but we're being explicit here
|
|
|
|
// because it's important for the permissions to work.
|
|
|
|
params.push("-u", "0:0");
|
|
|
|
|
|
|
|
// Tell Synapse not to switch UID
|
|
|
|
params.push("-e", "UID=0");
|
|
|
|
params.push("-e", "GID=0");
|
|
|
|
} else {
|
|
|
|
params.push("-u", `${userInfo.uid}:${userInfo.gid}`);
|
2023-04-14 13:02:59 +02:00
|
|
|
}
|
2022-07-27 17:22:14 +02:00
|
|
|
}
|
|
|
|
|
2022-09-07 17:42:39 +02:00
|
|
|
const args = [
|
|
|
|
"run",
|
2022-12-12 12:24:14 +01:00
|
|
|
"--name",
|
|
|
|
`${opts.containerName}-${crypto.randomBytes(4).toString("hex")}`,
|
2022-09-07 17:42:39 +02:00
|
|
|
"-d",
|
2023-10-16 15:33:03 +02:00
|
|
|
"--rm",
|
2022-09-07 17:42:39 +02:00
|
|
|
...params,
|
|
|
|
opts.image,
|
|
|
|
];
|
|
|
|
|
2023-01-11 00:29:56 +01:00
|
|
|
if (opts.cmd) args.push(...opts.cmd);
|
2022-09-07 17:42:39 +02:00
|
|
|
|
2022-07-27 17:22:14 +02:00
|
|
|
return new Promise<string>((resolve, reject) => {
|
2022-09-07 17:42:39 +02:00
|
|
|
childProcess.execFile("docker", args, (err, stdout) => {
|
2022-07-27 17:22:14 +02:00
|
|
|
if (err) reject(err);
|
|
|
|
resolve(stdout.trim());
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2022-12-12 12:24:14 +01:00
|
|
|
export function dockerExec(args: { containerId: string; params: string[] }): Promise<void> {
|
2022-07-27 17:22:14 +02:00
|
|
|
return new Promise<void>((resolve, reject) => {
|
2022-12-12 12:24:14 +01:00
|
|
|
childProcess.execFile(
|
|
|
|
"docker",
|
|
|
|
["exec", args.containerId, ...args.params],
|
|
|
|
{ encoding: "utf8" },
|
|
|
|
(err, stdout, stderr) => {
|
|
|
|
if (err) {
|
|
|
|
console.log(stdout);
|
|
|
|
console.log(stderr);
|
|
|
|
reject(err);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
resolve();
|
|
|
|
},
|
|
|
|
);
|
2022-07-27 17:22:14 +02:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
export async function dockerLogs(args: {
|
|
|
|
containerId: string;
|
|
|
|
stdoutFile?: string;
|
|
|
|
stderrFile?: string;
|
|
|
|
}): Promise<void> {
|
|
|
|
const stdoutFile = args.stdoutFile ? await fse.open(args.stdoutFile, "w") : "ignore";
|
|
|
|
const stderrFile = args.stderrFile ? await fse.open(args.stderrFile, "w") : "ignore";
|
|
|
|
|
|
|
|
await new Promise<void>((resolve) => {
|
2022-12-12 12:24:14 +01:00
|
|
|
childProcess
|
|
|
|
.spawn("docker", ["logs", args.containerId], {
|
|
|
|
stdio: ["ignore", stdoutFile, stderrFile],
|
|
|
|
})
|
|
|
|
.once("close", resolve);
|
2022-07-27 17:22:14 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
if (args.stdoutFile) await fse.close(<number>stdoutFile);
|
|
|
|
if (args.stderrFile) await fse.close(<number>stderrFile);
|
|
|
|
}
|
|
|
|
|
2022-12-12 12:24:14 +01:00
|
|
|
export function dockerStop(args: { containerId: string }): Promise<void> {
|
2022-07-27 17:22:14 +02:00
|
|
|
return new Promise<void>((resolve, reject) => {
|
2022-12-12 12:24:14 +01:00
|
|
|
childProcess.execFile("docker", ["stop", args.containerId], (err) => {
|
2022-07-27 17:22:14 +02:00
|
|
|
if (err) reject(err);
|
|
|
|
resolve();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2022-12-12 12:24:14 +01:00
|
|
|
export function dockerRm(args: { containerId: string }): Promise<void> {
|
2022-07-27 17:22:14 +02:00
|
|
|
return new Promise<void>((resolve, reject) => {
|
2022-12-12 12:24:14 +01:00
|
|
|
childProcess.execFile("docker", ["rm", args.containerId], (err) => {
|
2022-07-27 17:22:14 +02:00
|
|
|
if (err) reject(err);
|
|
|
|
resolve();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2022-12-12 12:24:14 +01:00
|
|
|
export function dockerIp(args: { containerId: string }): Promise<string> {
|
2022-09-07 17:42:39 +02:00
|
|
|
return new Promise<string>((resolve, reject) => {
|
2022-12-12 12:24:14 +01:00
|
|
|
childProcess.execFile(
|
|
|
|
"docker",
|
|
|
|
["inspect", "-f", "{{ .NetworkSettings.IPAddress }}", args.containerId],
|
|
|
|
(err, stdout) => {
|
|
|
|
if (err) reject(err);
|
|
|
|
else resolve(stdout.trim());
|
|
|
|
},
|
|
|
|
);
|
2022-09-07 17:42:39 +02:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2023-04-14 13:02:59 +02:00
|
|
|
/**
|
|
|
|
* Detects whether the docker command is actually podman.
|
|
|
|
* To do this, it looks for "podman" in the output of "docker --help".
|
|
|
|
*/
|
|
|
|
export function isPodman(): Promise<boolean> {
|
|
|
|
return new Promise<boolean>((resolve, reject) => {
|
|
|
|
childProcess.execFile("docker", ["--help"], (err, stdout) => {
|
|
|
|
if (err) reject(err);
|
|
|
|
else resolve(stdout.toLowerCase().includes("podman"));
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2023-08-18 16:40:17 +02:00
|
|
|
/**
|
|
|
|
* Supply the right hostname to use to talk to the host machine. On Docker this
|
|
|
|
* is "host.docker.internal" and on Podman this is "host.containers.internal".
|
|
|
|
*/
|
|
|
|
export async function hostContainerName() {
|
|
|
|
return (await isPodman()) ? "host.containers.internal" : "host.docker.internal";
|
|
|
|
}
|
|
|
|
|
2022-07-27 17:22:14 +02:00
|
|
|
/**
|
|
|
|
* @type {Cypress.PluginConfig}
|
|
|
|
*/
|
|
|
|
export function docker(on: PluginEvents, config: PluginConfigOptions) {
|
|
|
|
on("task", {
|
|
|
|
dockerRun,
|
|
|
|
dockerExec,
|
|
|
|
dockerLogs,
|
|
|
|
dockerStop,
|
|
|
|
dockerRm,
|
2022-09-07 17:42:39 +02:00
|
|
|
dockerIp,
|
2022-07-27 17:22:14 +02:00
|
|
|
});
|
|
|
|
}
|