Skip to content

Commit 9fe7846

Browse files
Harikrishna PatnalaKishan Kavala
authored andcommitted
CLOUDSTACK-2728: 41-42 DB upgrade: add step to upgrade system templates
1 parent 91b1571 commit 9fe7846

1 file changed

Lines changed: 204 additions & 5 deletions

File tree

engine/schema/src/com/cloud/upgrade/dao/Upgrade410to420.java

Lines changed: 204 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -112,16 +112,215 @@ private void addIndexForAlert(Connection conn) {
112112
}
113113

114114
private void updateSystemVmTemplates(Connection conn) {
115-
PreparedStatement sql = null;
115+
116+
PreparedStatement pstmt = null;
117+
ResultSet rs = null;
118+
boolean xenserver = false;
119+
boolean kvm = false;
120+
boolean VMware = false;
121+
boolean Hyperv = false;
122+
boolean LXC = false;
123+
s_logger.debug("Updating System Vm template IDs");
124+
try{
125+
//Get all hypervisors in use
126+
try {
127+
pstmt = conn.prepareStatement("select distinct(hypervisor_type) from `cloud`.`cluster` where removed is null");
128+
rs = pstmt.executeQuery();
129+
while(rs.next()){
130+
if("XenServer".equals(rs.getString(1))){
131+
xenserver = true;
132+
} else if("KVM".equals(rs.getString(1))){
133+
kvm = true;
134+
} else if("VMware".equals(rs.getString(1))){
135+
VMware = true;
136+
} else if("Hyperv".equals(rs.getString(1))) {
137+
Hyperv = true;
138+
} else if("LXC".equals(rs.getString(1))) {
139+
LXC = true;
140+
}
141+
}
142+
} catch (SQLException e) {
143+
throw new CloudRuntimeException("Error while listing hypervisors in use", e);
144+
}
145+
146+
s_logger.debug("Updating XenSever System Vms");
147+
//XenServer
148+
try {
149+
//Get 4.2.0 xenserer system Vm template Id
150+
pstmt = conn.prepareStatement("select id from `cloud`.`vm_template` where name like 'systemvm-xenserver-4.2' and removed is null order by id desc limit 1");
151+
rs = pstmt.executeQuery();
152+
if(rs.next()){
153+
long templateId = rs.getLong(1);
154+
rs.close();
155+
pstmt.close();
156+
// change template type to SYSTEM
157+
pstmt = conn.prepareStatement("update `cloud`.`vm_template` set type='SYSTEM' where id = ?");
158+
pstmt.setLong(1, templateId);
159+
pstmt.executeUpdate();
160+
pstmt.close();
161+
// update templete ID of system Vms
162+
pstmt = conn.prepareStatement("update `cloud`.`vm_instance` set vm_template_id = ? where type <> 'User' and hypervisor_type = 'XenServer'");
163+
pstmt.setLong(1, templateId);
164+
pstmt.executeUpdate();
165+
pstmt.close();
166+
} else {
167+
if (xenserver){
168+
throw new CloudRuntimeException("4.2.0 XenServer SystemVm template not found. Cannot upgrade system Vms");
169+
} else {
170+
s_logger.warn("4.2.0 XenServer SystemVm template not found. XenServer hypervisor is not used, so not failing upgrade");
171+
}
172+
}
173+
} catch (SQLException e) {
174+
throw new CloudRuntimeException("Error while updating XenServer systemVm template", e);
175+
}
176+
177+
//KVM
178+
s_logger.debug("Updating KVM System Vms");
179+
try {
180+
//Get 4.2.0 KVM system Vm template Id
181+
pstmt = conn.prepareStatement("select id from `cloud`.`vm_template` where name = 'systemvm-kvm-4.2' and removed is null order by id desc limit 1");
182+
rs = pstmt.executeQuery();
183+
if(rs.next()){
184+
long templateId = rs.getLong(1);
185+
rs.close();
186+
pstmt.close();
187+
// change template type to SYSTEM
188+
pstmt = conn.prepareStatement("update `cloud`.`vm_template` set type='SYSTEM' where id = ?");
189+
pstmt.setLong(1, templateId);
190+
pstmt.executeUpdate();
191+
pstmt.close();
192+
// update templete ID of system Vms
193+
pstmt = conn.prepareStatement("update `cloud`.`vm_instance` set vm_template_id = ? where type <> 'User' and hypervisor_type = 'KVM'");
194+
pstmt.setLong(1, templateId);
195+
pstmt.executeUpdate();
196+
pstmt.close();
197+
} else {
198+
if (kvm){
199+
throw new CloudRuntimeException("4.2.0 KVM SystemVm template not found. Cannot upgrade system Vms");
200+
} else {
201+
s_logger.warn("4.2.0 KVM SystemVm template not found. KVM hypervisor is not used, so not failing upgrade");
202+
}
203+
}
204+
} catch (SQLException e) {
205+
throw new CloudRuntimeException("Error while updating KVM systemVm template", e);
206+
}
207+
208+
//VMware
209+
s_logger.debug("Updating VMware System Vms");
210+
try {
211+
//Get 4.2.0 VMware system Vm template Id
212+
pstmt = conn.prepareStatement("select id from `cloud`.`vm_template` where name = 'systemvm-vmware-4.2' and removed is null order by id desc limit 1");
213+
rs = pstmt.executeQuery();
214+
if(rs.next()){
215+
long templateId = rs.getLong(1);
216+
rs.close();
217+
pstmt.close();
218+
// change template type to SYSTEM
219+
pstmt = conn.prepareStatement("update `cloud`.`vm_template` set type='SYSTEM' where id = ?");
220+
pstmt.setLong(1, templateId);
221+
pstmt.executeUpdate();
222+
pstmt.close();
223+
// update templete ID of system Vms
224+
pstmt = conn.prepareStatement("update `cloud`.`vm_instance` set vm_template_id = ? where type <> 'User' and hypervisor_type = 'VMware'");
225+
pstmt.setLong(1, templateId);
226+
pstmt.executeUpdate();
227+
pstmt.close();
228+
} else {
229+
if (VMware){
230+
throw new CloudRuntimeException("4.2.0 VMware SystemVm template not found. Cannot upgrade system Vms");
231+
} else {
232+
s_logger.warn("4.2.0 VMware SystemVm template not found. VMware hypervisor is not used, so not failing upgrade");
233+
}
234+
}
235+
} catch (SQLException e) {
236+
throw new CloudRuntimeException("Error while updating VMware systemVm template", e);
237+
}
238+
239+
//Hyperv
240+
s_logger.debug("Updating Hyperv System Vms");
241+
try {
242+
//Get 4.2.0 Hyperv system Vm template Id
243+
pstmt = conn.prepareStatement("select id from `cloud`.`vm_template` where name = 'systemvm-hyperv-4.2' and removed is null order by id desc limit 1");
244+
rs = pstmt.executeQuery();
245+
if(rs.next()){
246+
long templateId = rs.getLong(1);
247+
rs.close();
248+
pstmt.close();
249+
// change template type to SYSTEM
250+
pstmt = conn.prepareStatement("update `cloud`.`vm_template` set type='SYSTEM' where id = ?");
251+
pstmt.setLong(1, templateId);
252+
pstmt.executeUpdate();
253+
pstmt.close();
254+
// update templete ID of system Vms
255+
pstmt = conn.prepareStatement("update `cloud`.`vm_instance` set vm_template_id = ? where type <> 'User' and hypervisor_type = 'Hyperv'");
256+
pstmt.setLong(1, templateId);
257+
pstmt.executeUpdate();
258+
pstmt.close();
259+
} else {
260+
if (Hyperv){
261+
throw new CloudRuntimeException("4.2.0 HyperV SystemVm template not found. Cannot upgrade system Vms");
262+
} else {
263+
s_logger.warn("4.2.0 Hyperv SystemVm template not found. Hyperv hypervisor is not used, so not failing upgrade");
264+
}
265+
}
266+
} catch (SQLException e) {
267+
throw new CloudRuntimeException("Error while updating Hyperv systemVm template", e);
268+
}
269+
270+
//LXC
271+
s_logger.debug("Updating LXC System Vms");
272+
try {
273+
//Get 4.2.0 LXC system Vm template Id
274+
pstmt = conn.prepareStatement("select id from `cloud`.`vm_template` where name = 'systemvm-lxc-4.2' and removed is null order by id desc limit 1");
275+
rs = pstmt.executeQuery();
276+
if(rs.next()){
277+
long templateId = rs.getLong(1);
278+
rs.close();
279+
pstmt.close();
280+
// change template type to SYSTEM
281+
pstmt = conn.prepareStatement("update `cloud`.`vm_template` set type='SYSTEM' where id = ?");
282+
pstmt.setLong(1, templateId);
283+
pstmt.executeUpdate();
284+
pstmt.close();
285+
// update templete ID of system Vms
286+
pstmt = conn.prepareStatement("update `cloud`.`vm_instance` set vm_template_id = ? where type <> 'User' and hypervisor_type = 'LXC'");
287+
pstmt.setLong(1, templateId);
288+
pstmt.executeUpdate();
289+
pstmt.close();
290+
} else {
291+
if (LXC){
292+
throw new CloudRuntimeException("4.2.0 LXC SystemVm template not found. Cannot upgrade system Vms");
293+
} else {
294+
s_logger.warn("4.2.0 LXC SystemVm template not found. LXC hypervisor is not used, so not failing upgrade");
295+
}
296+
}
297+
} catch (SQLException e) {
298+
throw new CloudRuntimeException("Error while updating LXC systemVm template", e);
299+
}
300+
s_logger.debug("Updating System Vm Template IDs Complete");
301+
}
302+
finally {
303+
try {
304+
if (rs != null) {
305+
rs.close();
306+
}
307+
308+
if (pstmt != null) {
309+
pstmt.close();
310+
}
311+
} catch (SQLException e) {
312+
}
313+
}
314+
pstmt = null;
116315
try {
117-
sql = conn.prepareStatement("update vm_template set image_data_store_id = 1 where type = 'SYSTEM' or type = 'BUILTIN'");
118-
sql.executeUpdate();
316+
pstmt = conn.prepareStatement("update vm_template set image_data_store_id = 1 where type = 'SYSTEM' or type = 'BUILTIN'");
317+
pstmt.executeUpdate();
119318
} catch (SQLException e) {
120319
throw new CloudRuntimeException("Failed to upgrade vm template data store uuid: " + e.toString());
121320
} finally {
122-
if (sql != null) {
321+
if (pstmt != null) {
123322
try {
124-
sql.close();
323+
pstmt.close();
125324
} catch (SQLException e) {
126325
}
127326
}

0 commit comments

Comments
 (0)