-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Fix duplicate dummy templates, and update guest os for dummy template #12780
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
base: 4.22
Are you sure you want to change the base?
Changes from all commits
cb9cc53
08cf2be
5d88f30
a02e455
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -16,6 +16,14 @@ | |||||
| // under the License. | ||||||
| package com.cloud.upgrade.dao; | ||||||
|
|
||||||
| import org.apache.cloudstack.vm.UnmanagedVMsManager; | ||||||
|
|
||||||
| import java.sql.Connection; | ||||||
| import java.sql.PreparedStatement; | ||||||
| import java.sql.ResultSet; | ||||||
| import java.util.ArrayList; | ||||||
| import java.util.List; | ||||||
|
|
||||||
| public class Upgrade42200to42210 extends DbUpgradeAbstractImpl implements DbUpgrade, DbUpgradeSystemVmTemplate { | ||||||
|
|
||||||
| @Override | ||||||
|
|
@@ -27,4 +35,46 @@ public String[] getUpgradableVersionRange() { | |||||
| public String getUpgradedVersion() { | ||||||
| return "4.22.1.0"; | ||||||
| } | ||||||
|
|
||||||
| @Override | ||||||
| public void performDataMigration(Connection conn) { | ||||||
| removeDuplicateDummyTemplates(conn); | ||||||
| } | ||||||
|
|
||||||
| private void removeDuplicateDummyTemplates(Connection conn) { | ||||||
| List<Long> templateIds = new ArrayList<>(); | ||||||
| try (PreparedStatement selectStmt = conn.prepareStatement(String.format("SELECT id FROM cloud.vm_template WHERE name='%s' ORDER BY id ASC", UnmanagedVMsManager.KVM_VM_IMPORT_DEFAULT_TEMPLATE_NAME))) { | ||||||
| ResultSet rs = selectStmt.executeQuery(); | ||||||
| while (rs.next()) { | ||||||
| templateIds.add(rs.getLong(1)); | ||||||
| } | ||||||
|
|
||||||
| if (templateIds.size() <= 1) { | ||||||
| return; | ||||||
| } | ||||||
|
|
||||||
| Long firstTemplateId = templateIds.get(0); | ||||||
|
|
||||||
| String updateTemplateSql = "UPDATE cloud.vm_instance SET vm_template_id = ? WHERE vm_template_id = ?"; | ||||||
| String deleteTemplateSql = "DELETE FROM cloud.vm_template WHERE id = ?"; | ||||||
|
|
||||||
| try (PreparedStatement updateTemplateStmt = conn.prepareStatement(updateTemplateSql); | ||||||
| PreparedStatement deleteTemplateStmt = conn.prepareStatement(deleteTemplateSql)) { | ||||||
| for (int i = 1; i < templateIds.size(); i++) { | ||||||
| Long duplicateTemplateId = templateIds.get(i); | ||||||
|
|
||||||
| // Update VM references | ||||||
| updateTemplateStmt.setLong(1, firstTemplateId); | ||||||
| updateTemplateStmt.setLong(2, duplicateTemplateId); | ||||||
| updateTemplateStmt.executeUpdate(); | ||||||
|
|
||||||
| // Delete duplicate dummy template | ||||||
| deleteTemplateStmt.setLong(1, duplicateTemplateId); | ||||||
| deleteTemplateStmt.executeUpdate(); | ||||||
| } | ||||||
| } | ||||||
| } catch (Exception e) { | ||||||
| logger.warn("Failed to clean duplicate kvm-default-vm-import-dummy-template entries", e); | ||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
| } | ||||||
| } | ||||||
| } | ||||||
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -33,3 +33,5 @@ UPDATE `cloud`.`alert` SET type = 34 WHERE name = 'ALERT.VR.PRIVATE.IFACE.MTU'; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| -- Update configuration 'kvm.ssh.to.agent' description and is_dynamic fields | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| UPDATE `cloud`.`configuration` SET description = 'True if the management server will restart the agent service via SSH into the KVM hosts after or during maintenance operations', is_dynamic = 1 WHERE name = 'kvm.ssh.to.agent'; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| UPDATE `cloud`.`vm_template` SET guest_os_id = 99 WHERE name = 'kvm-default-vm-import-dummy-template'; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| UPDATE `cloud`.`vm_template` SET guest_os_id = 99 WHERE name = 'kvm-default-vm-import-dummy-template'; | |
| UPDATE `cloud`.`vm_template` | |
| SET guest_os_id = 99 | |
| WHERE name = 'kvm-default-vm-import-dummy-template' | |
| AND type = 'SYSTEM' | |
| AND format = 'ISO'; |
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.
fine with name
Copilot
AI
Mar 10, 2026
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.
The PR description shows many duplicate rows for kvm-default-vm-import-dummy-template. This migration updates guest_os_id but does not address the existing duplicates, so the system can continue operating with ambiguous data (and findByName may pick an arbitrary row depending on query ordering). Consider adding a migration step to de-duplicate (e.g., keep a single canonical row and mark others removed/unused) to make behavior deterministic and align with the PR’s stated goal of fixing duplicates.
| UPDATE `cloud`.`vm_template` SET guest_os_id = 99 WHERE name = 'kvm-default-vm-import-dummy-template'; | |
| -- De-duplicate 'kvm-default-vm-import-dummy-template' entries and update guest_os_id | |
| -- Keep the template with the smallest id as the canonical one and mark others as removed | |
| UPDATE `cloud`.`vm_template` t | |
| JOIN ( | |
| SELECT MIN(id) AS keep_id | |
| FROM `cloud`.`vm_template` | |
| WHERE name = 'kvm-default-vm-import-dummy-template' | |
| ) k ON t.id != k.keep_id | |
| SET t.removed = NOW() | |
| WHERE t.name = 'kvm-default-vm-import-dummy-template' | |
| AND t.removed IS NULL; | |
| -- Update guest_os_id only for the canonical template | |
| UPDATE `cloud`.`vm_template` t | |
| JOIN ( | |
| SELECT MIN(id) AS keep_id | |
| FROM `cloud`.`vm_template` | |
| WHERE name = 'kvm-default-vm-import-dummy-template' | |
| AND removed IS NULL | |
| ) k ON t.id = k.keep_id | |
| SET t.guest_os_id = 99; |
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.
not needed
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.