Repository navigation
fix(pro): detach machines from Ubuntu Pro before removing the charm - #1028
Merged
Raven-182 merged 2 commits intoNov 25, 2026
Merged
Conversation
vsz-work
force-pushed
the
open-4916-pro-detach-on-disable
branch
from
November 12, 2026 13:57
38bd0e9 to
64ae196
Compare
vsz-work
marked this pull request as ready for review
November 12, 2026 14:36
vsz-work
force-pushed
the
open-4916-pro-detach-on-disable
branch
from
November 12, 2026 20:33
64ae196 to
84540fa
Compare
Raven-182
reviewed
Nov 12, 2026
Raven-182
left a comment
There was a problem hiding this comment.
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.
ahmad-can
requested changes
Nov 13, 2026
vsz-work
force-pushed
the
open-4916-pro-detach-on-disable
branch
from
November 13, 2026 13:52
84540fa to
cfd2894
Compare
vsz-work
marked this pull request as draft
November 13, 2026 13:58
vsz-work
marked this pull request as ready for review
November 13, 2026 16:18
Raven-182
reviewed
Nov 19, 2026
ahmad-can
previously approved these changes
Nov 20, 2026
|
@vsz-work please resolve the merge conflicts |
vsz-work
force-pushed
the
open-4916-pro-detach-on-disable
branch
from
November 23, 2026 12:27
cfd2894 to
eed1470
Compare
Author
|
Re-ran all relevant tests after latest changes, evidence in the PR description. |
vsz-work
force-pushed
the
open-4916-pro-detach-on-disable
branch
from
November 23, 2026 13:16
eed1470 to
6ca4412
Compare
Raven-182
previously approved these changes
Nov 23, 2026
Raven-182
left a comment
There was a problem hiding this comment.
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
freyes
requested changes
Nov 23, 2026
vsz-work
force-pushed
the
open-4916-pro-detach-on-disable
branch
from
November 24, 2026 12:12
6ca4412 to
b89ef73
Compare
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
force-pushed
the
open-4916-pro-detach-on-disable
branch
from
November 24, 2026 15:23
b89ef73 to
f5f4aa6
Compare
Raven-182
approved these changes
Nov 25, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
sunbeam disable proremoves theubuntu-proapplication, but the machines stay attached to the Pro subscription and keep using seats.sudo pro statuson a node still shows the machine as attached, and it's still listed on the Pro dashboard.Why: the charm (
ubuntu-prorev 45, and the legacyubuntu-advantageit replaces) detaches a machine only inconfig-changed, when its token goes from set to empty. Itsstop,removeandjuju-inforelation hooks do nothing. Disable is a single Terraform apply withtoken = "", and because the plan's resources usecount = 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
ubuntuprincipal onubuntu@24.04andubuntu-prorelated overjuju-info:What this PR changes:
DetachUbuntuProStep, which runs betweenTerraformInitStepand the Terraform removal:JujuHelper.set_app_config, i.e. through Juju, not Terraform. The app name and thetokenkey are the same for the legacyubuntu-advantagecharm, so this also works on clusters that were deployed with it. A Terraform-only first phase would make the provider replaceubuntu-advantagewithubuntu-probefore anything detached.DETACH_TIMEOUT) for everyubuntu-prounit to report Blocked "No token configured".sunbeam disable proor runningsudo pro detach --assume-yeson them. Re-running is safe: setting an already empty token triggers no hook, and units that already detached pass straight away.ubuntu-proisn't deployed. Other Juju errors, while checking the application or while waiting, fail the step instead of escaping as a traceback.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 raisesJujuException. It only treats "no units" as done when the subordinate isn't related, or isn't deployed.sunbeam enable pronow 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"". Runningenable prowith 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.JujuHelper.set_app_confignow always sends the values as YAML on stdin (juju config <app> --file -) instead of askey=valuearguments, 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 passingtoken=<value>would have written the token there. The two other callers ofset_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_configis fixed in a separate commit: the enable re-sync above is the first pro code to callJujuHelper.get_app_config, and it catchesJujuExceptionto fail the step. That helper had an existing bug: whenjuju configfailed for any reason other than "application not found", its error message referencedconfig_value, which is only set on success. The f-string raisedUnboundLocalError, so instead of a failed step, users would get a traceback.test_get_app_config_failshould have caught this, but it calledget_secretinstead ofget_app_config, so this path was never tested. The fix and the corrected test (which fails withUnboundLocalErrorwithout 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 proacceptsunknownapp status, which is also what anubuntu-proapp with no units reports. Enable can therefore succeed with nothing attached. I'll raise it separately.Related-bug: #2158218
QA steps
Unit tests:
New tests:
test_pro.py:TestDetachUbuntuProStepcovers the skip, a status failure inis_skip, the token emptied, the wait arguments, the app disappearing meanwhile, a config failure, a wait failure and a timeout.TestRunDisablePlanschecks the order: Init → Detach → Disable.test_juju.py:test_pending_subordinate_unitscovers 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).JujuException; an unrelated or removed subordinate has no units to wait for.test_set_app_config: the arguments are exactlyconfig <app> --file -, the token isn't in them, and stdin parses back to{<app>: config}.test_set_app_config_empty_value: disable'stoken: ""stays an empty string in the YAML.wait_subordinate_units_status.test_get_app_config_failnow callsget_app_config(it calledget_secret).On a single-node 2026.1 deployment, with a snap built from this branch and a personal Pro token:
Then check the Pro dashboard: the machine is no longer listed.
Legacy cluster: deploy from
2026.1/edge(stillubuntu-advantage), runenable pro,snap refreshto this build, rundisable pro. Expectattached: false.Failure path: with pro enabled, stop the
ubuntu-prounit agent on one node and rundisable pro. Expect a failure after the timeout that names the unit, withubuntu-prostill deployed. Start the agent again and re-run: it should succeed.Tested
ruff format,ruff checkandmypy sunbeamare clean2026.1/edgeenable → refresh → disableubuntu-pro. After the agent restarted, the machine detached and a re-run succeededjuju config --filejuju config --file -(stdin) for enable re-sync and disablesunbeam enable proargv lineSunbeam VM run (2026-11-12):
Before disable:
ubuntu-prorev 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):On the unit, the detach happens before any removal hook:
After disable,
ubuntu-prois gone fromopenstack-machines(application andjuju-inforelation):{ "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-propart ofjuju statusafter enable, the unit log detaching, andjuju status --relationsafter 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.jujud-machine-0; on Juju 3.x every unit on the node runs in it.sunbeam disable profailed after the 10 min timeout and didn't run the Terraform removal:ubuntu-prowas still deployed, the token was empty in Juju, and the machine was still attached.config-changedran and detached the machine (attached: false).sunbeam enable prowith the same token. Terraform saw no change, which is the drift case, and the re-sync set the token again:sunbeam disable prosucceeded:attached: false, andubuntu-proand its relation were removed.04-failure-path.pr.txt
Second review round run (2026-11-23): same VM, snap built from
eed14708.attachedwent fromtruetofalse.juju config ubuntu-pro token=""), the state a disable failing after the detach step leaves, then ransunbeam enable prowith 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:juju-bin, can read the snap's temporary file.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 inset_app_config.sunbeam enable pro <token>:attached: true. The live token already matched, so the only Juju config call was the read.sunbeam enable prowith 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.sunbeam disable pro: the same--file -call withtoken: "". The machine detached (attached: false) and the app was removed, so Juju sets an empty string from stdin liketoken="".After each step I checked the newest sunbeam log: no
token=on any Juju command, and the token itself only appears on the line wheresunbeam/main.pylogs thesunbeam enable pro <token>argv, which this PR doesn't change.Links
Jira card: OPEN-4916