Events 3.4.0, Accounts 1.4.0, contracts 1.12.0, agent 1.3.0: jobs from the dashboard, styled Accounts emails, .env from the Hub
- Events and Accounts: Background jobs are switched on or off and rescheduled from the admin; the choice is kept in the database (job_overrides), wins over the .env and applies at once. Accounts gets an Admin -> Background jobs page and per-job env flags, as Events. - Accounts: every email styled as Cross Events' (church colour, logo, button, footer), same plain text (src/lib/emails.ts). - Contracts 1.12.0: env-read / env-set agent jobs, envReadResultSchema, isSecretEnvName. - crosscode script: `crosscode env <church> <product>` reads the .env (secrets hidden) and, with --set/--unset/--restart, changes one file, rebuilds the frontend when its file changed, reloads, and puts the file back if the site doesn't answer (lib/envedit.cjs, tests/envedit.bats). - Agent 1.3.0: runs env-read and env-set. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -62,6 +62,25 @@ export type DemoProfile = z.infer<typeof demoProfileSchema>;
|
||||
/** The first agent that runs `seed-demo` jobs (an older one refuses the unknown type). */
|
||||
export const SEED_DEMO_MIN_AGENT = "1.2.0";
|
||||
|
||||
/** The first agent that runs `env-read` and `env-set` jobs (an older one refuses the unknown types). */
|
||||
export const ENV_JOBS_MIN_AGENT = "1.3.0";
|
||||
|
||||
/** A .env variable's name. */
|
||||
export const envNameSchema = z.string().max(100).regex(/^[A-Z][A-Z0-9_]*$/, "Expected an env variable name (A-Z, 0-9, _)");
|
||||
|
||||
/** A product's .env file, relative to `apps/<product>`: `backend/.env`, `frontend/.env.production`. */
|
||||
export const envFileSchema = z.string().max(100).regex(/^(backend|frontend)\/\.env(\.production)?$/, "Expected backend/.env or frontend/.env.production");
|
||||
|
||||
/**
|
||||
* Names whose values are secrets: never shown on the Hub (only whether they're set) and never
|
||||
* changed from it. Keep in step with SECRET_NAME in scripts/lib/envedit.cjs.
|
||||
*/
|
||||
export const SECRET_ENV_NAME = /(SECRET|PASS|TOKEN|PRIVATE|CREDENTIAL|_KEY$|_DSN$|DATABASE_URL|ADMIN_URL)/;
|
||||
export const isSecretEnvName = (name: string): boolean => SECRET_ENV_NAME.test(name);
|
||||
|
||||
/** At most this many changes in one `env-set` job. */
|
||||
export const ENV_SET_MAX = 50;
|
||||
|
||||
/** Each job type and the arguments it takes. Unknown keys are refused, not stripped. */
|
||||
export const jobArgsSchemas = {
|
||||
/** `crosscode install <church> --products … --frp --json` */
|
||||
@@ -95,6 +114,35 @@ export const jobArgsSchemas = {
|
||||
* marked as demo sites. `adminEmail`: the account kept (default: the release's own choice).
|
||||
*/
|
||||
"seed-demo": z.strictObject({ church: shortNameSchema, profile: demoProfileSchema, adminEmail: z.email().max(254).optional() }),
|
||||
/**
|
||||
* `crosscode env <church> <product> --json`: the product's .env values, secrets hidden (only
|
||||
* whether they're set). The report's `result` is an {@link envReadResultSchema}.
|
||||
*/
|
||||
"env-read": z.strictObject({ church: shortNameSchema, product: productSchema }),
|
||||
/**
|
||||
* `crosscode env <church> <product> --file <file> --set NAME=value … --unset NAME … [--restart]
|
||||
* --json`: changes values in one .env file (a copy is kept first), rebuilds the frontend when its
|
||||
* file changed (Next.js builds the values in), then reloads; on a failure the file is put back.
|
||||
* Never secrets, never what the server generates (DATABASE_URL, JWT_SECRET …): the script
|
||||
* refuses those too.
|
||||
*/
|
||||
"env-set": z.strictObject({
|
||||
church: shortNameSchema,
|
||||
product: productSchema,
|
||||
file: envFileSchema,
|
||||
set: z.record(envNameSchema, z.string().max(4000).regex(/^[^\r\n]*$/, "Values can't contain newlines")),
|
||||
unset: z.array(envNameSchema).max(ENV_SET_MAX),
|
||||
restart: z.boolean(),
|
||||
}).check((ctx) => {
|
||||
const a = ctx.value;
|
||||
const names = [...Object.keys(a.set), ...a.unset];
|
||||
const issue = (message: string) => ctx.issues.push({ code: "custom", message, input: a });
|
||||
if (names.length === 0) issue("Nothing to change");
|
||||
if (names.length > ENV_SET_MAX) issue(`At most ${ENV_SET_MAX} changes at a time`);
|
||||
const secret = names.find(isSecretEnvName);
|
||||
if (secret) issue(`${secret} is a secret: it can't be changed from the Hub`);
|
||||
if (a.unset.some((n) => Object.prototype.hasOwnProperty.call(a.set, n))) issue("A name can't be both set and removed");
|
||||
}),
|
||||
/** `crosscode export <church> --out <zip> --json`; the agent encrypts the ZIP and uploads it under `exportId`. */
|
||||
export: z.strictObject({ church: shortNameSchema, exportId: z.string().regex(/^exp_[A-Za-z0-9_-]{8,64}$/) }),
|
||||
/** `crosscode uninstall <church> --json` */
|
||||
@@ -119,7 +167,7 @@ export const CENTRAL_JOB_TYPES: readonly JobType[] = ["check-ports", "status", "
|
||||
|
||||
/** Time limits per job type, in minutes (§3). A job past it is stopped and reported as failed. */
|
||||
export const JOB_TIME_LIMIT_MINUTES: Record<JobType, number> = {
|
||||
install: 30, "add-product": 30, deploy: 20, adopt: 30, "adopt-undo": 15, restart: 5, "seed-demo": 15, export: 20,
|
||||
install: 30, "add-product": 30, deploy: 20, adopt: 30, "adopt-undo": 15, restart: 5, "seed-demo": 15, "env-read": 2, "env-set": 20, export: 20,
|
||||
uninstall: 20, purge: 10, "update-agent": 15, "check-ports": 2, status: 3,
|
||||
};
|
||||
|
||||
@@ -164,7 +212,7 @@ function jobOf<T extends JobType>(type: T) {
|
||||
|
||||
export const agentJobSchema = z.discriminatedUnion("type", [
|
||||
jobOf("install"), jobOf("add-product"), jobOf("deploy"), jobOf("adopt"), jobOf("adopt-undo"), jobOf("restart"), jobOf("seed-demo"),
|
||||
jobOf("export"), jobOf("uninstall"), jobOf("purge"), jobOf("update-agent"), jobOf("check-ports"), jobOf("status"),
|
||||
jobOf("env-read"), jobOf("env-set"), jobOf("export"), jobOf("uninstall"), jobOf("purge"), jobOf("update-agent"), jobOf("check-ports"), jobOf("status"),
|
||||
]);
|
||||
export type AgentJob = z.infer<typeof agentJobSchema>;
|
||||
export type UnsignedAgentJob = Omit<AgentJob, "signature">;
|
||||
@@ -321,6 +369,32 @@ export const envTemplateSchema = z.object({
|
||||
});
|
||||
export type EnvTemplate = z.infer<typeof envTemplateSchema>;
|
||||
|
||||
// ---------------------------------------------------------------------------------------------
|
||||
// Reading an install's .env (the env-read job, contracts 1.12.0)
|
||||
// ---------------------------------------------------------------------------------------------
|
||||
|
||||
/** One variable in an `env-read` result. `value` is null for a secret (`set` says whether it has one). */
|
||||
export const envReadVariableSchema = z.object({
|
||||
name: envNameSchema,
|
||||
value: z.string().max(4000).nullable(),
|
||||
secret: z.boolean(),
|
||||
set: z.boolean(),
|
||||
/** From the release's env template (null when the template doesn't list it). */
|
||||
layer: envLayerSchema.nullable(),
|
||||
description: z.string().max(400).nullable(),
|
||||
/** Whether the Hub may change it: not secret, not generated on the server. */
|
||||
editable: z.boolean(),
|
||||
});
|
||||
export type EnvReadVariable = z.infer<typeof envReadVariableSchema>;
|
||||
|
||||
/** The `result` of an `env-read` report: each of the product's .env files, variables in file order. */
|
||||
export const envReadResultSchema = z.object({
|
||||
product: productSchema,
|
||||
files: z.array(z.object({ file: envFileSchema, exists: z.boolean(), variables: z.array(envReadVariableSchema).max(300) })).max(10),
|
||||
readAt: dateTimeSchema,
|
||||
});
|
||||
export type EnvReadResult = z.infer<typeof envReadResultSchema>;
|
||||
|
||||
/** The variables a layer must provide for a template, that it doesn't (the Hub's check before a job). */
|
||||
export function missingRequired(template: EnvTemplate, layer: "server" | "defaults", values: Record<string, string | undefined>): string[] {
|
||||
const missing: string[] = [];
|
||||
|
||||
Reference in New Issue
Block a user