@caelo-cms/provisioning 0.11.21 → 0.12.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/adapter.d.ts +6 -1
- package/dist/adapter.d.ts.map +1 -1
- package/dist/agent-db.js +137 -9
- package/dist/bootstrap-token.d.ts +4 -3
- package/dist/bootstrap-token.d.ts.map +1 -1
- package/dist/bootstrap-token.js.map +1 -1
- package/dist/cli-routing.d.ts.map +1 -1
- package/dist/cli-routing.js +5 -0
- package/dist/cli-routing.js.map +1 -1
- package/dist/cli.js +4 -0
- package/dist/cli.js.map +1 -1
- package/dist/converge-job-gcp.d.ts +1 -16
- package/dist/converge-job-gcp.d.ts.map +1 -1
- package/dist/converge-job-gcp.js +10 -97
- package/dist/converge-job-gcp.js.map +1 -1
- package/dist/converge-job.d.ts +7 -46
- package/dist/converge-job.d.ts.map +1 -1
- package/dist/converge-job.js +22 -57
- package/dist/converge-job.js.map +1 -1
- package/dist/gateway-armor.d.ts.map +1 -1
- package/dist/gateway-armor.js +11 -3
- package/dist/gateway-armor.js.map +1 -1
- package/dist/gcp-names.d.ts +2 -0
- package/dist/gcp-names.d.ts.map +1 -1
- package/dist/gcp-names.js +4 -0
- package/dist/gcp-names.js.map +1 -1
- package/dist/index.d.ts +3 -2
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +1 -1
- package/dist/index.js.map +1 -1
- package/dist/infra-contract.d.ts +1 -1
- package/dist/infra-contract.d.ts.map +1 -1
- package/dist/infra-contract.js +14 -1
- package/dist/infra-contract.js.map +1 -1
- package/dist/job-handover-gcp.d.ts +40 -0
- package/dist/job-handover-gcp.d.ts.map +1 -0
- package/dist/job-handover-gcp.js +108 -0
- package/dist/job-handover-gcp.js.map +1 -0
- package/dist/job-handover.d.ts +141 -0
- package/dist/job-handover.d.ts.map +1 -0
- package/dist/job-handover.js +141 -0
- package/dist/job-handover.js.map +1 -0
- package/dist/owner-setup-token.d.ts +52 -0
- package/dist/owner-setup-token.d.ts.map +1 -0
- package/dist/owner-setup-token.js +111 -0
- package/dist/owner-setup-token.js.map +1 -0
- package/dist/project-status.d.ts +33 -0
- package/dist/project-status.d.ts.map +1 -1
- package/dist/project-status.js +116 -26
- package/dist/project-status.js.map +1 -1
- package/dist/stack-converge.d.ts.map +1 -1
- package/dist/stack-converge.js +9 -5
- package/dist/stack-converge.js.map +1 -1
- package/dist/static-security-headers.d.ts +6 -0
- package/dist/static-security-headers.d.ts.map +1 -1
- package/dist/static-security-headers.js +11 -1
- package/dist/static-security-headers.js.map +1 -1
- package/dist/upgrader-assess.d.ts +30 -3
- package/dist/upgrader-assess.d.ts.map +1 -1
- package/dist/upgrader-assess.js +77 -24
- package/dist/upgrader-assess.js.map +1 -1
- package/dist/upgrader-gcp.d.ts +4 -2
- package/dist/upgrader-gcp.d.ts.map +1 -1
- package/dist/upgrader-gcp.js +16 -3
- package/dist/upgrader-gcp.js.map +1 -1
- package/dist/upgrader-iam-preflight.d.ts +14 -3
- package/dist/upgrader-iam-preflight.d.ts.map +1 -1
- package/dist/upgrader-iam-preflight.js +40 -6
- package/dist/upgrader-iam-preflight.js.map +1 -1
- package/dist/upgrader-job.d.ts +51 -9
- package/dist/upgrader-job.d.ts.map +1 -1
- package/dist/upgrader-job.js +92 -37
- package/dist/upgrader-job.js.map +1 -1
- package/dist/upgrader-setup.d.ts +1 -1
- package/dist/upgrader-setup.d.ts.map +1 -1
- package/dist/upgrader-setup.js +5 -2
- package/dist/upgrader-setup.js.map +1 -1
- package/dist/wizards/gcp-install-job.d.ts.map +1 -1
- package/dist/wizards/gcp-install-job.js +11 -1
- package/dist/wizards/gcp-install-job.js.map +1 -1
- package/dist/wizards/gcp-pulumi.d.ts +0 -13
- package/dist/wizards/gcp-pulumi.d.ts.map +1 -1
- package/dist/wizards/gcp-pulumi.js +0 -12
- package/dist/wizards/gcp-pulumi.js.map +1 -1
- package/dist/wizards/gcp.d.ts.map +1 -1
- package/dist/wizards/gcp.js +21 -50
- package/dist/wizards/gcp.js.map +1 -1
- package/package.json +2 -2
- package/src/adapter.ts +6 -1
- package/src/bootstrap-token.ts +4 -3
- package/src/caddy-redirects.docker.test.ts +5 -0
- package/src/cli-routing.test.ts +1 -1
- package/src/cli-routing.ts +6 -0
- package/src/cli.ts +4 -0
- package/src/converge-job-gcp.test.ts +1 -40
- package/src/converge-job-gcp.ts +10 -100
- package/src/converge-job.test.ts +9 -3
- package/src/converge-job.ts +33 -115
- package/src/gateway-armor.ts +14 -3
- package/src/gateway-edge.test.ts +16 -1
- package/src/gcp-names.ts +4 -0
- package/src/gcp-url-map.test.ts +2 -0
- package/src/index.ts +3 -0
- package/src/infra-contract.test.ts +4 -3
- package/src/infra-contract.ts +14 -1
- package/src/job-handover-gcp.test.ts +129 -0
- package/src/job-handover-gcp.ts +135 -0
- package/src/job-handover.ts +259 -0
- package/src/owner-setup-token.test.ts +195 -0
- package/src/owner-setup-token.ts +138 -0
- package/src/project-status.test.ts +112 -3
- package/src/project-status.ts +141 -17
- package/src/stack-converge.ts +12 -5
- package/src/static-security-headers.test.ts +16 -0
- package/src/static-security-headers.ts +12 -1
- package/src/upgrader-assess.test.ts +75 -1
- package/src/upgrader-assess.ts +121 -57
- package/src/upgrader-gcp.ts +19 -5
- package/src/upgrader-iam-preflight.test.ts +28 -1
- package/src/upgrader-iam-preflight.ts +46 -6
- package/src/upgrader-job.test.ts +365 -31
- package/src/upgrader-job.ts +177 -53
- package/src/upgrader-setup.test.ts +17 -0
- package/src/upgrader-setup.ts +5 -2
- package/src/url-map-redirects-access.test.ts +1 -1
- package/src/wizards/gcp-install-job.ts +10 -1
- package/src/wizards/gcp-pulumi.ts +0 -18
- package/src/wizards/gcp.ts +24 -61
- package/stacks/aws/README.md +4 -2
- package/stacks/aws/index.ts +4 -8
- package/stacks/azure/README.md +4 -2
- package/stacks/azure/index.ts +4 -8
- package/stacks/gcp/README.md +6 -3
- package/stacks/gcp/index.ts +7 -8
- package/stacks/gcp-firebase/index.ts +4 -6
|
@@ -0,0 +1,129 @@
|
|
|
1
|
+
// SPDX-License-Identifier: MPL-2.0
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* What an update job does to itself (#819, #1029). The trust boundary
|
|
5
|
+
* rests on these argv: the image moves by digest and nothing else moves, and
|
|
6
|
+
* the new execution carries no override of args or env (no principal but
|
|
7
|
+
* the provisioner may run a job with overrides, CLAUDE.md §11.C).
|
|
8
|
+
*/
|
|
9
|
+
|
|
10
|
+
import { describe, expect, it } from "bun:test";
|
|
11
|
+
import type { GcloudRunner } from "./gcloud-retry.js";
|
|
12
|
+
import {
|
|
13
|
+
executionImageDigest,
|
|
14
|
+
jobExecutionNameError,
|
|
15
|
+
selfHandoverDeps,
|
|
16
|
+
startedExecutionName,
|
|
17
|
+
} from "./job-handover-gcp.js";
|
|
18
|
+
|
|
19
|
+
const D = `sha256:${"c".repeat(64)}`;
|
|
20
|
+
const OPS = `europe-west1-docker.pkg.dev/caelo-website/caelo-cms-images/ops@${D}`;
|
|
21
|
+
const JOB = "caelo-production-upgrader";
|
|
22
|
+
|
|
23
|
+
function fake(stdout = "caelo-production-upgrader-abc12\n") {
|
|
24
|
+
const calls: string[][] = [];
|
|
25
|
+
const run: GcloudRunner = async (args) => {
|
|
26
|
+
calls.push(args);
|
|
27
|
+
return { ok: true, stdout, stderr: "", exitCode: 0 };
|
|
28
|
+
};
|
|
29
|
+
const deps = selfHandoverDeps(run, {
|
|
30
|
+
job: JOB,
|
|
31
|
+
projectId: "acme",
|
|
32
|
+
region: "europe-west1",
|
|
33
|
+
execution: `${JOB}-x1`,
|
|
34
|
+
});
|
|
35
|
+
return { calls, deps };
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
describe("selfHandoverDeps", () => {
|
|
39
|
+
it("points the job at the digest and changes nothing else", async () => {
|
|
40
|
+
const { calls, deps } = fake();
|
|
41
|
+
await deps.pointJobAt(D);
|
|
42
|
+
expect(calls).toEqual([
|
|
43
|
+
[
|
|
44
|
+
"run",
|
|
45
|
+
"jobs",
|
|
46
|
+
"update",
|
|
47
|
+
JOB,
|
|
48
|
+
`--image=${OPS}`,
|
|
49
|
+
"--region=europe-west1",
|
|
50
|
+
"--project=acme",
|
|
51
|
+
"--quiet",
|
|
52
|
+
],
|
|
53
|
+
]);
|
|
54
|
+
});
|
|
55
|
+
|
|
56
|
+
it("starts the job with no override of its args, env or task", async () => {
|
|
57
|
+
const { calls, deps } = fake();
|
|
58
|
+
expect(await deps.startJob()).toEqual({
|
|
59
|
+
ok: true,
|
|
60
|
+
execution: "caelo-production-upgrader-abc12",
|
|
61
|
+
});
|
|
62
|
+
expect(calls).toEqual([
|
|
63
|
+
[
|
|
64
|
+
"run",
|
|
65
|
+
"jobs",
|
|
66
|
+
"execute",
|
|
67
|
+
JOB,
|
|
68
|
+
"--region=europe-west1",
|
|
69
|
+
"--project=acme",
|
|
70
|
+
"--async",
|
|
71
|
+
"--format=value(metadata.name)",
|
|
72
|
+
],
|
|
73
|
+
]);
|
|
74
|
+
});
|
|
75
|
+
|
|
76
|
+
it("reads this execution's image, and nothing outside Cloud Run", async () => {
|
|
77
|
+
const { calls, deps } = fake(
|
|
78
|
+
JSON.stringify({ spec: { template: { spec: { containers: [{ image: OPS }] } } } }),
|
|
79
|
+
);
|
|
80
|
+
expect(await deps.ownImageDigest()).toBe(D);
|
|
81
|
+
expect(calls[0]?.slice(0, 5)).toEqual(["run", "jobs", "executions", "describe", `${JOB}-x1`]);
|
|
82
|
+
const outside = selfHandoverDeps(
|
|
83
|
+
async () => ({ ok: false, stdout: "", stderr: "", exitCode: 1 }),
|
|
84
|
+
{
|
|
85
|
+
job: JOB,
|
|
86
|
+
projectId: "acme",
|
|
87
|
+
region: "europe-west1",
|
|
88
|
+
execution: null,
|
|
89
|
+
},
|
|
90
|
+
);
|
|
91
|
+
expect(await outside.ownImageDigest()).toBeNull();
|
|
92
|
+
});
|
|
93
|
+
});
|
|
94
|
+
|
|
95
|
+
describe("jobExecutionNameError", () => {
|
|
96
|
+
it("accepts a run of the job and no other name", () => {
|
|
97
|
+
expect(jobExecutionNameError(JOB, null)).toBeNull();
|
|
98
|
+
expect(jobExecutionNameError(JOB, `${JOB}-abc12`)).toBeNull();
|
|
99
|
+
expect(jobExecutionNameError(JOB, "caelo-production-converge-abc12")).toContain("not a run of");
|
|
100
|
+
expect(jobExecutionNameError(JOB, `${JOB}-ABC`)).toContain("not a run of");
|
|
101
|
+
expect(jobExecutionNameError(JOB, JOB)).toContain("not a run of");
|
|
102
|
+
});
|
|
103
|
+
});
|
|
104
|
+
|
|
105
|
+
describe("executionImageDigest", () => {
|
|
106
|
+
it("reads both shapes gcloud can print", () => {
|
|
107
|
+
const v1 = { spec: { template: { spec: { containers: [{ image: OPS }] } } } };
|
|
108
|
+
expect(executionImageDigest(JSON.stringify(v1))).toBe(D);
|
|
109
|
+
expect(
|
|
110
|
+
executionImageDigest(JSON.stringify({ template: { containers: [{ image: OPS }] } })),
|
|
111
|
+
).toBe(D);
|
|
112
|
+
});
|
|
113
|
+
|
|
114
|
+
it("is null for a tag, an empty description or bad JSON: never a guess", () => {
|
|
115
|
+
const tag = { template: { containers: [{ image: "x/ops:0.11.0" }] } };
|
|
116
|
+
expect(executionImageDigest(JSON.stringify(tag))).toBeNull();
|
|
117
|
+
expect(executionImageDigest("{}")).toBeNull();
|
|
118
|
+
expect(executionImageDigest("not json")).toBeNull();
|
|
119
|
+
});
|
|
120
|
+
});
|
|
121
|
+
|
|
122
|
+
describe("startedExecutionName", () => {
|
|
123
|
+
it("reads --format output, or gcloud's stderr line", () => {
|
|
124
|
+
expect(startedExecutionName("job-abc12\n", "")).toBe("job-abc12");
|
|
125
|
+
expect(startedExecutionName("", "Execution [job-abc12] has started")).toBe("job-abc12");
|
|
126
|
+
expect(startedExecutionName("", "")).toBeNull();
|
|
127
|
+
expect(startedExecutionName("Bad Name; rm -rf", "")).toBeNull();
|
|
128
|
+
});
|
|
129
|
+
});
|
|
@@ -0,0 +1,135 @@
|
|
|
1
|
+
// SPDX-License-Identifier: MPL-2.0
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* The Cloud Run side of an update job's hand-over (job-handover.ts): what
|
|
5
|
+
* the converge job (#819) and the upgrader job (#1029) do to themselves, as
|
|
6
|
+
* their own service accounts. Each holds exactly this on its own job:
|
|
7
|
+
* `run.jobs.update` and `run.jobs.run`, without `run.jobs.runWithOverrides`,
|
|
8
|
+
* and `actAs` on its own account.
|
|
9
|
+
*
|
|
10
|
+
* Two gcloud calls change anything, and tests pin both: `jobs update` with
|
|
11
|
+
* `--image` by digest and no other setting, and `jobs execute` with no flag
|
|
12
|
+
* that overrides the job's args or env.
|
|
13
|
+
*/
|
|
14
|
+
|
|
15
|
+
import type { GcloudRunner } from "./gcloud-retry.js";
|
|
16
|
+
import { describeExecution } from "./install-job.js";
|
|
17
|
+
import type { SelfHandoverDeps } from "./job-handover.js";
|
|
18
|
+
import { releaseImageRef, verifyCosignAll } from "./upgrade/release-images.js";
|
|
19
|
+
|
|
20
|
+
const DIGEST = /@(sha256:[0-9a-f]{64})/;
|
|
21
|
+
|
|
22
|
+
/**
|
|
23
|
+
* The image digest an execution of an update job runs, from its Cloud Run
|
|
24
|
+
* description (the execution's task template names the image it was
|
|
25
|
+
* started with). Null when it cannot be read or names no digest.
|
|
26
|
+
*/
|
|
27
|
+
export function executionImageDigest(describeJson: string): string | null {
|
|
28
|
+
let parsed: unknown;
|
|
29
|
+
try {
|
|
30
|
+
parsed = JSON.parse(describeJson);
|
|
31
|
+
} catch {
|
|
32
|
+
return null;
|
|
33
|
+
}
|
|
34
|
+
// gcloud prints the v1 resource (spec.template.spec.containers); the v2
|
|
35
|
+
// shape (template.containers) is accepted too. The first container is it.
|
|
36
|
+
const p = parsed as {
|
|
37
|
+
spec?: { template?: { spec?: { containers?: { image?: string }[] } } };
|
|
38
|
+
template?: { containers?: { image?: string }[] };
|
|
39
|
+
};
|
|
40
|
+
const image =
|
|
41
|
+
p.spec?.template?.spec?.containers?.[0]?.image ?? p.template?.containers?.[0]?.image ?? "";
|
|
42
|
+
return DIGEST.exec(image)?.[1] ?? null;
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
/**
|
|
46
|
+
* Why `CLOUD_RUN_EXECUTION` cannot name a run of `job`, or null when it
|
|
47
|
+
* can. The name keys the state lock's holder and the hand-over between the
|
|
48
|
+
* job's two stages, so a value that names another job's run (or is not a
|
|
49
|
+
* Cloud Run name at all) stops the job before it acts (#883 review). Cloud
|
|
50
|
+
* Run sets it and rejects user values for it; this is the second line.
|
|
51
|
+
*/
|
|
52
|
+
export function jobExecutionNameError(job: string, execution: string | null): string | null {
|
|
53
|
+
if (execution === null) return null;
|
|
54
|
+
if (!/^[a-z0-9-]{1,63}$/.test(execution) || !execution.startsWith(`${job}-`)) {
|
|
55
|
+
return `CLOUD_RUN_EXECUTION is "${execution.slice(0, 80)}", not a run of ${job}`;
|
|
56
|
+
}
|
|
57
|
+
return null;
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
/** The new execution's name from `gcloud run jobs execute --async`. */
|
|
61
|
+
export function startedExecutionName(stdout: string, stderr: string): string | null {
|
|
62
|
+
const name = stdout.trim().split(/\s+/)[0] || /Execution \[([^\]]+)\]/.exec(stderr)?.[1] || "";
|
|
63
|
+
return /^[a-z0-9-]+$/.test(name) ? name : null;
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
/**
|
|
67
|
+
* The real {@link SelfHandoverDeps} of update job `job`, except its version
|
|
68
|
+
* (the caller's image knows it).
|
|
69
|
+
*/
|
|
70
|
+
export function selfHandoverDeps(
|
|
71
|
+
run: GcloudRunner,
|
|
72
|
+
target: {
|
|
73
|
+
readonly job: string;
|
|
74
|
+
readonly projectId: string;
|
|
75
|
+
readonly region: string;
|
|
76
|
+
readonly execution: string | null;
|
|
77
|
+
},
|
|
78
|
+
): Omit<SelfHandoverDeps, "ownVersion"> {
|
|
79
|
+
const { job, projectId, region, execution } = target;
|
|
80
|
+
const location = [`--region=${region}`, `--project=${projectId}`];
|
|
81
|
+
return {
|
|
82
|
+
execution,
|
|
83
|
+
verifyImage: (slug, digest) => verifyCosignAll([{ slug, digest }]),
|
|
84
|
+
ownImageDigest: async () => {
|
|
85
|
+
if (!execution) return null;
|
|
86
|
+
const r = await run([
|
|
87
|
+
"run",
|
|
88
|
+
"jobs",
|
|
89
|
+
"executions",
|
|
90
|
+
"describe",
|
|
91
|
+
execution,
|
|
92
|
+
...location,
|
|
93
|
+
"--format=json",
|
|
94
|
+
]);
|
|
95
|
+
return r.ok ? executionImageDigest(r.stdout) : null;
|
|
96
|
+
},
|
|
97
|
+
pointJobAt: async (digest) => {
|
|
98
|
+
// Only the image changes: the identity, command and args stay what
|
|
99
|
+
// the job's setup deployed.
|
|
100
|
+
const update = await run([
|
|
101
|
+
"run",
|
|
102
|
+
"jobs",
|
|
103
|
+
"update",
|
|
104
|
+
job,
|
|
105
|
+
`--image=${releaseImageRef("ops", digest)}`,
|
|
106
|
+
...location,
|
|
107
|
+
"--quiet",
|
|
108
|
+
]);
|
|
109
|
+
return update.ok
|
|
110
|
+
? { ok: true }
|
|
111
|
+
: { ok: false, error: `update ${job}: ${update.stderr.trim()}` };
|
|
112
|
+
},
|
|
113
|
+
startJob: async () => {
|
|
114
|
+
const exec = await run([
|
|
115
|
+
"run",
|
|
116
|
+
"jobs",
|
|
117
|
+
"execute",
|
|
118
|
+
job,
|
|
119
|
+
...location,
|
|
120
|
+
"--async",
|
|
121
|
+
"--format=value(metadata.name)",
|
|
122
|
+
]);
|
|
123
|
+
if (!exec.ok) return { ok: false, error: `start ${job}: ${exec.stderr.trim()}` };
|
|
124
|
+
const started = startedExecutionName(exec.stdout, exec.stderr);
|
|
125
|
+
if (!started) {
|
|
126
|
+
return {
|
|
127
|
+
ok: false,
|
|
128
|
+
error: `${job} started but gcloud did not name the execution (${exec.stderr.trim()})`,
|
|
129
|
+
};
|
|
130
|
+
}
|
|
131
|
+
return { ok: true, execution: started };
|
|
132
|
+
},
|
|
133
|
+
executionState: (name) => describeExecution(run, { projectId, region, execution: name }),
|
|
134
|
+
};
|
|
135
|
+
}
|
|
@@ -0,0 +1,259 @@
|
|
|
1
|
+
// SPDX-License-Identifier: MPL-2.0
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* How an update job hands its request to the ops image of the release it
|
|
5
|
+
* installs (#819 for the converge job, #1029 for the upgrader). Both jobs
|
|
6
|
+
* have the same two stages, told apart by comparing the release their code
|
|
7
|
+
* belongs to with the request's:
|
|
8
|
+
*
|
|
9
|
+
* Stage 1 — the job runs the INSTALLED release's image. After its own
|
|
10
|
+
* gate it verifies the target's ops image, points itself at it BY
|
|
11
|
+
* DIGEST (only the image changes: identity, command and args stay) and
|
|
12
|
+
* starts a new execution, which takes the request over.
|
|
13
|
+
* Stage 2 — the job runs the TARGET's image and does the work.
|
|
14
|
+
*
|
|
15
|
+
* The point of the split: a fix to the logic that decides and installs
|
|
16
|
+
* reaches every install with the release that carries it. Without stage 1
|
|
17
|
+
* that logic always comes from the release being replaced.
|
|
18
|
+
*
|
|
19
|
+
* This file holds that sequence once, for both jobs, because its order is
|
|
20
|
+
* what keeps it safe: the signature before the image moves, the image moved
|
|
21
|
+
* under the state lock, the request released before the next run exists.
|
|
22
|
+
*/
|
|
23
|
+
|
|
24
|
+
import type { ExecutionState } from "./install-job.js";
|
|
25
|
+
import { compareSemver, type ReleaseManifest } from "./release-manifest.js";
|
|
26
|
+
import type { ClaimedUpdate, UpdateEndStatus, UpgraderRunResult } from "./upgrader-job.js";
|
|
27
|
+
import type { EventReporter, JobLog } from "./upgrader-reporter.js";
|
|
28
|
+
|
|
29
|
+
/** What a job's claim found. */
|
|
30
|
+
export interface JobClaim {
|
|
31
|
+
readonly request: ClaimedUpdate;
|
|
32
|
+
/**
|
|
33
|
+
* The request was already running: stage 1 handed it to this run, or a
|
|
34
|
+
* run that died left it. Fresh claims (pending → running) are false.
|
|
35
|
+
*/
|
|
36
|
+
readonly resume: boolean;
|
|
37
|
+
/** The execution the request names now (after a fresh claim: this one). */
|
|
38
|
+
readonly execution: string | null;
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
/** The part of a job's queue that moves a running request between runs. */
|
|
42
|
+
export interface HandoverQueue {
|
|
43
|
+
/**
|
|
44
|
+
* Point running request `requestId` at execution `to` — or at none
|
|
45
|
+
* (`null`: released for the next run to adopt) — only while it still
|
|
46
|
+
* names `from`. Resolves false when another run got there first.
|
|
47
|
+
*/
|
|
48
|
+
handover(requestId: string, from: string | null, to: string | null): Promise<boolean>;
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
/** What a job does to itself and asks about itself to hand over. */
|
|
52
|
+
export interface SelfHandoverDeps {
|
|
53
|
+
/** This Cloud Run execution; null outside Cloud Run. */
|
|
54
|
+
readonly execution: string | null;
|
|
55
|
+
/** The Caelo release this code belongs to (CAELO_VERSION of the image). */
|
|
56
|
+
readonly ownVersion: string;
|
|
57
|
+
/** Verify the cosign signature of the release image `slug@digest`. */
|
|
58
|
+
readonly verifyImage: (slug: "ops", digest: string) => Promise<boolean>;
|
|
59
|
+
/** The digest of the image this execution runs (from Cloud Run); null when unreadable. */
|
|
60
|
+
readonly ownImageDigest: () => Promise<string | null>;
|
|
61
|
+
/** Point this job at the ops image with `digest` (its identity, command and args stay). */
|
|
62
|
+
readonly pointJobAt: (
|
|
63
|
+
digest: string,
|
|
64
|
+
) => Promise<{ readonly ok: true } | { readonly ok: false; readonly error: string }>;
|
|
65
|
+
/** Start a new execution of this job; returns its name. */
|
|
66
|
+
readonly startJob: () => Promise<
|
|
67
|
+
| { readonly ok: true; readonly execution: string }
|
|
68
|
+
| { readonly ok: false; readonly error: string }
|
|
69
|
+
>;
|
|
70
|
+
/** Where an execution of this job stands (Cloud Run). */
|
|
71
|
+
readonly executionState: (execution: string) => Promise<ExecutionState>;
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
/** How a request ends because the run cannot go on. */
|
|
75
|
+
export interface HandoverBlocked {
|
|
76
|
+
readonly status: UpdateEndStatus;
|
|
77
|
+
readonly codes: readonly string[];
|
|
78
|
+
readonly reason: string;
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
/** Whether this run is the one to work on the request. */
|
|
82
|
+
export type Adoption =
|
|
83
|
+
| {
|
|
84
|
+
readonly kind: "work";
|
|
85
|
+
/** This run took a request another run started, or one no run names. */
|
|
86
|
+
readonly adopted: boolean;
|
|
87
|
+
}
|
|
88
|
+
/**
|
|
89
|
+
* This run ends without touching the request. `failed`: it could not tell
|
|
90
|
+
* whether another run has it, so the job's run reports an error.
|
|
91
|
+
*/
|
|
92
|
+
| { readonly kind: "leave"; readonly message: string; readonly failed?: boolean };
|
|
93
|
+
|
|
94
|
+
/**
|
|
95
|
+
* Decide whether this run may work on `claim`, adopting the request when
|
|
96
|
+
* another run started it: only once that run has ended, because two runs
|
|
97
|
+
* at once must never happen. An error here never ends the request: nothing
|
|
98
|
+
* yet says this run owns it, and the run that does may be alive.
|
|
99
|
+
*/
|
|
100
|
+
export async function adoptClaim(
|
|
101
|
+
claim: JobClaim,
|
|
102
|
+
deps: Pick<SelfHandoverDeps, "execution" | "executionState"> & {
|
|
103
|
+
readonly queue: HandoverQueue;
|
|
104
|
+
},
|
|
105
|
+
): Promise<Adoption> {
|
|
106
|
+
if (!claim.resume || (deps.execution !== null && claim.execution === deps.execution)) {
|
|
107
|
+
return { kind: "work", adopted: false };
|
|
108
|
+
}
|
|
109
|
+
if (!deps.execution) {
|
|
110
|
+
return {
|
|
111
|
+
kind: "leave",
|
|
112
|
+
message: "an update request is running; only a Cloud Run execution may adopt it",
|
|
113
|
+
};
|
|
114
|
+
}
|
|
115
|
+
try {
|
|
116
|
+
if (claim.execution) {
|
|
117
|
+
const live = await deps.executionState(claim.execution);
|
|
118
|
+
if (live === "running") {
|
|
119
|
+
return {
|
|
120
|
+
kind: "leave",
|
|
121
|
+
message: `execution ${claim.execution} is still installing; this run does nothing`,
|
|
122
|
+
};
|
|
123
|
+
}
|
|
124
|
+
}
|
|
125
|
+
if (!(await deps.queue.handover(claim.request.id, claim.execution, deps.execution))) {
|
|
126
|
+
return {
|
|
127
|
+
kind: "leave",
|
|
128
|
+
message: "another run adopted the request first; this run does nothing",
|
|
129
|
+
};
|
|
130
|
+
}
|
|
131
|
+
} catch (e) {
|
|
132
|
+
return {
|
|
133
|
+
kind: "leave",
|
|
134
|
+
failed: true,
|
|
135
|
+
message: `could not tell whether run ${claim.execution ?? "(none)"} still has the request, so this run leaves it alone: ${e instanceof Error ? e.message : String(e)}`,
|
|
136
|
+
};
|
|
137
|
+
}
|
|
138
|
+
return { kind: "work", adopted: true };
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
/** Whether this code belongs to another release than the request's: stage 1. */
|
|
142
|
+
export function isStageOne(ownVersion: string, targetVersion: string): boolean {
|
|
143
|
+
return compareSemver(ownVersion, targetVersion) !== 0;
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
/**
|
|
147
|
+
* Every digest the ops image the manifest signs is known by. Cloud Run
|
|
148
|
+
* records the linux/amd64 platform manifest it resolved, not the index the
|
|
149
|
+
* hand-over pinned (#1025), so an execution of the right image reads back
|
|
150
|
+
* as the platform form the manifest also signs.
|
|
151
|
+
*/
|
|
152
|
+
export function signedOpsDigests(m: ReleaseManifest): readonly string[] {
|
|
153
|
+
return [m.digests.ops, ...(m.platformDigests ? [m.platformDigests.ops] : [])];
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
/**
|
|
157
|
+
* Stage 2's check that the image this run executes is the one the manifest
|
|
158
|
+
* signs. Null when it is; how the request ends otherwise.
|
|
159
|
+
*
|
|
160
|
+
* @param own the digest Cloud Run names for this execution, null when unreadable
|
|
161
|
+
* @param noun what the timeline calls this job's image
|
|
162
|
+
*/
|
|
163
|
+
export function ownImageProblem(
|
|
164
|
+
m: ReleaseManifest,
|
|
165
|
+
own: string | null,
|
|
166
|
+
noun: string,
|
|
167
|
+
): HandoverBlocked | null {
|
|
168
|
+
const signed = signedOpsDigests(m);
|
|
169
|
+
if (own !== null && signed.includes(own)) return null;
|
|
170
|
+
return {
|
|
171
|
+
status: "refused",
|
|
172
|
+
codes: ["ops-image"],
|
|
173
|
+
reason: `Nothing was changed: this run's ${noun} image (${own ?? "unknown"}) is not the one Caelo's release workflow signed for ${m.version} (${signed.join(" or ")}). Start the update again from Settings → Updates.`,
|
|
174
|
+
};
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
/** What {@link handOverToRelease} works with: the job's closures over its run. */
|
|
178
|
+
export interface HandoverContext {
|
|
179
|
+
readonly deps: SelfHandoverDeps;
|
|
180
|
+
readonly queue: HandoverQueue;
|
|
181
|
+
readonly requestId: string;
|
|
182
|
+
/** What the timeline calls this job's image: `installer`, `upgrader`. */
|
|
183
|
+
readonly noun: string;
|
|
184
|
+
readonly reporter: EventReporter;
|
|
185
|
+
readonly log: JobLog;
|
|
186
|
+
/** Take the install's state lock; how the request ends when it cannot be had. */
|
|
187
|
+
readonly lock: () => HandoverBlocked | null;
|
|
188
|
+
readonly unlock: () => void;
|
|
189
|
+
readonly stop: (blocked: HandoverBlocked) => Promise<UpgraderRunResult>;
|
|
190
|
+
readonly leave: (message: string) => UpgraderRunResult;
|
|
191
|
+
}
|
|
192
|
+
|
|
193
|
+
/**
|
|
194
|
+
* Stage 1: hand the request to the target release's own signed ops image.
|
|
195
|
+
* `m` is a manifest whose signature verified and that the job's gate let
|
|
196
|
+
* through; nothing else about the install changes here.
|
|
197
|
+
*/
|
|
198
|
+
export async function handOverToRelease(
|
|
199
|
+
m: ReleaseManifest,
|
|
200
|
+
c: HandoverContext,
|
|
201
|
+
): Promise<UpgraderRunResult> {
|
|
202
|
+
const { deps, noun, reporter: r } = c;
|
|
203
|
+
if (!(await deps.verifyImage("ops", m.digests.ops))) {
|
|
204
|
+
return c.stop({
|
|
205
|
+
status: "refused",
|
|
206
|
+
codes: ["signature"],
|
|
207
|
+
reason: `The ${noun} image of ${m.version} is not signed by Caelo's release workflow, so nothing was changed.`,
|
|
208
|
+
});
|
|
209
|
+
}
|
|
210
|
+
// The target's image naming another version would hand over to itself
|
|
211
|
+
// for ever: each run would start the next. Only a broken build gets here.
|
|
212
|
+
const own = await deps.ownImageDigest();
|
|
213
|
+
if (own !== null && signedOpsDigests(m).includes(own)) {
|
|
214
|
+
return c.stop({
|
|
215
|
+
status: "refused",
|
|
216
|
+
codes: ["ops-image"],
|
|
217
|
+
reason: `Nothing was changed: this run already is the ${noun} image Caelo's release workflow signed for ${m.version}, but it reports version ${deps.ownVersion}. That release's image is broken; a newer release installs normally.`,
|
|
218
|
+
});
|
|
219
|
+
}
|
|
220
|
+
// Under the lock (the terminal `upgrade` redeploys this job too), and
|
|
221
|
+
// released before the new run starts: it takes the lock itself.
|
|
222
|
+
const locked = c.lock();
|
|
223
|
+
if (locked) return c.stop(locked);
|
|
224
|
+
let pointed: Awaited<ReturnType<SelfHandoverDeps["pointJobAt"]>>;
|
|
225
|
+
try {
|
|
226
|
+
pointed = await deps.pointJobAt(m.digests.ops);
|
|
227
|
+
} finally {
|
|
228
|
+
c.unlock();
|
|
229
|
+
}
|
|
230
|
+
if (!pointed.ok) {
|
|
231
|
+
return c.stop({
|
|
232
|
+
status: "failed",
|
|
233
|
+
codes: ["handover"],
|
|
234
|
+
reason: `Nothing was changed: the ${noun} of ${m.version} could not be prepared (${pointed.error}). Try again in a moment.`,
|
|
235
|
+
});
|
|
236
|
+
}
|
|
237
|
+
// Release the request BEFORE the new run exists: it adopts a request
|
|
238
|
+
// that names no run at once, while a run still named (this one, alive
|
|
239
|
+
// for a moment longer) would make it step back.
|
|
240
|
+
if (!(await c.queue.handover(c.requestId, deps.execution, null))) {
|
|
241
|
+
return c.leave("the request moved on before the hand-over; this run does nothing");
|
|
242
|
+
}
|
|
243
|
+
const started = await deps.startJob();
|
|
244
|
+
if (!started.ok) {
|
|
245
|
+
return c.stop({
|
|
246
|
+
status: "failed",
|
|
247
|
+
codes: ["handover"],
|
|
248
|
+
reason: `Nothing on the site was changed: the ${noun} of ${m.version} is ready but could not be started (${started.error}). Try again in a moment.`,
|
|
249
|
+
});
|
|
250
|
+
}
|
|
251
|
+
r.info(`The ${noun} of ${m.version} runs now (run ${started.execution}).`);
|
|
252
|
+
await r.flush();
|
|
253
|
+
c.log({
|
|
254
|
+
severity: "INFO",
|
|
255
|
+
message: `handed over to ${started.execution}`,
|
|
256
|
+
requestId: c.requestId,
|
|
257
|
+
});
|
|
258
|
+
return { exitCode: 0, requestId: c.requestId, status: null };
|
|
259
|
+
}
|