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);
        }
Example #2
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);
        }
        public void UpgradeWithSetting()
        {
            string currentExecutingDirectory = Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location);
            string jsonFilePath    = Path.Combine(currentExecutingDirectory, "Application_No_Setting.json");
            string descriptionJson = File.ReadAllText(jsonFilePath);

            SingleInstance.Application application = JsonConvert.DeserializeObject <SingleInstance.Application>(descriptionJson);

            string applicationTypeName    = "MeshApplicationType";
            string applicationTypeVersion = "v1";
            Uri    applicationName        = new Uri("fabric:/myapp");
            string tempBuildPath          = Path.Combine(TestUtility.TestDirectory, Guid.NewGuid().ToString());
            string outputPath             = Path.Combine(TestUtility.TestDirectory, Guid.NewGuid().ToString());

            string applicationId = Guid.NewGuid().ToString();

            this.imageBuilder.BuildSingleInstanceApplication(
                application,
                applicationTypeName,
                applicationTypeVersion,
                applicationId,
                applicationName,
                true, //generateDNS
                TimeSpan.MaxValue,
                tempBuildPath,
                outputPath, // output pkg build path
                false,      //use open network
                false,      //use local nat network
                "C:\\",
                new SingleInstance.GenerationConfig());

            BuildLayoutInfo         buildLayoutInfo     = new BuildLayoutInfo(this.imageStore, tempBuildPath);
            ApplicationInstanceType applicationInstance = TestUtility.GetApplicationInstance(buildLayoutInfo, applicationId, 1);

            TestUtility.VerifyStoreLayout(buildLayoutInfo);

            StoreLayoutSpecification storeLayoutSpec = StoreLayoutSpecification.Create();

            storeLayoutSpec.SetRoot(outputPath);
            string servicePackageFile = storeLayoutSpec.GetServicePackageFile(applicationTypeName, applicationId, "myBackendServicePkg", applicationInstance.ServicePackageRef[0].RolloutVersion);
            var    servicepackage     = buildLayoutInfo.ImageStoreWrapper.GetFromStore <ServicePackageType>(servicePackageFile, TestUtility.ImageStoreDefaultTimeout);

            string rolloutVersion1 = servicepackage.RolloutVersion;

            // Test Case 1: Instance Count change
            string jsonFilePath2    = Path.Combine(currentExecutingDirectory, "Application_No_Setting2.json");
            string descriptionJson2 = File.ReadAllText(jsonFilePath2);

            SingleInstance.Application application2 = JsonConvert.DeserializeObject <SingleInstance.Application>(descriptionJson2);

            string applicationTypeVersion2 = "v2";

            this.imageBuilder.BuildSingleInstanceApplicationForUpgrade(
                application2,
                applicationTypeName,
                applicationTypeVersion,
                applicationTypeVersion2,
                applicationId,
                applicationInstance.Version,
                applicationName,
                true, //generateDNS
                TimeSpan.MaxValue,
                tempBuildPath,
                outputPath,
                false, //use open network
                false, //use local nat network
                "C:\\",
                new SingleInstance.GenerationConfig());

            ApplicationInstanceType applicationInstance2 = TestUtility.GetApplicationInstance(buildLayoutInfo, applicationId, 2);
            string servicePackageFile2 = storeLayoutSpec.GetServicePackageFile(applicationTypeName, applicationId, "myBackendServicePkg", applicationInstance2.ServicePackageRef[0].RolloutVersion);
            var    servicepackage2     = buildLayoutInfo.ImageStoreWrapper.GetFromStore <ServicePackageType>(servicePackageFile2, TestUtility.ImageStoreDefaultTimeout);

            Verify.AreEqual(servicepackage2.ManifestVersion, "v1");

            string rolloutVersion2 = servicepackage2.RolloutVersion;

            Verify.AreEqual(rolloutVersion1, rolloutVersion2);
            TestUtility.VerifyStoreLayout(buildLayoutInfo);

            // Test Case 2: Adding settings
            string jsonSettingFilePath1    = Path.Combine(currentExecutingDirectory, "Application_Setting1.json");
            string settingDescriptionJson1 = File.ReadAllText(jsonSettingFilePath1);

            SingleInstance.Application applicationWithSetting1 = JsonConvert.DeserializeObject <SingleInstance.Application>(settingDescriptionJson1);

            string applicationTypeVersion3 = "v3";

            this.imageBuilder.BuildSingleInstanceApplicationForUpgrade(
                applicationWithSetting1,
                applicationTypeName,
                applicationTypeVersion2,
                applicationTypeVersion3,
                applicationId,
                applicationInstance2.Version,
                applicationName,
                true, //generateDNS
                TimeSpan.MaxValue,
                tempBuildPath,
                outputPath,
                false, //use open network
                false, //use localnat network
                "C:\\",
                new SingleInstance.GenerationConfig());
            ApplicationInstanceType applicationInstance3 = TestUtility.GetApplicationInstance(buildLayoutInfo, applicationId, 3);
            string servicePackageFile3 = storeLayoutSpec.GetServicePackageFile(applicationTypeName, applicationId, "myBackendServicePkg", applicationInstance3.ServicePackageRef[0].RolloutVersion);
            var    servicepackage3     = buildLayoutInfo.ImageStoreWrapper.GetFromStore <ServicePackageType>(servicePackageFile3, TestUtility.ImageStoreDefaultTimeout);

            Verify.AreEqual(servicepackage3.ManifestVersion, applicationTypeVersion3);

            string rolloutVersion3 = servicepackage3.RolloutVersion;

            Verify.AreNotEqual(rolloutVersion2, rolloutVersion3);
            TestUtility.VerifyStoreLayout(buildLayoutInfo);

            // Test Case 3: Changing settings only
            string jsonSettingFilePath2    = Path.Combine(currentExecutingDirectory, "Application_Setting2.json");
            string settingDescriptionJson2 = File.ReadAllText(jsonSettingFilePath2);

            SingleInstance.Application applicationWithSetting2 = JsonConvert.DeserializeObject <SingleInstance.Application>(settingDescriptionJson2);

            string applicationTypeVersion4 = "v4";

            this.imageBuilder.BuildSingleInstanceApplicationForUpgrade(
                applicationWithSetting2,
                applicationTypeName,
                applicationTypeVersion3,
                applicationTypeVersion4,
                applicationId,
                applicationInstance3.Version,
                applicationName,
                true, //generateDNS
                TimeSpan.MaxValue,
                tempBuildPath,
                outputPath,
                false, //use open network
                false, //use localnat network
                "C:\\",
                new SingleInstance.GenerationConfig());
            ApplicationInstanceType applicationInstance4 = TestUtility.GetApplicationInstance(buildLayoutInfo, applicationId, 4);
            string servicePackageFile4 = storeLayoutSpec.GetServicePackageFile(applicationTypeName, applicationId, "myBackendServicePkg", applicationInstance4.ServicePackageRef[0].RolloutVersion);
            var    servicepackage4     = buildLayoutInfo.ImageStoreWrapper.GetFromStore <ServicePackageType>(servicePackageFile4, TestUtility.ImageStoreDefaultTimeout);

            Verify.AreEqual(servicepackage4.ManifestVersion, applicationTypeVersion4);

            // Code package version remains the same for settings upgrade
            foreach (var codePackage in servicepackage4.DigestedCodePackage)
            {
                Verify.AreEqual(applicationTypeVersion3, codePackage.CodePackage.Version);
            }
            // Config package version is set to target version for settings upgrade
            foreach (var configPackage in servicepackage4.DigestedConfigPackage)
            {
                Verify.AreEqual(applicationTypeVersion4, configPackage.ConfigPackage.Version);
            }

            string rolloutVersion4 = servicepackage4.RolloutVersion;

            Verify.AreNotEqual(rolloutVersion3, rolloutVersion4);
            TestUtility.VerifyStoreLayout(buildLayoutInfo);

            // Test Case 4: Keep settings
            string jsonSettingFilePath3    = Path.Combine(currentExecutingDirectory, "Application_Setting2InstanceCount.json");
            string settingDescriptionJson3 = File.ReadAllText(jsonSettingFilePath3);

            SingleInstance.Application applicationWithSetting3 = JsonConvert.DeserializeObject <SingleInstance.Application>(settingDescriptionJson3);

            string applicationTypeVersion5 = "v5";

            this.imageBuilder.BuildSingleInstanceApplicationForUpgrade(
                applicationWithSetting3,
                applicationTypeName,
                applicationTypeVersion4,
                applicationTypeVersion5,
                applicationId,
                applicationInstance4.Version,
                applicationName,
                true, //generateDNS
                TimeSpan.MaxValue,
                tempBuildPath,
                outputPath,
                false, //use open network
                false, //use localnat network
                "C:\\",
                new SingleInstance.GenerationConfig());
            ApplicationInstanceType applicationInstance5 = TestUtility.GetApplicationInstance(buildLayoutInfo, applicationId, 5);
            string servicePackageFile5 = storeLayoutSpec.GetServicePackageFile(applicationTypeName, applicationId, "myBackendServicePkg", applicationInstance5.ServicePackageRef[0].RolloutVersion);
            var    servicepackage5     = buildLayoutInfo.ImageStoreWrapper.GetFromStore <ServicePackageType>(servicePackageFile5, TestUtility.ImageStoreDefaultTimeout);

            // SP manifest version remains the same
            Verify.AreEqual(servicepackage5.ManifestVersion, applicationTypeVersion4);

            // SP rollout version remains the same
            string rolloutVersion5 = servicepackage5.RolloutVersion;

            Verify.AreEqual(rolloutVersion4, rolloutVersion5);
            TestUtility.VerifyStoreLayout(buildLayoutInfo);

            // Test Case 5: Autoscaling - Instance Count change
            string jsonSettingFilePath4    = Path.Combine(currentExecutingDirectory, "Application_Setting3Autoscaling.json");
            string settingDescriptionJson4 = File.ReadAllText(jsonSettingFilePath4);

            SingleInstance.Application applicationWithSetting4 = JsonConvert.DeserializeObject <SingleInstance.Application>(settingDescriptionJson4);

            string applicationTypeVersion6 = "v6";

            this.imageBuilder.BuildSingleInstanceApplicationForUpgrade(
                applicationWithSetting4,
                applicationTypeName,
                applicationTypeVersion5,
                applicationTypeVersion6,
                applicationId,
                applicationInstance5.Version,
                applicationName,
                true, //generateDNS
                TimeSpan.MaxValue,
                tempBuildPath,
                outputPath,
                false, //use open network
                false, //use localnat network
                "C:\\",
                new SingleInstance.GenerationConfig());

            ApplicationInstanceType applicationInstance6 = TestUtility.GetApplicationInstance(buildLayoutInfo, applicationId, 6);
            string servicePackageFile6 = storeLayoutSpec.GetServicePackageFile(applicationTypeName, applicationId, "myBackendServicePkg", applicationInstance6.ServicePackageRef[0].RolloutVersion);
            var    servicepackage6     = buildLayoutInfo.ImageStoreWrapper.GetFromStore <ServicePackageType>(servicePackageFile6, TestUtility.ImageStoreDefaultTimeout);

            Verify.AreEqual(servicepackage6.ManifestVersion, "v4");

            string rolloutVersion6 = servicepackage6.RolloutVersion;

            Verify.AreEqual(rolloutVersion5, rolloutVersion6);
            TestUtility.VerifyStoreLayout(buildLayoutInfo);

            // Test Case 6: Changing settings and image at the same time
            string jsonSettingFilePath5    = Path.Combine(currentExecutingDirectory, "Application_Setting3.json");
            string settingDescriptionJson5 = File.ReadAllText(jsonSettingFilePath5);

            SingleInstance.Application applicationWithSetting5 = JsonConvert.DeserializeObject <SingleInstance.Application>(settingDescriptionJson5);

            string applicationTypeVersion7 = "v7";

            this.imageBuilder.BuildSingleInstanceApplicationForUpgrade(
                applicationWithSetting5,
                applicationTypeName,
                applicationTypeVersion6,
                applicationTypeVersion7,
                applicationId,
                applicationInstance6.Version,
                applicationName,
                true, //generateDNS
                TimeSpan.MaxValue,
                tempBuildPath,
                outputPath,
                false, //use open network
                false, //use localnat network
                "C:\\",
                new SingleInstance.GenerationConfig());

            ApplicationInstanceType applicationInstance7 = TestUtility.GetApplicationInstance(buildLayoutInfo, applicationId, 7);
            string servicePackageFile7 = storeLayoutSpec.GetServicePackageFile(applicationTypeName, applicationId, "myBackendServicePkg", applicationInstance7.ServicePackageRef[0].RolloutVersion);
            var    servicepackage7     = buildLayoutInfo.ImageStoreWrapper.GetFromStore <ServicePackageType>(servicePackageFile7, TestUtility.ImageStoreDefaultTimeout);

            Verify.AreEqual(servicepackage7.ManifestVersion, applicationTypeVersion7);

            // Code package version and config package version are set to target version applicationTypeVersion7
            foreach (var codePackage in servicepackage7.DigestedCodePackage)
            {
                Verify.AreEqual(applicationTypeVersion7, codePackage.CodePackage.Version);
            }
            foreach (var configPackage in servicepackage7.DigestedConfigPackage)
            {
                Verify.AreEqual(applicationTypeVersion7, configPackage.ConfigPackage.Version);
            }

            string rolloutVersion7 = servicepackage7.RolloutVersion;

            Verify.AreNotEqual(rolloutVersion6, rolloutVersion7);
            TestUtility.VerifyStoreLayout(buildLayoutInfo);
        }