rhtyd commented on a change in pull request #2211: CLOUDSTACK-10013: Migrate 
systemvmtemplate to Debian9
URL: https://github.com/apache/cloudstack/pull/2211#discussion_r158234432
 
 

 ##########
 File path: engine/schema/src/com/cloud/upgrade/dao/Upgrade41000to41100.java
 ##########
 @@ -52,8 +64,181 @@ public boolean supportsRollingUpgrade() {
 
     @Override
     public void performDataMigration(Connection conn) {
+        updateSystemVmTemplates(conn);
     }
 
+    @SuppressWarnings("serial")
+    private void updateSystemVmTemplates(final Connection conn) {
+        LOG.debug("Updating System Vm template IDs");
+        // Get all hypervisors in use
+        final Set<Hypervisor.HypervisorType> hypervisorsListInUse = new 
HashSet<Hypervisor.HypervisorType>();
+        try (PreparedStatement pstmt = conn.prepareStatement("select 
distinct(hypervisor_type) from `cloud`.`cluster` where removed is null"); 
ResultSet rs = pstmt.executeQuery()) {
+            while (rs.next()) {
+                switch (Hypervisor.HypervisorType.getType(rs.getString(1))) {
+                    case XenServer:
+                        
hypervisorsListInUse.add(Hypervisor.HypervisorType.XenServer);
+                        break;
+                    case KVM:
+                        
hypervisorsListInUse.add(Hypervisor.HypervisorType.KVM);
+                        break;
+                    case VMware:
+                        
hypervisorsListInUse.add(Hypervisor.HypervisorType.VMware);
+                        break;
+                    case Hyperv:
+                        
hypervisorsListInUse.add(Hypervisor.HypervisorType.Hyperv);
+                        break;
+                    case LXC:
+                        
hypervisorsListInUse.add(Hypervisor.HypervisorType.LXC);
+                        break;
+                    case Ovm3:
+                        
hypervisorsListInUse.add(Hypervisor.HypervisorType.Ovm3);
+                        break;
+                    default:
+                        break;
+                }
+            }
+        } catch (final SQLException e) {
+            LOG.error("updateSystemVmTemplates: Exception caught while getting 
hypervisor types from clusters: " + e.getMessage());
+            throw new CloudRuntimeException("updateSystemVmTemplates:Exception 
while getting hypervisor types from clusters", e);
+        }
+
+        final Map<Hypervisor.HypervisorType, String> NewTemplateNameList = new 
HashMap<Hypervisor.HypervisorType, String>() {
+            {
+                put(Hypervisor.HypervisorType.KVM, "systemvm-kvm-4.11");
+                put(Hypervisor.HypervisorType.VMware, "systemvm-vmware-4.11");
+                put(Hypervisor.HypervisorType.XenServer, 
"systemvm-xenserver-4.11");
+                put(Hypervisor.HypervisorType.Hyperv, "systemvm-hyperv-4.11");
+                put(Hypervisor.HypervisorType.LXC, "systemvm-lxc-4.11");
+                put(Hypervisor.HypervisorType.Ovm3, "systemvm-ovm3-4.11");
+            }
+        };
+
+        final Map<Hypervisor.HypervisorType, String> 
routerTemplateConfigurationNames = new HashMap<Hypervisor.HypervisorType, 
String>() {
+            {
+                put(Hypervisor.HypervisorType.KVM, "router.template.kvm");
+                put(Hypervisor.HypervisorType.VMware, 
"router.template.vmware");
+                put(Hypervisor.HypervisorType.XenServer, 
"router.template.xenserver");
+                put(Hypervisor.HypervisorType.Hyperv, 
"router.template.hyperv");
+                put(Hypervisor.HypervisorType.LXC, "router.template.lxc");
+                put(Hypervisor.HypervisorType.Ovm3, "router.template.ovm3");
+            }
+        };
+
+        final Map<Hypervisor.HypervisorType, String> newTemplateUrl = new 
HashMap<Hypervisor.HypervisorType, String>() {
+            {
+                // FIXME: upload templates
+                put(Hypervisor.HypervisorType.KVM, 
"https://download.cloudstack.org/systemvm/4.11/systemvmtemplate-4.11.0-kvm.qcow2.bz2";);
+                put(Hypervisor.HypervisorType.VMware, 
"https://download.cloudstack.org/systemvm/4.11/systemvmtemplate-4.11.0-vmware.ova";);
+                put(Hypervisor.HypervisorType.XenServer, 
"https://download.cloudstack.org/systemvm/4.11/systemvmtemplate-4.11.0-xen.vhd.bz2";);
+                put(Hypervisor.HypervisorType.Hyperv, 
"https://download.cloudstack.org/systemvm/4.11/systemvmtemplate-4.11.0-hyperv.vhd.zip";);
+                put(Hypervisor.HypervisorType.LXC, 
"https://download.cloudstack.org/systemvm/4.11/systemvmtemplate-4.11.0-kvm.qcow2.bz2";);
+                put(Hypervisor.HypervisorType.Ovm3, 
"https://download.cloudstack.org/systemvm/4.11/systemvmtemplate-4.11.0-ovm.raw.bz2";);
+            }
+        };
+
+        final Map<Hypervisor.HypervisorType, String> newTemplateChecksum = new 
HashMap<Hypervisor.HypervisorType, String>() {
+            {
+                // FIXME: update checksums?
+                put(Hypervisor.HypervisorType.KVM, 
"bc2eac46f16a2ece6c19d4b89db41de3");
+                put(Hypervisor.HypervisorType.XenServer, 
"908c28a8d4c232f960e0f84af7f86c80");
+                put(Hypervisor.HypervisorType.VMware, 
"970bfb070a80bd74820881d8149643c1");
+                put(Hypervisor.HypervisorType.Hyperv, 
"0adb35bd9f92e80d3fc63fcdd9bb55e5");
+                put(Hypervisor.HypervisorType.LXC, 
"bc2eac46f16a2ece6c19d4b89db41de3");
+                put(Hypervisor.HypervisorType.Ovm3, 
"94a41f0a5361933813bb34a51df56f56");
+            }
+        };
+
+        for (final Map.Entry<Hypervisor.HypervisorType, String> 
hypervisorAndTemplateName : NewTemplateNameList.entrySet()) {
+            LOG.debug("Updating " + hypervisorAndTemplateName.getKey() + " 
System Vms");
+            try (PreparedStatement pstmt = conn.prepareStatement("select id 
from `cloud`.`vm_template` where name = ? and removed is null order by id desc 
limit 1")) {
+                // Get 4.11.0 systemvm template id for corresponding hypervisor
+                long templateId = -1;
+                pstmt.setString(1, hypervisorAndTemplateName.getValue());
+                try (ResultSet rs = pstmt.executeQuery()) {
+                    if (rs.next()) {
+                        templateId = rs.getLong(1);
+                    }
+                } catch (final SQLException e) {
+                    LOG.error("updateSystemVmTemplates: Exception caught while 
getting ids of templates: " + e.getMessage());
+                    throw new CloudRuntimeException("updateSystemVmTemplates: 
Exception caught while getting ids of templates", e);
+                }
+
+                // change template type to SYSTEM
+                if (templateId != -1) {
+                    try (PreparedStatement templ_type_pstmt = 
conn.prepareStatement("update `cloud`.`vm_template` set type='SYSTEM' where id 
= ?");) {
+                        templ_type_pstmt.setLong(1, templateId);
+                        templ_type_pstmt.executeUpdate();
+                    } catch (final SQLException e) {
+                        LOG.error("updateSystemVmTemplates:Exception while 
updating template with id " + templateId + " to be marked as 'system': " + 
e.getMessage());
+                        throw new 
CloudRuntimeException("updateSystemVmTemplates:Exception while updating 
template with id " + templateId + " to be marked as 'system'", e);
+                    }
+                    // update template ID of system Vms
+                    try (PreparedStatement update_templ_id_pstmt = conn
+                            .prepareStatement("update `cloud`.`vm_instance` 
set vm_template_id = ? where type <> 'User' and hypervisor_type = ?");) {
+                        update_templ_id_pstmt.setLong(1, templateId);
+                        update_templ_id_pstmt.setString(2, 
hypervisorAndTemplateName.getKey().toString());
+                        update_templ_id_pstmt.executeUpdate();
+                    } catch (final Exception e) {
+                        LOG.error("updateSystemVmTemplates:Exception while 
setting template for " + hypervisorAndTemplateName.getKey().toString() + " to " 
+ templateId
+                                + ": " + e.getMessage());
+                        throw new 
CloudRuntimeException("updateSystemVmTemplates:Exception while setting template 
for " + hypervisorAndTemplateName.getKey().toString() + " to "
+                                + templateId, e);
+                    }
 
 Review comment:
   This is part of the usual systemvmtemplate upgrade procedure. Before 
upgrading CloudStack, users are required to register the new 4.11 template and 
during upgrade, this code will update this for the new templates with the name 
such as `systemvm-xxxx-4.11` where `xxxx` is the hypervisor. So, this happens 
for those new 4.11 templates and not the default installed or previous 
templates.
   Shortly after merging of this PR, I'll update the install/upgrade docs such 
as we did for 4.10:
   
http://docs.cloudstack.apache.org/projects/cloudstack-release-notes/en/4.10/upgrade/upgrade-4.5.html#update-system-vm-templates

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
us...@infra.apache.org


With regards,
Apache Git Services

Reply via email to