From 48c8e64ae9c872d339b0fb8a2fc7fa453862fada Mon Sep 17 00:00:00 2001 From: Harikrishna Patnala Date: Sat, 10 Aug 2013 15:36:06 +0530 Subject: [PATCH] CLOUDSTACK-4123: 3.0.6 to ASF 4.2 Upgrade: On Adding VMWare Cluster to the Setup, 3.0.6 System VM Template for VMWare is downloaded instead of 4.2 Updating the new system template URLs for the existing templates during upgrade to 4.2. If new 4.2 system template is registered before upgrade then marking the old templates as removed during upgrade. --- .../cloud/storage/dao/VMTemplateDaoImpl.java | 2 +- .../cloud/upgrade/dao/Upgrade412to420.java | 30 +++++++++++++++++++ .../consoleproxy/ConsoleProxyManagerImpl.java | 15 +++------- .../cloud/resource/ResourceManagerImpl.java | 2 ++ .../SecondaryStorageManagerImpl.java | 15 +++------- 5 files changed, 41 insertions(+), 23 deletions(-) diff --git a/engine/schema/src/com/cloud/storage/dao/VMTemplateDaoImpl.java b/engine/schema/src/com/cloud/storage/dao/VMTemplateDaoImpl.java index 924322583f0..2aca203ca6d 100755 --- a/engine/schema/src/com/cloud/storage/dao/VMTemplateDaoImpl.java +++ b/engine/schema/src/com/cloud/storage/dao/VMTemplateDaoImpl.java @@ -334,6 +334,7 @@ public class VMTemplateDaoImpl extends GenericDaoBase implem tmpltTypeHyperSearch.done(); readySystemTemplateSearch = createSearchBuilder(); + readySystemTemplateSearch.and("removed", readySystemTemplateSearch.entity().getRemoved(), SearchCriteria.Op.NULL); readySystemTemplateSearch.and("templateType", readySystemTemplateSearch.entity().getTemplateType(), SearchCriteria.Op.EQ); SearchBuilder templateDownloadSearch = _templateDataStoreDao.createSearchBuilder(); templateDownloadSearch.and("downloadState", templateDownloadSearch.entity().getDownloadState(), SearchCriteria.Op.EQ); @@ -800,7 +801,6 @@ public class VMTemplateDaoImpl extends GenericDaoBase implem if (tmplts.size() > 0) { if (hypervisorType == HypervisorType.Any) { - Collections.shuffle(tmplts); return tmplts.get(0); } for (VMTemplateVO tmplt : tmplts) { diff --git a/engine/schema/src/com/cloud/upgrade/dao/Upgrade412to420.java b/engine/schema/src/com/cloud/upgrade/dao/Upgrade412to420.java index d703cc3e52c..501633a2153 100644 --- a/engine/schema/src/com/cloud/upgrade/dao/Upgrade412to420.java +++ b/engine/schema/src/com/cloud/upgrade/dao/Upgrade412to420.java @@ -301,6 +301,24 @@ public class Upgrade412to420 implements DbUpgrade { } }; + Map newTemplateUrl = new HashMap(){ + { put(HypervisorType.XenServer, "http://download.cloud.com/templates/4.2/systemvmtemplate-2013-06-12-master-xen.vhd.bz2"); + put(HypervisorType.VMware, "http://download.cloud.com/templates/4.2/systemvmtemplate-4.2-vh7.ova"); + put(HypervisorType.KVM, "http://download.cloud.com/templates/4.2/systemvmtemplate-2013-06-12-master-kvm.qcow2.bz2"); + put(HypervisorType.LXC, "http://download.cloud.com/templates/acton/acton-systemvm-02062012.qcow2.bz2"); + put(HypervisorType.Hyperv, "http://download.cloud.com/templates/4.2/systemvmtemplate-2013-06-12-master-xen.vhd.bz2"); + } + }; + + Map newTemplateChecksum = new HashMap(){ + { put(HypervisorType.XenServer, "fb1b6e032a160d86f2c28feb5add6d83"); + put(HypervisorType.VMware, "8fde62b1089e5844a9cd3b9b953f9596"); + put(HypervisorType.KVM, "6cea42b2633841648040becb588bd8f0"); + put(HypervisorType.LXC, "2755de1f9ef2ce4d6f2bee2efbb4da92"); + put(HypervisorType.Hyperv, "fb1b6e032a160d86f2c28feb5add6d83"); + } + }; + for (Map.Entry hypervisorAndTemplateName : NewTemplateNameList.entrySet()){ s_logger.debug("Updating " + hypervisorAndTemplateName.getKey() + " System Vms"); try { @@ -312,6 +330,11 @@ public class Upgrade412to420 implements DbUpgrade { long templateId = rs.getLong(1); rs.close(); pstmt.close(); + // Mark the old system templates as removed + pstmt = conn.prepareStatement("UPDATE `cloud`.`vm_template` SET removed = now() WHERE hypervisor_type = ? AND type = 'SYSTEM' AND removed is null"); + pstmt.setString(1, hypervisorAndTemplateName.getKey().toString()); + pstmt.executeUpdate(); + pstmt.close(); // change template type to SYSTEM pstmt = conn.prepareStatement("update `cloud`.`vm_template` set type='SYSTEM' where id = ?"); pstmt.setLong(1, templateId); @@ -334,6 +357,13 @@ public class Upgrade412to420 implements DbUpgrade { throw new CloudRuntimeException("4.2.0 " + hypervisorAndTemplateName.getKey() + " SystemVm template not found. Cannot upgrade system Vms"); } else { s_logger.warn("4.2.0 " + hypervisorAndTemplateName.getKey() + " SystemVm template not found. " + hypervisorAndTemplateName.getKey() + " hypervisor is not used, so not failing upgrade"); + // Update the latest template URLs for corresponding hypervisor + pstmt = conn.prepareStatement("UPDATE `cloud`.`vm_template` SET url = ? , checksum = ? WHERE hypervisor_type = ? AND type = 'SYSTEM' AND removed is null order by id desc limit 1"); + pstmt.setString(1, newTemplateUrl.get(hypervisorAndTemplateName.getKey())); + pstmt.setString(2, newTemplateChecksum.get(hypervisorAndTemplateName.getKey())); + pstmt.setString(3, hypervisorAndTemplateName.getKey().toString()); + pstmt.executeUpdate(); + pstmt.close(); } } } catch (SQLException e) { diff --git a/server/src/com/cloud/consoleproxy/ConsoleProxyManagerImpl.java b/server/src/com/cloud/consoleproxy/ConsoleProxyManagerImpl.java index a58395457b0..c5c0c0c1677 100755 --- a/server/src/com/cloud/consoleproxy/ConsoleProxyManagerImpl.java +++ b/server/src/com/cloud/consoleproxy/ConsoleProxyManagerImpl.java @@ -647,17 +647,10 @@ public class ConsoleProxyManagerImpl extends ManagerBase implements ConsoleProxy } VMTemplateVO template = null; - HypervisorType defaultHypervisor = _resourceMgr.getDefaultHypervisor(dataCenterId); - if (defaultHypervisor != HypervisorType.None) { - template = _templateDao.findSystemVMReadyTemplate(dataCenterId, defaultHypervisor); - if (template == null) { - throw new CloudRuntimeException("Not able to find the System template or not downloaded in zone " + dataCenterId + " corresponding to default System vm hypervisor " + defaultHypervisor); - } - } else { - template = _templateDao.findSystemVMReadyTemplate(dataCenterId, HypervisorType.Any); - if (template == null) { - throw new CloudRuntimeException("Not able to find the System templates or not downloaded in zone " + dataCenterId); - } + HypervisorType availableHypervisor = _resourceMgr.getAvailableHypervisor(dataCenterId); + template = _templateDao.findSystemVMReadyTemplate(dataCenterId, availableHypervisor); + if (template == null) { + throw new CloudRuntimeException("Not able to find the System templates or not downloaded in zone " + dataCenterId); } Map context = createProxyInstance(dataCenterId, template); diff --git a/server/src/com/cloud/resource/ResourceManagerImpl.java b/server/src/com/cloud/resource/ResourceManagerImpl.java index e5441af707b..d6069a4ef98 100755 --- a/server/src/com/cloud/resource/ResourceManagerImpl.java +++ b/server/src/com/cloud/resource/ResourceManagerImpl.java @@ -25,6 +25,7 @@ import java.util.Iterator; import java.util.List; import java.util.Map; import java.util.Set; +import java.util.Collections; import javax.ejb.Local; import javax.inject.Inject; @@ -1394,6 +1395,7 @@ public class ResourceManagerImpl extends ManagerBase implements ResourceManager, if (defaultHype == HypervisorType.None) { List supportedHypes = getSupportedHypervisorTypes(zoneId, false, null); if (supportedHypes.size() > 0) { + Collections.shuffle(supportedHypes); defaultHype = supportedHypes.get(0); } } diff --git a/server/src/com/cloud/storage/secondary/SecondaryStorageManagerImpl.java b/server/src/com/cloud/storage/secondary/SecondaryStorageManagerImpl.java index 17cbf5c878c..3c1165b2a5b 100755 --- a/server/src/com/cloud/storage/secondary/SecondaryStorageManagerImpl.java +++ b/server/src/com/cloud/storage/secondary/SecondaryStorageManagerImpl.java @@ -572,17 +572,10 @@ public class SecondaryStorageManagerImpl extends ManagerBase implements Secondar } VMTemplateVO template = null; - HypervisorType defaultHypervisor = _resourceMgr.getDefaultHypervisor(dataCenterId); - if (defaultHypervisor != HypervisorType.None) { - template = _templateDao.findSystemVMReadyTemplate(dataCenterId, defaultHypervisor); - if (template == null) { - throw new CloudRuntimeException("Not able to find the System template or not downloaded in zone " + dataCenterId + " corresponding to default System vm hypervisor " + defaultHypervisor); - } - } else { - template = _templateDao.findSystemVMReadyTemplate(dataCenterId, HypervisorType.Any); - if (template == null) { - throw new CloudRuntimeException("Not able to find the System templates or not downloaded in zone " + dataCenterId); - } + HypervisorType availableHypervisor = _resourceMgr.getAvailableHypervisor(dataCenterId); + template = _templateDao.findSystemVMReadyTemplate(dataCenterId, availableHypervisor); + if (template == null) { + throw new CloudRuntimeException("Not able to find the System templates or not downloaded in zone " + dataCenterId); } SecondaryStorageVmVO secStorageVm = new SecondaryStorageVmVO(id, _serviceOffering.getId(), name, template.getId(), template.getHypervisorType(), template.getGuestOSId(), dataCenterId,