Skip to content

Add nh deployment release list, show, create and update - #29

Merged
joshk merged 2 commits into
fix/deployment-create-updatefrom
feature/deployment-release-commands
Nov 23, 2026
Merged

joshk merged 2 commits into
fix/deployment-create-updatefrom
feature/deployment-release-commands

Conversation

@joshk

@joshk joshk commented Nov 22, 2026 •

Copy link
Copy Markdown
Contributor

Stacked on #25 (fix/deployment-create-update), so this PR only shows the release work. Retarget it to main once #25 merges. The new commands need nerves-hub/nerves_hub_web#3338 on the server. That PR is open, not merged. On a server without it, the release commands fail with an upgrade message, and deployment update / firmware upload --deploy keep working as they did in #25.

What changed

nh deployment release list|show|create|update

New file cmd/deployment_release.go. All four commands support -o json.

  • list <group>: a table with number, current marker (*), firmware version and UUID, archive, required, delta status, created by and inserted at. Newest first, in the server's order.
  • show <group> <number>: every field, including description, notes, connecting code and its mode.
  • create <group> --firmware [--archive] [--required] [--description] [--notes] [--connecting-code | --connecting-code-file] [--connecting-code-mode first|last|override]
  • update <group> <number> [--required[=false]] [--connecting-code | --connecting-code-file | --clear-connecting-code] [--connecting-code-mode]

The client is in internal/api/deployment_release.go (ListDeploymentReleases, GetDeploymentRelease, CreateDeploymentRelease, UpdateDeploymentRelease). It checks input the way deployment.go does: org, product and group are required, a release number must be 1 or more, firmware is required on create, the mode must be first, last or override, and an update has to change something. The request bodies follow DeploymentReleaseController in #3338:

  • POST sends a flat body with only the fields given. The controller reads firmware and archive itself and takes @create_fields from the rest.
  • PATCH sends only required, connecting_code and connecting_code_mode (@update_fields). --clear-connecting-code sends "connecting_code": null.

Telling an old server from a missing group or release

In #3338, FallbackController returns ErrorJSON "404.json" for a group or release that doesn't exist. That's the same body Phoenix gives for a route that doesn't exist. So neither the status nor the body says why a 404 happened. Instead, only after a release endpoint returns 404, the client asks follow-up questions whose answers differ (explainReleaseNotFound):

  1. For a single release (show and update), it lists the group's releases. If that succeeds, the server has the API and the group exists, so the error is deployment group "x" has no release N.
  2. It then gets the group with GET /deployments/:name, which every server has. If that succeeds, the release route is the missing piece, and the error is ErrReleasesUnsupported: "this NervesHub server doesn't support deployment group releases; upgrade NervesHub to use them". If that also returns 404, the error is deployment group "x" not found in org/product.

Errors other than 404 (401 permission refusals, 422, 5xx) are passed on with no extra requests.

I looked at cmd/device_list_query_support.go and didn't use its pattern. That probe works because a new server answers a probe query differently (422) from an old one. Here a new server also returns 404 for a group that doesn't exist, so a probe sent up front can't settle the question. The check only runs after a 404 anyway, so there's nothing worth caching.

deployment update and firmware upload --deploy use the new endpoint

UpdateDeployment now creates its release through sendRelease. That tries POST .../releases first. If the POST returns 404 and the group is known to exist, it falls back to #25's release PUT. The group counts as known if the settings PUT in the same update succeeded, or if the archive-only path already read the group's current firmware. Otherwise the client gets the group first, and a 404 there is returned as is, with no PUT. A 422 from the POST (for example "The current release already has this firmware and archive") is returned and never triggers the fallback.

The order from #25 is unchanged: settings (including --state off), then the release, then --state on.

The POST returns a release, but deployment update prints the group, including with -o json. So after a POST the group is read back with one GET, unless the closing --state on PUT already returned it. If that read fails, the error says the release was created.

deployFirmware in cmd/firmware_upload.go now calls a new DeployFirmware client method. It uses the same POST-then-fallback logic but skips the read-back, because the upload never uses the group. Without it, a failed GET after a successful release would report that group as failed and skip the remaining --deploy groups (found in review).

Choices I made (Josh, please check)

  • The release commands never fall back to PUT. An old server has no way to list, show or patch a release. A PUT that creates a release would silently drop --description, --notes and the connecting code. So on an old server these commands fail with the upgrade message.
  • An empty connecting code is refused (for example an empty --connecting-code-file). The server would take "" as "remove it". Removing it takes --clear-connecting-code instead, so an empty file can't wipe the code by accident.
  • Nullable fields decode to "" (archive_uuid, description, notes, connecting_code), as the existing Deployment type does with archive_uuid. created_by is a pointer, so a null author stays null in -o json.
  • Archive carry-over is unchanged. A new release has no archive unless --archive is passed, in deployment release create and in deployment update --firmware alike. That matches #3338 (DeploymentReleaseCreationRequest: "Left out, the release has no archive, whatever the previous release had"), which leaves it as an open question for Josh. If the answer is that releases should keep the archive, the CLI could read the current release and pass it along.

Server code checked

I read #3338's head (f2f0c9a). I found no place where its description and its code disagree.

  • lib/nerves_hub_web/router.ex: get/post /:name/releases, plus get/patch /:name/releases/:number. There's no PUT on a release, so the client uses PATCH.
  • lib/nerves_hub_web/controllers/api/deployment_release_controller.ex: the fields each write takes, 201 on create, and get_release/2 returning 404 for a non-integer number, 0, or a number past int4.
  • lib/nerves_hub_web/controllers/api/deployment_release_json.ex: the release shape, including is_current and created_by (user with email, api_key, or nil).
  • lib/nerves_hub_web/controllers/api/schemas/deployment_release_schemas.ex: the connecting_code_mode and delta_status enums.
  • lib/nerves_hub_web/controllers/api/fallback_controller.ex and error_json.ex: the 404 body that's identical in every case.

Tests

  • internal/api/deployment_release_test.go: each method's method, path and body; decoding every field; validation; old-server detection for all four methods (no PUT ever sent); a missing release; a missing group; other errors passed through; and UpdateDeployment falling back on an old server (release only, settings plus --state on order, archive only), not falling back on a missing group or a 422, and reporting a failed read-back.
  • cmd/deployment_release_test.go: each command's table output, JSON, flags and validation; connecting code from stdin; the upgrade message from every command; and deployment update against an old server.
  • I updated the existing deployment update and firmware upload --deploy tests for the new endpoint, and added TestFirmwareUploadDeploysOnServerWithoutReleaseAPI.

go test ./..., go vet ./... and gofmt -l . are clean.

Not verified: none of this has been run against a real NervesHub with #3338. The tests use httptest fakes modelled on the server code above.

🤖 Generated with Claude Code

@joshk
joshk added this pull request to stack #30 November 23, 2026 23:49
joshk and others added 2 commits November 24, 2026 12:49
NervesHub now has endpoints for a deployment group's releases
(nerves-hub/nerves_hub_web#3338). These commands use them, and
`nh deployment update` and `nh firmware upload --deploy` create their
release with POST .../releases where the server has it, falling back to
the release PUT on older servers. The request order stays the same:
settings first, then the release, then --state on.

An older server answers every release route with the same 404 body as a
missing group or release, so a 404 is explained by asking for the group
(and, for one release, the group's release list).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
`firmware upload --deploy` went through UpdateDeployment, which reads the
group back after POST .../releases. A failed read turned a release that
had been made into an error and stopped the remaining groups. The new
DeployFirmware creates the release, with the same fallback, and stops.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@joshk
joshk force-pushed the feature/deployment-release-commands branch from 8ff3d30 to 5a1898d 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