Skip to content

Commit 7c3637a

Browse files
Fix duplicate dummy templates, and update guest os for dummy template (#12780)
* Fix duplicate dummy template 'kvm-default-vm-import-dummy-template' entries * Update guest os id of dummy template to 99 (Other Linux (64-bit)) from existing id: 1 (CentOS 4.5 (32-bit)) * update migration path to remove duplicate dummy templates
1 parent 7107d28 commit 7c3637a

File tree

5 files changed

+64
-8
lines changed

5 files changed

+64
-8
lines changed

api/src/main/java/org/apache/cloudstack/vm/UnmanagedVMsManager.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@
2626

2727
public interface UnmanagedVMsManager extends VmImportService, UnmanageVMService, PluggableService, Configurable {
2828

29+
String VM_IMPORT_DEFAULT_TEMPLATE_NAME = "system-default-vm-import-dummy-template.iso";
30+
String KVM_VM_IMPORT_DEFAULT_TEMPLATE_NAME = "kvm-default-vm-import-dummy-template";
2931
ConfigKey<Boolean> UnmanageVMPreserveNic = new ConfigKey<>("Advanced", Boolean.class, "unmanage.vm.preserve.nics", "false",
3032
"If set to true, do not remove VM nics (and its MAC addresses) when unmanaging a VM, leaving them allocated but not reserved. " +
3133
"If set to false, nics are removed and MAC addresses can be reassigned", true, ConfigKey.Scope.Zone);

engine/schema/src/main/java/com/cloud/upgrade/dao/Upgrade42200to42210.java

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,14 @@
1616
// under the License.
1717
package com.cloud.upgrade.dao;
1818

19+
import org.apache.cloudstack.vm.UnmanagedVMsManager;
20+
21+
import java.sql.Connection;
22+
import java.sql.PreparedStatement;
23+
import java.sql.ResultSet;
24+
import java.util.ArrayList;
25+
import java.util.List;
26+
1927
public class Upgrade42200to42210 extends DbUpgradeAbstractImpl implements DbUpgrade, DbUpgradeSystemVmTemplate {
2028

2129
@Override
@@ -27,4 +35,47 @@ public String[] getUpgradableVersionRange() {
2735
public String getUpgradedVersion() {
2836
return "4.22.1.0";
2937
}
38+
39+
@Override
40+
public void performDataMigration(Connection conn) {
41+
removeDuplicateKVMImportTemplates(conn);
42+
}
43+
44+
private void removeDuplicateKVMImportTemplates(Connection conn) {
45+
List<Long> templateIds = new ArrayList<>();
46+
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))) {
47+
ResultSet rs = selectStmt.executeQuery();
48+
while (rs.next()) {
49+
templateIds.add(rs.getLong(1));
50+
}
51+
52+
if (templateIds.size() <= 1) {
53+
return;
54+
}
55+
56+
logger.info("Removing duplicate template " + UnmanagedVMsManager.KVM_VM_IMPORT_DEFAULT_TEMPLATE_NAME + " entries");
57+
Long firstTemplateId = templateIds.get(0);
58+
59+
String updateTemplateSql = "UPDATE cloud.vm_instance SET vm_template_id = ? WHERE vm_template_id = ?";
60+
String deleteTemplateSql = "DELETE FROM cloud.vm_template WHERE id = ?";
61+
62+
try (PreparedStatement updateTemplateStmt = conn.prepareStatement(updateTemplateSql);
63+
PreparedStatement deleteTemplateStmt = conn.prepareStatement(deleteTemplateSql)) {
64+
for (int i = 1; i < templateIds.size(); i++) {
65+
Long duplicateTemplateId = templateIds.get(i);
66+
67+
// Update VM references
68+
updateTemplateStmt.setLong(1, firstTemplateId);
69+
updateTemplateStmt.setLong(2, duplicateTemplateId);
70+
updateTemplateStmt.executeUpdate();
71+
72+
// Delete duplicate dummy template
73+
deleteTemplateStmt.setLong(1, duplicateTemplateId);
74+
deleteTemplateStmt.executeUpdate();
75+
}
76+
}
77+
} catch (Exception e) {
78+
logger.warn("Failed to remove duplicate template " + UnmanagedVMsManager.KVM_VM_IMPORT_DEFAULT_TEMPLATE_NAME + " entries", e);
79+
}
80+
}
3081
}

engine/schema/src/main/resources/META-INF/db/schema-42200to42210.sql

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,3 +33,5 @@ UPDATE `cloud`.`alert` SET type = 34 WHERE name = 'ALERT.VR.PRIVATE.IFACE.MTU';
3333

3434
-- Update configuration 'kvm.ssh.to.agent' description and is_dynamic fields
3535
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';
36+
37+
UPDATE `cloud`.`vm_template` SET guest_os_id = 99 WHERE name = 'kvm-default-vm-import-dummy-template';

engine/storage/datamotion/src/main/java/org/apache/cloudstack/storage/motion/StorageSystemDataMotionStrategy.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -148,8 +148,8 @@
148148
import java.util.stream.Collectors;
149149
import org.apache.commons.collections.CollectionUtils;
150150

151-
import static org.apache.cloudstack.vm.UnmanagedVMsManagerImpl.KVM_VM_IMPORT_DEFAULT_TEMPLATE_NAME;
152-
import static org.apache.cloudstack.vm.UnmanagedVMsManagerImpl.VM_IMPORT_DEFAULT_TEMPLATE_NAME;
151+
import static org.apache.cloudstack.vm.UnmanagedVMsManager.KVM_VM_IMPORT_DEFAULT_TEMPLATE_NAME;
152+
import static org.apache.cloudstack.vm.UnmanagedVMsManager.VM_IMPORT_DEFAULT_TEMPLATE_NAME;
153153

154154
public class StorageSystemDataMotionStrategy implements DataMotionStrategy {
155155
protected Logger logger = LogManager.getLogger(getClass());

server/src/main/java/org/apache/cloudstack/vm/UnmanagedVMsManagerImpl.java

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -200,9 +200,8 @@
200200
import static org.apache.cloudstack.vm.ImportVmTask.Step.Importing;
201201

202202
public class UnmanagedVMsManagerImpl implements UnmanagedVMsManager {
203-
public static final String VM_IMPORT_DEFAULT_TEMPLATE_NAME = "system-default-vm-import-dummy-template.iso";
204-
public static final String KVM_VM_IMPORT_DEFAULT_TEMPLATE_NAME = "kvm-default-vm-import-dummy-template";
205203
protected Logger logger = LogManager.getLogger(UnmanagedVMsManagerImpl.class);
204+
private static final long OTHER_LINUX_64_GUEST_OS_ID = 99;
206205
private static final List<Hypervisor.HypervisorType> importUnmanagedInstancesSupportedHypervisors =
207206
Arrays.asList(Hypervisor.HypervisorType.VMware, Hypervisor.HypervisorType.KVM);
208207

@@ -325,7 +324,7 @@ private VMTemplateVO createDefaultDummyVmImportTemplate(boolean isKVM) {
325324
try {
326325
template = VMTemplateVO.createSystemIso(templateDao.getNextInSequence(Long.class, "id"), templateName, templateName, true,
327326
"", true, 64, Account.ACCOUNT_ID_SYSTEM, "",
328-
"VM Import Default Template", false, 1);
327+
"VM Import Default Template", false, OTHER_LINUX_64_GUEST_OS_ID);
329328
template.setState(VirtualMachineTemplate.State.Inactive);
330329
template = templateDao.persist(template);
331330
if (template == null) {
@@ -1487,11 +1486,13 @@ private ServiceOfferingVO getServiceOfferingForImportInstance(Long serviceOfferi
14871486
protected VMTemplateVO getTemplateForImportInstance(Long templateId, Hypervisor.HypervisorType hypervisorType) {
14881487
VMTemplateVO template;
14891488
if (templateId == null) {
1490-
template = templateDao.findByName(VM_IMPORT_DEFAULT_TEMPLATE_NAME);
1489+
boolean isKVMHypervisor = Hypervisor.HypervisorType.KVM.equals(hypervisorType);
1490+
String templateName = (isKVMHypervisor) ? KVM_VM_IMPORT_DEFAULT_TEMPLATE_NAME : VM_IMPORT_DEFAULT_TEMPLATE_NAME;
1491+
template = templateDao.findByName(templateName);
14911492
if (template == null) {
1492-
template = createDefaultDummyVmImportTemplate(Hypervisor.HypervisorType.KVM == hypervisorType);
1493+
template = createDefaultDummyVmImportTemplate(isKVMHypervisor);
14931494
if (template == null) {
1494-
throw new InvalidParameterValueException(String.format("Default VM import template with unique name: %s for hypervisor: %s cannot be created. Please use templateid parameter for import", VM_IMPORT_DEFAULT_TEMPLATE_NAME, hypervisorType.toString()));
1495+
throw new InvalidParameterValueException(String.format("Default VM import template with unique name: %s for hypervisor: %s cannot be created. Please use templateid parameter for import", templateName, hypervisorType.toString()));
14951496
}
14961497
}
14971498
} else {

0 commit comments

Comments
 (0)