Esempio n. 1
0
        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);
        }