Skip to content

Make every deployment create and update flag take effect - #25

Merged
joshk merged 1 commit into
mainfrom
fix/deployment-create-update
Nov 23, 2026
Merged

joshk merged 1 commit into
mainfrom
fix/deployment-create-update

Conversation

@joshk

@joshk joshk commented Nov 22, 2026

Copy link
Copy Markdown
Contributor

Fixes the deployment create/update findings from the API review. Every flag on nh deployment create and nh deployment update now takes effect, and show displays the fields it used to leave out.

All references are to nerves_hub_web origin/main.

Bugs fixed

1. create without --version or --tag got a 422.
DeploymentGroup.create_changeset (lib/nerves_hub/managed_deployments/deployment_group.ex) does cast_embed(:conditions, required: true, ...), and the CLI left conditions out when it was empty. The create body now always sends conditions, defaulting to {"tags": [], "version": ""}.

2. create --state on was ignored.
create_changeset casts only name, delta_updatable, platform, architecture, notes, so is_active (and state) are dropped and every new group starts off. CreateDeployment now stops sending state in the POST. When --state on is given, it sends a follow-up PUT {"deployment": {"state": "on"}}, which goes through maybe_active_from_state → update_changeset. If that PUT fails, the error says the group was created but is still off, and wraps the API error. --state off needs nothing extra, so it stays a single request.

3. update --firmware X dropped every other flag.
DeploymentGroupController.update has two clauses. If the body has firmware, firmware_id, archive or archive_id, it calls create_deployment_release, which casts only description, notes, required, connecting_code(_mode) (DeploymentRelease.new_changeset) and ignores state, conditions, delta_updatable and group notes. UpdateDeployment now sends two requests when both kinds of change are given:

  • release: {"deployment": {"firmware", "archive"?, "required"?}}
  • group settings: {"deployment": {"state"?, "conditions"?, "delta_updatable"?, "notes"?}}. This request never carries firmware/archive, so it reaches the update_changeset clause.

Order, which the server gives no single request for:

  1. group settings, including --state off, but not --state on
  2. the new release
  3. {"state": "on"}, if asked for

The new release then lands only after the conditions and delta setting are in place, and never on a group that's being turned off. A group being turned on starts with the new firmware. Only the requests that are needed get sent: --firmware alone is still one PUT. If a later request fails, the error says which steps already took effect and wraps the *APIError.

4. Fields the CLI ignored.

  • Deployment now decodes notes, archive_uuid, conditions.tag_operator and current_release.required (all in deployment_group_json.ex). show prints Tag operator, Archive, Release required and Notes.
  • New flags, each checked against what the server casts:
    • --notes (create + update): cast by both create_changeset and update_changeset.
    • --tag-operator and|or (create + update): conditions_changeset casts tag_operator (Ecto.Enum [:and, :or]).
    • --archive <uuid> (update only): the release clause resolves archive with Archives.get/2.
    • --required (update only): DeploymentRelease.new_changeset casts required.

Decisions (made without Josh; open to change)

  • --archive and --required are update-only. The create endpoint doesn't accept them; it builds the first release from the firmware alone. CreateDeployment returns an error if they're set, rather than silently dropping them.
  • --archive on its own keeps the current firmware. new_changeset does validate_required([:firmware]), so an archive-only PUT would get a 422. UpdateDeployment reads the group first and sends its current firmware_uuid along with the archive.
  • --required without --firmware or --archive is refused locally. A release with the same firmware and archive fails validate_differs_from_current_release, so there's nothing for required to apply to.
  • A new release keeps only the archive you pass. If you pass --firmware without --archive, the new release has no archive. That's the server's behaviour (the dashboard's new-release form works the same way). nh doesn't carry the old archive over; the help text says so.
  • Clearing conditions. --version "" sends "version": "" and --tag "" sends "tags": []. conditions_changeset treats both as clearing. conditions uses on_replace: :update, so a partial conditions object leaves the other condition fields alone.
  • --firmware "" / --archive "" are refused instead of being treated as unchanged.
  • Release notes/description aren't exposed. In the release clause, notes means the release's notes, so --notes is only ever sent with the group-settings request. A --release-notes flag could come later.

firmware upload --deploy

Not changed. It sends only {"deployment": {"firmware": uuid}}, a single request to the release clause, which is exactly what it needs. Like update --firmware, the new release has no archive even if the group's previous release had one. That's worth knowing, but changing it is a product decision, so I left it.

OpenAPI spec vs. server code

DeploymentGroupCreationRequest (controllers/api/schemas/deployment_group_schemas.ex) lists state as a create field, but create_changeset never casts it, so the spec is wrong there. The update schema describes notes as group notes, but in a request that also has firmware, notes goes to the new release instead. archive isn't in the update schema, though the controller accepts it. I followed the code in each case.

Not touched

internal/api/errors.go, which belongs to fix/nested-changeset-errors (now merged, #20). A 422 such as one on conditions.version uses that formatting. The branch is based on main after #20, and the tests still pass.

Review

Ran code-review at level high on the diff before committing (signing was blocked at first, so there was no PR yet). It found three things, all fixed:

  • important: update --firmware X --tag beta on an active group sent the release before the tag change, so X could reach devices that only matched the old tags. Fixed with the settings → release → turn-on order above.
  • nit: the create help suggested update --required works on its own. It now says required belongs with a new release (--archive or --firmware).
  • nit: multi-line notes broke the show table. Each line now gets its own row.

Tests

  • New internal/api/deployment_test.go: httptest server that records every request (method, path, body). It covers always-sent conditions, create + turn-on, a failed turn-on, refused release fields on create, settings-only/release-only updates, the split and its order (including --state off), archive-only with a GET for the current firmware, partial-failure errors at each step, a failed first step passed on unchanged, and decoding the new fields.
  • cmd/deployment_test.go: create with every flag (POST then PUT), create with no conditions, no release flags on create, bad --tag-operator, update with firmware + settings (two PUTs, cleared conditions, JSON output), archive update, --required validation, and the new show fields.
  • show puts each line of multi-line notes on its own row.
  • go test ./..., go vet ./... and gofmt -l . are clean.

Not verified against a live server.

🤖 Generated with Claude Code

joshk added a commit to nerves-hub/nerves_hub_web that referenced this pull request Nov 23, 2026
Adds `/deployments/:name/releases` to the API: list a deployment group's
releases, read one, create one, and change whether one is required or
what its connecting code is.

Until now the only way to make a release over the API was `PUT
/deployments/:name` with `firmware` or `archive`, and that request
ignores every other group setting in its body. The `nh` CLI has to split
one update into as many as three requests, and it can't list releases or
edit `required` or the connecting code on a release that already exists
(found in nerves-hub/nh#25). A later `nh` PR adds `nh deployment release
list|show|create|update` against these endpoints.

`PUT /deployments/:name` keeps working as it does, so older CLIs are
unaffected. Its OpenAPI description now points at the new endpoints.

## Endpoints

All four sit under
`/api/orgs/:org_name/products/:product_name/deployments/:name/releases`
and accept a user token or a product API key. A release is identified by
its `number` within the group.

| Method and path | Needs | Returns |
| --- | --- | --- |
| `GET /releases` | membership | 200, newest first |
| `GET /releases/:number` | membership | 200, or 404 |
| `POST /releases` | `deployment_group:release:manage` | 201 with a
`location` header, or 422 |
| `PATCH /releases/:number` | `deployment_group:release:manage` | 200,
or 404, or 422 |

Every endpoint returns 404 for a group the product doesn't have. They
also check `require_every_device`, as the group endpoints do, so a role
limited to tagged devices can't use them.

`POST` body, flat (not wrapped in `deployment` the way the `PUT` is):

```json
{
  "firmware": "d9f8c63a-1234-5678-abcd-ef0123456789",
  "archive": "6a1f0c2e-...",
  "required": false,
  "description": "Fixes the Wi-Fi reconnect loop",
  "notes": "Roll out after the field test passes",
  "connecting_code": "Logger.info(\"connected\")",
  "connecting_code_mode": "last"
}
```

Only `firmware` is required; an `archive` left out or sent as `""` means
none. A `null` for `required` or `connecting_code_mode` is a 422; leave
them out for the defaults (`false`, `last`). Here the connecting code is
the release's own; the `PUT` drops it because there it means the
group's.

`PATCH` body, every field optional, anything left out keeps its value:

```json
{ "required": true, "connecting_code": null, "connecting_code_mode": "first" }
```

`null` or `""` removes the connecting code. The firmware, archive,
description and notes of a release can't change once it's made, so the
endpoint ignores them if sent. `DeploymentRelease` holds which fields
each changeset casts; the controller passes the body through.

A release, under `data` (a list for `GET /releases`):

```json
{
  "number": 3,
  "firmware": { "uuid": "d9f8...", "version": "1.0.1", "platform": "rpi0", "architecture": "arm" },
  "archive_uuid": null,
  "required": false,
  "description": "Fixes the Wi-Fi reconnect loop",
  "notes": null,
  "connecting_code": null,
  "connecting_code_mode": "last",
  "delta_status": "ready",
  "is_current": true,
  "created_by": { "type": "user", "name": "Jane Doe", "email": "jane@example.com" },
  "inserted_at": "2026-11-22T10:00:00",
  "updated_at": "2026-11-22T10:00:00"
}
```

The timestamps are UTC written without an offset, as the deployment
group's are. `created_by` is `{"type": "api_key", "name": ...}` for a
release a product API key made, and `null` for releases made before
authors were recorded. `connecting_code_mode` is `first`, `last` or
`override`; `delta_status` is `ready`, `preparing` or `failed`.

422s use the usual changeset shape, for example `{"errors": {"firmware":
["The current release already has this firmware and archive"]}}`.

## A permission for releases: `deployment_group:release:manage`

Making a release used to need `deployment_group:update`, the same
permission as changing the group's settings. Everything done to a
release now needs `deployment_group:release:manage` instead, in the web
UI, Slack and the API: creating and editing releases, approving,
skipping and retrying workflow steps, and retrying or deleting deltas.
The built-in `manage` and `admin` roles have it.
`deployment_group:update` keeps the group's Settings tab.

`PUT /deployments/:name` with `firmware` or `archive` makes a release,
so it needs the new permission. A `PUT` without either still needs
`deployment_group:update`.

Creating a deployment group needs `deployment_group:release:manage` as
well as `deployment_group:create`, in the web UI and in `POST
/deployments`. A new group starts with a release of the firmware it's
given, so without this a role with `deployment_group:create` and
`deployment_group:toggle` could ship firmware.

A migration gives `deployment_group:release:manage` to every custom role
that has `deployment_group:update`, so those roles keep editing and
approving releases. A role with `deployment_group:create` but not
`deployment_group:update` isn't given it, since that would let it
approve steps and send releases on every existing group. Such a role
can't create groups until an admin adds the permission. An API key with
a custom role gets the permission through that role.

## Deltas are queued after a release is saved

Queueing deltas for a new release, for one marked required, or for a
group whose delta updates were turned on, ran inside that save's own
transaction. Queueing a delta runs a transaction of its own, and when
that one failed, Ecto rolled back the outer one too. The release or
setting was never saved, and the API answered 500 (or the group's
Settings tab crashed, on a `MatchError`). It now happens after the
commit, as the web UI's comment always said it did. A delta that can't
be queued is logged and sent to Sentry, and devices that need it get the
whole firmware. An error raised while queueing is caught the same way,
so the save still answers with what it saved and the orchestrator is
still told about it.

A release that reads `ready` before its deltas are queued would be sent
as whole firmware, so a release that will queue deltas now starts
`preparing`: a new release, or one marked required, in a group that's on
with delta updates on. The recalculation after the queueing settles it,
to `ready` if it has nothing to wait for. If the node goes down between
the commit and that recalculation, the group's orchestrator settles the
release on its two-minute tick, so the worst case is a rollout held back
for a few minutes rather than for good. The tick leaves a release
changed in the last minute alone, since its deltas may still be on their
way into the queue. When a release's delta status changes, the
orchestrator reads the group again, so it picks up a release whose own
announcement never went out.

## Deploying

Saving a custom role on a node still running the old code drops
`deployment_group:release:manage`, because that node's role editor has
no checkbox for it. If roles might be edited during the rollout, run the
migration's statement again once every node is on the new code. It only
adds the permission where it's missing:

```sql
UPDATE org_roles
SET permissions = ARRAY(
  SELECT DISTINCT p FROM unnest(permissions || ARRAY['deployment_group:release:manage']) AS p ORDER BY p
)
WHERE 'deployment_group:update' = ANY(permissions)
  AND NOT 'deployment_group:release:manage' = ANY(permissions);
```

## Choices I made without Josh

- A `firmware` or `archive` UUID the product doesn't have gets a 422
`does not exist`. The `PUT` instead drops an unknown archive silently
and creates a release without it, which a new endpoint has no reason to
copy.
- Connecting code runs on devices, and editing it takes the same
permission as the rest of a release. Anyone holding
`deployment_group:release:manage` (a member, or a `manage` API key) can
set it over the API as well as in the web UI.
- `created_by` includes the user's email. Any member can already list
the organization's users with their emails. A product API key can't
reach that list, so a key now sees the email of whoever made a release
in its product. Say if that should be the name only.
- `update_deployment_release/3` takes an API key as well as a user, and
its audit entries name the key ("API key CI marked release 2 as required
...").
- `ManagedDeployments.get_deployment_release_by_number/2` is new, and
loads a release the same way `list_deployment_releases/1` loads each
release.

## Open question: should a new release keep the previous release's
archive?

The web UI's New release form starts with the current release's archive
selected, so a release made there keeps it unless someone clears it. The
API doesn't: a release made without an `archive` has none, even when the
current release has one. I left the API that way, and its `POST` docs
say so, so the CLI can pass the archive along itself. Should the API
match the form?

🤖 Generated with [Claude Code](https://claude.com/claude-code)

---------

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
Co-authored-by: U7D Bot <lars+underbot@underjord.io>
@joshk
joshk added this pull request to stack #30 November 23, 2026 23:49
The server ignored or rejected several deployment writes:

- create without --version or --tag got a 422, because create_changeset
  requires conditions. The body now always carries a conditions object.
- create --state on was ignored, because create_changeset doesn't cast
  is_active. nh now turns the group on with a PUT after creating it.
- update --firmware dropped every other flag, because the controller's
  release clause only creates a release. The settings (including
  --state off) now go first, then the release, then --state on, so a
  release never reaches devices outside the new conditions or a group
  that is being turned off.

Also adds --tag-operator and --notes to create and update, --archive and
--required to update, lets --version "" and --tag "" clear a condition,
and shows notes, tag operator, archive and whether the current release is
required.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@joshk
joshk force-pushed the fix/deployment-create-update branch from 435ba0b to 655f146 Compare November 23, 2026 23:49
@joshk
joshk merged commit 9b6574e into main Nov 23, 2026
1 check passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant