8787import com .cloud .utils .script .Script ;
8888import com .cloud .vm .dao .VMInstanceDao ;
8989import com .cloud .vm .dao .VMInstanceDaoImpl ;
90+ import org .apache .cloudstack .engine .subsystem .api .storage .ObjectInDataStoreStateMachine ;
91+ import org .apache .cloudstack .framework .config .dao .ConfigurationDao ;
92+ import org .apache .cloudstack .framework .config .dao .ConfigurationDaoImpl ;
93+ import org .apache .cloudstack .framework .config .impl .ConfigurationVO ;
94+ import org .apache .cloudstack .storage .datastore .db .ImageStoreDao ;
95+ import org .apache .cloudstack .storage .datastore .db .ImageStoreDaoImpl ;
96+ import org .apache .cloudstack .storage .datastore .db .ImageStoreDetailsDao ;
97+ import org .apache .cloudstack .storage .datastore .db .ImageStoreDetailsDaoImpl ;
98+ import org .apache .cloudstack .storage .datastore .db .ImageStoreVO ;
99+ import org .apache .cloudstack .storage .datastore .db .TemplateDataStoreDao ;
100+ import org .apache .cloudstack .storage .datastore .db .TemplateDataStoreVO ;
101+ import org .apache .cloudstack .utils .security .DigestHelper ;
102+ import org .apache .commons .lang3 .StringUtils ;
103+ import org .apache .logging .log4j .Logger ;
104+ import org .apache .logging .log4j .LogManager ;
105+ import org .ini4j .Ini ;
106+
107+ import javax .inject .Inject ;
108+ import java .io .BufferedReader ;
109+ import java .io .File ;
110+ import java .io .FileReader ;
111+ import java .io .IOException ;
112+ import java .net .URI ;
113+ import java .nio .file .Files ;
114+ import java .nio .file .Path ;
115+ import java .nio .file .Paths ;
116+ import java .sql .Connection ;
117+ import java .sql .Date ;
118+ import java .util .ArrayList ;
119+ import java .util .Arrays ;
120+ import java .util .Collection ;
121+ import java .util .Collections ;
122+ import java .util .HashMap ;
123+ import java .util .HashSet ;
124+ import java .util .List ;
125+ import java .util .Locale ;
126+ import java .util .Map ;
127+ import java .util .Objects ;
128+ import java .util .Set ;
129+ import java .util .UUID ;
130+ import java .util .stream .Collectors ;
90131
91132public class SystemVmTemplateRegistration {
92133 protected static Logger LOGGER = LogManager .getLogger (SystemVmTemplateRegistration .class );
@@ -768,81 +809,76 @@ private static void cleanupStore(Long templateId, String filePath) {
768809 }
769810 }
770811
771- private void validateTemplates1 (Set <Hypervisor .HypervisorType > hypervisorsInUse ) {
772- Set <String > hypervisors = hypervisorsInUse .stream ().
773- map (Hypervisor .HypervisorType ::name ).map (name -> name .toLowerCase (Locale .ROOT )).map (this ::getHypervisorName ).collect (Collectors .toSet ());
774- List <String > templates = new ArrayList <>();
775- for (Hypervisor .HypervisorType hypervisorType : hypervisorsInUse ) {
776- templates .add (NewTemplateMap .get (hypervisorType ).getFilename ());
777- }
778-
812+ private void validateTemplates (List <Pair <Hypervisor .HypervisorType , String >> hypervisorsArchInUse ) {
779813 boolean templatesFound = true ;
780- for (String hypervisor : hypervisors ) {
781- String matchedTemplate = templates .stream ().filter (x -> x .contains (hypervisor )).findAny ().orElse (null );
814+ for (Pair <Hypervisor .HypervisorType , String > hypervisorArch : hypervisorsArchInUse ) {
815+ MetadataTemplateDetails matchedTemplate = NewTemplateMap .values ()
816+ .stream ()
817+ .filter (x -> x .getHypervisorType ().equals (hypervisorArch .first ()) &&
818+ Objects .equals (x .getArch (), hypervisorArch .second ()))
819+ .findAny ()
820+ .orElse (null );
782821 if (matchedTemplate == null ) {
783822 templatesFound = false ;
784823 break ;
785824 }
786-
787- File tempFile = new File (TEMPLATES_PATH + matchedTemplate );
825+ if (CPU .archARM64Identifier .equals (matchedTemplate .getArch ())) {
826+ LOGGER .debug ("Skipping checksum comparison for the template file and metadata as the arch for template is {}" ,
827+ matchedTemplate .getArch ());
828+ }
829+ File tempFile = new File (TEMPLATES_PATH + matchedTemplate .getFilename ());
788830 String templateChecksum = DigestHelper .calculateChecksum (tempFile );
789- String checksumFromMap = NewTemplateMap . get ( getHypervisorType ( hypervisor )) .getChecksum ();
790- if (! templateChecksum . equals ( checksumFromMap )) {
791- LOGGER . error ( "Checksum mismatch: {} != {}" , templateChecksum , checksumFromMap );
831+ if (! templateChecksum . equals ( matchedTemplate .getChecksum ())) {
832+ LOGGER . error ( "Checksum {} for file {} does not match checksum {} from metadata" ,
833+ templateChecksum , matchedTemplate . getChecksum () );
792834 templatesFound = false ;
793835 break ;
794836 }
795837 }
796-
797838 if (!templatesFound ) {
798839 String errMsg = "SystemVm template not found. Cannot upgrade system Vms" ;
799840 LOGGER .error (errMsg );
800841 throw new CloudRuntimeException (errMsg );
801842 }
802843 }
803844
804- private void validateTemplates ( Set < Hypervisor . HypervisorType > hypervisorsInUse ) {
805- Set <String > hypervisors = hypervisorsInUse . stream ().
806- map ( Hypervisor . HypervisorType :: name ). map ( name -> name . toLowerCase ( Locale . ROOT )). map ( this :: getHypervisorName ). collect ( Collectors . toSet ());
807- List < String > templates = new ArrayList <>( );
808- for ( Hypervisor . HypervisorType hypervisorType : hypervisorsInUse ) {
809- templates . add ( NewTemplateMap . get ( hypervisorType ). getFilename ());
810- }
811-
812- boolean templatesFound = true ;
813- for ( String hypervisor : hypervisors ) {
814- String matchedTemplate = templates . stream (). filter ( x -> x . contains ( hypervisor )). findAny (). orElse ( null );
815- if ( matchedTemplate == null ) {
816- templatesFound = false ;
817- break ;
818- }
819-
820- File tempFile = new File ( TEMPLATES_PATH + matchedTemplate );
821- String templateChecksum = DigestHelper . calculateChecksum ( tempFile );
822- String checksumFromMap = NewTemplateMap . get ( getHypervisorType ( hypervisor )). getChecksum ();
823- if (! templateChecksum . equals ( checksumFromMap )) {
824- LOGGER . error ( "Checksum mismatch: {} != {}" , templateChecksum , checksumFromMap );
825- templatesFound = false ;
826- break ;
845+ protected void registerTemplatesForZone ( long zoneId ) {
846+ Pair <String , Long > storeUrlAndId = getNfsStoreInZone ( zoneId );
847+ String nfsVersion = getNfsVersion ( storeUrlAndId . second ());
848+ mountStore ( storeUrlAndId . first (), filePath , nfsVersion );
849+ List < String > hypervisorList = fetchAllHypervisors ( zoneId );
850+ for ( String hypervisor : hypervisorList ) {
851+ Hypervisor . HypervisorType hypervisorType = Hypervisor . HypervisorType . getType ( hypervisor );
852+ MetadataTemplateDetails templateDetails = getMetadataTemplateDetails ( hypervisorType , null );
853+ Long templateId = getRegisteredTemplateId ( templateDetails . getName ()) ;
854+ if ( templateId != null ) {
855+ VMTemplateVO templateVO = vmTemplateDao . findById ( templateId );
856+ TemplateDataStoreVO templateDataStoreVO = templateDataStoreDao . findByStoreTemplate ( storeUrlAndId . second (), templateId );
857+ if ( templateDataStoreVO != null ) {
858+ String installPath = templateDataStoreVO . getInstallPath () ;
859+ if ( validateIfSeeded ( storeUrlAndId . first (), installPath , nfsVersion )) {
860+ continue ;
861+ }
862+ }
863+ if ( templateVO != null ) {
864+ registerTemplate ( hypervisorType , templateDetails . getName (), storeUrlAndId , templateVO , templateDataStoreVO , filePath );
865+ updateRegisteredTemplateDetails ( templateId , templateDetails );
866+ continue ;
867+ }
827868 }
828- }
829-
830- if (!templatesFound ) {
831- String errMsg = "SystemVm template not found. Cannot upgrade system Vms" ;
832- LOGGER .error (errMsg );
833- throw new CloudRuntimeException (errMsg );
869+ registerTemplate (hypervisorType , templateDetails .getName (), storeUrlAndId , filePath );
834870 }
835871 }
836872
837- public void registerTemplates (Set < Hypervisor .HypervisorType > hypervisorsInUse ) {
873+ public void registerTemplates (List < Pair < Hypervisor .HypervisorType , String >> hypervisorsArchInUse ) {
838874 GlobalLock lock = GlobalLock .getInternLock ("UpgradeDatabase-Lock" );
839875 try {
840876 LOGGER .info ("Grabbing lock to register templates." );
841877 if (!lock .lock (LOCK_WAIT_TIMEOUT )) {
842878 throw new CloudRuntimeException ("Unable to acquire lock to register SystemVM template." );
843879 }
844880 try {
845- validateTemplates (hypervisorsInUse );
881+ validateTemplates (hypervisorsArchInUse );
846882 // Perform Registration if templates not already registered
847883 Transaction .execute (new TransactionCallbackNoReturn () {
848884 @ Override
@@ -855,31 +891,7 @@ public void doInTransactionWithoutResult(final TransactionStatus status) {
855891 if (filePath == null ) {
856892 throw new CloudRuntimeException ("Failed to create temporary file path to mount the store" );
857893 }
858- Pair <String , Long > storeUrlAndId = getNfsStoreInZone (zoneId );
859- String nfsVersion = getNfsVersion (storeUrlAndId .second ());
860- mountStore (storeUrlAndId .first (), filePath , nfsVersion );
861- List <String > hypervisorList = fetchAllHypervisors (zoneId );
862- for (String hypervisor : hypervisorList ) {
863- Hypervisor .HypervisorType hypervisorType = Hypervisor .HypervisorType .getType (hypervisor );
864- MetadataTemplateDetails templateDetails = getMetadataTemplateDetails (hypervisorType , null );
865- Long templateId = getRegisteredTemplateId (templateDetails .getName ());
866- if (templateId != null ) {
867- VMTemplateVO templateVO = vmTemplateDao .findById (templateId );
868- TemplateDataStoreVO templateDataStoreVO = templateDataStoreDao .findByStoreTemplate (storeUrlAndId .second (), templateId );
869- if (templateDataStoreVO != null ) {
870- String installPath = templateDataStoreVO .getInstallPath ();
871- if (validateIfSeeded (templateDataStoreVO , storeUrlAndId .first (), installPath , nfsVersion )) {
872- continue ;
873- }
874- }
875- if (templateVO != null ) {
876- registerTemplate (hypervisorType , templateDetails .getName (), storeUrlAndId , templateVO , templateDataStoreVO , filePath );
877- updateRegisteredTemplateDetails (templateId , templateDetails );
878- continue ;
879- }
880- }
881- registerTemplate (hypervisorType , templateDetails .getName (), storeUrlAndId , filePath );
882- }
894+ registerTemplatesForZone (zoneId );
883895 unmountStore (filePath );
884896 } catch (Exception e ) {
885897 unmountStore (filePath );
@@ -947,7 +959,10 @@ public void doInTransactionWithoutResult(final TransactionStatus status) {
947959 if (templateId != null ) {
948960 updateRegisteredTemplateDetails (templateId , templateDetails );
949961 } else {
950- if (hypervisorsInUse .stream ().anyMatch ().contains (templateDetails .getHypervisorType ())) {
962+ boolean isHypervisorArchMatchMetadata = hypervisorsInUse .stream ()
963+ .anyMatch (p -> p .first ().equals (templateDetails .getHypervisorType ())
964+ || Objects .equals (p .second (), templateDetails .getArch ()));
965+ if (isHypervisorArchMatchMetadata ) {
951966 try {
952967 registerTemplates (hypervisorsInUse );
953968 break ;
0 commit comments