protected async Task <ApplicationInstanceContext> CreateAndSortInstanceAsync( int appInstanceVersion, Uri nameUri, TimeoutHelper timeoutHelper, ApplicationTypeContext validatedApplicationTypeContext = null) { ApplicationTypeContext applicationTypeContext = (validatedApplicationTypeContext == null) ? await this.GetApplicationTypeContextAsync(timeoutHelper) : validatedApplicationTypeContext; IDictionary <string, string> validatedParameters = VaidateAndMergeParameters(this.UserParameters, applicationTypeContext.ApplicationManifest.Parameters); ImageBuilder.TraceSource.WriteInfo( TraceType, "Creating ApplicationInstance, ApplicationPackage and ServicePackages from ApplicationManifest and ServiceManifest. ApplicationTypeName:{0}, ApplicationTypeVersion:{1}.", this.ApplicationTypeName, this.ApplicationTypeVersion); RolloutVersion defaultRolloutVersion = RolloutVersion.CreateRolloutVersion(); ApplicationInstanceBuilder applicationInstanceBuilder = new ApplicationInstanceBuilder(applicationTypeContext, validatedParameters, ImageStoreWrapper, timeoutHelper); ApplicationPackageType applicationPackage = applicationInstanceBuilder.BuildApplicationPackage(this.ApplicationId, nameUri, defaultRolloutVersion.ToString()); ApplicationInstanceType applicationInstance = applicationInstanceBuilder.BuildApplicationInstanceType(this.ApplicationId, nameUri, appInstanceVersion, defaultRolloutVersion.ToString()); IEnumerable <ServicePackageType> servicePackages = applicationInstanceBuilder.BuildServicePackage(RolloutVersion.CreateRolloutVersion(appInstanceVersion).ToString()); // Sort parameterizable elements in ApplicationPackage ImageBuilderSorterUtility.SortApplicationPackageType(applicationPackage); return(new ApplicationInstanceContext( applicationInstance, applicationPackage, servicePackages, validatedParameters)); }
public ApplicationInstanceContext( ApplicationInstanceType appInstance, ApplicationPackageType appPackage, IEnumerable <ServicePackageType> servicePackages, IDictionary <string, string> mergedApplicationParameters) { this.ApplicationInstance = appInstance; this.ApplicationPackage = appPackage; this.ServicePackages = servicePackages; this.MergedApplicationParameters = mergedApplicationParameters; }
public static void SortApplicationPackageType(ApplicationPackageType applicationPackage) { // Since User names and Group names can be parameterized, the actual values are known only after creating the ApplicationInstance. // Hence the Users and Groups on ApplicationInstance needs to be sorted. Non-parameterized name are sorted using the Manifest during // provisioning. applicationPackage.DigestedEnvironment.Principals.Groups = SortGroups(applicationPackage.DigestedEnvironment.Principals.Groups); applicationPackage.DigestedEnvironment.Principals.Users = SortUsers(applicationPackage.DigestedEnvironment.Principals.Users); if (applicationPackage.DigestedEnvironment.Diagnostics != null) { SortDiagnostics(applicationPackage.DigestedEnvironment.Diagnostics); } }
private void UpdateTargetApplicationPackage(ApplicationPackageType currentApplicationPackageType, ApplicationPackageType targetApplicationPackageType) { bool hasPoliciesChanged = ImageBuilderUtility.IsNotEqual <ApplicationPoliciesType>( currentApplicationPackageType.DigestedEnvironment.Policies, targetApplicationPackageType.DigestedEnvironment.Policies); bool hasPrincipalsChanged = ImageBuilderUtility.IsNotEqual <SecurityPrincipalsType>( currentApplicationPackageType.DigestedEnvironment.Principals, targetApplicationPackageType.DigestedEnvironment.Principals); bool hasDiagnosticsChanged = ImageBuilderUtility.IsNotEqual <DiagnosticsType>( currentApplicationPackageType.DigestedEnvironment.Diagnostics, targetApplicationPackageType.DigestedEnvironment.Diagnostics); bool hasApplicationEnvironmentChanged = hasPoliciesChanged || hasPrincipalsChanged || hasDiagnosticsChanged; bool hasSecretsCertificateChanged = ImageBuilderUtility.IsNotEqual <FabricCertificateType>( currentApplicationPackageType.DigestedCertificates.SecretsCertificate, targetApplicationPackageType.DigestedCertificates.SecretsCertificate); bool hasEndpointCertificateChanged = ImageBuilderUtility.IsNotEqual <EndpointCertificateType>( currentApplicationPackageType.DigestedCertificates.EndpointCertificate, targetApplicationPackageType.DigestedCertificates.EndpointCertificate); // Computer the target RolloutVersion RolloutVersion currentRolloutVersion = RolloutVersion.CreateRolloutVersion(currentApplicationPackageType.RolloutVersion); RolloutVersion targetRolloutVersion = currentRolloutVersion; if (hasApplicationEnvironmentChanged || hasSecretsCertificateChanged || hasEndpointCertificateChanged) { targetRolloutVersion = currentRolloutVersion.NextMajorRolloutVersion(); } // Update the RolloutVersion on the target ApplicationInstance targetApplicationPackageType.DigestedEnvironment.RolloutVersion = hasApplicationEnvironmentChanged ? targetRolloutVersion.ToString() : currentApplicationPackageType.DigestedEnvironment.RolloutVersion; targetApplicationPackageType.DigestedCertificates.RolloutVersion = (hasSecretsCertificateChanged || hasEndpointCertificateChanged) ? targetRolloutVersion.ToString() : currentApplicationPackageType.DigestedCertificates.RolloutVersion; targetApplicationPackageType.RolloutVersion = targetRolloutVersion.ToString(); }
public async Task UpgradeInstaceAsync(string outputFolder, TimeSpan timeout) { TimeoutHelper timeoutHelper = new TimeoutHelper(timeout); ImageBuilder.TraceSource.WriteInfo( TraceType, "Starting UpgradeInstace. ApplicationTypeName:{0}, TargetApplicationTypeVersion:{1}, CurrentApplicationInstance:{2}, ApplicationId:{3}, Timeout:{4}", this.ApplicationTypeName, this.ApplicationTypeVersion, this.currentApplicationInstanceVersion, this.ApplicationId, timeoutHelper.GetRemainingTime()); StoreLayoutSpecification storeLayoutSpecification = StoreLayoutSpecification.Create(); // Read the current ApplicationInstance and ApplicationPackage from the store string currentApplicationInstanceFile = storeLayoutSpecification.GetApplicationInstanceFile(this.ApplicationTypeName, this.ApplicationId, this.currentApplicationInstanceVersion.ToString(CultureInfo.InvariantCulture)); ApplicationInstanceType currentApplicationInstanceType = this.ImageStoreWrapper.GetFromStore <ApplicationInstanceType>(currentApplicationInstanceFile, timeoutHelper.GetRemainingTime()); string currentApplicationPackageFile = storeLayoutSpecification.GetApplicationPackageFile(this.ApplicationTypeName, this.ApplicationId, currentApplicationInstanceType.ApplicationPackageRef.RolloutVersion); ApplicationPackageType currentApplicationPackageType = this.ImageStoreWrapper.GetFromStore <ApplicationPackageType>(currentApplicationPackageFile, timeoutHelper.GetRemainingTime()); // Read the current ServicePackages from the store List <Task <ServicePackageType> > getServicePackageTasks = new List <Task <ServicePackageType> >(); TimeSpan remainingTime = timeoutHelper.GetRemainingTime(); foreach (ApplicationInstanceTypeServicePackageRef servicePackageRef in currentApplicationInstanceType.ServicePackageRef) { string currentServicePackageFile = storeLayoutSpecification.GetServicePackageFile( this.ApplicationTypeName, this.ApplicationId, servicePackageRef.Name, servicePackageRef.RolloutVersion); var getServicePackageTask = this.ImageStoreWrapper.GetFromStoreAsync <ServicePackageType>(currentServicePackageFile, remainingTime); getServicePackageTasks.Add(getServicePackageTask); } await Task.WhenAll(getServicePackageTasks); Collection <ServicePackageType> currentServicePackages = new Collection <ServicePackageType>(); getServicePackageTasks.ForEach(task => currentServicePackages.Add(task.Result)); timeoutHelper.ThrowIfExpired(); ApplicationInstanceContext targetAppInstanceContext = await base.CreateAndSortInstanceAsync( currentApplicationInstanceType.Version + 1, new Uri(currentApplicationPackageType.NameUri), timeoutHelper); // Validate the target ApplicationInstance and ServicePackages this.ValidateApplicationInstance(targetAppInstanceContext); // Update the Rollout version on the target ApplicationInstance this.UpdateTargetApplicationPackage(currentApplicationPackageType, targetAppInstanceContext.ApplicationPackage); targetAppInstanceContext.ApplicationInstance.ApplicationPackageRef.RolloutVersion = targetAppInstanceContext.ApplicationPackage.RolloutVersion; // Update the Rollout version on the target ServicePackages foreach (ServicePackageType targetServicePackage in targetAppInstanceContext.ServicePackages) { ServicePackageType matchingCurrentServicePackageType = currentServicePackages.FirstOrDefault( currentServicePackage => ImageBuilderUtility.Equals(currentServicePackage.Name, targetServicePackage.Name)); this.UpdateTargetServicePackage(matchingCurrentServicePackageType, targetServicePackage); ApplicationInstanceTypeServicePackageRef matchingServicePackageRef = targetAppInstanceContext.ApplicationInstance.ServicePackageRef.First( servicePackageRef => ImageBuilderUtility.Equals(servicePackageRef.Name, targetServicePackage.Name)); matchingServicePackageRef.RolloutVersion = targetServicePackage.RolloutVersion; } StoreLayoutSpecification clusterManagerOutputSpecification = null; if (outputFolder != null) { clusterManagerOutputSpecification = StoreLayoutSpecification.Create(); clusterManagerOutputSpecification.SetRoot(outputFolder); } timeoutHelper.ThrowIfExpired(); // Upload the target ApplicationInstance and ServicePackages to the store // Also, write the target ApplicationInstance and ServicePackages to the CM output folder await this.UploadInstanceAsync( targetAppInstanceContext.ApplicationInstance, targetAppInstanceContext.ApplicationPackage, targetAppInstanceContext.ServicePackages, storeLayoutSpecification, clusterManagerOutputSpecification, false, timeoutHelper); // Write the current ApplicationInstance and ServicePackages to the CM output folder await this.UploadInstanceAsync( currentApplicationInstanceType, currentApplicationPackageType, currentServicePackages, null /* Do not upload to store*/, clusterManagerOutputSpecification, true, timeoutHelper); ImageBuilder.TraceSource.WriteInfo( TraceType, "Completed UpgradeInstace. ApplicationTypeName:{0}, TargetApplicationTypeVersion:{1}, CurrentApplicationInstance:{2}, ApplicationId:{3}", this.ApplicationTypeName, this.ApplicationTypeVersion, this.currentApplicationInstanceVersion, this.ApplicationId); }
protected async Task UploadInstanceAsync( ApplicationInstanceType applicationInstanceType, ApplicationPackageType applicationPackageType, IEnumerable <ServicePackageType> servicePackages, StoreLayoutSpecification storeLayoutSpecification /* If null, don't upload to store */, StoreLayoutSpecification clusterManagerOutputSpecification /* If null, dont write to output folder*/, bool shouldOverwrite, TimeoutHelper timeoutHelper) { List <Task> uploadTasks = new List <Task>(); // Write ApplicationInstance to Store if (storeLayoutSpecification != null) { ImageBuilder.TraceSource.WriteInfo( TraceType, "Starting to upload the ApplicationInstance, ApplicationPackage, ServicePackage to the store. ApplicationTypeName:{0}, ApplicationTypeVersion:{1}.", applicationInstanceType.ApplicationTypeName, applicationInstanceType.ApplicationTypeVersion); string applicationInstanceLocation = storeLayoutSpecification.GetApplicationInstanceFile( applicationInstanceType.ApplicationTypeName, applicationInstanceType.ApplicationId, applicationInstanceType.Version.ToString(CultureInfo.InvariantCulture)); var uploadInstanceTask = this.ImageStoreWrapper.SetToStoreAsync <ApplicationInstanceType>(applicationInstanceLocation, applicationInstanceType, timeoutHelper.GetRemainingTime(), shouldOverwrite); uploadTasks.Add(uploadInstanceTask); string applicationPackageLocation = storeLayoutSpecification.GetApplicationPackageFile( applicationPackageType.ApplicationTypeName, applicationPackageType.ApplicationId, applicationPackageType.RolloutVersion); var uploadApplicationPackageTask = this.ImageStoreWrapper.SetToStoreAsync <ApplicationPackageType>(applicationPackageLocation, applicationPackageType, timeoutHelper.GetRemainingTime(), shouldOverwrite); uploadTasks.Add(uploadApplicationPackageTask); TimeSpan remainingTime = timeoutHelper.GetRemainingTime(); foreach (ServicePackageType servicePackage in servicePackages) { // Write ServicePackage to Store string servicePackageLocation = storeLayoutSpecification.GetServicePackageFile( applicationInstanceType.ApplicationTypeName, applicationInstanceType.ApplicationId, servicePackage.Name, servicePackage.RolloutVersion); var uploadServicePackageTask = this.ImageStoreWrapper.SetToStoreAsync <ServicePackageType>(servicePackageLocation, servicePackage, remainingTime, shouldOverwrite); uploadTasks.Add(uploadServicePackageTask); } await Task.WhenAll(uploadTasks); } // Write ApplicationInstance to the outputFolder for the CM if (clusterManagerOutputSpecification != null) { ImageBuilder.TraceSource.WriteInfo( TraceType, "Starting to write the ApplicationInstance, ApplicationPackage, ServicePackage to folder {0}. ApplicationTypeName:{1}, ApplicationTypeVersion:{2}.", clusterManagerOutputSpecification.GetRoot(), applicationInstanceType.ApplicationTypeName, applicationInstanceType.ApplicationTypeVersion); string clusterManagerApplicationInstanceLocation = clusterManagerOutputSpecification.GetApplicationInstanceFile( applicationInstanceType.ApplicationTypeName, applicationInstanceType.ApplicationId, applicationInstanceType.Version.ToString(CultureInfo.InvariantCulture)); ImageBuilderUtility.WriteXml <ApplicationInstanceType>(clusterManagerApplicationInstanceLocation, applicationInstanceType); string clusterManagerApplicationPackageLocation = clusterManagerOutputSpecification.GetApplicationPackageFile( applicationPackageType.ApplicationTypeName, applicationPackageType.ApplicationId, applicationPackageType.RolloutVersion); ImageBuilderUtility.WriteXml <ApplicationPackageType>(clusterManagerApplicationPackageLocation, applicationPackageType); foreach (ServicePackageType servicePackage in servicePackages) { string clusterManagerServicePackageLocation = clusterManagerOutputSpecification.GetServicePackageFile( applicationInstanceType.ApplicationTypeName, applicationInstanceType.ApplicationId, servicePackage.Name, servicePackage.RolloutVersion); ImageBuilderUtility.WriteXml <ServicePackageType>(clusterManagerServicePackageLocation, servicePackage); } } ImageBuilder.TraceSource.WriteInfo( TraceType, "Completed uploading/writing ApplicationInstance, ApplicationPackage, ServicePackage to the store/folder. ApplicationTypeName:{0}, ApplicationTypeVersion:{1}.", applicationInstanceType.ApplicationTypeName, applicationInstanceType.ApplicationTypeVersion); }