|
17 | 17 |
|
18 | 18 | package com.cloud.upgrade.dao; |
19 | 19 |
|
20 | | -import com.cloud.utils.exception.CloudRuntimeException; |
21 | | -import com.cloud.utils.script.Script; |
22 | | -import org.apache.log4j.Logger; |
23 | | - |
24 | 20 | import java.io.File; |
25 | 21 | import java.sql.Connection; |
| 22 | +import java.sql.PreparedStatement; |
| 23 | +import java.sql.ResultSet; |
| 24 | +import java.sql.SQLException; |
| 25 | +import java.util.HashMap; |
| 26 | +import java.util.HashSet; |
| 27 | +import java.util.Map; |
| 28 | +import java.util.Set; |
| 29 | + |
| 30 | +import org.apache.log4j.Logger; |
| 31 | + |
| 32 | +import com.cloud.hypervisor.Hypervisor; |
| 33 | +import com.cloud.utils.exception.CloudRuntimeException; |
| 34 | +import com.cloud.utils.script.Script; |
26 | 35 |
|
27 | 36 | public class Upgrade41000to41100 implements DbUpgrade { |
28 | 37 | final static Logger LOG = Logger.getLogger(Upgrade41000to41100.class); |
@@ -53,8 +62,181 @@ public File[] getPrepareScripts() { |
53 | 62 |
|
54 | 63 | @Override |
55 | 64 | public void performDataMigration(Connection conn) { |
| 65 | + updateSystemVmTemplates(conn); |
| 66 | + } |
| 67 | + |
| 68 | + @SuppressWarnings("serial") |
| 69 | + private void updateSystemVmTemplates(final Connection conn) { |
| 70 | + LOG.debug("Updating System Vm template IDs"); |
| 71 | + // Get all hypervisors in use |
| 72 | + final Set<Hypervisor.HypervisorType> hypervisorsListInUse = new HashSet<Hypervisor.HypervisorType>(); |
| 73 | + try (PreparedStatement pstmt = conn.prepareStatement("select distinct(hypervisor_type) from `cloud`.`cluster` where removed is null"); ResultSet rs = pstmt.executeQuery()) { |
| 74 | + while (rs.next()) { |
| 75 | + switch (Hypervisor.HypervisorType.getType(rs.getString(1))) { |
| 76 | + case XenServer: |
| 77 | + hypervisorsListInUse.add(Hypervisor.HypervisorType.XenServer); |
| 78 | + break; |
| 79 | + case KVM: |
| 80 | + hypervisorsListInUse.add(Hypervisor.HypervisorType.KVM); |
| 81 | + break; |
| 82 | + case VMware: |
| 83 | + hypervisorsListInUse.add(Hypervisor.HypervisorType.VMware); |
| 84 | + break; |
| 85 | + case Hyperv: |
| 86 | + hypervisorsListInUse.add(Hypervisor.HypervisorType.Hyperv); |
| 87 | + break; |
| 88 | + case LXC: |
| 89 | + hypervisorsListInUse.add(Hypervisor.HypervisorType.LXC); |
| 90 | + break; |
| 91 | + case Ovm3: |
| 92 | + hypervisorsListInUse.add(Hypervisor.HypervisorType.Ovm3); |
| 93 | + break; |
| 94 | + default: |
| 95 | + break; |
| 96 | + } |
| 97 | + } |
| 98 | + } catch (final SQLException e) { |
| 99 | + LOG.error("updateSystemVmTemplates: Exception caught while getting hypervisor types from clusters: " + e.getMessage()); |
| 100 | + throw new CloudRuntimeException("updateSystemVmTemplates:Exception while getting hypervisor types from clusters", e); |
| 101 | + } |
| 102 | + |
| 103 | + final Map<Hypervisor.HypervisorType, String> NewTemplateNameList = new HashMap<Hypervisor.HypervisorType, String>() { |
| 104 | + { |
| 105 | + put(Hypervisor.HypervisorType.KVM, "systemvm-kvm-4.11"); |
| 106 | + put(Hypervisor.HypervisorType.VMware, "systemvm-vmware-4.11"); |
| 107 | + put(Hypervisor.HypervisorType.XenServer, "systemvm-xenserver-4.11"); |
| 108 | + put(Hypervisor.HypervisorType.Hyperv, "systemvm-hyperv-4.11"); |
| 109 | + put(Hypervisor.HypervisorType.LXC, "systemvm-lxc-4.11"); |
| 110 | + put(Hypervisor.HypervisorType.Ovm3, "systemvm-ovm3-4.11"); |
| 111 | + } |
| 112 | + }; |
| 113 | + |
| 114 | + final Map<Hypervisor.HypervisorType, String> routerTemplateConfigurationNames = new HashMap<Hypervisor.HypervisorType, String>() { |
| 115 | + { |
| 116 | + put(Hypervisor.HypervisorType.KVM, "router.template.kvm"); |
| 117 | + put(Hypervisor.HypervisorType.VMware, "router.template.vmware"); |
| 118 | + put(Hypervisor.HypervisorType.XenServer, "router.template.xenserver"); |
| 119 | + put(Hypervisor.HypervisorType.Hyperv, "router.template.hyperv"); |
| 120 | + put(Hypervisor.HypervisorType.LXC, "router.template.lxc"); |
| 121 | + put(Hypervisor.HypervisorType.Ovm3, "router.template.ovm3"); |
| 122 | + } |
| 123 | + }; |
| 124 | + |
| 125 | + final Map<Hypervisor.HypervisorType, String> newTemplateUrl = new HashMap<Hypervisor.HypervisorType, String>() { |
| 126 | + { |
| 127 | + // FIXME: upload templates |
| 128 | + put(Hypervisor.HypervisorType.KVM, "https://download.cloudstack.org/systemvm/4.11/systemvm64template-4.11.0-kvm.qcow2.bz2"); |
| 129 | + put(Hypervisor.HypervisorType.VMware, "https://download.cloudstack.org/systemvm/4.11/systemvm64template-4.11.0-vmware.ova"); |
| 130 | + put(Hypervisor.HypervisorType.XenServer, "https://download.cloudstack.org/systemvm/4.11/systemvm64template-4.11.0-xen.vhd.bz2"); |
| 131 | + put(Hypervisor.HypervisorType.Hyperv, "https://download.cloudstack.org/systemvm/4.11/systemvm64template-4.11.0-hyperv.vhd.zip"); |
| 132 | + put(Hypervisor.HypervisorType.LXC, "https://download.cloudstack.org/systemvm/4.11/systemvm64template-4.11.0-kvm.qcow2.bz2"); |
| 133 | + put(Hypervisor.HypervisorType.Ovm3, "https://download.cloudstack.org/systemvm/4.11/systemvm64template-4.11.0-ovm.raw.bz2"); |
| 134 | + } |
| 135 | + }; |
| 136 | + |
| 137 | + final Map<Hypervisor.HypervisorType, String> newTemplateChecksum = new HashMap<Hypervisor.HypervisorType, String>() { |
| 138 | + { |
| 139 | + // FIXME: update checksums? |
| 140 | + put(Hypervisor.HypervisorType.KVM, "bc2eac46f16a2ece6c19d4b89db41de3"); |
| 141 | + put(Hypervisor.HypervisorType.XenServer, "908c28a8d4c232f960e0f84af7f86c80"); |
| 142 | + put(Hypervisor.HypervisorType.VMware, "970bfb070a80bd74820881d8149643c1"); |
| 143 | + put(Hypervisor.HypervisorType.Hyperv, "0adb35bd9f92e80d3fc63fcdd9bb55e5"); |
| 144 | + put(Hypervisor.HypervisorType.LXC, "bc2eac46f16a2ece6c19d4b89db41de3"); |
| 145 | + put(Hypervisor.HypervisorType.Ovm3, "94a41f0a5361933813bb34a51df56f56"); |
| 146 | + } |
| 147 | + }; |
| 148 | + |
| 149 | + for (final Map.Entry<Hypervisor.HypervisorType, String> hypervisorAndTemplateName : NewTemplateNameList.entrySet()) { |
| 150 | + LOG.debug("Updating " + hypervisorAndTemplateName.getKey() + " System Vms"); |
| 151 | + try (PreparedStatement pstmt = conn.prepareStatement("select id from `cloud`.`vm_template` where name = ? and removed is null order by id desc limit 1")) { |
| 152 | + // Get 4.11.0 systemvm template id for corresponding hypervisor |
| 153 | + long templateId = -1; |
| 154 | + pstmt.setString(1, hypervisorAndTemplateName.getValue()); |
| 155 | + try (ResultSet rs = pstmt.executeQuery()) { |
| 156 | + if (rs.next()) { |
| 157 | + templateId = rs.getLong(1); |
| 158 | + } |
| 159 | + } catch (final SQLException e) { |
| 160 | + LOG.error("updateSystemVmTemplates: Exception caught while getting ids of templates: " + e.getMessage()); |
| 161 | + throw new CloudRuntimeException("updateSystemVmTemplates: Exception caught while getting ids of templates", e); |
| 162 | + } |
| 163 | + |
| 164 | + // change template type to SYSTEM |
| 165 | + if (templateId != -1) { |
| 166 | + try (PreparedStatement templ_type_pstmt = conn.prepareStatement("update `cloud`.`vm_template` set type='SYSTEM' where id = ?");) { |
| 167 | + templ_type_pstmt.setLong(1, templateId); |
| 168 | + templ_type_pstmt.executeUpdate(); |
| 169 | + } catch (final SQLException e) { |
| 170 | + LOG.error("updateSystemVmTemplates:Exception while updating template with id " + templateId + " to be marked as 'system': " + e.getMessage()); |
| 171 | + throw new CloudRuntimeException("updateSystemVmTemplates:Exception while updating template with id " + templateId + " to be marked as 'system'", e); |
| 172 | + } |
| 173 | + // update template ID of system Vms |
| 174 | + try (PreparedStatement update_templ_id_pstmt = conn |
| 175 | + .prepareStatement("update `cloud`.`vm_instance` set vm_template_id = ? where type <> 'User' and hypervisor_type = ?");) { |
| 176 | + update_templ_id_pstmt.setLong(1, templateId); |
| 177 | + update_templ_id_pstmt.setString(2, hypervisorAndTemplateName.getKey().toString()); |
| 178 | + update_templ_id_pstmt.executeUpdate(); |
| 179 | + } catch (final Exception e) { |
| 180 | + LOG.error("updateSystemVmTemplates:Exception while setting template for " + hypervisorAndTemplateName.getKey().toString() + " to " + templateId |
| 181 | + + ": " + e.getMessage()); |
| 182 | + throw new CloudRuntimeException("updateSystemVmTemplates:Exception while setting template for " + hypervisorAndTemplateName.getKey().toString() + " to " |
| 183 | + + templateId, e); |
| 184 | + } |
| 185 | + |
| 186 | + // Change value of global configuration parameter |
| 187 | + // router.template.* for the corresponding hypervisor |
| 188 | + try (PreparedStatement update_pstmt = conn.prepareStatement("UPDATE `cloud`.`configuration` SET value = ? WHERE name = ?");) { |
| 189 | + update_pstmt.setString(1, hypervisorAndTemplateName.getValue()); |
| 190 | + update_pstmt.setString(2, routerTemplateConfigurationNames.get(hypervisorAndTemplateName.getKey())); |
| 191 | + update_pstmt.executeUpdate(); |
| 192 | + } catch (final SQLException e) { |
| 193 | + LOG.error("updateSystemVmTemplates:Exception while setting " + routerTemplateConfigurationNames.get(hypervisorAndTemplateName.getKey()) + " to " |
| 194 | + + hypervisorAndTemplateName.getValue() + ": " + e.getMessage()); |
| 195 | + throw new CloudRuntimeException("updateSystemVmTemplates:Exception while setting " |
| 196 | + + routerTemplateConfigurationNames.get(hypervisorAndTemplateName.getKey()) + " to " + hypervisorAndTemplateName.getValue(), e); |
| 197 | + } |
| 198 | + |
| 199 | + // Change value of global configuration parameter |
| 200 | + // minreq.sysvmtemplate.version for the ACS version |
| 201 | + try (PreparedStatement update_pstmt = conn.prepareStatement("UPDATE `cloud`.`configuration` SET value = ? WHERE name = ?");) { |
| 202 | + update_pstmt.setString(1, "4.11.0"); |
| 203 | + update_pstmt.setString(2, "minreq.sysvmtemplate.version"); |
| 204 | + update_pstmt.executeUpdate(); |
| 205 | + } catch (final SQLException e) { |
| 206 | + LOG.error("updateSystemVmTemplates:Exception while setting 'minreq.sysvmtemplate.version' to 4.11.0: " + e.getMessage()); |
| 207 | + throw new CloudRuntimeException("updateSystemVmTemplates:Exception while setting 'minreq.sysvmtemplate.version' to 4.11.0", e); |
| 208 | + } |
| 209 | + } else { |
| 210 | + if (hypervisorsListInUse.contains(hypervisorAndTemplateName.getKey())) { |
| 211 | + throw new CloudRuntimeException(getUpgradedVersion() + hypervisorAndTemplateName.getKey() + " SystemVm template not found. Cannot upgrade system Vms"); |
| 212 | + } else { |
| 213 | + LOG.warn(getUpgradedVersion() + hypervisorAndTemplateName.getKey() + " SystemVm template not found. " + hypervisorAndTemplateName.getKey() |
| 214 | + + " hypervisor is not used, so not failing upgrade"); |
| 215 | + // Update the latest template URLs for corresponding |
| 216 | + // hypervisor |
| 217 | + try (PreparedStatement update_templ_url_pstmt = conn |
| 218 | + .prepareStatement("UPDATE `cloud`.`vm_template` SET url = ? , checksum = ? WHERE hypervisor_type = ? AND type = 'SYSTEM' AND removed is null order by id desc limit 1");) { |
| 219 | + update_templ_url_pstmt.setString(1, newTemplateUrl.get(hypervisorAndTemplateName.getKey())); |
| 220 | + update_templ_url_pstmt.setString(2, newTemplateChecksum.get(hypervisorAndTemplateName.getKey())); |
| 221 | + update_templ_url_pstmt.setString(3, hypervisorAndTemplateName.getKey().toString()); |
| 222 | + update_templ_url_pstmt.executeUpdate(); |
| 223 | + } catch (final SQLException e) { |
| 224 | + LOG.error("updateSystemVmTemplates:Exception while updating 'url' and 'checksum' for hypervisor type " |
| 225 | + + hypervisorAndTemplateName.getKey().toString() + ": " + e.getMessage()); |
| 226 | + throw new CloudRuntimeException("updateSystemVmTemplates:Exception while updating 'url' and 'checksum' for hypervisor type " |
| 227 | + + hypervisorAndTemplateName.getKey().toString(), e); |
| 228 | + } |
| 229 | + } |
| 230 | + } |
| 231 | + } catch (final SQLException e) { |
| 232 | + LOG.error("updateSystemVmTemplates:Exception while getting ids of templates: " + e.getMessage()); |
| 233 | + throw new CloudRuntimeException("updateSystemVmTemplates:Exception while getting ids of templates", e); |
| 234 | + } |
| 235 | + } |
| 236 | + LOG.debug("Updating System Vm Template IDs Complete"); |
56 | 237 | } |
57 | 238 |
|
| 239 | + |
58 | 240 | @Override |
59 | 241 | public File[] getCleanupScripts() { |
60 | 242 | String script = Script.findScript("", "db/schema-41000to41100-cleanup.sql"); |
|
0 commit comments