2023-11-27 14:40:45 +00:00
|
|
|
/*
|
2024-09-09 13:57:16 +00:00
|
|
|
Copyright 2024 New Vector Ltd.
|
2023-11-27 14:40:45 +00:00
|
|
|
Copyright 2023 The Matrix.org Foundation C.I.C.
|
|
|
|
|
2024-09-09 13:57:16 +00:00
|
|
|
SPDX-License-Identifier: AGPL-3.0-only OR GPL-3.0-only
|
|
|
|
Please see LICENSE files in the repository root for full details.
|
2023-11-27 14:40:45 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
import * as path from "node:path";
|
|
|
|
import * as os from "node:os";
|
|
|
|
import * as fse from "fs-extra";
|
|
|
|
|
|
|
|
import { getFreePort } from "../../utils/port";
|
|
|
|
import { Homeserver, HomeserverConfig, HomeserverInstance, StartHomeserverOpts } from "../";
|
|
|
|
import { randB64Bytes } from "../../utils/rand";
|
|
|
|
import { Synapse } from "../synapse";
|
|
|
|
import { Docker } from "../../docker";
|
|
|
|
|
|
|
|
const dockerConfigDir = "/etc/dendrite/";
|
|
|
|
const dendriteConfigFile = "dendrite.yaml";
|
|
|
|
|
|
|
|
// Surprisingly, Dendrite implements the same register user Admin API Synapse, so we can just extend it
|
|
|
|
export class Dendrite extends Synapse implements Homeserver, HomeserverInstance {
|
|
|
|
protected image = "matrixdotorg/dendrite-monolith:main";
|
|
|
|
protected entrypoint = "/usr/bin/dendrite";
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Start a dendrite instance: the template must be the name of one of the templates
|
|
|
|
* in the playwright/plugins/dendritedocker/templates directory
|
|
|
|
* @param opts
|
|
|
|
*/
|
|
|
|
public async start(opts: StartHomeserverOpts): Promise<HomeserverInstance> {
|
|
|
|
const denCfg = await cfgDirFromTemplate(this.image, opts);
|
|
|
|
|
|
|
|
console.log(`Starting dendrite with config dir ${denCfg.configDir}...`);
|
|
|
|
|
|
|
|
const dendriteId = await this.docker.run({
|
|
|
|
image: this.image,
|
|
|
|
params: [
|
|
|
|
"-v",
|
|
|
|
`${denCfg.configDir}:` + dockerConfigDir,
|
|
|
|
"-p",
|
|
|
|
`${denCfg.port}:8008/tcp`,
|
|
|
|
"--entrypoint",
|
|
|
|
this.entrypoint,
|
|
|
|
],
|
|
|
|
containerName: `react-sdk-playwright-dendrite`,
|
|
|
|
cmd: ["--config", dockerConfigDir + dendriteConfigFile, "--really-enable-open-registration", "true", "run"],
|
|
|
|
});
|
|
|
|
|
|
|
|
console.log(`Started dendrite with id ${dendriteId} on port ${denCfg.port}.`);
|
|
|
|
|
|
|
|
// Await Dendrite healthcheck
|
|
|
|
await this.docker.exec([
|
|
|
|
"curl",
|
|
|
|
"--connect-timeout",
|
|
|
|
"30",
|
|
|
|
"--retry",
|
|
|
|
"30",
|
|
|
|
"--retry-delay",
|
|
|
|
"1",
|
|
|
|
"--retry-all-errors",
|
|
|
|
"--silent",
|
|
|
|
"http://localhost:8008/_matrix/client/versions",
|
|
|
|
]);
|
|
|
|
|
2023-12-16 10:01:26 +00:00
|
|
|
const dockerUrl = `http://${await this.docker.getContainerIp()}:8008`;
|
2023-11-27 14:40:45 +00:00
|
|
|
this.config = {
|
|
|
|
...denCfg,
|
|
|
|
serverId: dendriteId,
|
2023-12-16 10:01:26 +00:00
|
|
|
dockerUrl,
|
2023-11-27 14:40:45 +00:00
|
|
|
};
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
2023-12-14 13:49:35 +00:00
|
|
|
public async stop(): Promise<string[]> {
|
2023-11-27 14:40:45 +00:00
|
|
|
if (!this.config) throw new Error("Missing existing dendrite instance, did you call stop() before start()?");
|
|
|
|
|
|
|
|
const dendriteLogsPath = path.join("playwright", "dendritelogs", this.config.serverId);
|
|
|
|
await fse.ensureDir(dendriteLogsPath);
|
|
|
|
|
|
|
|
await this.docker.persistLogsToFile({
|
|
|
|
stdoutFile: path.join(dendriteLogsPath, "stdout.log"),
|
|
|
|
stderrFile: path.join(dendriteLogsPath, "stderr.log"),
|
|
|
|
});
|
|
|
|
|
|
|
|
await this.docker.stop();
|
|
|
|
|
|
|
|
await fse.remove(this.config.configDir);
|
|
|
|
|
|
|
|
console.log(`Stopped dendrite id ${this.config.serverId}.`);
|
2023-12-14 13:49:35 +00:00
|
|
|
|
|
|
|
return [path.join(dendriteLogsPath, "stdout.log"), path.join(dendriteLogsPath, "stderr.log")];
|
2023-11-27 14:40:45 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export class Pinecone extends Dendrite {
|
|
|
|
protected image = "matrixdotorg/dendrite-demo-pinecone:main";
|
|
|
|
protected entrypoint = "/usr/bin/dendrite-demo-pinecone";
|
|
|
|
}
|
|
|
|
|
2023-12-16 10:01:26 +00:00
|
|
|
async function cfgDirFromTemplate(
|
|
|
|
dendriteImage: string,
|
|
|
|
opts: StartHomeserverOpts,
|
|
|
|
): Promise<Omit<HomeserverConfig, "dockerUrl">> {
|
2023-11-27 14:40:45 +00:00
|
|
|
const template = "default"; // XXX: for now we only have one template
|
|
|
|
const templateDir = path.join(__dirname, "templates", template);
|
|
|
|
|
|
|
|
const stats = await fse.stat(templateDir);
|
|
|
|
if (!stats?.isDirectory) {
|
|
|
|
throw new Error(`No such template: ${template}`);
|
|
|
|
}
|
|
|
|
const tempDir = await fse.mkdtemp(path.join(os.tmpdir(), "react-sdk-dendritedocker-"));
|
|
|
|
|
|
|
|
// copy the contents of the template dir, omitting homeserver.yaml as we'll template that
|
|
|
|
console.log(`Copy ${templateDir} -> ${tempDir}`);
|
|
|
|
await fse.copy(templateDir, tempDir, { filter: (f) => path.basename(f) !== dendriteConfigFile });
|
|
|
|
|
|
|
|
const registrationSecret = randB64Bytes(16);
|
|
|
|
|
|
|
|
const port = await getFreePort();
|
|
|
|
const baseUrl = `http://localhost:${port}`;
|
|
|
|
|
|
|
|
// now copy homeserver.yaml, applying substitutions
|
|
|
|
console.log(`Gen ${path.join(templateDir, dendriteConfigFile)}`);
|
|
|
|
let hsYaml = await fse.readFile(path.join(templateDir, dendriteConfigFile), "utf8");
|
|
|
|
hsYaml = hsYaml.replace(/{{REGISTRATION_SECRET}}/g, registrationSecret);
|
|
|
|
await fse.writeFile(path.join(tempDir, dendriteConfigFile), hsYaml);
|
|
|
|
|
|
|
|
const docker = new Docker();
|
|
|
|
await docker.run({
|
|
|
|
image: dendriteImage,
|
2024-02-20 14:21:10 +00:00
|
|
|
params: ["--entrypoint=", "-v", `${tempDir}:/mnt`],
|
2023-11-27 14:40:45 +00:00
|
|
|
containerName: `react-sdk-playwright-dendrite-keygen`,
|
|
|
|
cmd: ["/usr/bin/generate-keys", "-private-key", "/mnt/matrix_key.pem"],
|
|
|
|
});
|
|
|
|
|
|
|
|
return {
|
|
|
|
port,
|
|
|
|
baseUrl,
|
|
|
|
configDir: tempDir,
|
|
|
|
registrationSecret,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
export function isDendrite(): boolean {
|
|
|
|
return process.env["PLAYWRIGHT_HOMESERVER"] === "dendrite" || process.env["PLAYWRIGHT_HOMESERVER"] === "pinecone";
|
|
|
|
}
|