2020-08-06 09:31:45 +00:00
|
|
|
#!/usr/bin/env node
|
2021-07-15 20:56:25 +00:00
|
|
|
import http from "node:https";
|
|
|
|
import fs from "node:fs";
|
|
|
|
import path from "node:path";
|
|
|
|
import { spawn } from "node:child_process";
|
2020-08-06 09:31:45 +00:00
|
|
|
|
2021-07-15 20:56:25 +00:00
|
|
|
import decompress from "decompress";
|
|
|
|
import decompressTargz from "decompress-targz";
|
2020-08-06 09:31:45 +00:00
|
|
|
|
|
|
|
const args = process.argv.slice(2);
|
2021-08-03 08:48:56 +00:00
|
|
|
const version = args[0] || "15.0.0";
|
2020-08-06 09:31:45 +00:00
|
|
|
|
2021-01-21 12:09:50 +00:00
|
|
|
const folder = "server";
|
|
|
|
const fileName = path.join(folder, `keycloak-${version}.tar.gz`);
|
2020-08-06 09:31:45 +00:00
|
|
|
const serverPath = path.join(folder, `keycloak-${version}`);
|
2021-01-21 12:09:50 +00:00
|
|
|
const extension = process.platform === "win32" ? ".bat" : ".sh";
|
2020-08-06 09:31:45 +00:00
|
|
|
|
|
|
|
if (!fs.existsSync(folder)) {
|
|
|
|
fs.mkdirSync(folder);
|
|
|
|
}
|
|
|
|
|
2021-07-15 20:56:25 +00:00
|
|
|
async function decompressKeycloak() {
|
|
|
|
try {
|
|
|
|
await decompress(fileName, folder, {
|
|
|
|
plugins: [decompressTargz()],
|
|
|
|
});
|
|
|
|
|
|
|
|
console.log("Files decompressed");
|
|
|
|
} catch (error) {
|
|
|
|
console.error(error);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-08-06 09:31:45 +00:00
|
|
|
const run = () => {
|
2021-07-15 20:56:25 +00:00
|
|
|
const addProc = spawn(
|
|
|
|
path.join(serverPath, "bin", `add-user-keycloak${extension}`),
|
|
|
|
["--user", "admin", "--password", "admin"]
|
|
|
|
);
|
|
|
|
|
2021-01-28 15:04:48 +00:00
|
|
|
addProc.on("exit", () => {
|
|
|
|
const proc = spawn(path.join(serverPath, "bin", `standalone${extension}`), [
|
|
|
|
"-Djboss.socket.binding.port-offset=100",
|
2021-07-16 08:51:09 +00:00
|
|
|
"-Dprofile.feature.newadmin=enabled",
|
2021-01-28 15:04:48 +00:00
|
|
|
]);
|
|
|
|
proc.stdout.on("data", (data) => {
|
|
|
|
console.log(data.toString());
|
|
|
|
});
|
2020-08-06 09:31:45 +00:00
|
|
|
});
|
2021-01-21 12:09:50 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
const request = (url, file) => {
|
|
|
|
http.get(url, (response) => {
|
|
|
|
if (response.statusCode == 302) {
|
|
|
|
request(response.headers.location, file);
|
|
|
|
} else {
|
|
|
|
response.pipe(file);
|
|
|
|
response.on("end", () => {
|
|
|
|
console.log("Downloaded keycloak");
|
|
|
|
decompressKeycloak().then(() => run());
|
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|
|
|
|
};
|
2020-08-06 09:31:45 +00:00
|
|
|
|
|
|
|
if (!fs.existsSync(fileName)) {
|
|
|
|
const file = fs.createWriteStream(fileName);
|
2021-01-21 12:09:50 +00:00
|
|
|
request(
|
|
|
|
`https://github.com/keycloak/keycloak/releases/download/${version}/keycloak-${version}.tar.gz`,
|
|
|
|
file
|
|
|
|
);
|
2020-08-06 09:31:45 +00:00
|
|
|
} else if (!fs.existsSync(serverPath)) {
|
|
|
|
decompressKeycloak().then(() => run());
|
|
|
|
} else {
|
|
|
|
run();
|
|
|
|
}
|