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