From f7d3ba5ee82222ade4c063b1505a8916cc2d2de0 Mon Sep 17 00:00:00 2001 From: Sandrino Di Mattia <655409+sandrinodimattia@users.noreply.github.com> Date: Wed, 11 Nov 2026 10:50:34 +0100 Subject: [PATCH] feat: show images in SSH status, remove roles per run, reject unused --tag - Managed SSH `status` includes each container's image. Only secret values are redacted now: deploys record the secret variable names (never values) in `.compoza.json`. Stacks deployed by earlier versions keep redacting every dotenv value. Service and image patterns are fully anchored, and longer values are redacted before shorter ones. - `destinations..roles.: null` and `local.roles.: null` remove a role for that run: no build, image, Git check, validation, override entry or allowlisted secrets. Removals are checked when the config loads, including unselected destinations. A role the selected destination removes stays removed for `--local`. - `--tag` fails when no role has a build, and naming a removed role says so. --- .agents/skills/compoza/SKILL.md | 21 +++-- .claude/skills/compoza/SKILL.md | 21 +++-- docs/commands.md | 4 +- docs/configuration.md | 14 ++- docs/secrets.md | 2 +- docs/ssh-deployment.md | 2 +- src/cli.ts | 4 +- src/config/deploy.test.ts | 151 ++++++++++++++++++++++++++++++++ src/config/deploy.ts | 116 +++++++++++++++++++++--- src/config/secrets.test.ts | 54 +++++++++++- src/deploy.ts | 23 ++++- src/plan.ts | 2 +- src/remote/runtime.sh | 5 +- src/remote/status.jq | 15 +++- src/roles-deploy.test.ts | 112 +++++++++++++++++++++++ src/roles.ts | 12 ++- src/ssh-plan.test.ts | 112 +++++++++++++++++++++++ src/ssh-plan.ts | 11 +-- src/tags.test.ts | 35 +++++++- src/tags.ts | 29 +++++- 20 files changed, 683 insertions(+), 62 deletions(-) diff --git a/.agents/skills/compoza/SKILL.md b/.agents/skills/compoza/SKILL.md index 05db7dd..8a3f54f 100644 --- a/.agents/skills/compoza/SKILL.md +++ b/.agents/skills/compoza/SKILL.md @@ -82,7 +82,9 @@ service's container. The top-level `environment` and `secrets` stay Compose interpolation inputs. A name set in a role must not also be in the service's Compose `environment:`, and role secrets need a secret source other than `none`. `check` reports these problems without fetching secrets. The generated override, -`compoza.roles.yaml`, holds `${NAME}` references, never secret values. +`compoza.roles.yaml`, holds `${NAME}` references, never secret values. A destination +or `local.roles` can change a role's `environment` and `secrets`, or remove the role +for that run with `null`; it cannot change `build`. ## Recover with a known image tag @@ -95,11 +97,11 @@ compoza status infra `--tag` skips Git/builds and reuses images with the current local configuration and currently selected secret source. `--tag ` applies to every role with `build`, and `--tag =` overrides it for one role; repeat the flag as needed. Every role -with `build` needs a tag. A missing source checkout is allowed for `--tag`, stacks whose -roles build nothing, status, logs and lifecycle commands. There is no release rollback -command, saved configuration history or backup folder. Check out an earlier app -revision separately when its configuration is required. Never imply that database -changes are reversed. +with `build` needs a tag, and `--tag` fails when no role builds. A missing source +checkout is allowed for `--tag`, stacks whose roles build nothing, status, logs and +lifecycle commands. There is no release rollback command, saved configuration history +or backup folder. Check out an earlier app revision separately when its configuration +is required. Never imply that database changes are reversed. SSH stacks contain `compose.yaml`, private `.env`, declared assets and one nonsecret `.compoza.json` lifecycle metadata file, which records each role's tag. The roles @@ -141,8 +143,11 @@ authorized before publishing. Do not change section paths to work around a failu desktop account may be saved with `compoza config set 1password.account`. Leave changing it, and `hosts link`/`unlink`, to the user unless asked: they affect every later run. Do not fetch a personal vault just to validate implementation changes. -- Do not print resolved Compose config, dotenv contents or provider exceptions. Managed status omits command/image/container-name - fields; requested application logs can still contain secrets and need care. +- Do not print resolved Compose config, dotenv contents or provider exceptions. Managed + status shows only service, image, state, health and exit code, with secret values redacted + (all dotenv values for stacks deployed by an earlier version; `.compoza.json` records + the secret variable names); requested application logs can still contain secrets and + need care. ## Diagnose and report diff --git a/.claude/skills/compoza/SKILL.md b/.claude/skills/compoza/SKILL.md index 05db7dd..8a3f54f 100644 --- a/.claude/skills/compoza/SKILL.md +++ b/.claude/skills/compoza/SKILL.md @@ -82,7 +82,9 @@ service's container. The top-level `environment` and `secrets` stay Compose interpolation inputs. A name set in a role must not also be in the service's Compose `environment:`, and role secrets need a secret source other than `none`. `check` reports these problems without fetching secrets. The generated override, -`compoza.roles.yaml`, holds `${NAME}` references, never secret values. +`compoza.roles.yaml`, holds `${NAME}` references, never secret values. A destination +or `local.roles` can change a role's `environment` and `secrets`, or remove the role +for that run with `null`; it cannot change `build`. ## Recover with a known image tag @@ -95,11 +97,11 @@ compoza status infra `--tag` skips Git/builds and reuses images with the current local configuration and currently selected secret source. `--tag ` applies to every role with `build`, and `--tag =` overrides it for one role; repeat the flag as needed. Every role -with `build` needs a tag. A missing source checkout is allowed for `--tag`, stacks whose -roles build nothing, status, logs and lifecycle commands. There is no release rollback -command, saved configuration history or backup folder. Check out an earlier app -revision separately when its configuration is required. Never imply that database -changes are reversed. +with `build` needs a tag, and `--tag` fails when no role builds. A missing source +checkout is allowed for `--tag`, stacks whose roles build nothing, status, logs and +lifecycle commands. There is no release rollback command, saved configuration history +or backup folder. Check out an earlier app revision separately when its configuration +is required. Never imply that database changes are reversed. SSH stacks contain `compose.yaml`, private `.env`, declared assets and one nonsecret `.compoza.json` lifecycle metadata file, which records each role's tag. The roles @@ -141,8 +143,11 @@ authorized before publishing. Do not change section paths to work around a failu desktop account may be saved with `compoza config set 1password.account`. Leave changing it, and `hosts link`/`unlink`, to the user unless asked: they affect every later run. Do not fetch a personal vault just to validate implementation changes. -- Do not print resolved Compose config, dotenv contents or provider exceptions. Managed status omits command/image/container-name - fields; requested application logs can still contain secrets and need care. +- Do not print resolved Compose config, dotenv contents or provider exceptions. Managed + status shows only service, image, state, health and exit code, with secret values redacted + (all dotenv values for stacks deployed by an earlier version; `.compoza.json` records + the secret variable names); requested application logs can still contain secrets and + need care. ## Diagnose and report diff --git a/docs/commands.md b/docs/commands.md index 8f88150..a7334f5 100644 --- a/docs/commands.md +++ b/docs/commands.md @@ -112,7 +112,7 @@ compoza deploy infra --tag 1a2b3c4d5e6f compoza deploy infra --tag 1a2b3c4d5e6f --tag worker=9f8e7d6c5b4a ``` -Every role with a `build` must end up with a tag. Naming an unknown role, a role without `build` or the same role twice fails, and so does a second bare tag. A tag follows Docker's rules: letters, digits, underscores, dots and dashes, at most 128 characters, not starting with a dot or dash. That rejects `--tag web:abc`, a likely typo for `--tag web=abc`. The **current local configuration** is used; there is no saved configuration history. +Every role with a `build` must end up with a tag. Naming an unknown role, a role without `build` or the same role twice fails, and so does a second bare tag. A tag follows Docker's rules: letters, digits, underscores, dots and dashes, at most 128 characters, not starting with a dot or dash. That rejects `--tag web:abc`, a likely typo for `--tag web=abc`. On a stack where no role has a `build`, any `--tag` fails, because there are no images to reuse. The **current local configuration** is used; there is no saved configuration history. A failed deploy can leave new containers or routes running: there is no automatic rollback. Inspect `status` and `logs`, then deploy a known tag. See [Images and tags](configuration.md#images-and-tags), [Local runs](configuration.md#local-runs) and, for SSH execution, the [deploy transaction](ssh-deployment.md#deploy-transaction). @@ -129,7 +129,7 @@ NAME IMAGE COMMAND SERVICE CREATED hello-web-web-1 nginx:alpine "/docker-entrypoint.…" web 2 seconds ago Up 1 second 127.0.0.1:8080->80/tcp ``` -With `execution: ssh` and a managed secret source, status shows only the service, state, health and exit code, with dotenv values redacted. +With `execution: ssh` and a managed secret source, status prints one JSON object per container with only its service, image, state, health and exit code, with managed secret values redacted (all dotenv values for a stack deployed by an earlier version). An image that does not look like an image reference shows as `(image)`. ## logs diff --git a/docs/configuration.md b/docs/configuration.md index 9ca6056..06abcbb 100644 --- a/docs/configuration.md +++ b/docs/configuration.md @@ -410,7 +410,7 @@ Without `roles` there is no extra file and nothing changes. - The image of a role with `build` is `-:`, prefixed with the registry for registry transport. compoza sets it as the service's `image`. - A build requires a clean checkout with at least one commit in each role's repository. Each repository is checked once, and its roles are tagged with the first twelve characters of its commit. `--allow-dirty` permits a dirty checkout and tags `-dirty`. -- `--tag` reuses images built earlier and skips Git and builds, so it works from a deploy folder outside any checkout. `--tag ` applies to every role with `build`, and `--tag =` overrides it for one role; repeat the flag as needed. Every role with `build` must end up with a tag. `--tag` always uses the **current local configuration**; there is no saved Compose configuration history. +- `--tag` reuses images built earlier and skips Git and builds, so it works from a deploy folder outside any checkout. `--tag ` applies to every role with `build`, and `--tag =` overrides it for one role; repeat the flag as needed. Every role with `build` must end up with a tag. A stack where no role has `build` (after any [role removal](#destinations)) has no images to reuse, so any `--tag` fails. Naming a role that a destination or `local.roles` removed fails and says the role is removed for the run. `--tag` always uses the **current local configuration**; there is no saved Compose configuration history. - Progress messages show the single tag when all roles share it, and `web=abc worker=def` otherwise. - Without any role that has `build` there is no Git, Dockerfile or build-context requirement, and no build or application-registry authentication check. Compose still pulls missing third-party images, and the deploy is tagged `third-party`. @@ -431,7 +431,7 @@ Compose resolves relative bind mounts on the host, so host paths must be absolut ### Local runs -`local` overrides `composeFiles`, `environment` and the complete secret source for `--local` runs. `local.roles` sets `environment` and `secrets` per role and merges over the app's roles the way [destinations](#destinations) do; `build` cannot be changed. When the app declares a production secret source, an explicit `local.secrets` is required, including `none: {}` when local Compose needs no secrets. +`local` overrides `composeFiles`, `environment` and the complete secret source for `--local` runs. `local.roles` sets `environment` and `secrets` per role and merges over the app's roles the way [destinations](#destinations) do; `build` cannot be changed. Setting a role to `null` removes it from local runs (no build, image, environment, secrets or Compose validation), for a stack whose local Compose files leave a service out: `local: { roles: { worker: null } }`. The removal is checked when the config loads (see [Destinations](#destinations)). If the Compose files still declare the service, it runs as they define it, without compoza's image or role settings. When the app declares a production secret source, an explicit `local.secrets` is required, including `none: {}` when local Compose needs no secrets. `--local` builds into this machine's Docker and starts the stack here, without remote readiness checks, transport or routes. It keeps this client build-and-run behavior regardless of `execution`. @@ -465,6 +465,16 @@ destinations: - When supplied, a destination **replaces** `host`, `projectName`, `composeFiles`, `execution`, the entire `remote` object, `profiles`, `ingress`, `health`, `directories`, `content` and the complete `secrets` source. - A destination's `environment` **merges** over the app defaults. - A destination's `roles` take `environment` and `secrets` per role, without `build`: destinations share the source. For each role, `environment` merges key by key over the app role's, and `secrets` replaces the app role's list. A role named only in the destination is allowed and sets environment only. +- A destination removes a role with `null`, for a destination whose Compose files have no such service. The role gets no build, image, environment or secrets, is not checked against the Compose files, is not Git-checked or tagged, and its secrets are no longer added to a 1Password `variables` allowlist. If the selected Compose files still declare the service, it runs as they define it, without compoza's image or role settings. Every `null` in every destination and in `local.roles` is checked when the config loads, including destinations you do not select, and must name a role in the top-level `roles`; otherwise loading fails with the file name and a message such as `Destination "staging" removes role "wrker", which is not a role; roles: web, worker`. A role removed by the selected destination stays removed in a `--local` run: `local.roles` entries for it are ignored. `null` is accepted only under `destinations..roles` and `local.roles`, not in the top-level `roles`. + +```yaml +destinations: + staging: + composeFiles: [compose.yaml] # has no worker service + roles: + worker: null +``` + - `name` remains the image prefix; `projectName` selects the Compose project and the Caddy site-file key. - Unknown destination names fail. diff --git a/docs/secrets.md b/docs/secrets.md index af4b33c..ccf3643 100644 --- a/docs/secrets.md +++ b/docs/secrets.md @@ -162,7 +162,7 @@ roles: - Only services whose role lists a secret receive it. Any other service that needs the value still maps it in Compose. - The override holds a `${NAME}` reference, never the value. The value stays in the interpolation pool: the stack `.env` over SSH, the process environment for client execution. - With `variables: []` as above, the 1Password read returns exactly the role secrets. -- `destinations..roles..secrets` replaces that role's list, and `local.roles` does the same for `--local` runs, which read the local source. +- `destinations..roles..secrets` replaces that role's list, and `local.roles` does the same for `--local` runs, which read the local source. A role set to `null` in either place is removed for that run, so its secrets are no longer added to a 1Password `variables` allowlist. A role secret that is missing from the source, reserved, or also set in the service's Compose `environment:` fails validation before anything is built. See [Environment and secrets](configuration.md#environment-and-secrets) for every rule. diff --git a/docs/ssh-deployment.md b/docs/ssh-deployment.md index 15f989d..48b3122 100644 --- a/docs/ssh-deployment.md +++ b/docs/ssh-deployment.md @@ -189,7 +189,7 @@ With [roles](configuration.md#how-roles-reach-services), the generated `compoza. Before replacing the deployed files or running `up`, Compose validates the normalized file with the selected env and overlay. A private JSON model check rejects undeclared relative files, unsafe writable asset binds and relative secret/env paths. Resolved Compose configuration and third-party diagnostics are suppressed because they can contain secrets; no validation log is left in the stack. -Managed status projects only service/state/health/exit-code fields and redacts selected dotenv values. Requested application logs can still contain secrets; applications are responsible for their logging policy. +Managed status projects only service/image/state/health/exit-code fields (the image only when it is a well-formed reference, otherwise `(image)`) and redacts the values of the managed secret variables. `.compoza.json` records those variable names, never their values; a stack deployed by an earlier version has no list, so all dotenv values are redacted until it is redeployed. Requested application logs can still contain secrets; applications are responsible for their logging policy. ## Deploy transaction diff --git a/src/cli.ts b/src/cli.ts index c89d417..16537e8 100644 --- a/src/cli.ts +++ b/src/cli.ts @@ -490,8 +490,8 @@ const status = Command.make( helpText([ "Lists the app's containers on its host with docker compose ps --all, so a stopped stack " + 'shows as stopped instead of missing. It changes nothing.', - 'With execution: ssh and a managed secret source, only the service, state, health and ' + - 'exit code are shown, with dotenv values redacted.', + 'With execution: ssh and a managed secret source, only the service, image, state, health ' + + 'and exit code are shown, with secret values redacted.', ]), ), Command.withExamples([{ command: 'compoza status infra' }]), diff --git a/src/config/deploy.test.ts b/src/config/deploy.test.ts index f1dd550..f4a3fce 100644 --- a/src/config/deploy.test.ts +++ b/src/config/deploy.test.ts @@ -14,6 +14,8 @@ import { environmentOf, ingressOf, loadApp, + removedRolesOf, + roleRemovalIssue, rolesOf, selectDestination, workingDirectoryOf, @@ -306,6 +308,131 @@ describe('roles', () => { ); }); +describe('role removal', () => { + const decode = (input: unknown) => Schema.decodeUnknownEffect(DeployConfig)(input); + + it.effect('accepts null under destination and local roles only', () => + Effect.gen(function* () { + const config = yield* decode({ + ...valid, + destinations: { dev: { roles: { builder: null } } }, + local: { roles: { web: null } }, + }); + assert.strictEqual(config.destinations?.dev?.roles?.builder, null); + assert.strictEqual(config.local?.roles?.web, null); + + const error = yield* decode({ ...valid, roles: { web: null } }).pipe(Effect.flip); + assert.strictEqual(error._tag, 'SchemaError'); + }), + ); + + it.effect('removes a role from the selected destination, including its build', () => + Effect.gen(function* () { + const config = yield* decode({ + ...valid, + destinations: { dev: { roles: { builder: null, web: { environment: { X: '1' } } } } }, + }); + const { config: selected } = selectDestination({ dir: '/d', config }, 'dev'); + assert.notProperty(selected.roles ?? {}, 'builder'); + assert.deepStrictEqual(Object.keys(selected.roles ?? {}), ['web']); + assert.deepStrictEqual(buildingRoles(selected), ['web']); + assert.deepStrictEqual(buildingRoles(config), ['builder', 'web']); + }), + ); + + it.effect('drops the roles key when a destination removes every role', () => + Effect.gen(function* () { + const config = yield* decode({ + ...valid, + destinations: { dev: { roles: { builder: null, web: null } } }, + }); + const { config: selected } = selectDestination({ dir: '/d', config }, 'dev'); + assert.notProperty(selected, 'roles'); + assert.deepStrictEqual(buildingRoles(selected), []); + }), + ); + + it.effect('keeps a role the destination removed out of a local run with the destination', () => + Effect.gen(function* () { + const both = yield* decode({ + ...valid, + destinations: { dev: { roles: { builder: null } } }, + local: { roles: { builder: null } }, + }); + const selectedBoth = selectDestination({ dir: '/d', config: both }, 'dev'); + assert.deepStrictEqual(Object.keys(rolesOf(selectedBoth.config, { local: true })), ['web']); + + const settings = yield* decode({ + ...valid, + destinations: { dev: { roles: { builder: null } } }, + local: { + roles: { builder: { environment: { X: '1' } }, web: { environment: { Y: '2' } } }, + }, + }); + const selectedSettings = selectDestination({ dir: '/d', config: settings }, 'dev'); + const local = rolesOf(selectedSettings.config, { local: true }); + assert.deepStrictEqual(Object.keys(local), ['web']); + assert.strictEqual(local.web?.environment?.Y, '2'); + assert.deepStrictEqual(removedRolesOf(selectedSettings, { local: true }), ['builder']); + + const onlyLocal = yield* decode({ + ...valid, + destinations: { dev: { environment: { A: '1' } } }, + local: { roles: { builder: null } }, + }); + const selectedLocal = selectDestination({ dir: '/d', config: onlyLocal }, 'dev'); + assert.deepStrictEqual(Object.keys(rolesOf(selectedLocal.config, { local: true })), ['web']); + assert.deepStrictEqual(Object.keys(rolesOf(selectedLocal.config)), ['builder', 'web']); + assert.deepStrictEqual(removedRolesOf(selectedLocal, { local: true }), ['builder']); + assert.deepStrictEqual(removedRolesOf(selectedLocal), []); + }), + ); + + it.effect('names the first removal of a role the app does not define', () => + Effect.gen(function* () { + const destination = yield* decode({ + ...valid, + destinations: { dev: { roles: { wrker: null } } }, + }); + assert.strictEqual( + roleRemovalIssue(destination), + 'Destination "dev" removes role "wrker", which is not a role; roles: builder, web', + ); + + const local = yield* decode({ ...valid, local: { roles: { wrker: null } } }); + assert.strictEqual( + roleRemovalIssue(local), + 'local.roles removes role "wrker", which is not a role; roles: builder, web', + ); + + const valid2 = yield* decode({ + ...valid, + destinations: { dev: { roles: { builder: null, other: { environment: { A: '1' } } } } }, + local: { roles: { web: null } }, + }); + assert.isUndefined(roleRemovalIssue(valid2)); + + const bare = yield* decode({ + name: 'demo', + host: 'atlas', + composeFiles: ['c.yaml'], + local: { roles: { a: null } }, + }); + assert.include(roleRemovalIssue(bare), 'roles: none'); + }), + ); + + it.effect('removes a role for local runs only', () => + Effect.gen(function* () { + const config = yield* decode({ ...valid, local: { roles: { builder: null } } }); + assert.deepStrictEqual(Object.keys(rolesOf(config, { local: true })), ['web']); + assert.deepStrictEqual(buildingRoles(config, { local: true }), ['web']); + assert.deepStrictEqual(Object.keys(rolesOf(config)), ['builder', 'web']); + assert.deepStrictEqual(buildingRoles(config), ['builder', 'web']); + }), + ); +}); + describe('loadApp', () => { it.effect('reads a folder or the file itself and resolves the folder', () => withTempDir((dir) => @@ -350,6 +477,30 @@ describe('loadApp', () => { ).pipe(Effect.provide(NodeServices.layer)), ); + it.effect( + 'reports a role removal of an undefined role in any destination as a config error', + () => + withTempDir((dir) => + Effect.gen(function* () { + const file = join(dir, 'deploy.yml'); + yield* Effect.promise(() => + writeFile( + file, + YAML.stringify({ ...valid, destinations: { dev: { roles: { wrker: null } } } }), + ), + ); + + const error = yield* loadApp(dir).pipe(Effect.flip); + + assert.strictEqual( + error.message, + `${file}: Destination "dev" removes role "wrker", which is not a role; ` + + 'roles: builder, web', + ); + }), + ).pipe(Effect.provide(NodeServices.layer)), + ); + it.effect('fails with the dedicated message when targets is still configured', () => withTempDir((dir) => Effect.gen(function* () { diff --git a/src/config/deploy.ts b/src/config/deploy.ts index 0f3c102..5660f41 100644 --- a/src/config/deploy.ts +++ b/src/config/deploy.ts @@ -1,8 +1,7 @@ import { Effect, type FileSystem, Path, Schema } from 'effect'; import { resolve } from 'node:path'; -import type { DeployError } from '../errors.ts'; - +import { DeployError } from '../errors.ts'; import { Ingress, SecretsConfig, @@ -138,9 +137,10 @@ export const Role = Schema.Struct({ }); /** - * Role settings by Compose service name. + * Role changes by Compose service name: settings to merge over the role, or `null` to remove the + * role for the run. */ -const RoleSettingsByName = Schema.Record(RoleName, RoleSettings); +const RoleChangesByName = Schema.Record(RoleName, Schema.NullOr(RoleSettings)); /** * Settings that replace the deployment's for `--local` runs against this machine's Docker. @@ -164,9 +164,9 @@ export const LocalOverrides = Schema.Struct({ /** * Role environment merged over the app's roles for a local run, and secrets replacing a role's - * list. + * list. A role set to `null` is removed for the local run. */ - roles: Schema.optionalKey(RoleSettingsByName), + roles: Schema.optionalKey(RoleChangesByName), }); /** @@ -257,9 +257,10 @@ export const Destination = Schema.Struct({ environment: Schema.optionalKey(Schema.Record(Schema.String, Schema.String)), /** - * Role environment merged over the app's roles, and secrets replacing a role's list. + * Role environment merged over the app's roles, and secrets replacing a role's list. A role set + * to `null` is removed for this destination. */ - roles: Schema.optionalKey(RoleSettingsByName), + roles: Schema.optionalKey(RoleChangesByName), /** * The ingress replacing the app default, including `none` for private destinations. @@ -421,6 +422,12 @@ export interface App { * The decoded `deploy.yaml`. */ readonly config: DeployConfig; + + /** + * The roles the selected destination removed, which `config.roles` no longer lists. Empty when no + * destination is selected. + */ + readonly removedRoles?: ReadonlyArray; } /** @@ -496,17 +503,23 @@ export function environmentOf( } /** - * Merges role settings from a destination or local run over a base set of roles: per role, + * Merges role changes from a destination or local run over a base set of roles: per role, * `environment` merges over the base's and `secrets` replaces the base's list when given. A role - * named only in `overrides` is kept as an environment-only role. + * named only in `overrides` is kept as an environment-only role. A role set to `null` is removed + * entirely, and removing a role the base does not have changes nothing. */ export function mergeRoles( base: Readonly> | undefined, - overrides: Readonly> | undefined, + overrides: Readonly> | undefined, ): Record { const merged: Record = { ...base }; for (const [name, settings] of Object.entries(overrides ?? {})) { const role = Object.hasOwn(merged, name) ? merged[name] : undefined; + if (settings === null) { + delete merged[name]; + continue; + } + const environment = { ...role?.environment, ...settings.environment }; const secrets = settings.secrets ?? role?.secrets; merged[name] = { @@ -520,7 +533,34 @@ export function mergeRoles( } /** - * The roles for a run: the app's, with the `local.roles` settings merged over them for a local run. + * The first role removal in `config` that names a role the app's top-level `roles` does not define, + * as a message, or `undefined` when every removal is valid. Destinations are checked in + * configuration order, then `local.roles`, so a typo in a destination that is not selected is still + * found. + */ +export function roleRemovalIssue(config: DeployConfig): string | undefined { + const defined = Object.keys(config.roles ?? {}); + const sources = [ + ...Object.entries(config.destinations ?? {}).map(([name, destination]) => ({ + subject: `Destination "${name}"`, + changes: destination.roles, + })), + { subject: 'local.roles', changes: config.local?.roles }, + ]; + for (const { subject, changes } of sources) { + const unknown = removedBy(changes).find((name) => !defined.includes(name)); + if (unknown !== undefined) { + const choices = defined.join(', ') || 'none'; + return `${subject} removes role "${unknown}", which is not a role; roles: ${choices}`; + } + } + + return undefined; +} + +/** + * The roles for a run: the app's (already merged with the selected destination), with the + * `local.roles` changes applied over them for a local run. */ export function rolesOf( config: DeployConfig, @@ -554,6 +594,11 @@ export const loadApp = Effect.fn('loadApp')(function* ( `No ${file}. Point compoza at a folder holding a ${DEPLOY_FILE}.`, { privateInput: true, removedKeys: { targets: TARGETS_REMOVED_MESSAGE } }, ); + const removalIssue = roleRemovalIssue(config); + if (removalIssue !== undefined) { + return yield* new DeployError({ message: `${file}: ${removalIssue}` }); + } + return { dir: path.dirname(file), file, config }; }); @@ -580,6 +625,24 @@ const deployFileFor = Effect.fn('deployFileFor')(function* (target: string) { return (yield* fileExists(alternate)) ? alternate : preferred; }); +/** + * The `local` entry for a destination's config, as a spread: `local.roles` without the roles that + * the destination removes, so a local run cannot bring one back or fail on removing it twice. + * Nothing is added when the app has no `local` block. + */ +function localWithoutRemoved( + local: typeof LocalOverrides.Type | undefined, + destinationRoles: Readonly> | undefined, +): { readonly local?: typeof LocalOverrides.Type } { + if (local?.roles === undefined) { + return local === undefined ? {} : { local }; + } + + const removed = removedBy(destinationRoles); + const kept = Object.entries(local.roles).filter(([name]) => !removed.includes(name)); + return { local: { ...local, roles: Object.fromEntries(kept) } }; +} + /** * Selects a destination before host resolution without changing the shared source or image prefix. * @@ -598,14 +661,39 @@ export function selectDestination(app: App, name?: string): App { } const { roles: _roles, ...rest } = selected; + const { roles: _appRoles, ...base } = app.config; const roles = mergeRoles(app.config.roles, selected.roles); const config = { - ...app.config, + ...base, ...rest, environment: { ...app.config.environment, ...selected.environment }, ...(Object.keys(roles).length === 0 ? {} : { roles }), + ...localWithoutRemoved(app.config.local, selected.roles), }; - return { ...app, config }; + return { ...app, config, removedRoles: removedBy(selected.roles) }; +} + +/** + * The roles that `changes` set to `null`. + */ +function removedBy( + changes: Readonly> | undefined, +): ReadonlyArray { + return Object.entries(changes ?? {}) + .filter(([, change]) => change === null) + .map(([name]) => name); +} + +/** + * The roles removed for a run, by the selected destination and, for a local run, by `local.roles`, + * in that order and without duplicates. + */ +export function removedRolesOf( + app: App, + { local = false }: RunOptions = {}, +): ReadonlyArray { + const names = [...(app.removedRoles ?? []), ...(local ? removedBy(app.config.local?.roles) : [])]; + return [...new Set(names)]; } /** diff --git a/src/config/secrets.test.ts b/src/config/secrets.test.ts index e2be4b3..02f19d1 100644 --- a/src/config/secrets.test.ts +++ b/src/config/secrets.test.ts @@ -38,7 +38,7 @@ const allowlisted = (variables: ReadonlyArray): App => ({ /** * The app with `roles` merged over its own, role settings only. */ -function withRoles(base: App, roles: Readonly>): App { +function withRoles(base: App, roles: Readonly>): App { return { ...base, config: { ...base.config, roles: mergeRoles(base.config.roles, roles) } }; } @@ -166,6 +166,58 @@ describe('secret sources', () => { ), ); + describe('removed roles', () => { + const reader = Effect.provideService(OnePasswordReader, async () => [ + { name: 'TOKEN', value: 'first', masked: true }, + { name: 'WEB_TOKEN', value: 'second', masked: true }, + { name: 'WORKER_TOKEN', value: 'SENTINEL_EXCLUDED', masked: true }, + ]); + const twoRoles = (base: App): App => + withRoles(base, { web: { secrets: ['WEB_TOKEN'] }, worker: { secrets: ['WORKER_TOKEN'] } }); + + it.effect('leaves the secrets of a role removed by a destination out of the allowlist', () => + Effect.gen(function* () { + const base = twoRoles(allowlisted(['TOKEN'])); + const withDestination: App = { + ...base, + config: { + ...base.config, + destinations: { staging: { roles: { worker: null } } }, + }, + }; + assert.deepStrictEqual(yield* readSecrets(selectDestination(withDestination, 'staging')), { + TOKEN: 'first', + WEB_TOKEN: 'second', + }); + assert.deepStrictEqual(yield* readSecrets(withDestination), { + TOKEN: 'first', + WEB_TOKEN: 'second', + WORKER_TOKEN: 'SENTINEL_EXCLUDED', + }); + }).pipe(Effect.provide(NodeServices.layer), reader), + ); + + it.effect('leaves the secrets of a role removed by local.roles out of a local run', () => + Effect.gen(function* () { + const base = twoRoles(app); + const local: App = { + ...base, + config: { + ...base.config, + local: { + secrets: { '1password': { environmentId: 'e', variables: ['TOKEN'] } }, + roles: { worker: null }, + }, + }, + }; + assert.deepStrictEqual(yield* readSecrets(local, { local: true }), { + TOKEN: 'first', + WEB_TOKEN: 'second', + }); + }).pipe(Effect.provide(NodeServices.layer), reader), + ); + }); + it.effect('imports every variable, role secrets included, without an allowlist', () => Effect.gen(function* () { const selected = withRoles(app, { web: { secrets: ['ROLE_TOKEN'] } }); diff --git a/src/deploy.ts b/src/deploy.ts index 6ccffa5..e3cb8f9 100644 --- a/src/deploy.ts +++ b/src/deploy.ts @@ -10,7 +10,13 @@ import type { Phase } from './progress.ts'; import { ROLES_OVERRIDE_FILE } from './bundle.ts'; import { blocking, formatChecks, runChecks } from './checks.ts'; import { mkdirScript, printableStep } from './command.ts'; -import { type App, directoriesOf, loadApp, selectDestination } from './config/deploy.ts'; +import { + type App, + directoriesOf, + loadApp, + removedRolesOf, + selectDestination, +} from './config/deploy.ts'; import { dockerHost, type HostConfig, hostFor, loadHostsForRun } from './config/hosts.ts'; import { readSecrets } from './config/secrets.ts'; import { DeployError, platformFailure, tryPlan } from './errors.ts'; @@ -132,16 +138,25 @@ export const resolve = Effect.fn('resolve')(function* ( * commit nor a clean checkout matters, and it has to work from a deploy folder that is no longer * inside a checkout, where `git rev-parse` exits 128. The context is then only a placeholder for * `PlanOptions`, which needs one even though no build step reads it. An app that builds nothing has - * no sources and does not consult Git, though `--tag` values are still parsed and validated. + * no sources and does not consult Git, and `--tag` fails because there are no images to reuse. */ const chooseSource = Effect.fn('chooseSource')(function* ( app: App, options: DeployOptions, ): Effect.fn.Return { - const roles = buildingRoles(app.config).map((role) => buildSettingsOf(app, role)); + const roles = buildingRoles(app.config, { local: options.local }).map((role) => + buildSettingsOf(app, role), + ); if (options.tags.length > 0) { - const tags = yield* tryPlan(() => parseTagFlags(options.tags, app.config), 'Invalid --tag'); + const tags = yield* tryPlan( + () => + parseTagFlags(options.tags, app.config, { + local: options.local, + removed: removedRolesOf(app, options), + }), + 'Invalid --tag', + ); const reused: Record = {}; for (const settings of roles) { const tag = Object.hasOwn(tags, settings.role) ? tags[settings.role] : undefined; diff --git a/src/plan.ts b/src/plan.ts index ecd8b03..97160bb 100644 --- a/src/plan.ts +++ b/src/plan.ts @@ -123,7 +123,7 @@ export function planDeployment(options: PlanOptions): ReadonlyArray { const steps: Array = []; if (!options.reuseImages) { - for (const role of buildingRoles(app.config)) { + for (const role of buildingRoles(app.config, { local })) { const source = Object.hasOwn(options.sources, role) ? options.sources[role] : undefined; if (source === undefined) { throw new Error(`No source for role "${role}"`); diff --git a/src/remote/runtime.sh b/src/remote/runtime.sh index e59c398..05c108e 100644 --- a/src/remote/runtime.sh +++ b/src/remote/runtime.sh @@ -44,7 +44,8 @@ compoza_metadata() { (.files | file_paths) and (.ownedFiles | file_paths) and ((.files - .ownedFiles) | length == 0) and (.overlay | . == null or (type == "string" and startswith("/") and (explode | all(. >= 32)))) and - (.managed | type == "boolean") and (.unroute | type == "string"))' "$metadata" >/dev/null 2>&1 || { compoza_fail 'Invalid deployed lifecycle metadata; redeploy with the current CLI'; return 1; } + (.managed | type == "boolean") and (.unroute | type == "string") and + ((has("secretNames") | not) or (.secretNames | type == "array" and all(.[]; type == "string" and test("^[A-Za-z_][A-Za-z0-9_]*$")))))' "$metadata" >/dev/null 2>&1 || { compoza_fail 'Invalid deployed lifecycle metadata; redeploy with the current CLI'; return 1; } } # An existing stack file is overwritten only when the previous metadata identifies it as tool-owned. @@ -167,7 +168,7 @@ compoza_require_service() { compoza_compose_safe() { if ! jq -e '.managed' "$metadata" >/dev/null 2>&1; then compoza_compose "$@"; return; fi case "${1-}" in - ps) compoza_compose "$@" --format json 2>/dev/null | jq -c --rawfile snapshot "$env_file" "$status_filter" 2>/dev/null || { compoza_fail 'Cannot safely read deployed status; diagnostics suppressed'; return 1; };; + ps) compoza_compose "$@" --format json 2>/dev/null | jq -c --rawfile snapshot "$env_file" --slurpfile metadata "$metadata" "$status_filter" 2>/dev/null || { compoza_fail 'Cannot safely read deployed status; diagnostics suppressed'; return 1; };; logs) compoza_compose "$@" 2>/dev/null;; *) if ! compoza_compose "$@" >/dev/null 2>&1; then compoza_fail 'Compose action failed; secret-bearing diagnostics suppressed'; return 1; fi;; esac diff --git a/src/remote/status.jq b/src/remote/status.jq index f0b2e93..4d39884 100644 --- a/src/remote/status.jq +++ b/src/remote/status.jq @@ -1,13 +1,24 @@ # Snapshots are tool-generated JSON-string dotenv. Parse values privately, never as shell code. def escape_controls: explode | map(if . < 32 then ([.] | implode | tojson | .[1:-1]) else [.] | implode end) | join(""); +# Only the variables that .compoza.json lists as secrets are redacted. A file from an older version +# has no list, so every value is redacted. Longest values go first so a short value that is part of +# a longer one cannot leave the rest of the longer one visible. def values: - $snapshot | split("\n") | map(select(contains("=")) | split("=") | .[1:] | join("=") | escape_controls | fromjson | gsub("\\$\\$"; "$")) | map(select(length > 0)); + ($metadata[0].secretNames // null) as $names | + $snapshot | split("\n") | map( + select(contains("=")) + | split("=") + | {name: .[0], value: (.[1:] | join("=") | escape_controls | fromjson | gsub("\\$\\$"; "$"))} + | select(.name as $name | $names == null or ($names | index($name)) != null) + | .value + ) | map(select(length > 0)) | unique | sort_by(length) | reverse; def redact($values): reduce $values[] as $value (. ; split($value) | join("***")); values as $values | (if type == "array" then .[] else . end) | { - service: ((if (.Service | type) == "string" and (.Service | test("^[a-zA-Z0-9][a-zA-Z0-9_.-]*$")) then .Service else "(service)" end) | redact($values)), + service: ((if (.Service | type) == "string" and (.Service | test("\\A[a-zA-Z0-9][a-zA-Z0-9_.-]*\\z")) then .Service else "(service)" end) | redact($values)), + image: ((if (.Image | type) == "string" and (.Image | length) <= 512 and (.Image | test("\\A[a-zA-Z0-9][a-zA-Z0-9._-]*(:[0-9]+)?(/[a-zA-Z0-9][a-zA-Z0-9._-]*)*(:[a-zA-Z0-9_][a-zA-Z0-9_.-]{0,127})?(@sha256:[a-fA-F0-9]{64})?\\z")) then .Image else "(image)" end) | redact($values)), state: (.State as $state | if ["running", "exited", "created", "restarting", "paused", "removing", "dead"] | index($state) then $state else "unknown" end), health: (.Health as $health | if ["healthy", "unhealthy", "starting", ""] | index($health) then $health else "unknown" end), exitCode: (if (.ExitCode | type) == "number" and (.ExitCode | floor) == .ExitCode then .ExitCode else null end) diff --git a/src/roles-deploy.test.ts b/src/roles-deploy.test.ts index ea5615f..24c97a4 100644 --- a/src/roles-deploy.test.ts +++ b/src/roles-deploy.test.ts @@ -283,6 +283,118 @@ describe('deploying roles', () => { }).pipe(Effect.scoped, Effect.provide(TestConsole.layer)), ); + it.effect('fails --tag when no role has a build', () => + Effect.gen(function* () { + const f = yield* fixture; + yield* f.write({ roles: { db: roles.db } }); + const error = yield* deploy({ ...f.options, tags: ['v1'] }).pipe( + Effect.provide(f.services), + Effect.flip, + ); + assert.strictEqual( + error.message, + 'No role has a build, so there are no images to reuse; deploy without --tag', + ); + assert.lengthOf(f.steps, 0); + }).pipe(Effect.scoped, Effect.provide(TestConsole.layer)), + ); + + it.effect('does not build, tag, check or configure a role a destination removes', () => + Effect.gen(function* () { + const f = yield* fixture; + yield* f.write({ roles, destinations: { staging: { roles: { worker: null } } } }); + yield* deploy({ ...f.options, local: false, destination: 'staging' }).pipe( + Effect.provide(f.services), + Effect.provideService(DryRun, false), + ); + const lines = yield* TestConsole.logLines; + + const builds = f.steps.filter((step) => 'program' in step && step.args.includes('buildx')); + assert.lengthOf(builds, 1); + assert.deepStrictEqual( + f.gitCalls.filter((call) => call.startsWith('hasCommits')), + [`hasCommits ${f.webRepo}`], + ); + assert.notInclude(f.overrides[0]!, 'worker'); + assert.strictEqual(lines[0], 'Deploying demo aaaaaaaaaaaa to host (ssh://fixture)'); + }).pipe(Effect.scoped, Effect.provide(TestConsole.layer)), + ); + + it.effect('rejects --tag for a role a destination removes', () => + Effect.gen(function* () { + const f = yield* fixture; + yield* f.write({ roles, destinations: { staging: { roles: { worker: null } } } }); + const error = yield* deploy({ + ...f.options, + local: false, + destination: 'staging', + tags: ['worker=1'], + }).pipe(Effect.provide(f.services), Effect.flip); + assert.include( + error.message, + 'Role "worker" is removed for this run, so --tag cannot name it', + ); + }).pipe(Effect.scoped, Effect.provide(TestConsole.layer)), + ); + + it.effect('does not build, tag, check or configure a role local.roles removes', () => + Effect.gen(function* () { + const f = yield* fixture; + yield* f.write({ roles, local: { roles: { worker: null } } }); + yield* deploy(f.options).pipe(Effect.provide(f.services)); + const lines = yield* TestConsole.logLines; + + const builds = f.steps.filter((step) => 'program' in step && step.args.includes('buildx')); + assert.lengthOf(builds, 1); + assert.deepStrictEqual( + f.gitCalls.filter((call) => call.startsWith('hasCommits')), + [`hasCommits ${f.webRepo}`], + ); + assert.notInclude(f.overrides[0]!, 'worker'); + assert.strictEqual(lines[0], 'Deploying demo aaaaaaaaaaaa to local Docker'); + }).pipe(Effect.scoped, Effect.provide(TestConsole.layer)), + ); + + it.effect('keeps a role removed by the destination out of a --destination --local run', () => + Effect.gen(function* () { + const f = yield* fixture; + yield* f.write({ + roles, + destinations: { staging: { roles: { worker: null } } }, + local: { roles: { worker: { environment: { X: '1' } } } }, + }); + yield* deploy({ ...f.options, destination: 'staging' }).pipe(Effect.provide(f.services)); + + const builds = f.steps.filter((step) => 'program' in step && step.args.includes('buildx')); + assert.lengthOf(builds, 1); + assert.notInclude(f.overrides[0]!, 'worker'); + }).pipe(Effect.scoped, Effect.provide(TestConsole.layer)), + ); + + it.effect('accepts a role removed by both the destination and local.roles', () => + Effect.gen(function* () { + const f = yield* fixture; + yield* f.write({ + roles, + destinations: { staging: { roles: { worker: null } } }, + local: { roles: { worker: null } }, + }); + yield* deploy({ ...f.options, destination: 'staging' }).pipe(Effect.provide(f.services)); + + assert.notInclude(f.overrides[0]!, 'worker'); + }).pipe(Effect.scoped, Effect.provide(TestConsole.layer)), + ); + + it.effect('reports a local removal of an undefined role without building', () => + Effect.gen(function* () { + const f = yield* fixture; + yield* f.write({ roles, local: { roles: { wrker: null } } }); + const error = yield* deploy(f.options).pipe(Effect.provide(f.services), Effect.flip); + assert.include(error.message, 'local.roles removes role "wrker"'); + assert.lengthOf(f.steps, 0); + }).pipe(Effect.scoped, Effect.provide(TestConsole.layer)), + ); + it.effect('fails before building when a role is not a Compose service', () => Effect.gen(function* () { const f = yield* fixture; diff --git a/src/roles.ts b/src/roles.ts index f95798f..8973206 100644 --- a/src/roles.ts +++ b/src/roles.ts @@ -79,11 +79,15 @@ export interface RoleBuildSettings { } /** - * The names of the roles that build an image, in configuration order. Only the app's roles can - * build: destinations and local runs share the source. + * The names of the roles that build an image for a run, in configuration order. Only the app's + * roles can build, and a role that the selected destination or a local run removes with `null` + * builds nothing. */ -export function buildingRoles(config: DeployConfig): ReadonlyArray { - return Object.entries(config.roles ?? {}) +export function buildingRoles( + config: DeployConfig, + options: RunOptions = {}, +): ReadonlyArray { + return Object.entries(rolesOf(config, options)) .filter(([, role]) => role.build !== undefined) .map(([name]) => name); } diff --git a/src/ssh-plan.test.ts b/src/ssh-plan.test.ts index 5ee1961..97063dc 100644 --- a/src/ssh-plan.test.ts +++ b/src/ssh-plan.test.ts @@ -456,6 +456,7 @@ describe('flat SSH stack transactions', { timeout: FIXTURE_TIMEOUT_MS }, () => { JSON.stringify([ { Service: 'a.b-worker', + Image: 'registry.example/app-a.b:1.2.3', State: 'running', Health: 'healthy', ExitCode: 0, @@ -469,10 +470,32 @@ describe('flat SSH stack transactions', { timeout: FIXTURE_TIMEOUT_MS }, () => { assert.deepStrictEqual(JSON.parse(String(result.stdout)), { service: '***-worker', + image: 'registry.example/app-***:1.2.3', state: 'running', health: 'healthy', exitCode: 0, }); + + const imageOf = (image: unknown): unknown => { + writeFileSync( + join(dir, 'status.json'), + JSON.stringify([{ Service: 'web', State: 'running', Health: '', Image: image }]), + ); + + return JSON.parse(String(run(deployedScript(app, ['ps', '--all'], false)).stdout)).image; + }; + + assert.strictEqual( + imageOf('localhost:5000/team/app-web:v1'), + 'localhost:5000/team/app-web:v1', + ); + assert.strictEqual(imageOf(`app@sha256:${'a'.repeat(64)}`), `app@sha256:${'a'.repeat(64)}`); + assert.strictEqual(imageOf('app-a.b:1'), 'app-***:1'); + assert.strictEqual(imageOf('bad image\n:1'), '(image)'); + assert.strictEqual(imageOf('app:1\n'), '(image)'); + assert.strictEqual(imageOf('app@sha256:abc'), '(image)'); + assert.strictEqual(imageOf(42), '(image)'); + assert.strictEqual(imageOf(undefined), '(image)'); assert.notInclude( String(run(deployedScript(app, ['stop'], true)).stdout), 'PRIVATE_DIAGNOSTIC', @@ -484,6 +507,95 @@ describe('flat SSH stack transactions', { timeout: FIXTURE_TIMEOUT_MS }, () => { assert.notStrictEqual(run(deployedScript(app, ['ps', '--all'], false)).status, 0); })); + describe('status redaction', () => { + const statusOf = ( + { + dir, + run, + app, + }: { dir: string; run: (script: string) => ReturnType; app: App }, + service: string, + image: string, + ): { + service: string; + image: string; + state: string; + health: string; + exitCode: number | null; + } => { + writeFileSync( + join(dir, 'status.json'), + JSON.stringify([{ Service: service, State: 'running', Health: '', Image: image }]), + ); + const result = run(deployedScript(app, ['ps', '--all'], false)); + succeeded(result); + + return JSON.parse(String(result.stdout)); + }; + + it('records the sorted secret names and redacts only secret values', () => + fixture((f) => { + Object.assign(f.app.config, { + environment: { WEB_CONCURRENCY: '2', POSTGRES_USER: 'postgres' }, + }); + succeeded(f.deploy('3f2a2b9', { ZED: 'zzz', API_KEY: 'key' })); + + const recorded = JSON.parse(readFileSync(join(f.root, '.compoza.json'), 'utf8')); + assert.deepStrictEqual(recorded.secretNames, ['API_KEY', 'ZED']); + assert.notInclude(readFileSync(join(f.root, '.compoza.json'), 'utf8'), 'zzz'); + assert.deepStrictEqual(statusOf(f, 'shop-web', 'postgres:16'), { + service: 'shop-web', + image: 'postgres:16', + state: 'running', + health: '', + exitCode: null, + }); + assert.strictEqual(statusOf(f, 'shop-web', 'shop-web:3f2a2b9').image, 'shop-web:3f2a2b9'); + assert.strictEqual(statusOf(f, 'web-key', 'app-zzz:1').service, 'web-***'); + assert.strictEqual(statusOf(f, 'web', 'app-zzz:1').image, 'app-***:1'); + })); + + it('redacts every value for metadata that predates the secret name list', () => + fixture((f) => { + Object.assign(f.app.config, { environment: { POSTGRES_USER: 'postgres' } }); + succeeded(f.deploy('one', { TOKEN: 'zzz' })); + + const file = join(f.root, '.compoza.json'); + const { secretNames: _names, ...legacy } = JSON.parse(readFileSync(file, 'utf8')); + writeFileSync(file, JSON.stringify(legacy)); + + assert.strictEqual(statusOf(f, 'web', 'postgres:16').image, '***:16'); + assert.strictEqual(statusOf(f, 'web', 'app-zzz:1').image, 'app-***:1'); + })); + + it('rejects metadata whose secret names are not variable names', () => + fixture((f) => { + succeeded(f.deploy('one', { TOKEN: 'zzz' })); + + const file = join(f.root, '.compoza.json'); + const metadata = JSON.parse(readFileSync(file, 'utf8')); + writeFileSync(file, JSON.stringify({ ...metadata, secretNames: ['a b'] })); + + assert.notStrictEqual(f.run(deployedScript(f.app, ['ps', '--all'], false)).status, 0); + })); + + it('redacts the longest value first so overlapping values leave no remainder', () => + fixture((f) => { + succeeded(f.deploy('one', { SHORT: 'ab', LONG: 'abc' })); + + assert.strictEqual(statusOf(f, 'web', 'x/abc:1').image, 'x/***:1'); + })); + + it('rejects a service or image with a trailing newline', () => + fixture((f) => { + succeeded(f.deploy('one', { TOKEN: 'zzz' })); + + const status = statusOf(f, 'web\n', 'app:1\n'); + assert.strictEqual(status.service, '(service)'); + assert.strictEqual(status.image, '(image)'); + })); + }); + it('blocks malformed metadata or live Compose configuration before lifecycle actions', () => fixture(({ app, dir, root, deploy, run }) => { succeeded(deploy()); diff --git a/src/ssh-plan.ts b/src/ssh-plan.ts index f15476b..c0366e4 100644 --- a/src/ssh-plan.ts +++ b/src/ssh-plan.ts @@ -231,7 +231,7 @@ export function deploymentScript( const ingress = ingressFor(app)?.plan(app, host); const url = app.config.health?.url ?? ingress?.healthUrl; - const metadata = deploymentMetadata(app, tags, files, ingress, secrets !== undefined); + const metadata = deploymentMetadata(app, tags, files, ingress, secrets); const environment = generatedEnvironment(app) + secretEnvironment({ ...secrets }); const script = transactionScript({ @@ -438,15 +438,15 @@ function assertOverlaySeparate(app: App, root: string, outputs: ReadonlyArray>, files: ReadonlyArray, ingress: IngressPlan | undefined, - managed: boolean, + secrets: Readonly> | undefined, ): string { return JSON.stringify({ version: 1, @@ -456,7 +456,8 @@ function deploymentMetadata( files, ownedFiles: files, overlay: app.config.remote?.overlayFile ?? null, - managed, + managed: secrets !== undefined, + secretNames: Object.keys(secrets ?? {}).toSorted(), unroute: ingress === undefined ? '' : ingressOperationScript(ingress.unroute), }); } diff --git a/src/tags.test.ts b/src/tags.test.ts index bebd865..a54fb5f 100644 --- a/src/tags.test.ts +++ b/src/tags.test.ts @@ -66,7 +66,40 @@ describe('parseTagFlags', () => { it('names no roles when the app has none', () => { const bare: DeployConfig = { name: 'demo', host: 'atlas', composeFiles: ['compose.yaml'] }; assert.throws(() => parseTagFlags(['web=1'], bare), 'roles: none'); - assert.deepStrictEqual(parseTagFlags(['abc'], bare), {}); + assert.deepStrictEqual(parseTagFlags([], bare), {}); + }); + + it('fails any --tag when no role has a build', () => { + const bare: DeployConfig = { name: 'demo', host: 'atlas', composeFiles: ['compose.yaml'] }; + const message = 'No role has a build, so there are no images to reuse; deploy without --tag'; + assert.throws(() => parseTagFlags(['abc'], bare), message); + + const thirdParty: DeployConfig = { ...bare, roles: { db: { environment: { A: '1' } } } }; + assert.throws(() => parseTagFlags(['v1'], thirdParty), message); + assert.throws(() => parseTagFlags(['db=v1'], thirdParty), 'Role "db" has no build'); + }); + + it('treats a role removed for a local run as unknown', () => { + const removed: DeployConfig = { ...config, local: { roles: { worker: null } } }; + assert.deepStrictEqual(parseTagFlags(['abc'], removed, { local: true }), { web: 'abc' }); + assert.throws( + () => parseTagFlags(['worker=1'], removed, { local: true }), + 'Unknown role "worker" in --tag; roles: web, postgres', + ); + assert.deepStrictEqual(parseTagFlags(['abc'], removed), { web: 'abc', worker: 'abc' }); + }); + + it('says a role removed for the run is removed instead of unknown', () => { + const removed: DeployConfig = { ...config, local: { roles: { worker: null } } }; + assert.throws( + () => parseTagFlags(['worker=1'], removed, { local: true, removed: ['worker'] }), + 'Role "worker" is removed for this run, so --tag cannot name it', + ); + }); + + it('fails --tag when every building role is removed', () => { + const removed: DeployConfig = { ...config, local: { roles: { web: null, worker: null } } }; + assert.throws(() => parseTagFlags(['abc'], removed, { local: true }), 'No role has a build'); }); }); diff --git a/src/tags.ts b/src/tags.ts index 30f9fb3..bd58b5f 100644 --- a/src/tags.ts +++ b/src/tags.ts @@ -1,6 +1,7 @@ -import type { DeployConfig } from './config/deploy.ts'; +import type { DeployConfig, RunOptions } from './config/deploy.ts'; import type { RoleSources } from './roles.ts'; +import { rolesOf } from './config/deploy.ts'; import { buildingRoles } from './roles.ts'; /** @@ -14,20 +15,32 @@ const THIRD_PARTY_TAG = 'third-party'; */ const TAG_PATTERN = /^[A-Za-z0-9_][A-Za-z0-9_.-]{0,127}$/; +/** + * What `parseTagFlags` needs to know about the run. + */ +export interface TagOptions extends RunOptions { + /** + * The roles the run removed, so `--tag` can say so instead of calling them unknown. + */ + readonly removed?: ReadonlyArray; +} + /** * Reads the values of the repeatable `--tag` flag into one tag per building role. * * `--tag ` is the default for every building role and `--tag =` overrides it for * one role. Throws an `Error` naming the problem when a role is unknown or has no `build`, a role * or the bare tag is given twice, a tag is empty or not a valid Docker tag, or a building role is - * left without a tag. + * left without a tag. Any `--tag` fails when no role builds, since there are no images to reuse. A + * role that a destination or local run removed cannot be named. */ export function parseTagFlags( values: ReadonlyArray, config: DeployConfig, + options: TagOptions = {}, ): Readonly> { - const roles = Object.keys(config.roles ?? {}); - const building = buildingRoles(config); + const roles = Object.keys(rolesOf(config, options)); + const building = buildingRoles(config, options); const named = new Map(); const bare: Array = []; @@ -54,6 +67,10 @@ export function parseTagFlags( continue; } + if (options.removed?.includes(role) === true && !roles.includes(role)) { + throw new Error(`Role "${role}" is removed for this run, so --tag cannot name it`); + } + if (!roles.includes(role)) { throw new Error(`Unknown role "${role}" in --tag; roles: ${roles.join(', ') || 'none'}`); } @@ -72,6 +89,10 @@ export function parseTagFlags( named.set(role, tag); } + if (building.length === 0 && values.length > 0) { + throw new Error('No role has a build, so there are no images to reuse; deploy without --tag'); + } + if (bare.length > 1) { throw new Error('Only one --tag without a role is allowed; use --tag = per role'); }