@@ -82,126 +82,131 @@ public File[] getCleanupScripts() {
8282 }
8383
8484 private void updateSystemVmTemplates (Connection conn ) {
85- PreparedStatement pstmt = null ;
86- ResultSet rs = null ;
8785 s_logger .debug ("Updating System Vm template IDs" );
88- try {
86+ try {
8987 //Get all hypervisors in use
9088 Set <Hypervisor .HypervisorType > hypervisorsListInUse = new HashSet <Hypervisor .HypervisorType >();
91- try {
92- pstmt = conn .prepareStatement ("select distinct(hypervisor_type) from `cloud`.`cluster` where removed is null" );
93- rs = pstmt .executeQuery ();
94- while (rs .next ()){
95- switch (Hypervisor .HypervisorType .getType (rs .getString (1 ))) {
96- case XenServer : hypervisorsListInUse .add (Hypervisor .HypervisorType .XenServer );
97- break ;
98- case KVM : hypervisorsListInUse .add (Hypervisor .HypervisorType .KVM );
99- break ;
100- case VMware : hypervisorsListInUse .add (Hypervisor .HypervisorType .VMware );
101- break ;
102- case Hyperv : hypervisorsListInUse .add (Hypervisor .HypervisorType .Hyperv );
103- break ;
104- case LXC : hypervisorsListInUse .add (Hypervisor .HypervisorType .LXC );
105- break ;
89+ try (PreparedStatement sel_hyp_type_pstmt = conn .prepareStatement ("select distinct(hypervisor_type) from `cloud`.`cluster` where removed is null" );) {
90+ try (ResultSet hyp_type_rs = sel_hyp_type_pstmt .executeQuery ();) {
91+ while (hyp_type_rs .next ()) {
92+ switch (Hypervisor .HypervisorType .getType (hyp_type_rs .getString (1 ))) {
93+ case XenServer :
94+ hypervisorsListInUse .add (Hypervisor .HypervisorType .XenServer );
95+ break ;
96+ case KVM :
97+ hypervisorsListInUse .add (Hypervisor .HypervisorType .KVM );
98+ break ;
99+ case VMware :
100+ hypervisorsListInUse .add (Hypervisor .HypervisorType .VMware );
101+ break ;
102+ case Hyperv :
103+ hypervisorsListInUse .add (Hypervisor .HypervisorType .Hyperv );
104+ break ;
105+ case LXC :
106+ hypervisorsListInUse .add (Hypervisor .HypervisorType .LXC );
107+ break ;
108+ }
106109 }
110+ } catch (SQLException e ) {
111+ throw new CloudRuntimeException ("updateSystemVmTemplates:Exception:" + e .getMessage (), e );
107112 }
108113 } catch (SQLException e ) {
109- throw new CloudRuntimeException ("Error while listing hypervisors in use" , e );
114+ throw new CloudRuntimeException ("updateSystemVmTemplates:Exception:" + e . getMessage () , e );
110115 }
111-
112- Map < Hypervisor . HypervisorType , String > NewTemplateNameList = new HashMap < Hypervisor . HypervisorType , String >() {
113- { put (Hypervisor .HypervisorType .XenServer , "systemvm-xenserver-4.5" );
116+ Map < Hypervisor . HypervisorType , String > NewTemplateNameList = new HashMap < Hypervisor . HypervisorType , String >() {
117+ {
118+ put (Hypervisor .HypervisorType .XenServer , "systemvm-xenserver-4.5" );
114119 put (Hypervisor .HypervisorType .VMware , "systemvm-vmware-4.5" );
115120 put (Hypervisor .HypervisorType .KVM , "systemvm-kvm-4.5" );
116121 put (Hypervisor .HypervisorType .LXC , "systemvm-lxc-4.5" );
117122 put (Hypervisor .HypervisorType .Hyperv , "systemvm-hyperv-4.5" );
118123 }
119124 };
120-
121- Map < Hypervisor . HypervisorType , String > routerTemplateConfigurationNames = new HashMap < Hypervisor . HypervisorType , String >() {
122- { put (Hypervisor .HypervisorType .XenServer , "router.template.xen" );
125+ Map < Hypervisor . HypervisorType , String > routerTemplateConfigurationNames = new HashMap < Hypervisor . HypervisorType , String >() {
126+ {
127+ put (Hypervisor .HypervisorType .XenServer , "router.template.xen" );
123128 put (Hypervisor .HypervisorType .VMware , "router.template.vmware" );
124129 put (Hypervisor .HypervisorType .KVM , "router.template.kvm" );
125130 put (Hypervisor .HypervisorType .LXC , "router.template.lxc" );
126131 put (Hypervisor .HypervisorType .Hyperv , "router.template.hyperv" );
127132 }
128133 };
129-
130- Map < Hypervisor . HypervisorType , String > newTemplateUrl = new HashMap < Hypervisor . HypervisorType , String >() {
131- { put (Hypervisor .HypervisorType .XenServer , "http://download.cloud.com/templates/4.5/systemvm64template-4.5-xen.vhd.bz2" );
134+ Map < Hypervisor . HypervisorType , String > newTemplateUrl = new HashMap < Hypervisor . HypervisorType , String >() {
135+ {
136+ put (Hypervisor .HypervisorType .XenServer , "http://download.cloud.com/templates/4.5/systemvm64template-4.5-xen.vhd.bz2" );
132137 put (Hypervisor .HypervisorType .VMware , "http://download.cloud.com/templates/4.5/systemvm64template-4.5-vmware.ova" );
133138 put (Hypervisor .HypervisorType .KVM , "http://download.cloud.com/templates/4.5/systemvm64template-4.5-kvm.qcow2.bz2" );
134139 put (Hypervisor .HypervisorType .LXC , "http://download.cloud.com/templates/4.5/systemvm64template-4.5-kvm.qcow2.bz2" );
135140 put (Hypervisor .HypervisorType .Hyperv , "http://download.cloud.com/templates/4.5/systemvm64template-4.5-hyperv.vhd.zip" );
136141 }
137142 };
138-
139- Map < Hypervisor . HypervisorType , String > newTemplateChecksum = new HashMap < Hypervisor . HypervisorType , String >() {
140- { put (Hypervisor .HypervisorType .XenServer , "2b15ab4401c2d655264732d3fc600241" );
143+ Map < Hypervisor . HypervisorType , String > newTemplateChecksum = new HashMap < Hypervisor . HypervisorType , String >() {
144+ {
145+ put (Hypervisor .HypervisorType .XenServer , "2b15ab4401c2d655264732d3fc600241" );
141146 put (Hypervisor .HypervisorType .VMware , "3106a79a4ce66cd7f6a7c50e93f2db57" );
142147 put (Hypervisor .HypervisorType .KVM , "aa9f501fecd3de1daeb9e2f357f6f002" );
143148 put (Hypervisor .HypervisorType .LXC , "aa9f501fecd3de1daeb9e2f357f6f002" );
144149 put (Hypervisor .HypervisorType .Hyperv , "70bd30ea02ee9ed67d2c6b85c179cee9" );
145150 }
146151 };
147-
148- for (Map .Entry <Hypervisor .HypervisorType , String > hypervisorAndTemplateName : NewTemplateNameList .entrySet ()){
152+ for (Map .Entry <Hypervisor .HypervisorType , String > hypervisorAndTemplateName : NewTemplateNameList .entrySet ()) {
149153 s_logger .debug ("Updating " + hypervisorAndTemplateName .getKey () + " System Vms" );
150- try {
154+ try (PreparedStatement sel_templ_pstmt = conn .prepareStatement ("select id from `cloud`.`vm_template` where name = ? and removed is null order by id desc limit 1" );)
155+ {
151156 //Get 4.5.0 system Vm template Id for corresponding hypervisor
152- pstmt = conn .prepareStatement ("select id from `cloud`.`vm_template` where name = ? and removed is null order by id desc limit 1" );
153- pstmt .setString (1 , hypervisorAndTemplateName .getValue ());
154- rs = pstmt .executeQuery ();
155- if (rs .next ()){
156- long templateId = rs .getLong (1 );
157- rs .close ();
158- pstmt .close ();
159- pstmt = conn .prepareStatement ("update `cloud`.`vm_template` set type='SYSTEM' where id = ?" );
160- pstmt .setLong (1 , templateId );
161- pstmt .executeUpdate ();
162- pstmt .close ();
163- // update templete ID of system Vms
164- pstmt = conn .prepareStatement ("update `cloud`.`vm_instance` set vm_template_id = ? where type <> 'User' and hypervisor_type = ?" );
165- pstmt .setLong (1 , templateId );
166- pstmt .setString (2 , hypervisorAndTemplateName .getKey ().toString ());
167- pstmt .executeUpdate ();
168- pstmt .close ();
169- // Change value of global configuration parameter router.template.* for the corresponding hypervisor
170- pstmt = conn .prepareStatement ("UPDATE `cloud`.`configuration` SET value = ? WHERE name = ?" );
171- pstmt .setString (1 , hypervisorAndTemplateName .getValue ());
172- pstmt .setString (2 , routerTemplateConfigurationNames .get (hypervisorAndTemplateName .getKey ()));
173- pstmt .executeUpdate ();
174- pstmt .close ();
175- } else {
176- if (hypervisorsListInUse .contains (hypervisorAndTemplateName .getKey ())){
177- throw new CloudRuntimeException ("4.5.0 " + hypervisorAndTemplateName .getKey () + " SystemVm template not found. Cannot upgrade system Vms" );
157+ sel_templ_pstmt .setString (1 , hypervisorAndTemplateName .getValue ());
158+ try (ResultSet rs = sel_templ_pstmt .executeQuery ();)
159+ {
160+ if (rs .next ()) {
161+ long templateId = rs .getLong (1 );
162+ try (PreparedStatement update_cloud_templ_pstmt = conn .prepareStatement ("update `cloud`.`vm_template` set type='SYSTEM' where id = ?" );)
163+ {
164+ update_cloud_templ_pstmt .setLong (1 , templateId );
165+ update_cloud_templ_pstmt .executeUpdate ();
166+ // update templete ID of system Vms
167+ }catch (SQLException e ) {
168+ throw new CloudRuntimeException ("updateSystemVmTemplates:Exception:" + e .getMessage (), e );
169+ }
170+ try (PreparedStatement update_instance_pstmt = conn .prepareStatement ("update `cloud`.`vm_instance` set vm_template_id = ? where type <> 'User' and hypervisor_type = ?" );) {
171+ update_instance_pstmt .setLong (1 , templateId );
172+ update_instance_pstmt .setString (2 , hypervisorAndTemplateName .getKey ().toString ());
173+ update_instance_pstmt .executeUpdate ();
174+ }catch (SQLException e ) {
175+ throw new CloudRuntimeException ("updateSystemVmTemplates:Exception:" + e .getMessage (), e );
176+ }
177+ // Change value of global configuration parameter router.template.* for the corresponding hypervisor
178+ try (PreparedStatement update_cloud_pstmt = conn .prepareStatement ("UPDATE `cloud`.`configuration` SET value = ? WHERE name = ?" );)
179+ {
180+ update_cloud_pstmt .setString (1 , hypervisorAndTemplateName .getValue ());
181+ update_cloud_pstmt .setString (2 , routerTemplateConfigurationNames .get (hypervisorAndTemplateName .getKey ()));
182+ update_cloud_pstmt .executeUpdate ();
183+ }catch (SQLException e ) {
184+ throw new CloudRuntimeException ("updateSystemVmTemplates:Exception:" + e .getMessage (), e );
185+ }
178186 } else {
179- s_logger .warn ("4.5.0 " + hypervisorAndTemplateName .getKey () + " SystemVm template not found. " + hypervisorAndTemplateName .getKey () + " hypervisor is not used, so not failing upgrade" );
180- // Update the latest template URLs for corresponding hypervisor
181- 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" );
182- pstmt .setString (1 , newTemplateUrl .get (hypervisorAndTemplateName .getKey ()));
183- pstmt .setString (2 , newTemplateChecksum .get (hypervisorAndTemplateName .getKey ()));
184- pstmt .setString (3 , hypervisorAndTemplateName .getKey ().toString ());
185- pstmt .executeUpdate ();
186- pstmt .close ();
187+ if (hypervisorsListInUse .contains (hypervisorAndTemplateName .getKey ())) {
188+ throw new CloudRuntimeException ("4.5.0 " + hypervisorAndTemplateName .getKey () + " SystemVm template not found. Cannot upgrade system Vms" );
189+ } else {
190+ s_logger .warn ("4.5.0 " + hypervisorAndTemplateName .getKey () + " SystemVm template not found. " + hypervisorAndTemplateName .getKey () + " hypervisor is not used, so not failing upgrade" );
191+ // Update the latest template URLs for corresponding hypervisor
192+ try (PreparedStatement update_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" );) {
193+ update_pstmt .setString (1 , newTemplateUrl .get (hypervisorAndTemplateName .getKey ()));
194+ update_pstmt .setString (2 , newTemplateChecksum .get (hypervisorAndTemplateName .getKey ()));
195+ update_pstmt .setString (3 , hypervisorAndTemplateName .getKey ().toString ());
196+ update_pstmt .executeUpdate ();
197+ } catch (SQLException e ) {
198+ throw new CloudRuntimeException ("updateSystemVmTemplates:Exception:" + e .getMessage (), e );
199+ }
200+ }
187201 }
202+ } catch (SQLException e ) {
203+ throw new CloudRuntimeException ("updateSystemVmTemplates:Exception:" + e .getMessage (), e );
188204 }
189- } catch (SQLException e ) {
190- throw new CloudRuntimeException ("Error while updating " + hypervisorAndTemplateName .getKey () +" systemVm template" , e );
191- }
192- }
193- s_logger .debug ("Updating System Vm Template IDs Complete" );
194- } finally {
195- try {
196- if (rs != null ) {
197- rs .close ();
198205 }
199-
200- if (pstmt != null ) {
201- pstmt .close ();
202- }
203- } catch (SQLException e ) {
206+ s_logger .debug ("Updating System Vm Template IDs Complete" );
204207 }
208+ }catch (SQLException e ){
209+ throw new CloudRuntimeException ("updateSystemVmTemplates:Exception:" + e .getMessage (), e );
205210 }
206211 }
207212
0 commit comments