-
Notifications
You must be signed in to change notification settings - Fork 0
fix(proxmox): make host registration idempotent on name #137
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
pparage
wants to merge
2
commits into
dev
Choose a base branch
from
fix/proxmox-host-seed-idempotent
base: dev
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,101 @@ | ||
| """dedupe proxmox_hosts, then enforce a unique name | ||
|
|
||
| Until now nothing stopped a second host row under an existing name, and the | ||
| deploy bundle re-POSTs the same name on every scenario run — so field | ||
| databases carry one duplicate per re-run. The duplicates are not inert: | ||
| ``deployments.target_host_id`` points at whichever one existed when the | ||
| deployment was created. | ||
|
|
||
| Collapsing keys on ``added_at``: the earliest row under a name is the one the | ||
| first deploy registered, so it is the row most likely to be referenced and the | ||
| one whose id callers may have recorded. Deployments on the later duplicates are | ||
| repointed at it before those rows are deleted, so nothing is left dangling. | ||
|
|
||
| Revision ID: 0002_proxmox_host_unique_name | ||
| Revises: 0001_v1_initial | ||
| Create Date: 2026-08-10 | ||
| """ | ||
| import logging | ||
|
|
||
| import sqlalchemy as sa | ||
| from alembic import op | ||
|
|
||
| revision = '0002_proxmox_host_unique_name' | ||
| down_revision = '0001_v1_initial' | ||
| branch_labels = None | ||
| depends_on = None | ||
|
|
||
| log = logging.getLogger("alembic.runtime.migration") | ||
|
|
||
|
|
||
| def upgrade() -> None: | ||
| conn = op.get_bind() | ||
|
|
||
| # added_at first, id as a deterministic tie-break for rows registered | ||
| # within the same clock tick. | ||
| rows = conn.execute( | ||
| sa.text("SELECT id, name FROM proxmox_hosts ORDER BY name, added_at, id") | ||
| ).fetchall() | ||
|
|
||
| groups: dict[str, list[str]] = {} | ||
| for host_id, name in rows: | ||
| groups.setdefault(name, []).append(host_id) | ||
|
|
||
| for name, ids in groups.items(): | ||
| if len(ids) == 1: | ||
| continue | ||
| keeper_id, *loser_ids = ids | ||
|
|
||
| # The id is the oldest row's, because deployments reference it. The | ||
| # connection details are the NEWEST row's: duplicates accumulated one | ||
| # per scenario re-run, so the last registration holds the credentials | ||
| # in force. Keeping the first row wholesale would resurrect a token | ||
| # that may since have been rotated away, and every deployment | ||
| # repointed at it would start failing auth the moment this ran. | ||
| conn.execute( | ||
| sa.text( | ||
| "UPDATE proxmox_hosts SET" | ||
| " api_url = latest.api_url," | ||
| " node_name = latest.node_name," | ||
| " token_ref = latest.token_ref," | ||
| " token_scope = latest.token_scope," | ||
| " default_bridge = latest.default_bridge," | ||
| " protected_vmids_override_json =" | ||
| " latest.protected_vmids_override_json," | ||
| " last_health_check_json = latest.last_health_check_json" | ||
| " FROM (SELECT * FROM proxmox_hosts WHERE id = :newest) AS latest" | ||
| " WHERE proxmox_hosts.id = :keeper" | ||
| ), | ||
| {"newest": loser_ids[-1], "keeper": keeper_id}, | ||
| ) | ||
|
|
||
| for loser_id in loser_ids: | ||
| moved = conn.execute( | ||
| sa.text( | ||
| "UPDATE deployments SET target_host_id = :keeper" | ||
| " WHERE target_host_id = :loser" | ||
| ), | ||
| {"keeper": keeper_id, "loser": loser_id}, | ||
| ).rowcount | ||
| conn.execute( | ||
| sa.text("DELETE FROM proxmox_hosts WHERE id = :loser"), | ||
| {"loser": loser_id}, | ||
| ) | ||
| log.info( | ||
| "proxmox_hosts: collapsed duplicate %r (%s) into %s, " | ||
| "repointed %d deployment(s)", | ||
| name, loser_id, keeper_id, moved, | ||
| ) | ||
| log.info( | ||
| "proxmox_hosts: %r kept id %s with the connection details from %s", | ||
| name, keeper_id, loser_ids[-1], | ||
| ) | ||
|
|
||
| with op.batch_alter_table("proxmox_hosts") as batch: | ||
| batch.create_unique_constraint("uq_proxmox_host_name", ["name"]) | ||
|
|
||
|
|
||
| def downgrade() -> None: | ||
| # Only the constraint is reversible; the collapsed rows are gone for good. | ||
| with op.batch_alter_table("proxmox_hosts") as batch: | ||
| batch.drop_constraint("uq_proxmox_host_name", type_="unique") | ||
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
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
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
Oops, something went wrong.
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
When duplicate registrations contain a rotated token or updated URL/node, sorting oldest-first and retaining that entire row permanently deletes the newest configuration while repointing every deployment to the stale credentials. This can make existing deployments and health checks fail immediately after upgrade, before another deploy bundle happens to re-register the host. Keep the oldest ID if identity preservation is required, but copy the newest duplicate's mutable host fields onto it before deleting the later rows.
Useful? React with 👍 / 👎.