2323import java .sql .ResultSet ;
2424import java .sql .SQLException ;
2525import java .util .ArrayList ;
26+ import java .util .HashMap ;
27+ import java .util .HashSet ;
2628import java .util .List ;
29+ import java .util .Map ;
30+ import java .util .Set ;
2731
2832import org .apache .log4j .Logger ;
2933
34+ import com .cloud .hypervisor .Hypervisor ;
3035import com .cloud .utils .exception .CloudRuntimeException ;
3136import com .cloud .utils .script .Script ;
3237
@@ -60,6 +65,7 @@ public File[] getPrepareScripts() {
6065
6166 @ Override
6267 public void performDataMigration (final Connection conn ) {
68+ updateSystemVmTemplates (conn );
6369 updateVMInstanceUserId (conn );
6470 addIndexForVMInstance (conn );
6571 }
@@ -139,9 +145,9 @@ private void removeBumPriorityColumn(final Connection conn) {
139145 }
140146 }
141147
142- private void addIndexForVMInstance (Connection conn ) {
148+ private void addIndexForVMInstance (final Connection conn ) {
143149 // Drop index if it exists
144- List <String > indexList = new ArrayList <String >();
150+ final List <String > indexList = new ArrayList <String >();
145151 s_logger .debug ("Dropping index i_vm_instance__instance_name from vm_instance table if it exists" );
146152 indexList .add ("i_vm_instance__instance_name" );
147153 DbUpgradeUtils .dropKeysIfExist (conn , "vm_instance" , indexList , false );
@@ -150,7 +156,7 @@ private void addIndexForVMInstance(Connection conn) {
150156 try (PreparedStatement pstmt = conn .prepareStatement ("ALTER TABLE `cloud`.`vm_instance` ADD INDEX `i_vm_instance__instance_name`(`instance_name`)" );) {
151157 pstmt .executeUpdate ();
152158 s_logger .debug ("Added index i_vm_instance__instance_name to vm_instance table" );
153- } catch (SQLException e ) {
159+ } catch (final SQLException e ) {
154160 throw new CloudRuntimeException ("Unable to add index i_vm_instance__instance_name to vm_instance table for the column instance_name" , e );
155161 }
156162 }
@@ -165,4 +171,173 @@ public File[] getCleanupScripts() {
165171 return new File [] { new File (script ) };
166172 }
167173
174+ @ SuppressWarnings ("serial" )
175+ private void updateSystemVmTemplates (final Connection conn ) {
176+ s_logger .debug ("Updating System Vm template IDs" );
177+ // Get all hypervisors in use
178+ final Set <Hypervisor .HypervisorType > hypervisorsListInUse = new HashSet <Hypervisor .HypervisorType >();
179+ try (PreparedStatement pstmt = conn .prepareStatement ("select distinct(hypervisor_type) from `cloud`.`cluster` where removed is null" ); ResultSet rs = pstmt .executeQuery ()) {
180+ while (rs .next ()) {
181+ switch (Hypervisor .HypervisorType .getType (rs .getString (1 ))) {
182+ case XenServer :
183+ hypervisorsListInUse .add (Hypervisor .HypervisorType .XenServer );
184+ break ;
185+ case KVM :
186+ hypervisorsListInUse .add (Hypervisor .HypervisorType .KVM );
187+ break ;
188+ case VMware :
189+ hypervisorsListInUse .add (Hypervisor .HypervisorType .VMware );
190+ break ;
191+ case Hyperv :
192+ hypervisorsListInUse .add (Hypervisor .HypervisorType .Hyperv );
193+ break ;
194+ case LXC :
195+ hypervisorsListInUse .add (Hypervisor .HypervisorType .LXC );
196+ break ;
197+ case Ovm3 :
198+ hypervisorsListInUse .add (Hypervisor .HypervisorType .Ovm3 );
199+ break ;
200+ default : // no action on cases Any, BareMetal, None, Ovm,
201+ // Parralels, Simulator and VirtualBox:
202+ break ;
203+ }
204+ }
205+ } catch (final SQLException e ) {
206+ s_logger .error ("updateSystemVmTemplates:Exception while getting hypervisor types from clusters: " + e .getMessage ());
207+ throw new CloudRuntimeException ("updateSystemVmTemplates:Exception while getting hypervisor types from clusters" , e );
208+ }
209+
210+ final Map <Hypervisor .HypervisorType , String > NewTemplateNameList = new HashMap <Hypervisor .HypervisorType , String >() {
211+ {
212+ put (Hypervisor .HypervisorType .XenServer , "systemvm-xenserver-4.6" );
213+ put (Hypervisor .HypervisorType .VMware , "systemvm-vmware-4.6" );
214+ put (Hypervisor .HypervisorType .KVM , "systemvm-kvm-4.6" );
215+ put (Hypervisor .HypervisorType .LXC , "systemvm-lxc-4.6" );
216+ put (Hypervisor .HypervisorType .Hyperv , "systemvm-hyperv-4.6" );
217+ put (Hypervisor .HypervisorType .Ovm3 , "systemvm-ovm3-4.6" );
218+ }
219+ };
220+
221+ final Map <Hypervisor .HypervisorType , String > routerTemplateConfigurationNames = new HashMap <Hypervisor .HypervisorType , String >() {
222+ {
223+ put (Hypervisor .HypervisorType .XenServer , "router.template.xenserver" );
224+ put (Hypervisor .HypervisorType .VMware , "router.template.vmware" );
225+ put (Hypervisor .HypervisorType .KVM , "router.template.kvm" );
226+ put (Hypervisor .HypervisorType .LXC , "router.template.lxc" );
227+ put (Hypervisor .HypervisorType .Hyperv , "router.template.hyperv" );
228+ put (Hypervisor .HypervisorType .Ovm3 , "router.template.ovm3" );
229+ }
230+ };
231+
232+ final Map <Hypervisor .HypervisorType , String > newTemplateUrl = new HashMap <Hypervisor .HypervisorType , String >() {
233+ {
234+ put (Hypervisor .HypervisorType .XenServer , "http://cloudstack.apt-get.eu/systemvm/4.6/systemvm64template-4.6.0-xen.vhd.bz2" );
235+ put (Hypervisor .HypervisorType .VMware , "http://cloudstack.apt-get.eu/systemvm/4.6/systemvm64template-4.6.0-vmware.ova" );
236+ put (Hypervisor .HypervisorType .KVM , "http://cloudstack.apt-get.eu/systemvm/4.6/systemvm64template-4.6.0-kvm.qcow2.bz2" );
237+ put (Hypervisor .HypervisorType .LXC , "http://cloudstack.apt-get.eu/systemvm/4.6/systemvm64template-4.6.0-kvm.qcow2.bz2" );
238+ put (Hypervisor .HypervisorType .Hyperv , "http://cloudstack.apt-get.eu/systemvm/4.6/systemvm64template-4.6.0-hyperv.vhd.zip" );
239+ put (Hypervisor .HypervisorType .Ovm3 , "http://cloudstack.apt-get.eu/systemvm/4.6/systemvm64template-4.6.0-ovm.raw.bz2" );
240+ }
241+ };
242+
243+ final Map <Hypervisor .HypervisorType , String > newTemplateChecksum = new HashMap <Hypervisor .HypervisorType , String >() {
244+ {
245+ put (Hypervisor .HypervisorType .XenServer , "8886f554a499ec5405b6f203d9d36460" );
246+ put (Hypervisor .HypervisorType .VMware , "4b415224fe00b258f66cad9fce9f73fc" );
247+ put (Hypervisor .HypervisorType .KVM , "c059b0d051e0cd6fbe9d5d4fc40c7e5d" );
248+ put (Hypervisor .HypervisorType .LXC , "c059b0d051e0cd6fbe9d5d4fc40c7e5d" );
249+ put (Hypervisor .HypervisorType .Hyperv , "53e24bddfa56ea3139ed37af4b519013" );
250+ put (Hypervisor .HypervisorType .Ovm3 , "c8577d27b2daafb2d9a4ed307ce2f00f" );
251+ }
252+ };
253+
254+ for (final Map .Entry <Hypervisor .HypervisorType , String > hypervisorAndTemplateName : NewTemplateNameList .entrySet ()) {
255+ s_logger .debug ("Updating " + hypervisorAndTemplateName .getKey () + " System Vms" );
256+ try (PreparedStatement pstmt = conn .prepareStatement ("select id from `cloud`.`vm_template` where name = ? and removed is null order by id desc limit 1" )) {
257+ // Get 4.6.0 system Vm template Id for corresponding hypervisor
258+ long templateId = -1 ;
259+ pstmt .setString (1 , hypervisorAndTemplateName .getValue ());
260+ try (ResultSet rs = pstmt .executeQuery ()) {
261+ if (rs .next ()) {
262+ templateId = rs .getLong (1 );
263+ }
264+ } catch (final SQLException e ) {
265+ s_logger .error ("updateSystemVmTemplates:Exception while getting ids of templates: " + e .getMessage ());
266+ throw new CloudRuntimeException ("updateSystemVmTemplates:Exception while getting ids of templates" , e );
267+ }
268+
269+ // change template type to SYSTEM
270+ if (templateId != -1 ) {
271+ try (PreparedStatement templ_type_pstmt = conn .prepareStatement ("update `cloud`.`vm_template` set type='SYSTEM' where id = ?" );) {
272+ templ_type_pstmt .setLong (1 , templateId );
273+ templ_type_pstmt .executeUpdate ();
274+ } catch (final SQLException e ) {
275+ s_logger .error ("updateSystemVmTemplates:Exception while updating template with id " + templateId + " to be marked as 'system': " + e .getMessage ());
276+ throw new CloudRuntimeException ("updateSystemVmTemplates:Exception while updating template with id " + templateId + " to be marked as 'system'" , e );
277+ }
278+ // update template ID of system Vms
279+ try (PreparedStatement update_templ_id_pstmt = conn
280+ .prepareStatement ("update `cloud`.`vm_instance` set vm_template_id = ? where type <> 'User' and hypervisor_type = ?" );) {
281+ update_templ_id_pstmt .setLong (1 , templateId );
282+ update_templ_id_pstmt .setString (2 , hypervisorAndTemplateName .getKey ().toString ());
283+ update_templ_id_pstmt .executeUpdate ();
284+ } catch (final Exception e ) {
285+ s_logger .error ("updateSystemVmTemplates:Exception while setting template for " + hypervisorAndTemplateName .getKey ().toString () + " to " + templateId
286+ + ": " + e .getMessage ());
287+ throw new CloudRuntimeException ("updateSystemVmTemplates:Exception while setting template for " + hypervisorAndTemplateName .getKey ().toString () + " to "
288+ + templateId , e );
289+ }
290+
291+ // Change value of global configuration parameter
292+ // router.template.* for the corresponding hypervisor
293+ try (PreparedStatement update_pstmt = conn .prepareStatement ("UPDATE `cloud`.`configuration` SET value = ? WHERE name = ?" );) {
294+ update_pstmt .setString (1 , hypervisorAndTemplateName .getValue ());
295+ update_pstmt .setString (2 , routerTemplateConfigurationNames .get (hypervisorAndTemplateName .getKey ()));
296+ update_pstmt .executeUpdate ();
297+ } catch (final SQLException e ) {
298+ s_logger .error ("updateSystemVmTemplates:Exception while setting " + routerTemplateConfigurationNames .get (hypervisorAndTemplateName .getKey ()) + " to "
299+ + hypervisorAndTemplateName .getValue () + ": " + e .getMessage ());
300+ throw new CloudRuntimeException ("updateSystemVmTemplates:Exception while setting "
301+ + routerTemplateConfigurationNames .get (hypervisorAndTemplateName .getKey ()) + " to " + hypervisorAndTemplateName .getValue (), e );
302+ }
303+
304+ // Change value of global configuration parameter
305+ // minreq.sysvmtemplate.version for the ACS version
306+ try (PreparedStatement update_pstmt = conn .prepareStatement ("UPDATE `cloud`.`configuration` SET value = ? WHERE name = ?" );) {
307+ update_pstmt .setString (1 , getUpgradedVersion ());
308+ update_pstmt .setString (2 , "minreq.sysvmtemplate.version" );
309+ update_pstmt .executeUpdate ();
310+ } catch (final SQLException e ) {
311+ s_logger .error ("updateSystemVmTemplates:Exception while setting 'minreq.sysvmtemplate.version' to 4.6.0: " + e .getMessage ());
312+ throw new CloudRuntimeException ("updateSystemVmTemplates:Exception while setting 'minreq.sysvmtemplate.version' to 4.6.0" , e );
313+ }
314+ } else {
315+ if (hypervisorsListInUse .contains (hypervisorAndTemplateName .getKey ())) {
316+ throw new CloudRuntimeException (getUpgradedVersion () + hypervisorAndTemplateName .getKey () + " SystemVm template not found. Cannot upgrade system Vms" );
317+ } else {
318+ s_logger .warn (getUpgradedVersion () + hypervisorAndTemplateName .getKey () + " SystemVm template not found. " + hypervisorAndTemplateName .getKey ()
319+ + " hypervisor is not used, so not failing upgrade" );
320+ // Update the latest template URLs for corresponding
321+ // hypervisor
322+ try (PreparedStatement update_templ_url_pstmt = conn
323+ .prepareStatement ("UPDATE `cloud`.`vm_template` SET url = ? , checksum = ? WHERE hypervisor_type = ? AND type = 'SYSTEM' AND removed is null order by id desc limit 1" );) {
324+ update_templ_url_pstmt .setString (1 , newTemplateUrl .get (hypervisorAndTemplateName .getKey ()));
325+ update_templ_url_pstmt .setString (2 , newTemplateChecksum .get (hypervisorAndTemplateName .getKey ()));
326+ update_templ_url_pstmt .setString (3 , hypervisorAndTemplateName .getKey ().toString ());
327+ update_templ_url_pstmt .executeUpdate ();
328+ } catch (final SQLException e ) {
329+ s_logger .error ("updateSystemVmTemplates:Exception while updating 'url' and 'checksum' for hypervisor type "
330+ + hypervisorAndTemplateName .getKey ().toString () + ": " + e .getMessage ());
331+ throw new CloudRuntimeException ("updateSystemVmTemplates:Exception while updating 'url' and 'checksum' for hypervisor type "
332+ + hypervisorAndTemplateName .getKey ().toString (), e );
333+ }
334+ }
335+ }
336+ } catch (final SQLException e ) {
337+ s_logger .error ("updateSystemVmTemplates:Exception while getting ids of templates: " + e .getMessage ());
338+ throw new CloudRuntimeException ("updateSystemVmTemplates:Exception while getting ids of templates" , e );
339+ }
340+ }
341+ s_logger .debug ("Updating System Vm Template IDs Complete" );
342+ }
168343}
0 commit comments