2024-05-20 15:34:07 +02:00
|
|
|
import { containerName } from "../src/bin/shared/constants";
|
|
|
|
import child_process from "child_process";
|
|
|
|
import { SemVer } from "../src/bin/tools/SemVer";
|
|
|
|
import { join as pathJoin, relative as pathRelative } from "path";
|
|
|
|
import chalk from "chalk";
|
2024-06-10 20:51:00 +02:00
|
|
|
import { Deferred } from "evt/tools/Deferred";
|
|
|
|
import { assert } from "tsafe/assert";
|
|
|
|
import { is } from "tsafe/is";
|
2024-05-20 15:34:07 +02:00
|
|
|
|
2024-06-10 20:51:00 +02:00
|
|
|
(async () => {
|
|
|
|
{
|
|
|
|
const dCompleted = new Deferred<void>();
|
|
|
|
|
2024-06-15 01:06:06 +02:00
|
|
|
const child = child_process.spawn(
|
|
|
|
"docker",
|
|
|
|
[
|
|
|
|
...["exec", containerName],
|
|
|
|
...["/opt/keycloak/bin/kc.sh", "export"],
|
|
|
|
...["--dir", "/tmp"],
|
|
|
|
...["--realm", "myrealm"],
|
|
|
|
...["--users", "realm_file"]
|
|
|
|
],
|
|
|
|
{ shell: true }
|
|
|
|
);
|
2024-06-10 20:51:00 +02:00
|
|
|
|
|
|
|
let output = "";
|
|
|
|
|
|
|
|
const onExit = (code: number | null) => {
|
|
|
|
dCompleted.reject(new Error(`Exited with code ${code}`));
|
|
|
|
};
|
|
|
|
|
|
|
|
child.on("exit", onExit);
|
|
|
|
|
|
|
|
child.stdout.on("data", data => {
|
|
|
|
const outputStr = data.toString("utf8");
|
|
|
|
|
|
|
|
if (outputStr.includes("Export finished successfully")) {
|
|
|
|
child.removeListener("exit", onExit);
|
|
|
|
|
|
|
|
child.kill();
|
|
|
|
|
|
|
|
dCompleted.resolve();
|
|
|
|
}
|
|
|
|
|
|
|
|
output += outputStr;
|
|
|
|
});
|
|
|
|
|
|
|
|
child.stderr.on("data", data => (output += chalk.red(data.toString("utf8"))));
|
|
|
|
|
|
|
|
try {
|
|
|
|
await dCompleted.pr;
|
|
|
|
} catch (error) {
|
|
|
|
assert(is<Error>(error));
|
|
|
|
|
|
|
|
console.log(chalk.red(error.message));
|
|
|
|
|
|
|
|
console.log(output);
|
|
|
|
|
|
|
|
process.exit(1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const keycloakMajorVersionNumber = SemVer.parse(
|
|
|
|
child_process
|
|
|
|
.execSync(`docker inspect --format '{{.Config.Image}}' ${containerName}`)
|
|
|
|
.toString("utf8")
|
|
|
|
.trim()
|
|
|
|
.split(":")[1]
|
|
|
|
).major;
|
|
|
|
|
|
|
|
const targetFilePath = pathRelative(
|
|
|
|
process.cwd(),
|
|
|
|
pathJoin(
|
|
|
|
__dirname,
|
|
|
|
"..",
|
|
|
|
"src",
|
|
|
|
"bin",
|
|
|
|
"start-keycloak",
|
|
|
|
`myrealm-realm-${keycloakMajorVersionNumber}.json`
|
|
|
|
)
|
|
|
|
);
|
|
|
|
|
|
|
|
run(`docker cp ${containerName}:/tmp/myrealm-realm.json ${targetFilePath}`);
|
|
|
|
|
|
|
|
console.log(`${chalk.green(`✓ Exported realm to`)} ${chalk.bold(targetFilePath)}`);
|
|
|
|
})();
|
2024-05-20 15:34:07 +02:00
|
|
|
|
|
|
|
function run(command: string) {
|
|
|
|
console.log(chalk.grey(`$ ${command}`));
|
|
|
|
|
2024-05-20 15:48:51 +02:00
|
|
|
return child_process.execSync(command, { stdio: "inherit" });
|
2024-05-20 15:34:07 +02:00
|
|
|
}
|