Skip to content

fix(pro): detach machines from Ubuntu Pro before removing the charm - #1028

Merged
Raven-182 merged 2 commits into
canonical:mainfrom
vsz-work:open-4916-pro-detach-on-disable
Nov 25, 2026
Merged

Raven-182 merged 2 commits into
canonical:mainfrom
vsz-work:open-4916-pro-detach-on-disable

Conversation

@vsz-work

@vsz-work vsz-work commented Nov 11, 2026 •

Copy link
Copy Markdown

sunbeam disable pro removes the ubuntu-pro application, but the machines stay attached to the Pro subscription and keep using seats. sudo pro status on a node still shows the machine as attached, and it's still listed on the Pro dashboard.

Why: the charm (ubuntu-pro rev 45, and the legacy ubuntu-advantage it replaces) detaches a machine only in config-changed, when its token goes from set to empty. Its stop, remove and juju-info relation hooks do nothing. Disable is a single Terraform apply with token = "", and because the plan's resources use count = var.token != "" ? 1 : 0, that apply removes the app straight away. No hook ever detaches.

I confirmed this outside Sunbeam, on Juju 3.6.29 / LXD, with a ubuntu principal on ubuntu@24.04 and ubuntu-pro related over juju-info:

Operation Machine after
Empty the token detached in ~20 s; unit Blocked "No token configured"
Remove the relation while attached still attached
Remove the application while attached (what disable does today) still attached
Empty the token, wait for Blocked, then remove the application detached

What this PR changes:

  • New DetachUbuntuProStep, which runs between TerraformInitStep and the Terraform removal:
    • Empties the token with JujuHelper.set_app_config, i.e. through Juju, not Terraform. The app name and the token key are the same for the legacy ubuntu-advantage charm, so this also works on clusters that were deployed with it. A Terraform-only first phase would make the provider replace ubuntu-advantage with ubuntu-pro before anything detached.
    • Waits up to 10 min (DETACH_TIMEOUT) for every ubuntu-pro unit to report Blocked "No token configured".
    • If a unit doesn't get there, disable fails and keeps the application. The error names the units and their last status, and suggests re-running sunbeam disable pro or running sudo pro detach --assume-yes on them. Re-running is safe: setting an already empty token triggers no hook, and units that already detached pass straight away.
    • Is skipped when ubuntu-pro isn't deployed. Other Juju errors, while checking the application or while waiting, fail the step instead of escaping as a traceback.
  • New JujuHelper.wait_subordinate_units_status. The existing waits only check a subordinate's application status, which turns Blocked as soon as one unit does, so they can't tell when every machine has detached. The new method checks each subordinate unit under its principal's units. While the subordinate is related to the principal, every principal unit must have one: a principal unit without it is reported as pending (sunbeam-machine/1 (no ubuntu-pro unit)), so the removal can't run while a machine may still be attached. If the principal application is missing, it raises JujuException. It only treats "no units" as done when the subordinate isn't related, or isn't deployed.
  • sunbeam enable pro now sets the token again if a failed disable left it empty. If disable fails after the detach step, the token is empty in Juju but Terraform state still holds the old one: the provider only reads back config values that differ from the charm default, and the token's default is "". Running enable pro with the same token would then plan no change and leave the machines detached. After the apply, enable reads the live token. If it differs, enable sets it through Juju and waits up to 5 min for the units to become Active.
  • This PR doesn't log the token or put it on a command line. JujuHelper.set_app_config now always sends the values as YAML on stdin (juju config <app> --file -) instead of as key=value arguments, so they don't show in the process list. This also keeps them out of the sunbeam log: jubilant logs every Juju command with its full arguments (cli: juju ...), but not its stdin, so passing token=<value> would have written the token there. The two other callers of set_app_config, the MySQL refresh (pause-after-unit-refresh) and the upgrade re-apply of manifest config, use the same path now; both pass plain values.

Why get_app_config is fixed in a separate commit: the enable re-sync above is the first pro code to call JujuHelper.get_app_config, and it catches JujuException to fail the step. That helper had an existing bug: when juju config failed for any reason other than "application not found", its error message referenced config_value, which is only set on success. The f-string raised UnboundLocalError, so instead of a failed step, users would get a traceback. test_get_app_config_fail should have caught this, but it called get_secret instead of get_app_config, so this path was never tested. The fix and the corrected test (which fails with UnboundLocalError without the fix) are in their own commit because the bug isn't specific to pro, and so it can be reviewed or cherry-picked on its own.

Not in scope, but found along the way: sunbeam enable pro accepts unknown app status, which is also what an ubuntu-pro app with no units reports. Enable can therefore succeed with nothing attached. I'll raise it separately.

Related-bug: #2158218

QA steps

Unit tests:

cd sunbeam-python
tox -e py3 -- tests/unit/sunbeam/features/test_pro.py tests/unit/sunbeam/core/test_juju.py
tox -e pep8 && tox -e mypy

New tests:

  • test_pro.py:
    • TestDetachUbuntuProStep covers the skip, a status failure in is_skip, the token emptied, the wait arguments, the app disappearing meanwhile, a config failure, a wait failure and a timeout.
    • TestRunDisablePlans checks the order: Init → Detach → Disable.
    • The enable token re-sync: the live token matches (nothing set), the live token is empty (set again through a file, and waited for), the wait times out, and reading the config fails.
  • test_juju.py:
    • test_pending_subordinate_units covers all units detached, one still attached, one Blocked for another reason, other subordinates on the same machine, and a principal unit without the subordinate (pending).
    • A missing principal application raises JujuException; an unrelated or removed subordinate has no units to wait for.
    • test_set_app_config: the arguments are exactly config <app> --file -, the token isn't in them, and stdin parses back to {<app>: config}.
    • test_set_app_config_empty_value: disable's token: "" stays an empty string in the YAML.
    • The ready and timeout cases of wait_subordinate_units_status.
    • test_get_app_config_fail now calls get_app_config (it called get_secret).

On a single-node 2026.1 deployment, with a snap built from this branch and a personal Pro token:

sunbeam enable pro <token>
sudo pro status --format json | jq .attached          # true

sunbeam disable pro
sudo pro status --format json | jq .attached          # false
juju status -m admin/openstack-machines ubuntu-pro          # application not found

Then check the Pro dashboard: the machine is no longer listed.

Legacy cluster: deploy from 2026.1/edge (still ubuntu-advantage), run enable pro, snap refresh to this build, run disable pro. Expect attached: false.

Failure path: with pro enabled, stop the ubuntu-pro unit agent on one node and run disable pro. Expect a failure after the timeout that names the unit, with ubuntu-pro still deployed. Start the agent again and re-run: it should succeed.

Tested

Check Result
Unit suite 4738 passed. ruff format, ruff check and mypy sunbeam are clean
Charm lifecycle, LXD lab (outside Sunbeam) As in the table above
Sunbeam VM, new charm: enable → disable The machine detached before the app was removed
Sunbeam VM, legacy charm: 2026.1/edge enable → refresh → disable The machine detached before the app was removed
Failure path: disable with the node's Juju agent stopped Disable failed after 600 s naming the unit, and kept ubuntu-pro. After the agent restarted, the machine detached and a re-run succeeded
Enable after a failed disable, same token (re-sync) Terraform planned no change (drift confirmed). The re-sync set the token again and the machine reattached
Second review round: stricter subordinate wait, healthy node Enable → disable succeeded; the machine detached before the app was removed
Second review round: re-sync through juju config --file Works inside the snap: the token was set from the temp file and the machine reattached. No token in the Juju command line
Second review round: disable, agent stopped Failed after 600 s naming the unit, app kept
Third review round: juju config --file - (stdin) for enable re-sync and disable Re-sync reattached the machine and disable detached it, both through stdin. No token in the sunbeam logs outside the sunbeam enable pro argv line

Sunbeam VM run (2026-11-12):

Before disable: ubuntu-pro rev 45 (latest/stable), Active "Attached (esm-apps,esm-infra,livepatch)":

{ "attached": true, "machine_id_present": true, "services": ["esm-apps", "esm-infra", "livepatch"] }

sunbeam disable pro, from the sunbeam log (trimmed):

11:33:56,4   Running step 'Detach Ubuntu Pro'
11:33:56,149 Waiting for 'ubuntu-pro' units under 'sunbeam-machine' to be ['blocked'] with message ['No token configured']
11:34:47,540 Finished running step 'Detach Ubuntu Pro'. Result: COMPLETED
11:34:47,542 Running step 'Disable Ubuntu Pro'
11:34:48,565 juju_integration.juju_info[0]: delete...
11:34:55,922 juju_application.ubuntu_pro[0]: delete complete (0s)
11:34:55,939 Apply complete! Resources: 0 added, 0 changed, 2 destroyed.

On the unit, the detach happens before any removal hook:

unit-ubuntu-pro-2: 11:34:08 INFO unit.ubuntu-pro/2.juju-log Detaching ubuntu-advantage subscription
unit-ubuntu-pro-2: 11:34:50 INFO juju.worker.uniter.operation ran "stop" hook
unit-ubuntu-pro-2: 11:34:50 INFO juju.worker.uniter.operation ran "remove" hook

After disable, ubuntu-pro is gone from openstack-machines (application and juju-info relation):

{ "attached": false, "machine_id_present": false, "services": [] }

The whole disable took about 1 min. The charm started detaching 12 s after the token was emptied. Most of the rest of the step is the wait, which needs several consecutive status checks 10 s apart.

Attached: the ubuntu-pro part of juju status after enable, the unit log detaching, and juju status --relations after disable.

01-pro-attached.pr.json
02-unit-detach.pr.log
03-juju-status-relations.pr.txt

Failure path run (2026-11-13): same VM, snap built from cfd2894d.

  1. With pro enabled, I stopped jujud-machine-0; on Juju 3.x every unit on the node runs in it. sunbeam disable pro failed after the 10 min timeout and didn't run the Terraform removal:
    Error: Timed out after 600 seconds waiting for ubuntu-pro units: ubuntu-pro/3 (unknown: agent lost, see 'juju show-status-log ubuntu-pro/3'). ubuntu-pro was not removed. Run 'sunbeam disable pro' again, or run 'sudo pro detach --assume-yes' on the machines listed.
    
    Afterwards ubuntu-pro was still deployed, the token was empty in Juju, and the machine was still attached.
  2. I started the agent again. The queued config-changed ran and detached the machine (attached: false).
  3. sunbeam enable pro with the same token. Terraform saw no change, which is the drift case, and the re-sync set the token again:
    15:13:07 Apply complete! Resources: 0 added, 0 changed, 0 destroyed.
    15:13:08 Waiting for 'ubuntu-pro' units under 'sunbeam-machine' to be ['active'] with message None
    15:15:03 Finished running step 'Enable Ubuntu Pro'. Result: COMPLETED
    
    The machine was attached again. Without the re-sync, this enable would have failed with "No token configured".
  4. sunbeam disable pro succeeded: attached: false, and ubuntu-pro and its relation were removed.

04-failure-path.pr.txt

Second review round run (2026-11-23): same VM, snap built from eed14708.

  1. Enable, then disable on a healthy node, with the stricter wait. The detach took 51 s, then Terraform removed the app:
    12:53:38 Running step 'Detach Ubuntu Pro'
    12:54:29 Finished running step 'Detach Ubuntu Pro'. Result: COMPLETED
    12:54:34 Finished running step 'Disable Ubuntu Pro'. Result: COMPLETED
    
    attached went from true to false.
  2. Re-sync: I emptied the token by hand (juju config ubuntu-pro token=""), the state a disable failing after the detach step leaves, then ran sunbeam enable pro with the same token. Terraform saw no change, and the re-sync set the token through a file. jubilant logs every Juju command it runs, so the sunbeam log shows the exact arguments:
    13:13:18 Apply complete! Resources: 0 added, 0 changed, 0 destroyed.
    13:13:18 jubilant INFO cli: juju config --model sunbeam-controller:admin/openstack-machines --format json ubuntu-pro
    13:13:18 jubilant INFO cli: juju config --model sunbeam-controller:admin/openstack-machines ubuntu-pro --file /tmp/tmp1ejwsnyi.yaml
    13:13:18 Waiting for 'ubuntu-pro' units under 'sunbeam-machine' to be ['active'] with message None
    13:15:00 Finished running step 'Enable Ubuntu Pro'. Result: COMPLETED
    
    The machine was attached again. This also confirms that juju, which runs inside the openstack snap through juju-bin, can read the snap's temporary file.
  3. Disable with the node's Juju agent stopped still fails cleanly and keeps the app:
    Error: Timed out after 600 seconds waiting for ubuntu-pro units: ubuntu-pro/19 (unknown: agent lost, see 'juju show-status-log ubuntu-pro/19'). ubuntu-pro was not removed. Run 'sunbeam disable pro' again, or run 'sudo pro detach --assume-yes' on the machines listed.
    

05-review-round-2.pr.txt

Third review round run (2026-11-24): same VM, snap built from b89ef73d. Following review, the temporary file used in step 2 above is replaced by stdin (juju config <app> --file -), and that is now the only path in set_app_config.

  1. sunbeam enable pro <token>: attached: true. The live token already matched, so the only Juju config call was the read.
  2. Re-sync: I emptied the token by hand, then ran sunbeam enable pro with the same token. The sunbeam log shows the token set through stdin, cli: juju config --model sunbeam-controller:admin/openstack-machines ubuntu-pro --file -, and the machine attached again.
  3. sunbeam disable pro: the same --file - call with token: "". The machine detached (attached: false) and the app was removed, so Juju sets an empty string from stdin like token="".

After each step I checked the newest sunbeam log: no token= on any Juju command, and the token itself only appears on the line where sunbeam/main.py logs the sunbeam enable pro <token> argv, which this PR doesn't change.

Links

Jira card: OPEN-4916

@vsz-work
vsz-work force-pushed the open-4916-pro-detach-on-disable branch from 38bd0e9 to 64ae196 Compare November 12, 2026 13:57
@vsz-work
vsz-work marked this pull request as ready for review November 12, 2026 14:36
@vsz-work
vsz-work force-pushed the open-4916-pro-detach-on-disable branch from 64ae196 to 84540fa Compare November 12, 2026 20:33

@Raven-182 Raven-182 left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The changes here make sense. One thought though, it would make sense for the Ubuntu Pro charm itself to handle detachment on removal. I wonder if the current behaviour is intentional. It may be worth raising an issue on ubuntu-pro-operator.

Comment thread sunbeam-python/sunbeam/core/juju.py Resolved
Comment thread sunbeam-python/sunbeam/core/juju.py Resolved
@vsz-work
vsz-work force-pushed the open-4916-pro-detach-on-disable branch from 84540fa to cfd2894 Compare November 13, 2026 13:52
@vsz-work
vsz-work marked this pull request as draft November 13, 2026 13:58
@vsz-work
vsz-work marked this pull request as ready for review November 13, 2026 16:18
@Raven-182
Raven-182 requested a review from ahmad-can November 16, 2026 22:03
@hmlanigan
hmlanigan requested a review from Raven-182 November 19, 2026 14:41

@Raven-182 Raven-182 left a comment •

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good overall, one little comment

ahmad-can
ahmad-can previously approved these changes Nov 20, 2026
Comment thread sunbeam-python/sunbeam/core/juju.py Resolved
@hmlanigan

Copy link
Copy Markdown

@vsz-work please resolve the merge conflicts

@vsz-work

vsz-work commented Nov 23, 2026 •

Copy link
Copy Markdown
Author

Re-ran all relevant tests after latest changes, evidence in the PR description.

@vsz-work
vsz-work force-pushed the open-4916-pro-detach-on-disable branch from eed1470 to 6ca4412 Compare November 23, 2026 13:16
@Raven-182
Raven-182 requested a review from ahmad-can November 23, 2026 15:42
Raven-182
Raven-182 previously approved these changes Nov 23, 2026

@Raven-182 Raven-182 left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for addressing the comments! I see we're treating a missing relation as having no subordinate units. I guess there's a possibility that machines could still be attached in that case, but I think it's ok to let cleanup proceed since we normally detach before removal. LGTM

Comment thread sunbeam-python/sunbeam/core/juju.py Resolved
Comment thread sunbeam-python/sunbeam/core/juju.py Resolved
When `juju config` failed for any reason other than the application
not being found, get_app_config built its error message from
config_value, which is only set on success. The f-string raised
UnboundLocalError instead of the intended JujuException, so callers
catching JujuException crashed.

test_get_app_config_fail set up the failing `juju config` call but then
called get_secret, so this path was never tested. Point it at
get_app_config; it fails with UnboundLocalError without the fix.

The ubuntu-pro enable step now reads the application config, and
relies on this raising JujuException.

Jira: OPEN-4916
Assisted-By: Claude Code (Claude Opus 5.5)
Related-Bug: #2158218
Signed-off-by: Vítor Zeni <vitor.zeni@bairesdev.com>
`sunbeam disable pro` removed the ubuntu-pro application, but the
machines stayed attached to the Pro subscription and kept using seats.

The charm only detaches a machine in config-changed, when its token
goes from set to empty. Its stop, remove and juju-info relation hooks
do nothing, so removing the application (which a single terraform
apply with token="" does, because the resource count depends on the
token) leaves every machine attached.

Add a DetachUbuntuProStep that runs before the terraform removal. It
empties the token through Juju and waits until every ubuntu-pro unit
reports Blocked "No token configured". The application name and the
token key are the same for the legacy ubuntu-advantage charm, so this
also works on clusters deployed with it. If a unit does not detach
within the timeout, disable fails and keeps the application, naming
the units and the manual `pro detach` fallback. Running disable again
is safe.

Add JujuHelper.wait_subordinate_units_status. The existing waits only
check a subordinate's application status, which turns blocked as soon
as one unit does, so they cannot tell when all machines have detached.
While the subordinate is related to the principal, a principal unit
without a subordinate unit counts as pending, and a missing principal
application raises JujuException, so the removal cannot run while a
machine may still be attached.

If disable fails after the detach, the token is empty in Juju but
Terraform state still holds the old one: the provider only reads back
non-default config values, and the charm's token default is "". A
later `enable pro` with the same token would then change nothing. The
enable step now compares the live token after the apply, sets it again
if it differs and waits for the units to become active.

set_app_config now always sends the values as YAML on stdin
(`juju config <app> --file -`) instead of as command-line arguments,
so the token does not show in the process list or in jubilant's
command log.

Jira: OPEN-4916
Assisted-By: Claude Code (Claude Opus 5.5)
Related-Bug: #2158218
Signed-off-by: Vítor Zeni <vitor.zeni@bairesdev.com>
@vsz-work
vsz-work force-pushed the open-4916-pro-detach-on-disable branch from b89ef73 to f5f4aa6 Compare November 24, 2026 15:23

@freyes freyes left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@Raven-182
Raven-182 merged commit 63a6749 into canonical:main Nov 25, 2026
6 checks 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.

5 participants