blogwright 0.4.0 → 0.5.0
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/README.md +4 -1
- package/agent/agent-manifest.json +1 -1
- package/agent/server.js +2610 -616
- package/dist/adapters/process-aws-login.d.ts +19 -0
- package/dist/adapters/process-aws-login.js +28 -0
- package/dist/agent-package.js +2 -3
- package/dist/artifacts.d.ts +73 -0
- package/dist/artifacts.js +218 -0
- package/dist/aws-session.d.ts +118 -0
- package/dist/aws-session.js +360 -0
- package/dist/bin.js +6 -2
- package/dist/cache-control.d.ts +66 -0
- package/dist/cache-control.js +176 -0
- package/dist/cli.d.ts +22 -3
- package/dist/cli.js +122 -31
- package/dist/commands.d.ts +41 -9
- package/dist/commands.js +135 -126
- package/dist/context.d.ts +27 -5
- package/dist/context.js +59 -19
- package/dist/deploy.d.ts +78 -6
- package/dist/deploy.js +136 -52
- package/dist/init.d.ts +14 -2
- package/dist/init.js +145 -4
- package/dist/known-commands.d.ts +11 -9
- package/dist/known-commands.js +15 -9
- package/dist/live-release.d.ts +78 -0
- package/dist/live-release.js +159 -0
- package/dist/lock.d.ts +50 -0
- package/dist/lock.js +120 -0
- package/dist/nodes.d.ts +18 -0
- package/dist/nodes.js +405 -30
- package/dist/plugin-commands.d.ts +21 -0
- package/dist/plugin-commands.js +54 -3
- package/dist/plugins.d.ts +11 -9
- package/dist/plugins.js +29 -16
- package/dist/ports.d.ts +22 -1
- package/dist/releases.d.ts +54 -0
- package/dist/releases.js +527 -0
- package/dist/render.d.ts +18 -1
- package/dist/render.js +38 -6
- package/dist/repo.js +2 -3
- package/dist/rkey.d.ts +4 -3
- package/dist/rkey.js +4 -3
- package/dist/serving.d.ts +28 -0
- package/dist/serving.js +47 -0
- package/dist/verify.d.ts +45 -0
- package/dist/verify.js +183 -0
- package/dist/whoami.d.ts +23 -0
- package/dist/whoami.js +88 -0
- package/package.json +8 -8
package/dist/lock.js
ADDED
|
@@ -0,0 +1,120 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The environment lock (`build/lock.json`): one command at a time may write
|
|
3
|
+
* an environment's live `site/` or its live-release record. `deploy`,
|
|
4
|
+
* `rollback`, `stage`, `promote` and `delete` take it before they change
|
|
5
|
+
* anything and release it when they finish, failed or not, so two of them can
|
|
6
|
+
* never interleave their writes - a deploy's builder listing `site/` while a
|
|
7
|
+
* promotion copies into it, or two deploys racing for the same objects.
|
|
8
|
+
*
|
|
9
|
+
* The lock is a conditional PUT (`If-None-Match: *`), so exactly one caller
|
|
10
|
+
* creates it. A command that dies without releasing it - a killed CI job, a
|
|
11
|
+
* closed laptop - leaves it behind; the next command refuses, naming who held
|
|
12
|
+
* it, for what and since when, and `--force-unlock` replaces it once that
|
|
13
|
+
* holder is known to be gone. Nothing expires it on a timer: a long build is
|
|
14
|
+
* indistinguishable from a dead one, and guessing wrong would re-open the race.
|
|
15
|
+
*/
|
|
16
|
+
import { randomUUID } from 'node:crypto';
|
|
17
|
+
/** Where an environment's lock lives - outside every builder grant. */
|
|
18
|
+
export const ENV_LOCK_KEY = 'build/lock.json';
|
|
19
|
+
function isEnvLock(value) {
|
|
20
|
+
if (typeof value !== 'object' || value === null)
|
|
21
|
+
return false;
|
|
22
|
+
const record = value;
|
|
23
|
+
return ['id', 'holder', 'command', 'startedAt'].every((field) => typeof record[field] === 'string');
|
|
24
|
+
}
|
|
25
|
+
/** The lock currently held, `unreadable` for an object that is not one, or undefined. */
|
|
26
|
+
async function readEnvLock(ctx) {
|
|
27
|
+
const text = await ctx.clients.s3.getObjectText(ctx.names.bucket, ENV_LOCK_KEY);
|
|
28
|
+
if (text === undefined)
|
|
29
|
+
return undefined;
|
|
30
|
+
try {
|
|
31
|
+
const parsed = JSON.parse(text);
|
|
32
|
+
return isEnvLock(parsed) ? parsed : 'unreadable';
|
|
33
|
+
}
|
|
34
|
+
catch {
|
|
35
|
+
return 'unreadable';
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
/** One line naming a lock's holder, command, revision and age. */
|
|
39
|
+
function describeEnvLock(lock, now = Date.now()) {
|
|
40
|
+
const minutes = Math.max(0, Math.round((now - Date.parse(lock.startedAt)) / 60_000));
|
|
41
|
+
const what = lock.hash ? `${lock.command} ${lock.hash}` : lock.command;
|
|
42
|
+
return `${what} by ${lock.holder}, started ${lock.startedAt} (${minutes} min ago)`;
|
|
43
|
+
}
|
|
44
|
+
function lockedError(ctx, command, held) {
|
|
45
|
+
const location = `s3://${ctx.names.bucket}/${ENV_LOCK_KEY}`;
|
|
46
|
+
const holder = held === 'unreadable'
|
|
47
|
+
? `an unreadable lock at ${location}`
|
|
48
|
+
: `${describeEnvLock(held)} (${location})`;
|
|
49
|
+
return new Error(`"${ctx.env}" is locked by ${holder}, so ${command} would interleave its writes with that ` +
|
|
50
|
+
'command - nothing was changed. Wait for it to finish; if it is no longer running ' +
|
|
51
|
+
`(a cancelled job, a killed process), re-run with \`--force-unlock\` to replace the lock.`);
|
|
52
|
+
}
|
|
53
|
+
/**
|
|
54
|
+
* Take the environment's lock for `command`, or throw naming whoever holds
|
|
55
|
+
* it. With `forceUnlock`, a held lock is first removed, with a warning.
|
|
56
|
+
*/
|
|
57
|
+
export async function acquireEnvLock(ctx, command, hash, opts = {}) {
|
|
58
|
+
if (opts.forceUnlock) {
|
|
59
|
+
const held = await readEnvLock(ctx);
|
|
60
|
+
if (held !== undefined) {
|
|
61
|
+
ctx.logger.warn(`--force-unlock: replacing the lock held by ` +
|
|
62
|
+
`${held === 'unreadable' ? 'an unreadable lock' : describeEnvLock(held)}`);
|
|
63
|
+
await ctx.clients.s3.deleteObject(ctx.names.bucket, ENV_LOCK_KEY);
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
const lock = {
|
|
67
|
+
id: randomUUID(),
|
|
68
|
+
holder: ctx.identity.arn,
|
|
69
|
+
command,
|
|
70
|
+
...(hash ? { hash } : {}),
|
|
71
|
+
startedAt: new Date().toISOString(),
|
|
72
|
+
};
|
|
73
|
+
const created = await ctx.clients.s3.putObjectIfAbsent(ctx.names.bucket, ENV_LOCK_KEY, JSON.stringify(lock, null, 2), 'application/json');
|
|
74
|
+
if (created)
|
|
75
|
+
return lock;
|
|
76
|
+
// Released between the PUT and this read: report it as held anyway, since
|
|
77
|
+
// retrying silently could starve, and the operator can simply run again.
|
|
78
|
+
throw lockedError(ctx, command, (await readEnvLock(ctx)) ?? 'unreadable');
|
|
79
|
+
}
|
|
80
|
+
/**
|
|
81
|
+
* Release `lock` if it is still the one held. A lock replaced by
|
|
82
|
+
* `--force-unlock` belongs to someone else now and is left alone. Never
|
|
83
|
+
* throws: the command's own outcome must not be masked by its cleanup.
|
|
84
|
+
*/
|
|
85
|
+
export async function releaseEnvLock(ctx, lock) {
|
|
86
|
+
try {
|
|
87
|
+
const held = await readEnvLock(ctx);
|
|
88
|
+
if (held === undefined)
|
|
89
|
+
return;
|
|
90
|
+
if (held === 'unreadable' || held.id !== lock.id) {
|
|
91
|
+
ctx.logger.warn(`the environment lock was replaced while ${lock.command} ran (--force-unlock?) - ` +
|
|
92
|
+
'leaving the new one in place');
|
|
93
|
+
return;
|
|
94
|
+
}
|
|
95
|
+
await ctx.clients.s3.deleteObject(ctx.names.bucket, ENV_LOCK_KEY);
|
|
96
|
+
}
|
|
97
|
+
catch (err) {
|
|
98
|
+
ctx.logger.warn(`could not release the environment lock s3://${ctx.names.bucket}/${ENV_LOCK_KEY} ` +
|
|
99
|
+
`(${err instanceof Error ? err.message : String(err)}); the next command will refuse ` +
|
|
100
|
+
'until it is removed with `--force-unlock`');
|
|
101
|
+
}
|
|
102
|
+
}
|
|
103
|
+
/** Run `fn` holding the environment's lock, releasing it however `fn` ends. */
|
|
104
|
+
export async function withEnvLock(ctx, command, hash, opts, fn) {
|
|
105
|
+
const lock = await acquireEnvLock(ctx, command, hash, opts);
|
|
106
|
+
try {
|
|
107
|
+
return await fn();
|
|
108
|
+
}
|
|
109
|
+
finally {
|
|
110
|
+
await releaseEnvLock(ctx, lock);
|
|
111
|
+
}
|
|
112
|
+
}
|
|
113
|
+
/** Refuse when the environment is locked - for `destroy`, which removes the lock with the bucket. */
|
|
114
|
+
export async function assertEnvUnlocked(ctx, command, opts = {}) {
|
|
115
|
+
if (opts.forceUnlock)
|
|
116
|
+
return;
|
|
117
|
+
const held = await readEnvLock(ctx);
|
|
118
|
+
if (held !== undefined)
|
|
119
|
+
throw lockedError(ctx, command, held);
|
|
120
|
+
}
|
package/dist/nodes.d.ts
CHANGED
|
@@ -1,6 +1,15 @@
|
|
|
1
1
|
import { type ResourceNode as CoreResourceNode } from 'blogwright-core';
|
|
2
2
|
import type { OpsContext } from './context.js';
|
|
3
3
|
type ResourceNode = CoreResourceNode<OpsContext>;
|
|
4
|
+
/**
|
|
5
|
+
* Recorded on the exec role's outputs whenever its policy is applied - true
|
|
6
|
+
* when that policy grants release staging - so `stage` can refuse up front on
|
|
7
|
+
* a stack bootstrapped before it existed rather than fail inside the MicroVM
|
|
8
|
+
* after a full build. A flag rather than a read of the policy itself: the CI
|
|
9
|
+
* deploy role that runs `stage` has no IAM read access, and granting it
|
|
10
|
+
* `iam:GetRolePolicy` to check one statement would widen it for little gain.
|
|
11
|
+
*/
|
|
12
|
+
export declare const RELEASE_STAGING_OUTPUT = "releaseStaging";
|
|
4
13
|
export type BuilderImageAction = 'create' | 'update' | 'skip';
|
|
5
14
|
/**
|
|
6
15
|
* Decide what a builder-image reconcile should do: create when the image is missing or
|
|
@@ -30,6 +39,15 @@ export declare const PLACEHOLDER_ARTIFACT_KEY = "web/artifacts/placeholder.zip";
|
|
|
30
39
|
* Zipped with a fixed timestamp so identical bytes never churn.
|
|
31
40
|
*/
|
|
32
41
|
export declare function placeholderPackageZip(): Uint8Array;
|
|
42
|
+
/**
|
|
43
|
+
* The deploy-time half of the `headers` reconcile: converge the recorded
|
|
44
|
+
* policy's headers in place. Attaching, detaching, creating and deleting the
|
|
45
|
+
* policy change the distribution, which stays a `bootstrap` action (the CI
|
|
46
|
+
* deploy role cannot update the distribution), so those cases warn with the
|
|
47
|
+
* command that applies them instead. Whether the policy is attached comes from
|
|
48
|
+
* the record bootstrap keeps, so the deploy role needs no distribution read.
|
|
49
|
+
*/
|
|
50
|
+
export declare function reconcileResponseHeadersOnDeploy(ctx: OpsContext): Promise<void>;
|
|
33
51
|
/**
|
|
34
52
|
* The workflow's OIDC subject claim, scoped per environment to match how each one
|
|
35
53
|
* deploys: previews from any PR ref; staging from pushes to main; production from the
|