102 lines
3.6 KiB
TypeScript
102 lines
3.6 KiB
TypeScript
import { readFileSync } from "node:fs";
|
|
|
|
const FULL_GIT_SHA_PATTERN = /^[0-9a-f]{40}$/;
|
|
const IMAGE_REVISION_PATH = "/myapp/.image-revision";
|
|
const DEFAULT_CONTROLLER_PORT = "8080";
|
|
const HEALTH_TIMEOUT_MS = 9_000;
|
|
const CONTROLLER_SERVICE = "hivegrid-portal-domain-controller";
|
|
|
|
type ControllerDeployHealthIo = {
|
|
stderr?: Pick<NodeJS.WriteStream, "write">;
|
|
stdout: Pick<NodeJS.WriteStream, "write">;
|
|
};
|
|
|
|
type ControllerDeployHealthDependencies = {
|
|
fetchProbe?: (url: string, init: RequestInit) => Promise<Pick<Response, "status" | "json">>;
|
|
readImageRevision?: () => string;
|
|
timeoutSignal?: () => AbortSignal;
|
|
};
|
|
|
|
type ControllerProbeResponse = {
|
|
status?: unknown;
|
|
service?: unknown;
|
|
revision?: unknown;
|
|
};
|
|
|
|
function fail(io: ControllerDeployHealthIo, message: string): number {
|
|
io.stderr?.write(`${message}\n`);
|
|
return 1;
|
|
}
|
|
|
|
export async function runControllerDeployHealthCli(
|
|
expectedRevision: string,
|
|
env: NodeJS.ProcessEnv = process.env,
|
|
io: ControllerDeployHealthIo = process,
|
|
dependencies: ControllerDeployHealthDependencies = {}
|
|
): Promise<number> {
|
|
if (!FULL_GIT_SHA_PATTERN.test(expectedRevision)) {
|
|
return fail(io, "Portal-domain controller deployment health requires a full lowercase Git SHA.");
|
|
}
|
|
|
|
let imageRevision = "";
|
|
try {
|
|
imageRevision = (dependencies.readImageRevision ?? (() => readFileSync(IMAGE_REVISION_PATH, "utf8")))().trim();
|
|
} catch {
|
|
return fail(io, "Portal-domain controller deployment image revision is unavailable.");
|
|
}
|
|
if (imageRevision !== expectedRevision || env.APP_REVISION !== expectedRevision) {
|
|
return fail(io, "Portal-domain controller deployment revision does not match the expected image.");
|
|
}
|
|
|
|
const port = env.PORTAL_DOMAIN_CONTROLLER_PORT || env.PORT || DEFAULT_CONTROLLER_PORT;
|
|
if (!/^\d{1,5}$/.test(port) || Number(port) < 1 || Number(port) > 65_535) {
|
|
return fail(io, "Portal-domain controller deployment health port is invalid.");
|
|
}
|
|
|
|
try {
|
|
const fetchProbe = dependencies.fetchProbe ?? fetch;
|
|
const timeoutSignal = dependencies.timeoutSignal ?? (() => AbortSignal.timeout(HEALTH_TIMEOUT_MS));
|
|
const [healthResponse, readinessResponse] = await Promise.all([
|
|
fetchProbe(`http://127.0.0.1:${port}/healthz`, {
|
|
headers: { Accept: "application/json" },
|
|
signal: timeoutSignal(),
|
|
}),
|
|
fetchProbe(`http://127.0.0.1:${port}/readyz`, {
|
|
headers: { Accept: "application/json" },
|
|
signal: timeoutSignal(),
|
|
}),
|
|
]);
|
|
if (healthResponse.status !== 200 || readinessResponse.status !== 200) {
|
|
return fail(io, "Portal-domain controller deployment health or readiness is not healthy.");
|
|
}
|
|
const [health, readiness] = (await Promise.all([healthResponse.json(), readinessResponse.json()])) as [
|
|
ControllerProbeResponse,
|
|
ControllerProbeResponse,
|
|
];
|
|
if (
|
|
health.status !== "ok" ||
|
|
health.service !== CONTROLLER_SERVICE ||
|
|
health.revision !== expectedRevision ||
|
|
readiness.status !== "ready" ||
|
|
readiness.service !== CONTROLLER_SERVICE
|
|
) {
|
|
return fail(io, "Portal-domain controller deployment probes do not match the expected runtime.");
|
|
}
|
|
} catch {
|
|
return fail(io, "Portal-domain controller deployment probe failed.");
|
|
}
|
|
|
|
io.stdout.write(`${JSON.stringify({ status: "ready", service: CONTROLLER_SERVICE, revision: expectedRevision })}\n`);
|
|
return 0;
|
|
}
|
|
|
|
if (import.meta.url === `file://${process.argv[1]}`) {
|
|
runControllerDeployHealthCli(process.argv[2] || "")
|
|
.then((exitCode) => {
|
|
process.exitCode = exitCode;
|
|
})
|
|
.catch(() => {
|
|
process.exitCode = 1;
|
|
});
|
|
}
|