@@ -105,9 +105,9 @@ public class SystemVmTemplateRegistration {
105105 private static final String METADATA_FILE = TEMPLATES_PATH + METADATA_FILE_NAME ;
106106 public static final String TEMPORARY_SECONDARY_STORE = "tmp" ;
107107 private static final String PARTIAL_TEMPLATE_FOLDER = String .format ("/template/tmpl/%d/" , Account .ACCOUNT_ID_SYSTEM );
108- private static final String storageScriptsDir = "scripts/storage/secondary" ;
108+ protected static final String STORAGE_SCRIPTS_DIR = "scripts/storage/secondary" ;
109109 private static final Integer OTHER_LINUX_ID = 99 ;
110- private static Integer LINUX_12_ID = 363 ;
110+ protected static Integer LINUX_12_ID = 363 ;
111111 private static final Integer SCRIPT_TIMEOUT = 1800000 ;
112112 private static final Integer LOCK_WAIT_TIMEOUT = 1200 ;
113113 protected static final String TEMPLATE_DOWNLOAD_URL_KEY = "downloadurl" ;
@@ -327,6 +327,17 @@ public void setUpdated(Date updated) {
327327 }
328328 };
329329
330+ protected static final Map <Hypervisor .HypervisorType , ImageFormat > HYPERVISOR_IMAGE_FORMAT_MAP = new HashMap <>() {
331+ {
332+ put (Hypervisor .HypervisorType .KVM , ImageFormat .QCOW2 );
333+ put (Hypervisor .HypervisorType .XenServer , ImageFormat .VHD );
334+ put (Hypervisor .HypervisorType .VMware , ImageFormat .OVA );
335+ put (Hypervisor .HypervisorType .Hyperv , ImageFormat .VHD );
336+ put (Hypervisor .HypervisorType .LXC , ImageFormat .QCOW2 );
337+ put (Hypervisor .HypervisorType .Ovm3 , ImageFormat .RAW );
338+ }
339+ };
340+
330341 protected static Map <Hypervisor .HypervisorType , Integer > hypervisorGuestOsMap = new HashMap <>() {
331342 {
332343 put (Hypervisor .HypervisorType .KVM , LINUX_12_ID );
@@ -338,17 +349,6 @@ public void setUpdated(Date updated) {
338349 }
339350 };
340351
341- protected static final Map <Hypervisor .HypervisorType , ImageFormat > hypervisorImageFormat = new HashMap <Hypervisor .HypervisorType , ImageFormat >() {
342- {
343- put (Hypervisor .HypervisorType .KVM , ImageFormat .QCOW2 );
344- put (Hypervisor .HypervisorType .XenServer , ImageFormat .VHD );
345- put (Hypervisor .HypervisorType .VMware , ImageFormat .OVA );
346- put (Hypervisor .HypervisorType .Hyperv , ImageFormat .VHD );
347- put (Hypervisor .HypervisorType .LXC , ImageFormat .QCOW2 );
348- put (Hypervisor .HypervisorType .Ovm3 , ImageFormat .RAW );
349- }
350- };
351-
352352 private static boolean isRunningInTest () {
353353 return "true" .equalsIgnoreCase (System .getProperty ("test.mode" ));
354354 }
@@ -399,7 +399,7 @@ private static String fetchTemplatesPath() {
399399 throw new CloudRuntimeException (errMsg );
400400 }
401401
402- private static void cleanupStore (Long templateId , String filePath ) {
402+ protected static void cleanupStore (Long templateId , String filePath ) {
403403 String destTempFolder = filePath + PARTIAL_TEMPLATE_FOLDER + String .valueOf (templateId );
404404 try {
405405 Files .deleteIfExists (Paths .get (destTempFolder ));
@@ -408,7 +408,7 @@ private static void cleanupStore(Long templateId, String filePath) {
408408 }
409409 }
410410
411- private static Pair <Long , Long > readTemplatePropertiesSizes (String path ) {
411+ protected static Pair <Long , Long > readTemplatePropertiesSizes (String path ) {
412412 File tmpFile = new File (path );
413413 Long size = null ;
414414 Long physicalSize = 0L ;
@@ -430,14 +430,8 @@ private static Pair<Long, Long> readTemplatePropertiesSizes(String path) {
430430 return new Pair <>(size , physicalSize );
431431 }
432432
433- private static void readTemplateProperties (String path , SystemVMTemplateDetails details ) {
434- Pair <Long , Long > templateSizes = readTemplatePropertiesSizes (path );
435- details .setSize (templateSizes .first ());
436- details .setPhysicalSize (templateSizes .second ());
437- }
438-
439433 protected static MetadataTemplateDetails getMetadataTemplateDetails (Hypervisor .HypervisorType hypervisorType ,
440- CPU .CPUArch arch ) {
434+ CPU .CPUArch arch ) {
441435 return METADATA_TEMPLATE_LIST
442436 .stream ()
443437 .filter (x -> Objects .equals (x .getHypervisorType (), hypervisorType ) &&
@@ -525,10 +519,49 @@ public static String parseMetadataFile() {
525519 return defaultSection .get ("version" ).trim ();
526520 }
527521
522+ public static void mountStore (String storeUrl , String path , String nfsVersion ) {
523+ try {
524+ if (storeUrl == null ) {
525+ return ;
526+ }
527+ URI uri = new URI (UriUtils .encodeURIComponent (storeUrl ));
528+ String host = uri .getHost ();
529+ String mountPath = uri .getPath ();
530+ Script .runSimpleBashScript (getMountCommand (nfsVersion , host + ":" + mountPath , path ));
531+ } catch (Exception e ) {
532+ String msg = "NFS Store URL is not in the correct format" ;
533+ LOGGER .error (msg , e );
534+ throw new CloudRuntimeException (msg , e );
535+ }
536+ }
537+
538+ public static void unmountStore (String filePath ) {
539+ try {
540+ LOGGER .info ("Unmounting store" );
541+ String umountCmd = String .format (UMOUNT_COMMAND , filePath );
542+ Script .runSimpleBashScript (umountCmd );
543+ try {
544+ Files .deleteIfExists (Paths .get (filePath ));
545+ } catch (IOException e ) {
546+ LOGGER .error (String .format ("Failed to cleanup mounted store at: %s" , filePath ), e );
547+ }
548+ } catch (Exception e ) {
549+ String msg = String .format ("Failed to unmount store mounted at %s" , filePath );
550+ LOGGER .error (msg , e );
551+ throw new CloudRuntimeException (msg , e );
552+ }
553+ }
554+
528555 protected File getTempDownloadDir () {
529556 return tempDownloadDir ;
530557 }
531558
559+ protected void readTemplateProperties (String path , SystemVMTemplateDetails details ) {
560+ Pair <Long , Long > templateSizes = readTemplatePropertiesSizes (path );
561+ details .setSize (templateSizes .first ());
562+ details .setPhysicalSize (templateSizes .second ());
563+ }
564+
532565 protected List <Long > getEligibleZoneIds () {
533566 List <Long > zoneIds = new ArrayList <>();
534567 List <ImageStoreVO > stores = imageStoreDao .findByProtocol ("nfs" );
@@ -587,7 +620,7 @@ private VMTemplateVO createTemplateObjectInDB(SystemVMTemplateDetails details) {
587620 return template ;
588621 }
589622
590- private VMTemplateZoneVO createOrUpdateTemplateZoneEntry (long zoneId , long templateId ) {
623+ protected VMTemplateZoneVO createOrUpdateTemplateZoneEntry (long zoneId , long templateId ) {
591624 VMTemplateZoneVO templateZoneVO = vmTemplateZoneDao .findByZoneTemplate (zoneId , templateId );
592625 if (templateZoneVO == null ) {
593626 templateZoneVO = new VMTemplateZoneVO (zoneId , templateId , new java .util .Date ());
@@ -601,7 +634,7 @@ private VMTemplateZoneVO createOrUpdateTemplateZoneEntry(long zoneId, long templ
601634 return templateZoneVO ;
602635 }
603636
604- private void createCrossZonesTemplateZoneRefEntries (Long templateId ) {
637+ protected void createCrossZonesTemplateZoneRefEntries (Long templateId ) {
605638 List <DataCenterVO > dcs = dataCenterDao .listAll ();
606639 for (DataCenterVO dc : dcs ) {
607640 VMTemplateZoneVO templateZoneVO = createOrUpdateTemplateZoneEntry (dc .getId (), templateId );
@@ -612,7 +645,7 @@ private void createCrossZonesTemplateZoneRefEntries(Long templateId) {
612645 }
613646 }
614647
615- private void createTemplateStoreRefEntry (SystemVMTemplateDetails details ) {
648+ protected void createTemplateStoreRefEntry (SystemVMTemplateDetails details ) {
616649 TemplateDataStoreVO templateDataStoreVO = new TemplateDataStoreVO (details .getStoreId (), details .getId (),
617650 details .getCreated (), 0 , VMTemplateStorageResourceAssoc .Status .NOT_DOWNLOADED ,
618651 null , null , null , details .getInstallPath (), details .getUrl ());
@@ -663,7 +696,7 @@ protected void updateSystemVMEntries(Long templateId, Hypervisor.HypervisorType
663696 vmInstanceDao .updateSystemVmTemplateId (templateId , hypervisorType );
664697 }
665698
666- private void updateHypervisorGuestOsMap () {
699+ protected void updateHypervisorGuestOsMap () {
667700 try {
668701 GuestOSVO guestOS = guestOSDao .findOneByDisplayName (DEFAULT_SYSTEM_VM_GUEST_OS_NAME );
669702 if (guestOS == null ) {
@@ -700,7 +733,7 @@ protected void updateConfigurationParams(Hypervisor.HypervisorType hypervisorTyp
700733 }
701734 }
702735
703- private void updateTemplateEntriesOnFailure (long templateId ) {
736+ protected void updateTemplateEntriesOnFailure (long templateId ) {
704737 VMTemplateVO template = vmTemplateDao .createForUpdate (templateId );
705738 template .setState (VirtualMachineTemplate .State .Inactive );
706739 vmTemplateDao .update (template .getId (), template );
@@ -713,9 +746,9 @@ private void updateTemplateEntriesOnFailure(long templateId) {
713746 templateDataStoreDao .remove (templateDataStoreVO .getId ());
714747 }
715748
716- private void setupTemplateOnStore (String templateName , MetadataTemplateDetails templateDetails ,
749+ protected void setupTemplateOnStore (String templateName , MetadataTemplateDetails templateDetails ,
717750 String destTempFolder ) throws CloudRuntimeException {
718- String setupTmpltScript = Script .findScript (storageScriptsDir , "setup-sysvm-tmplt" );
751+ String setupTmpltScript = Script .findScript (STORAGE_SCRIPTS_DIR , "setup-sysvm-tmplt" );
719752 if (setupTmpltScript == null ) {
720753 throw new CloudRuntimeException ("Unable to find the setup-sysvm-tmplt script" );
721754 }
@@ -735,9 +768,24 @@ private void setupTemplateOnStore(String templateName, MetadataTemplateDetails t
735768 }
736769 }
737770
771+ /**
772+ * Register or update a system VM template record and seed it on the target store.
773+ *
774+ * @param name display name of the template
775+ * @param templateDetails metadata for the template
776+ * @param url download URL of the template
777+ * @param checksum expected checksum of the template file
778+ * @param format image format of the template
779+ * @param guestOsId guest OS id
780+ * @param storeId target image store id
781+ * @param templateId existing template id if present, otherwise {@code null}
782+ * @param filePath temporary mount path for the store
783+ * @param templateDataStoreVO existing template-store mapping; may be {@code null}
784+ * @return the id of the template that was created or updated
785+ */
738786 protected Long performTemplateRegistrationOperations (String name , MetadataTemplateDetails templateDetails ,
739- String url , String checksum , ImageFormat format , long guestOsId ,
740- Long storeId , Long templateId , String filePath , TemplateDataStoreVO templateDataStoreVO ) {
787+ String url , String checksum , ImageFormat format , long guestOsId , Long storeId , Long templateId ,
788+ String filePath , TemplateDataStoreVO templateDataStoreVO ) {
741789 String templateName = UUID .randomUUID ().toString ();
742790 Date created = new Date (DateUtil .currentGMTTime ().getTime ());
743791 SystemVMTemplateDetails details = new SystemVMTemplateDetails (templateName , name , created , url , checksum ,
@@ -755,7 +803,9 @@ protected Long performTemplateRegistrationOperations(String name, MetadataTempla
755803 details .setId (templateId );
756804 String destTempFolderName = String .valueOf (templateId );
757805 String destTempFolder = filePath + PARTIAL_TEMPLATE_FOLDER + destTempFolderName ;
758- details .setInstallPath (PARTIAL_TEMPLATE_FOLDER + destTempFolderName + File .separator + templateName + "." + hypervisorImageFormat .get (templateDetails .getHypervisorType ()).getFileExtension ());
806+ details .setInstallPath (String .format ("%s%s%s%s.%s" , PARTIAL_TEMPLATE_FOLDER , destTempFolderName ,
807+ File .separator , templateName ,
808+ HYPERVISOR_IMAGE_FORMAT_MAP .get (templateDetails .getHypervisorType ()).getFileExtension ()));
759809 if (templateDataStoreVO == null ) {
760810 createTemplateStoreRefEntry (details );
761811 }
@@ -807,7 +857,7 @@ protected void registerNewTemplate(String name, MetadataTemplateDetails template
807857 Hypervisor .HypervisorType hypervisor = templateDetails .getHypervisorType ();
808858 try {
809859 templateId = performTemplateRegistrationOperations (name , templateDetails , templateDetails .getUrl (),
810- templateDetails .getChecksum (), hypervisorImageFormat .get (hypervisor ),
860+ templateDetails .getChecksum (), HYPERVISOR_IMAGE_FORMAT_MAP .get (hypervisor ),
811861 hypervisorGuestOsMap .get (hypervisor ), storeId , null , filePath , null );
812862 updateConfigurationParams (hypervisor , name , zoneId );
813863 updateSystemVMEntries (templateId , hypervisor );
@@ -913,6 +963,16 @@ protected void validateTemplates(List<Pair<Hypervisor.HypervisorType, CPU.CPUArc
913963 }
914964 }
915965
966+ /**
967+ * Register or ensure system VM templates are present on the NFS store for a given zone.
968+ *
969+ * Mounts the zone image store, enumerates hypervisors and architectures in the zone,
970+ * and for each template either adds an existing template to the store or registers
971+ * a new template as required.
972+ *
973+ * @param zoneId the zone id
974+ * @param storeMountPath temporary mount path for the store
975+ */
916976 protected void registerTemplatesForZone (long zoneId , String storeMountPath ) {
917977 Pair <String , Long > storeUrlAndId = getNfsStoreInZone (zoneId );
918978 String nfsVersion = getNfsVersion (storeUrlAndId .second ());
@@ -986,6 +1046,16 @@ public void doInTransactionWithoutResult(final TransactionStatus status) {
9861046 }
9871047 }
9881048
1049+ /**
1050+ * Update the DB record for an existing template to mark it as a system template,
1051+ * set the guest OS (if resolvable), and propagate the change to system VM entries
1052+ * and related configuration for the template's hypervisor.
1053+ *
1054+ * @param templateId id of the template to update
1055+ * @param templateDetails metadata used to update the template record
1056+ * @param zoneId zone id whose per-zone details (if any) should be cleared; may be null
1057+ * @throws CloudRuntimeException if updating the template record fails
1058+ */
9891059 protected void updateRegisteredTemplateDetails (Long templateId , MetadataTemplateDetails templateDetails ,
9901060 Long zoneId ) {
9911061 VMTemplateVO templateVO = vmTemplateDao .findById (templateId );
@@ -1002,7 +1072,6 @@ protected void updateRegisteredTemplateDetails(Long templateId, MetadataTemplate
10021072 }
10031073 Hypervisor .HypervisorType hypervisorType = templateDetails .getHypervisorType ();
10041074 updateSystemVMEntries (templateId , hypervisorType );
1005- // Change value of global configuration parameter router.template.* for the corresponding hypervisor and minreq.sysvmtemplate.version for the ACS version
10061075 updateConfigurationParams (hypervisorType , templateDetails .getName (), zoneId );
10071076 }
10081077
@@ -1086,39 +1155,6 @@ protected String getNfsVersion(long storeId) {
10861155 return null ;
10871156 }
10881157
1089- public static void mountStore (String storeUrl , String path , String nfsVersion ) {
1090- try {
1091- if (storeUrl == null ) {
1092- return ;
1093- }
1094- URI uri = new URI (UriUtils .encodeURIComponent (storeUrl ));
1095- String host = uri .getHost ();
1096- String mountPath = uri .getPath ();
1097- Script .runSimpleBashScript (getMountCommand (nfsVersion , host + ":" + mountPath , path ));
1098- } catch (Exception e ) {
1099- String msg = "NFS Store URL is not in the correct format" ;
1100- LOGGER .error (msg , e );
1101- throw new CloudRuntimeException (msg , e );
1102- }
1103- }
1104-
1105- public static void unmountStore (String filePath ) {
1106- try {
1107- LOGGER .info ("Unmounting store" );
1108- String umountCmd = String .format (UMOUNT_COMMAND , filePath );
1109- Script .runSimpleBashScript (umountCmd );
1110- try {
1111- Files .deleteIfExists (Paths .get (filePath ));
1112- } catch (IOException e ) {
1113- LOGGER .error (String .format ("Failed to cleanup mounted store at: %s" , filePath ), e );
1114- }
1115- } catch (Exception e ) {
1116- String msg = String .format ("Failed to unmount store mounted at %s" , filePath );
1117- LOGGER .error (msg , e );
1118- throw new CloudRuntimeException (msg , e );
1119- }
1120- }
1121-
11221158 /**
11231159 * Validate metadata for the given template's hypervisor/arch and add the existing template
11241160 * to the specified secondary store. On success, database entries are created/updated.
0 commit comments