keycloak_theme/src/bin/start-keycloak/start-keycloak.ts

672 lines
23 KiB
TypeScript
Raw Normal View History

2024-10-05 20:30:09 +02:00
import type { BuildContext } from "../shared/buildContext";
import { exclude } from "tsafe/exclude";
import {
CONTAINER_NAME,
KEYCLOAKIFY_SPA_DEV_SERVER_PORT,
KEYCLOAKIFY_LOGIN_JAR_BASENAME,
TEST_APP_URL
} from "../shared/constants";
2024-05-20 15:34:07 +02:00
import { SemVer } from "../tools/SemVer";
import { assert, type Equals } from "tsafe/assert";
2024-05-17 05:13:41 +02:00
import * as fs from "fs";
import {
join as pathJoin,
relative as pathRelative,
sep as pathSep,
basename as pathBasename
} from "path";
2024-05-17 05:13:41 +02:00
import * as child_process from "child_process";
2024-05-18 10:02:14 +02:00
import chalk from "chalk";
2024-05-20 02:27:40 +02:00
import chokidar from "chokidar";
import { waitForDebounceFactory } from "powerhooks/tools/waitForDebounce";
2024-05-20 15:34:07 +02:00
import { getThisCodebaseRootDirPath } from "../tools/getThisCodebaseRootDirPath";
import { getAbsoluteAndInOsFormatPath } from "../tools/getAbsoluteAndInOsFormatPath";
import cliSelect from "cli-select";
2024-05-20 19:30:04 +02:00
import * as runExclusive from "run-exclusive";
import { extractArchive } from "../tools/extractArchive";
import { appBuild } from "./appBuild";
import { keycloakifyBuild } from "./keycloakifyBuild";
2024-06-10 07:57:12 +02:00
import { isInside } from "../tools/isInside";
import { existsAsync } from "../tools/fs.existsAsync";
import { rm } from "../tools/fs.rm";
import { downloadAndExtractArchive } from "../tools/downloadAndExtractArchive";
import { startViteDevServer } from "./startViteDevServer";
import { getSupportedKeycloakMajorVersions } from "./realmConfig/defaultConfig";
import { getSupportedDockerImageTags } from "./getSupportedDockerImageTags";
import { getRealmConfig } from "./realmConfig";
2024-10-05 20:30:09 +02:00
export async function command(params: {
buildContext: BuildContext;
cliCommandOptions: {
port: number | undefined;
keycloakVersion: string | undefined;
realmJsonFilePath: string | undefined;
};
}) {
2024-05-18 11:40:09 +02:00
exit_if_docker_not_installed: {
let commandOutput: string | undefined = undefined;
2024-05-18 11:09:04 +02:00
try {
commandOutput = child_process
.execSync("docker --version", {
stdio: ["ignore", "pipe", "ignore"]
})
?.toString("utf8");
2024-05-18 11:09:04 +02:00
} catch {}
if (commandOutput?.includes("Docker") || commandOutput?.includes("podman")) {
2024-05-18 11:40:09 +02:00
break exit_if_docker_not_installed;
2024-05-18 11:09:04 +02:00
}
console.log(
[
`${chalk.red("Docker required.")}`,
2024-05-20 15:48:51 +02:00
`Install it with Docker Desktop: ${chalk.bold.underline(
"https://www.docker.com/products/docker-desktop/"
)}`,
2024-05-18 11:09:04 +02:00
`(or any other way)`
].join(" ")
);
process.exit(1);
}
2024-05-18 11:40:09 +02:00
exit_if_docker_not_running: {
2024-05-18 11:09:04 +02:00
let isDockerRunning: boolean;
try {
2024-05-20 15:48:51 +02:00
child_process.execSync("docker info", { stdio: "ignore" });
2024-05-18 11:09:04 +02:00
isDockerRunning = true;
} catch {
isDockerRunning = false;
}
if (isDockerRunning) {
2024-05-18 11:40:09 +02:00
break exit_if_docker_not_running;
2024-05-18 11:09:04 +02:00
}
2024-05-20 15:48:51 +02:00
console.log(
[
`${chalk.red("Docker daemon is not running.")}`,
`Please start Docker Desktop and try again.`
].join(" ")
);
2024-05-18 11:40:09 +02:00
process.exit(1);
2024-05-18 11:09:04 +02:00
}
2024-10-05 20:30:09 +02:00
const { cliCommandOptions, buildContext } = params;
2024-12-17 10:44:24 +01:00
const { allSupportedTags, latestMajorTags } = await getSupportedDockerImageTags({
buildContext
});
const { dockerImageTag } = await (async () => {
if (cliCommandOptions.keycloakVersion !== undefined) {
const cliCommandOptions_keycloakVersion = cliCommandOptions.keycloakVersion;
2024-12-17 10:44:24 +01:00
const tag = allSupportedTags.find(tag =>
tag.startsWith(cliCommandOptions_keycloakVersion)
);
if (tag === undefined) {
console.log(
chalk.red(
[
`We could not find a Keycloak Docker image for ${cliCommandOptions_keycloakVersion}`,
`Example of valid values: --keycloak-version 26, --keycloak-version 26.0.7`
].join("\n")
)
);
process.exit(1);
}
return { dockerImageTag: tag };
}
if (buildContext.startKeycloakOptions.dockerImage !== undefined) {
return {
dockerImageTag: buildContext.startKeycloakOptions.dockerImage.tag
};
}
2024-05-17 05:13:41 +02:00
console.log(
[
chalk.cyan(
"On which version of Keycloak do you want to test your theme?"
),
chalk.gray(
"You can also explicitly provide the version with `npx keycloakify start-keycloak --keycloak-version 26` (or any other version)"
)
].join("\n")
);
2024-05-17 05:13:41 +02:00
const { value: tag } = await cliSelect<string>({
2024-12-17 10:44:24 +01:00
values: latestMajorTags
}).catch(() => {
process.exit(-1);
});
2024-05-17 05:13:41 +02:00
console.log(`${tag}`);
2024-05-17 05:13:41 +02:00
return { dockerImageTag: tag };
2024-05-17 05:13:41 +02:00
})();
const keycloakMajorVersionNumber = (() => {
const [wrap] = getSupportedKeycloakMajorVersions()
.map(majorVersionNumber => ({
majorVersionNumber,
index: dockerImageTag.indexOf(`${majorVersionNumber}`)
}))
.filter(({ index }) => index !== -1)
.sort((a, b) => a.index - b.index);
if (wrap === undefined) {
try {
const version = SemVer.parse(dockerImageTag);
console.error(
chalk.yellow(
`Keycloak version ${version.major} is not supported, supported versions are ${getSupportedKeycloakMajorVersions().join(", ")}`
)
);
process.exit(1);
} catch {
2024-12-14 14:44:30 +01:00
// NOTE: Latest version
const [n] = getSupportedKeycloakMajorVersions();
console.warn(
chalk.yellow(
2024-12-14 14:44:30 +01:00
`Could not determine the major Keycloak version number from the docker image tag ${dockerImageTag}. Assuming ${n}`
)
);
2024-12-14 14:44:30 +01:00
return n;
}
}
return wrap.majorVersionNumber;
})();
2024-05-17 05:13:41 +02:00
const { clientName, onRealmConfigChange, realmJsonFilePath, realmName, username } =
await getRealmConfig({
keycloakMajorVersionNumber,
realmJsonFilePath_userProvided: await (async () => {
if (cliCommandOptions.realmJsonFilePath !== undefined) {
return getAbsoluteAndInOsFormatPath({
pathIsh: cliCommandOptions.realmJsonFilePath,
cwd: process.cwd()
});
}
if (buildContext.startKeycloakOptions.realmJsonFilePath !== undefined) {
assert(
await existsAsync(
buildContext.startKeycloakOptions.realmJsonFilePath
),
`${pathRelative(process.cwd(), buildContext.startKeycloakOptions.realmJsonFilePath)} does not exist`
);
return buildContext.startKeycloakOptions.realmJsonFilePath;
}
return undefined;
})(),
buildContext
});
{
const { isAppBuildSuccess } = await appBuild({
buildContext
});
if (!isAppBuildSuccess) {
console.log(
chalk.red(
`App build failed, exiting. Try building your app (e.g 'npm run build') and see what's wrong.`
)
);
process.exit(1);
}
const { isKeycloakifyBuildSuccess } = await keycloakifyBuild({
buildForKeycloakMajorVersionNumber: keycloakMajorVersionNumber,
buildContext
});
if (!isKeycloakifyBuildSuccess) {
console.log(
chalk.red(
`Keycloakify build failed, exiting. Try running 'npx keycloakify build' and see what's wrong.`
)
);
process.exit(1);
}
}
const jarFilePath = fs
.readdirSync(buildContext.keycloakifyBuildDirPath)
.filter(fileBasename => fileBasename.endsWith(".jar"))
.map(fileBasename => pathJoin(buildContext.keycloakifyBuildDirPath, fileBasename))
.sort((a, b) => fs.statSync(b).mtimeMs - fs.statSync(a).mtimeMs)[0];
assert(jarFilePath !== undefined);
const extensionJarFilePaths = [
2024-12-15 19:45:05 +01:00
...(keycloakMajorVersionNumber <= 20
? (console.log(
chalk.yellow(
"WARNING: With older version of keycloak your changes to the realm configuration are not persisted"
)
),
[])
: [
pathJoin(
getThisCodebaseRootDirPath(),
"src",
"bin",
"start-keycloak",
KEYCLOAKIFY_LOGIN_JAR_BASENAME
)
]),
...(await Promise.all(
buildContext.startKeycloakOptions.extensionJars.map(async extensionJar => {
switch (extensionJar.type) {
case "path": {
assert(
await existsAsync(extensionJar.path),
`${extensionJar.path} does not exist`
);
return extensionJar.path;
}
case "url": {
const { archiveFilePath } = await downloadAndExtractArchive({
cacheDirPath: buildContext.cacheDirPath,
fetchOptions: buildContext.fetchOptions,
url: extensionJar.url,
uniqueIdOfOnArchiveFile: "no extraction",
onArchiveFile: async () => {}
});
return archiveFilePath;
}
}
assert<Equals<typeof extensionJar, never>>(false);
})
))
];
2024-06-10 07:57:12 +02:00
async function extractThemeResourcesFromJar() {
await extractArchive({
archiveFilePath: jarFilePath,
2024-06-10 07:57:12 +02:00
onArchiveFile: async ({ relativeFilePathInArchive, writeFile }) => {
if (isInside({ dirPath: "theme", filePath: relativeFilePathInArchive })) {
await writeFile({
filePath: pathJoin(
buildContext.keycloakifyBuildDirPath,
relativeFilePathInArchive
)
});
}
}
});
2024-06-10 07:57:12 +02:00
}
2024-06-10 07:57:12 +02:00
{
const destDirPath = pathJoin(buildContext.keycloakifyBuildDirPath, "theme");
if (await existsAsync(destDirPath)) {
await rm(destDirPath, { recursive: true });
}
}
2024-06-10 07:57:12 +02:00
await extractThemeResourcesFromJar();
const jarFilePath_cacheDir = pathJoin(
buildContext.cacheDirPath,
pathBasename(jarFilePath)
);
fs.copyFileSync(jarFilePath, jarFilePath_cacheDir);
2024-05-26 19:40:13 +02:00
try {
2024-07-13 19:33:59 +02:00
child_process.execSync(`docker rm --force ${CONTAINER_NAME}`, {
2024-05-26 19:40:13 +02:00
stdio: "ignore"
});
} catch {}
const port = cliCommandOptions.port ?? buildContext.startKeycloakOptions.port ?? 8080;
2024-12-02 00:41:12 +01:00
const doStartDevServer = (() => {
const hasSpaUi =
buildContext.implementedThemeTypes.admin.isImplemented ||
(buildContext.implementedThemeTypes.account.isImplemented &&
buildContext.implementedThemeTypes.account.type === "Single-Page");
if (!hasSpaUi) {
2024-12-02 00:41:12 +01:00
return false;
}
if (buildContext.bundler !== "vite") {
console.log(
chalk.yellow(
[
`WARNING: Since you are using ${buildContext.bundler} instead of Vite,`,
`you'll have to wait serval seconds for the changes you made on your account or admin theme to be reflected in the browser.\n`,
`For a better development experience, consider migrating to Vite.`
].join(" ")
)
);
2024-12-02 00:41:12 +01:00
return false;
}
if (keycloakMajorVersionNumber < 25) {
console.log(
chalk.yellow(
[
`WARNING: Your account or admin theme can't be tested with hot module replacement on Keycloak ${keycloakMajorVersionNumber}.`,
`This mean that you'll have to wait serval seconds for the changes to be reflected in the browser.`,
`For a better development experience, select a more recent version of Keycloak.`
].join("\n")
)
);
2024-12-02 00:41:12 +01:00
return false;
}
2024-12-02 00:41:12 +01:00
return true;
})();
2024-12-02 00:41:12 +01:00
let devServerPort: number | undefined = undefined;
if (doStartDevServer) {
const { port } = await startViteDevServer({ buildContext });
devServerPort = port;
}
const SPACE_PLACEHOLDER = "SPACE_PLACEHOLDER_xKLmdPd";
const dockerRunArgs: string[] = [
`-p${SPACE_PLACEHOLDER}${port}:8080`,
`--name${SPACE_PLACEHOLDER}${CONTAINER_NAME}`,
2024-12-15 18:15:36 +01:00
...(keycloakMajorVersionNumber >= 26
? [
`-e${SPACE_PLACEHOLDER}KC_BOOTSTRAP_ADMIN_USERNAME=admin`,
`-e${SPACE_PLACEHOLDER}KC_BOOTSTRAP_ADMIN_PASSWORD=admin`
]
: [
`-e${SPACE_PLACEHOLDER}KEYCLOAK_ADMIN=admin`,
`-e${SPACE_PLACEHOLDER}KEYCLOAK_ADMIN_PASSWORD=admin`
]),
...(devServerPort === undefined
? []
: [
`-e${SPACE_PLACEHOLDER}${KEYCLOAKIFY_SPA_DEV_SERVER_PORT}=${devServerPort}`
]),
...(buildContext.startKeycloakOptions.dockerExtraArgs.length === 0
? []
: [
buildContext.startKeycloakOptions.dockerExtraArgs.join(
SPACE_PLACEHOLDER
)
]),
...(realmJsonFilePath === undefined
? []
: [
`-v${SPACE_PLACEHOLDER}"${realmJsonFilePath}":/opt/keycloak/data/import/${realmName}-realm.json`
]),
`-v${SPACE_PLACEHOLDER}"${jarFilePath_cacheDir}":/opt/keycloak/providers/keycloak-theme.jar`,
...extensionJarFilePaths.map(
jarFilePath =>
`-v${SPACE_PLACEHOLDER}"${jarFilePath}":/opt/keycloak/providers/${pathBasename(jarFilePath)}`
),
...(keycloakMajorVersionNumber <= 20
? [`-e${SPACE_PLACEHOLDER}JAVA_OPTS=-Dkeycloak.profile=preview`]
: []),
...[
...buildContext.themeNames,
...(fs.existsSync(
2024-09-08 12:00:07 +02:00
pathJoin(buildContext.keycloakifyBuildDirPath, "theme", "account-v1")
)
2024-09-08 12:00:07 +02:00
? ["account-v1"]
: [])
]
.map(themeName => ({
localDirPath: pathJoin(
buildContext.keycloakifyBuildDirPath,
"theme",
themeName
),
containerDirPath: `/opt/keycloak/themes/${themeName}`
}))
.map(
({ localDirPath, containerDirPath }) =>
`-v${SPACE_PLACEHOLDER}"${localDirPath}":${containerDirPath}:rw`
),
...buildContext.environmentVariables
.map(({ name }) => ({ name, envValue: process.env[name] }))
.map(({ name, envValue }) =>
envValue === undefined ? undefined : { name, envValue }
)
.filter(exclude(undefined))
.map(
({ name, envValue }) =>
`--env${SPACE_PLACEHOLDER}${name}='${envValue.replace(/'/g, "'\\''")}'`
),
`${buildContext.startKeycloakOptions.dockerImage?.reference ?? "quay.io/keycloak/keycloak"}:${dockerImageTag}`,
"start-dev",
...(21 <= keycloakMajorVersionNumber && keycloakMajorVersionNumber < 24
? ["--features=declarative-user-profile"]
: []),
...(realmJsonFilePath === undefined ? [] : ["--import-realm"]),
...(buildContext.startKeycloakOptions.keycloakExtraArgs.length === 0
? []
: [
buildContext.startKeycloakOptions.keycloakExtraArgs.join(
SPACE_PLACEHOLDER
)
])
];
console.log(
chalk.blue(
[
`$ docker run \\`,
...dockerRunArgs
.map(arg => arg.replace(new RegExp(SPACE_PLACEHOLDER, "g"), " "))
.map(
(line, i, arr) =>
` ${line}${arr.length - 1 === i ? "" : " \\"}`
)
].join("\n")
)
);
2024-05-20 02:27:40 +02:00
const child = child_process.spawn(
"docker",
["run", ...dockerRunArgs.map(line => line.split(SPACE_PLACEHOLDER)).flat()],
{ shell: true }
);
2024-05-17 05:13:41 +02:00
child.stdout.on("data", async data => {
if (data.toString("utf8").includes("keycloakify-logging: REALM_CONFIG_CHANGED")) {
await onRealmConfigChange();
return;
}
process.stdout.write(data);
});
2024-05-18 07:53:06 +02:00
child.stderr.on("data", data => process.stderr.write(data));
2024-05-17 05:13:41 +02:00
2024-05-20 02:27:40 +02:00
child.on("exit", process.exit);
2024-06-09 09:15:16 +02:00
const srcDirPath = pathJoin(buildContext.projectDirPath, "src");
2024-05-20 02:27:40 +02:00
2024-05-18 10:02:14 +02:00
{
const kcHttpRelativePath = (() => {
const match = buildContext.startKeycloakOptions.dockerExtraArgs
.join(" ")
.match(/KC_HTTP_RELATIVE_PATH=([^ ]+)/);
if (match === null) {
return undefined;
}
return match[1];
})();
2024-05-18 10:02:14 +02:00
const handler = async (data: Buffer) => {
if (!data.toString("utf8").includes("Listening on: http://0.0.0.0:8080")) {
return;
}
child.stdout.off("data", handler);
await new Promise(resolve => setTimeout(resolve, 1_000));
console.log(
[
"",
`The ftl files from ${chalk.bold(
`.${pathSep}${pathRelative(process.cwd(), pathJoin(buildContext.keycloakifyBuildDirPath, "theme"))}`
)} are mounted in the Keycloak container.`,
2024-05-26 19:55:59 +02:00
"",
`Keycloak Admin console: ${chalk.cyan.bold(
`http://localhost:${port}${kcHttpRelativePath ?? ""}`
2024-05-26 19:55:59 +02:00
)}`,
`- user: ${chalk.cyan.bold("admin")}`,
`- password: ${chalk.cyan.bold("admin")}`,
"",
2024-05-18 10:02:14 +02:00
"",
`${chalk.green("Your theme is accessible at:")}`,
2024-05-20 15:48:51 +02:00
`${chalk.green("➜")} ${chalk.cyan.bold(
(() => {
const url = new URL(TEST_APP_URL);
if (port !== 8080) {
url.searchParams.set("port", `${port}`);
}
if (kcHttpRelativePath !== undefined) {
url.searchParams.set(
"kcHttpRelativePath",
kcHttpRelativePath
);
}
if (realmName !== "myrealm") {
url.searchParams.set("realm", realmName);
}
if (clientName !== "myclient") {
url.searchParams.set("client", clientName);
}
return url.href;
})()
2024-05-20 15:48:51 +02:00
)}`,
"",
"You can login with the following credentials:",
`- username: ${chalk.cyan.bold(username)}`,
`- password: ${chalk.cyan.bold("password123")}`,
2024-05-20 02:27:40 +02:00
"",
2024-05-20 15:48:51 +02:00
`Watching for changes in ${chalk.bold(
2024-06-09 09:15:16 +02:00
`.${pathSep}${pathRelative(process.cwd(), buildContext.projectDirPath)}`
2024-05-20 15:48:51 +02:00
)}`
2024-05-18 10:02:14 +02:00
].join("\n")
);
};
child.stdout.on("data", handler);
}
2024-05-20 02:27:40 +02:00
{
const runFullBuild = runExclusive.build(async () => {
2024-05-20 19:30:04 +02:00
console.log(chalk.cyan("Detected changes in the theme. Rebuilding ..."));
2024-05-20 15:34:07 +02:00
const { isAppBuildSuccess } = await appBuild({
2024-06-09 09:15:16 +02:00
buildContext
});
2024-05-20 02:27:40 +02:00
if (!isAppBuildSuccess) {
return;
2024-05-20 19:30:04 +02:00
}
2024-05-20 02:27:40 +02:00
const { isKeycloakifyBuildSuccess } = await keycloakifyBuild({
buildForKeycloakMajorVersionNumber: keycloakMajorVersionNumber,
2024-06-09 09:15:16 +02:00
buildContext
});
2024-05-20 02:42:57 +02:00
if (!isKeycloakifyBuildSuccess) {
return;
}
2024-05-20 19:30:04 +02:00
2024-06-10 07:57:12 +02:00
await extractThemeResourcesFromJar();
2024-05-20 19:30:04 +02:00
console.log(chalk.green("Theme rebuilt and updated in Keycloak."));
2024-05-20 19:30:04 +02:00
});
const { waitForDebounce } = waitForDebounceFactory({ delay: 400 });
2024-05-20 19:30:04 +02:00
chokidar
.watch(
[
srcDirPath,
2024-06-09 09:15:16 +02:00
buildContext.publicDirPath,
pathJoin(buildContext.projectDirPath, "package.json"),
pathJoin(buildContext.projectDirPath, "vite.config.ts"),
pathJoin(buildContext.projectDirPath, "vite.config.js"),
pathJoin(buildContext.projectDirPath, "index.html"),
pathJoin(getThisCodebaseRootDirPath(), "src")
],
{
ignoreInitial: true
}
)
.on("all", async (...[, filePath]) => {
ignore_account_spa: {
const doImplementAccountSpa =
buildContext.implementedThemeTypes.account.isImplemented &&
buildContext.implementedThemeTypes.account.type === "Single-Page";
if (!doImplementAccountSpa) {
break ignore_account_spa;
}
if (
!isInside({
dirPath: pathJoin(buildContext.themeSrcDirPath, "account"),
filePath
})
) {
break ignore_account_spa;
}
return;
}
ignore_admin: {
if (!buildContext.implementedThemeTypes.admin.isImplemented) {
break ignore_admin;
}
if (
!isInside({
dirPath: pathJoin(buildContext.themeSrcDirPath, "admin"),
filePath
})
) {
break ignore_admin;
}
return;
}
console.log(`Detected changes in ${filePath}`);
2024-05-20 19:30:04 +02:00
await waitForDebounce();
runFullBuild();
2024-05-20 15:48:51 +02:00
});
2024-05-20 02:27:40 +02:00
}
}