229 lines
7.1 KiB
JavaScript
229 lines
7.1 KiB
JavaScript
#!/usr/bin/env node
|
|
|
|
const [, , command, ...args] = process.argv;
|
|
|
|
const apiUrl = (
|
|
process.env.SELECTED_COOLIFY_API_URL ||
|
|
process.env.COOLIFY_STAGING_API_URL ||
|
|
process.env.COOLIFY_API_URL ||
|
|
""
|
|
).replace(/\/+$/, "");
|
|
const token =
|
|
process.env.SELECTED_COOLIFY_TOKEN ||
|
|
process.env.COOLIFY_STAGING_TOKEN ||
|
|
process.env.COOLIFY_TOKEN ||
|
|
"";
|
|
const delayMs = Number(process.env.COOLIFY_DEPLOYMENT_POLL_MS || "5000");
|
|
const attempts = Number(process.env.COOLIFY_DEPLOYMENT_WAIT_ATTEMPTS || "90");
|
|
const apiTimeoutMs = Number(process.env.COOLIFY_API_TIMEOUT_MS || "20000");
|
|
const COOLIFY_CUID_PATTERN = /^[a-z][a-z0-9]{23}$/;
|
|
const OCI_DIGEST_PATTERN = /^sha256:[0-9a-f]{64}$/;
|
|
|
|
function fail(message) {
|
|
process.stderr.write(`${message}\n`);
|
|
process.exit(1);
|
|
}
|
|
|
|
async function coolify(path, options = {}) {
|
|
if (!apiUrl || !token) {
|
|
fail("Coolify API URL and token are required");
|
|
}
|
|
|
|
let response;
|
|
try {
|
|
response = await fetch(`${apiUrl}${path}`, {
|
|
...options,
|
|
headers: {
|
|
Authorization: `Bearer ${token}`,
|
|
Accept: "application/json",
|
|
...(options.body ? { "Content-Type": "application/json" } : {}),
|
|
...options.headers,
|
|
},
|
|
signal: AbortSignal.timeout(apiTimeoutMs),
|
|
});
|
|
} catch {
|
|
fail("Coolify API request failed or timed out");
|
|
}
|
|
|
|
const text = await response.text();
|
|
let payload = {};
|
|
if (text.trim()) {
|
|
try {
|
|
payload = JSON.parse(text);
|
|
} catch {
|
|
payload = { message: text.trim() };
|
|
}
|
|
}
|
|
|
|
if (!response.ok) {
|
|
fail(`Coolify API request failed with HTTP ${response.status}`);
|
|
}
|
|
|
|
return payload;
|
|
}
|
|
|
|
function deploymentStatus(payload) {
|
|
return String(
|
|
payload.status ||
|
|
payload.deployment?.status ||
|
|
payload.data?.status ||
|
|
"unknown"
|
|
).toLowerCase();
|
|
}
|
|
|
|
function deploymentUuid(payload) {
|
|
const value =
|
|
payload.deployments?.[0]?.deployment_uuid ||
|
|
payload.deployment_uuid ||
|
|
payload.uuid ||
|
|
payload.deployment?.uuid ||
|
|
payload.data?.deployment_uuid ||
|
|
payload.data?.uuid ||
|
|
"";
|
|
return typeof value === "string" ? value : "";
|
|
}
|
|
|
|
function requireCoolifyCuid(value, label) {
|
|
if (!COOLIFY_CUID_PATTERN.test(value)) {
|
|
fail(`${label} must be a 24-character Coolify identifier`);
|
|
}
|
|
}
|
|
|
|
function digestTag(digest) {
|
|
if (!OCI_DIGEST_PATTERN.test(digest)) {
|
|
fail("Image digest must be a lowercase sha256 OCI digest");
|
|
}
|
|
return digest.replace(":", "-");
|
|
}
|
|
|
|
function normalized(value) {
|
|
return typeof value === "string" ? value.trim() : "";
|
|
}
|
|
|
|
async function trigger(resourceUuid) {
|
|
requireCoolifyCuid(resourceUuid, "Coolify resource UUID");
|
|
|
|
const payload = await coolify(
|
|
`/deploy?uuid=${encodeURIComponent(resourceUuid)}&force=true`,
|
|
{ method: "POST" }
|
|
);
|
|
const queuedDeployments = Array.isArray(payload.deployments)
|
|
? payload.deployments
|
|
: [];
|
|
const matchingDeployment = queuedDeployments.find(
|
|
(deployment) => deployment?.resource_uuid === resourceUuid
|
|
);
|
|
const uuid = matchingDeployment?.deployment_uuid || deploymentUuid(payload);
|
|
if (!uuid) {
|
|
fail("Coolify deployment UUID was not returned by the deploy request");
|
|
}
|
|
requireCoolifyCuid(uuid, "Coolify deployment UUID");
|
|
process.stdout.write(uuid);
|
|
}
|
|
|
|
async function waitForDeployment(uuid, label) {
|
|
requireCoolifyCuid(uuid, "Coolify deployment UUID");
|
|
|
|
for (let attempt = 1; attempt <= attempts; attempt += 1) {
|
|
const payload = await coolify(`/deployments/${encodeURIComponent(uuid)}`);
|
|
const status = deploymentStatus(payload);
|
|
process.stdout.write(`${label} deployment status: ${status}\n`);
|
|
|
|
if (["finished", "success", "succeeded", "completed", "done"].includes(status)) {
|
|
return;
|
|
}
|
|
if (["failed", "error", "cancelled", "canceled"].includes(status)) {
|
|
fail(`${label} deployment failed`);
|
|
}
|
|
await new Promise((resolve) => setTimeout(resolve, delayMs));
|
|
}
|
|
|
|
fail(`${label} deployment did not finish in time`);
|
|
}
|
|
|
|
async function setRelease(resourceUuid, expectedImage, digest) {
|
|
requireCoolifyCuid(resourceUuid, "Coolify resource UUID");
|
|
if (!expectedImage) {
|
|
fail("Expected registry image is required");
|
|
}
|
|
const expectedTag = digestTag(digest);
|
|
const path = `/applications/${encodeURIComponent(resourceUuid)}`;
|
|
const before = await coolify(path);
|
|
if (
|
|
normalized(before?.uuid) !== resourceUuid ||
|
|
normalized(before?.docker_registry_image_name) !== expectedImage
|
|
) {
|
|
fail("Coolify website resource identity does not match the expected registry image");
|
|
}
|
|
|
|
await coolify(path, {
|
|
method: "PATCH",
|
|
body: JSON.stringify({
|
|
docker_registry_image_tag: expectedTag,
|
|
pre_deployment_command: "",
|
|
post_deployment_command: "",
|
|
}),
|
|
});
|
|
const after = await coolify(path);
|
|
if (
|
|
normalized(after?.uuid) !== resourceUuid ||
|
|
normalized(after?.docker_registry_image_name) !== expectedImage ||
|
|
normalized(after?.docker_registry_image_tag) !== expectedTag
|
|
) {
|
|
fail("Coolify website resource did not persist the expected registry digest");
|
|
}
|
|
}
|
|
|
|
async function assertRelease(resourceUuid, expectedImage, digest) {
|
|
requireCoolifyCuid(resourceUuid, "Coolify resource UUID");
|
|
const expectedTag = digestTag(digest);
|
|
const app = await coolify(`/applications/${encodeURIComponent(resourceUuid)}`);
|
|
if (
|
|
normalized(app?.uuid) !== resourceUuid ||
|
|
normalized(app?.docker_registry_image_name) !== expectedImage
|
|
) {
|
|
fail("Coolify website release proof resolved the wrong resource");
|
|
}
|
|
if (normalized(app?.docker_registry_image_tag) !== expectedTag) {
|
|
fail("Coolify website is not pinned to the expected registry digest");
|
|
}
|
|
if (normalized(app?.status).toLowerCase() !== "running:healthy") {
|
|
fail("Coolify website is not running healthy at the expected registry digest");
|
|
}
|
|
}
|
|
|
|
async function currentRelease(resourceUuid, expectedImage) {
|
|
requireCoolifyCuid(resourceUuid, "Coolify resource UUID");
|
|
if (!expectedImage) {
|
|
fail("Expected registry image is required");
|
|
}
|
|
const app = await coolify(`/applications/${encodeURIComponent(resourceUuid)}`);
|
|
if (
|
|
normalized(app?.uuid) !== resourceUuid ||
|
|
normalized(app?.docker_registry_image_name) !== expectedImage
|
|
) {
|
|
fail("Coolify website release proof resolved the wrong resource");
|
|
}
|
|
const tag = normalized(app?.docker_registry_image_tag);
|
|
if (!/^sha256-[0-9a-f]{64}$/.test(tag)) {
|
|
fail("Coolify website is not pinned to an immutable rollback digest");
|
|
}
|
|
process.stdout.write(tag.replace("-", ":"));
|
|
}
|
|
|
|
if (command === "trigger") {
|
|
await trigger(args[0] || "");
|
|
} else if (command === "wait") {
|
|
await waitForDeployment(args[0] || "", args[1] || "Coolify");
|
|
} else if (command === "set-release") {
|
|
await setRelease(args[0] || "", args[1] || "", args[2] || "");
|
|
} else if (command === "assert-release") {
|
|
await assertRelease(args[0] || "", args[1] || "", args[2] || "");
|
|
} else if (command === "current-release") {
|
|
await currentRelease(args[0] || "", args[1] || "");
|
|
} else {
|
|
fail(
|
|
"Usage: coolify-deploy.mjs trigger <resource-uuid> | wait <deployment-uuid> <label> | set-release <resource-uuid> <image> <digest> | assert-release <resource-uuid> <image> <digest> | current-release <resource-uuid> <image>"
|
|
);
|
|
}
|