Ejemplo n.º 1
0
        public static bool IsEqual <T>(T param1, T param2)
            where T : class
        {
            if (param1 == null && param2 == null)
            {
                return(true);
            }

            if (param1 == null || param2 == null)
            {
                return(false);
            }

            var serializer = new XmlSerializer(typeof(T));

            using (MemoryStream stream1 = new MemoryStream(), stream2 = new MemoryStream())
            {
                serializer.Serialize(stream1, param1);
                serializer.Serialize(stream2, param2);

                stream1.Seek(0, SeekOrigin.Begin);
                stream2.Seek(0, SeekOrigin.Begin);

                return(ImageBuilderUtility.Equals(new StreamReader(stream1).ReadToEnd(), new StreamReader(stream2).ReadToEnd()));
            }
        }
        private static string GetProductVersionFromFilename(string codePath, string expectedFileSuffix)
        {
            string fileName = Path.GetFileName(codePath);

            string[] firstTokens = fileName.Split('_');
            if (firstTokens.Length != 2 || !ImageBuilderUtility.Equals(firstTokens[0], StringConstants.ServiceFabric))
            {
                ThrowInvalidCodeUpgradeFileName(fileName);
            }

            string[] secondTokens = firstTokens[1].Split('.');
            if (secondTokens.Length != 5 || !ImageBuilderUtility.Equals(secondTokens[4], expectedFileSuffix))
            {
                ThrowInvalidCodeUpgradeFileName(fileName);
            }

            for (int i = 0; i < secondTokens.Length - 1; i++)
            {
                int value;
                if (!int.TryParse(secondTokens[i], out value))
                {
                    ThrowInvalidCodeUpgradeFileName(fileName);
                }
            }

            return(string.Format(CultureInfo.InvariantCulture, "{0}.{1}.{2}.{3}", secondTokens[0], secondTokens[1], secondTokens[2], secondTokens[3]));
        }
Ejemplo n.º 3
0
        private void IntializeTargetPackageWithCurrentRolloutVersion(ServicePackageType currentServicePackage, ServicePackageType targetServicePackage)
        {
            //Set target rolloutversion to current before comparison
            for (int i = 0; i < targetServicePackage.DigestedCodePackage.Length; i++)
            {
                ServicePackageTypeDigestedCodePackage targetDigestedCodePackage = targetServicePackage.DigestedCodePackage[i];

                ServicePackageTypeDigestedCodePackage matchingCurrentDigestedCodePackage = currentServicePackage.DigestedCodePackage.FirstOrDefault(
                    digestedCodePackage => ImageBuilderUtility.Equals(digestedCodePackage.CodePackage.Name, targetDigestedCodePackage.CodePackage.Name));

                if (matchingCurrentDigestedCodePackage != null)
                {
                    targetDigestedCodePackage.RolloutVersion = matchingCurrentDigestedCodePackage.RolloutVersion;
                }
            }

            //Set target rolloutversion for config to current before comparison
            if (targetServicePackage.DigestedConfigPackage != null)
            {
                foreach (ServicePackageTypeDigestedConfigPackage targetDigestedConfigPackage in targetServicePackage.DigestedConfigPackage)
                {
                    ServicePackageTypeDigestedConfigPackage matchingCurrentDigestedConfigPackage = null;
                    if (currentServicePackage.DigestedConfigPackage != null)
                    {
                        matchingCurrentDigestedConfigPackage = currentServicePackage.DigestedConfigPackage.FirstOrDefault(
                            digestedConfigPackage => ImageBuilderUtility.Equals(digestedConfigPackage.ConfigPackage.Name, targetDigestedConfigPackage.ConfigPackage.Name));

                        if (matchingCurrentDigestedConfigPackage != null)
                        {
                            targetDigestedConfigPackage.RolloutVersion = matchingCurrentDigestedConfigPackage.RolloutVersion;
                        }
                    }
                }
            }

            //Set RolloutVersion for matching data packages to be same as current
            if (targetServicePackage.DigestedDataPackage != null)
            {
                foreach (ServicePackageTypeDigestedDataPackage targetDigestedDataPackage in targetServicePackage.DigestedDataPackage)
                {
                    ServicePackageTypeDigestedDataPackage matchingCurrentDigestedDataPackage = null;
                    if (currentServicePackage.DigestedDataPackage != null)
                    {
                        matchingCurrentDigestedDataPackage = currentServicePackage.DigestedDataPackage.FirstOrDefault(
                            digestedDataPackage => ImageBuilderUtility.Equals(digestedDataPackage.DataPackage.Name, targetDigestedDataPackage.DataPackage.Name));

                        if (matchingCurrentDigestedDataPackage != null)
                        {
                            targetDigestedDataPackage.RolloutVersion = matchingCurrentDigestedDataPackage.RolloutVersion;
                        }
                    }
                }
            }
        }
 private void VerifySourceLocation(string settingName, string sectionName, string type)
 {
     if (!ImageBuilderUtility.Equals("PlainText", type) &&
         !ImageBuilderUtility.Equals("Encrypted", type) &&
         !ImageBuilderUtility.Equals("SecretsStoreRef", type))
     {
         ImageBuilderUtility.TraceAndThrowValidationError(
             TraceType,
             StringResources.ImageBuilderError_ConfigOverrideTypeMismatch,
             settingName,
             "Type",
             sectionName,
             type,
             "PlainText/Encrypted/SecretsStoreRef");
     }
 }
            public bool AssemblyExists(AssemblyName assemblyName)
            {
                if (isArchive)
                {
                    return(ImageBuilderUtility.IsAnyInArchive(this.DirectoryOrArchivePath, new string[]
                    {
                        string.Format(CultureInfo.InvariantCulture, "{0}.exe", assemblyName.Name),
                        string.Format(CultureInfo.InvariantCulture, "{0}.dll", assemblyName.Name)
                    }));
                }
                else
                {
                    string searchPattern = string.Format(CultureInfo.InvariantCulture, "{0}.*", assemblyName.Name);
                    var    matchingFiles = Directory.EnumerateFiles(this.DirectoryOrArchivePath, searchPattern, SearchOption.AllDirectories);
                    int    count         = matchingFiles.Count(file => ImageBuilderUtility.Equals(Path.GetExtension(file), ".exe") || ImageBuilderUtility.Equals(Path.GetExtension(file), ".dll"));

                    return(count > 0);
                }
            }
        private static string ParseFileNameForProductVersion(string codePath)
        {
            string fileName = Path.GetFileName(codePath);

            string[] tokens = fileName.Split('.');
            if (tokens.Length != 6 ||
                !ImageBuilderUtility.Equals(tokens[0], StringConstants.WindowsFabric) ||
                !ImageBuilderUtility.Equals(tokens[5], StringConstants.Msi))
            {
                ThrowInvalidCodeUpgradeFileName(fileName);
            }

            for (int i = 1; i < tokens.Length - 1; i++)
            {
                int value;
                if (!int.TryParse(tokens[i], out value))
                {
                    ThrowInvalidCodeUpgradeFileName(fileName);
                }
            }

            return(string.Format(CultureInfo.InvariantCulture, "{0}.{1}.{2}.{3}", tokens[1], tokens[2], tokens[3], tokens[4]));
        }
Ejemplo n.º 7
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);
        }
Ejemplo n.º 8
0
        private void UpdateTargetServicePackage(ServicePackageType currentServicePackage, ServicePackageType targetServicePackage)
        {
            if (currentServicePackage == null)
            {
                // New ServicePackage added
                return;
            }

            IntializeTargetPackageWithCurrentRolloutVersion(currentServicePackage, targetServicePackage);

            var hasServiceTypesChanged = ImageBuilderUtility.IsNotEqual(
                currentServicePackage.DigestedServiceTypes.ServiceTypes,
                targetServicePackage.DigestedServiceTypes.ServiceTypes);

            var hasResourcesChanged = ImageBuilderUtility.IsNotEqual(
                currentServicePackage.DigestedResources.DigestedEndpoints,
                targetServicePackage.DigestedResources.DigestedEndpoints);

            if (!hasResourcesChanged)
            {
                hasResourcesChanged =
                    ImageBuilderUtility.IsNotEqual(
                        currentServicePackage.DigestedResources.DigestedCertificates,
                        targetServicePackage.DigestedResources.DigestedCertificates);
            }

            var hasDiagnosticsChanged = ImageBuilderUtility.IsNotEqual(
                currentServicePackage.Diagnostics,
                targetServicePackage.Diagnostics);

            var hasCodePackagesChanged = ImageBuilderUtility.IsNotEqual(
                currentServicePackage.DigestedCodePackage,
                targetServicePackage.DigestedCodePackage);

            var hasConfigPackagesChanged = ImageBuilderUtility.IsNotEqual(
                currentServicePackage.DigestedConfigPackage,
                targetServicePackage.DigestedConfigPackage);

            var hasDataPackagesChanged = ImageBuilderUtility.IsNotEqual(
                currentServicePackage.DigestedDataPackage,
                targetServicePackage.DigestedDataPackage);

            var requiresContainerGroupSetup = RequiresContainerGroupSetup(
                currentServicePackage,
                targetServicePackage);

            var isOnDemandActivationImpacted = false;

            if (hasCodePackagesChanged)
            {
                isOnDemandActivationImpacted = this.IsOnDemandCodePackageActivationImapcted(
                    currentServicePackage.DigestedCodePackage,
                    targetServicePackage.DigestedCodePackage);
            }

            // Compute the target RolloutVersion
            var            currentRolloutVersion = RolloutVersion.CreateRolloutVersion(currentServicePackage.RolloutVersion);
            RolloutVersion targetRolloutVersion  = null;

            if (hasResourcesChanged || hasServiceTypesChanged || requiresContainerGroupSetup || isOnDemandActivationImpacted)
            {
                targetRolloutVersion = currentRolloutVersion.NextMajorRolloutVersion();
            }
            else if (hasCodePackagesChanged || hasConfigPackagesChanged || hasDataPackagesChanged || hasDiagnosticsChanged)
            {
                targetRolloutVersion = currentRolloutVersion.NextMinorRolloutVersion();
            }

            // Update the RolloutVersion on the target ServicePackage
            string targetRolloutVerionString = (targetRolloutVersion != null) ? targetRolloutVersion.ToString() : null;

            targetServicePackage.RolloutVersion = (targetRolloutVerionString != null) ? targetRolloutVerionString : currentServicePackage.RolloutVersion;
            targetServicePackage.DigestedServiceTypes.RolloutVersion = hasServiceTypesChanged ? targetRolloutVerionString : currentServicePackage.DigestedServiceTypes.RolloutVersion;
            targetServicePackage.DigestedResources.RolloutVersion    = hasResourcesChanged ? targetRolloutVerionString : currentServicePackage.DigestedResources.RolloutVersion;

            foreach (var targetDigestedCodePackage in targetServicePackage.DigestedCodePackage)
            {
                var matchingCurrentDigestedCodePackage = currentServicePackage.DigestedCodePackage.FirstOrDefault(
                    digestedCodePackage => ImageBuilderUtility.Equals(digestedCodePackage.CodePackage.Name, targetDigestedCodePackage.CodePackage.Name));

                if (matchingCurrentDigestedCodePackage != null)
                {
                    targetDigestedCodePackage.RolloutVersion = hasCodePackagesChanged && ImageBuilderUtility.IsNotEqual(matchingCurrentDigestedCodePackage, targetDigestedCodePackage)
                        ? targetRolloutVerionString
                        : matchingCurrentDigestedCodePackage.RolloutVersion;
                }
                else
                {
                    targetDigestedCodePackage.RolloutVersion = targetRolloutVerionString;
                }
            }

            if (targetServicePackage.DigestedConfigPackage != null)
            {
                foreach (ServicePackageTypeDigestedConfigPackage targetDigestedConfigPackage in targetServicePackage.DigestedConfigPackage)
                {
                    bool hasChanged = true;
                    ServicePackageTypeDigestedConfigPackage matchingCurrentDigestedConfigPackage = null;
                    if (currentServicePackage.DigestedConfigPackage != null)
                    {
                        matchingCurrentDigestedConfigPackage = currentServicePackage.DigestedConfigPackage.FirstOrDefault(
                            digestedConfigPackage => ImageBuilderUtility.Equals(digestedConfigPackage.ConfigPackage.Name, targetDigestedConfigPackage.ConfigPackage.Name));

                        if (matchingCurrentDigestedConfigPackage != null)
                        {
                            if (hasConfigPackagesChanged)
                            {
                                hasChanged = ImageBuilderUtility.IsNotEqual <ServicePackageTypeDigestedConfigPackage>(
                                    matchingCurrentDigestedConfigPackage,
                                    targetDigestedConfigPackage);
                            }
                            else
                            {
                                hasChanged = false;
                            }
                        }
                    }

                    targetDigestedConfigPackage.RolloutVersion = hasChanged ? targetRolloutVerionString : matchingCurrentDigestedConfigPackage.RolloutVersion;
                }
            }

            if (targetServicePackage.DigestedDataPackage != null)
            {
                foreach (ServicePackageTypeDigestedDataPackage targetDigestedDataPackage in targetServicePackage.DigestedDataPackage)
                {
                    bool hasChanged = true;
                    ServicePackageTypeDigestedDataPackage matchingCurrentDigestedDataPackage = null;
                    if (currentServicePackage.DigestedDataPackage != null)
                    {
                        matchingCurrentDigestedDataPackage = currentServicePackage.DigestedDataPackage.FirstOrDefault(
                            digestedDataPackage => ImageBuilderUtility.Equals(digestedDataPackage.DataPackage.Name, targetDigestedDataPackage.DataPackage.Name));

                        if (matchingCurrentDigestedDataPackage != null)
                        {
                            if (hasDataPackagesChanged)
                            {
                                hasChanged = ImageBuilderUtility.IsNotEqual <ServicePackageTypeDigestedDataPackage>(
                                    matchingCurrentDigestedDataPackage,
                                    targetDigestedDataPackage);
                            }
                            else
                            {
                                hasChanged = false;
                            }
                        }
                    }

                    targetDigestedDataPackage.RolloutVersion = hasChanged ? targetRolloutVerionString : matchingCurrentDigestedDataPackage.RolloutVersion;
                }
            }
        }
        private void ValidateConfiguration(ApplicationTypeContext context)
        {
            Dictionary <string, IEnumerable <ConfigPackage> > configurationPackageDictionary = new Dictionary <string, IEnumerable <ConfigPackage> >();

            foreach (var serviceManifest in context.ServiceManifests)
            {
                configurationPackageDictionary.Add(serviceManifest.ServiceManifestType.Name, serviceManifest.ConfigPackages);
            }

            ApplicationManifestTypeServiceManifestImport[] serviceManifestImports = context.ApplicationManifest.ServiceManifestImport;
            bool hasEncryptedParameter = false;

            foreach (KeyValuePair <string, IEnumerable <ConfigPackage> > configKeyValuePair in configurationPackageDictionary)
            {
                if (configKeyValuePair.Value != null)
                {
                    ApplicationManifestTypeServiceManifestImport matchingServiceManifestImport = serviceManifestImports.FirstOrDefault <ApplicationManifestTypeServiceManifestImport>(
                        serviceManifestImport => ImageBuilderUtility.Equals(serviceManifestImport.ServiceManifestRef.ServiceManifestName, configKeyValuePair.Key));

                    ReleaseAssert.AssertIf(
                        matchingServiceManifestImport == null,
                        "Could not find matching ServiceManifest with Name {0}",
                        configKeyValuePair.Key);

                    foreach (ConfigPackage configurationPackage in configKeyValuePair.Value)
                    {
                        ImageBuilder.TraceSource.WriteInfo(
                            TraceType,
                            "Validating the configuration. ApplicationTypeName:{0}, ServiceManifestName:{1}, ConfigPackageName:{2}",
                            context.ApplicationManifest.ApplicationTypeName,
                            matchingServiceManifestImport.ServiceManifestRef.ServiceManifestName,
                            configurationPackage.ConfigPackageType.Name);

                        if (configurationPackage.SettingsType != null && configurationPackage.SettingsType.Section != null)
                        {
                            string configPackageDirectory = context.BuildLayoutSpecification.GetConfigPackageFolder(configKeyValuePair.Key, configurationPackage.ConfigPackageType.Name);
                            string configPackageArchive   = context.BuildLayoutSpecification.GetSubPackageArchiveFile(configPackageDirectory);

                            if (!FabricDirectory.Exists(configPackageDirectory) && !FabricFile.Exists(configPackageArchive))
                            {
                                continue;
                            }

                            ConfigOverrideType matchingConfigOverride = null;
                            if (matchingServiceManifestImport.ConfigOverrides != null)
                            {
                                matchingConfigOverride = matchingServiceManifestImport.ConfigOverrides.FirstOrDefault <ConfigOverrideType>(
                                    configOverride => ImageBuilderUtility.Equals(configOverride.Name, configurationPackage.ConfigPackageType.Name));
                            }

                            string settingsFileName = context.BuildLayoutSpecification.GetSettingsFile(configPackageDirectory);

                            DuplicateDetector sectionDuplicateDetector = new DuplicateDetector("Section", "Name", settingsFileName);
                            foreach (SettingsTypeSection section in configurationPackage.SettingsType.Section)
                            {
                                sectionDuplicateDetector.Add(section.Name);

                                SettingsOverridesTypeSection matchingConfigOverrideSection = null;
                                if (matchingConfigOverride != null && matchingConfigOverride.Settings != null)
                                {
                                    matchingConfigOverrideSection = matchingConfigOverride.Settings.FirstOrDefault <SettingsOverridesTypeSection>(
                                        configOverrideSection => ImageBuilderUtility.Equals(configOverrideSection.Name, section.Name));
                                }

                                if (section.Parameter != null)
                                {
                                    DuplicateDetector settingDuplicateDetector = new DuplicateDetector("Parameter", "Name", settingsFileName);
                                    foreach (SettingsTypeSectionParameter parameter in section.Parameter)
                                    {
                                        settingDuplicateDetector.Add(parameter.Name);

                                        if (!hasEncryptedParameter)
                                        {
                                            hasEncryptedParameter = parameter.IsEncrypted;
                                        }

                                        if (parameter.MustOverride)
                                        {
                                            SettingsOverridesTypeSectionParameter matchingOverrideParameter = null;
                                            if (matchingConfigOverrideSection != null && matchingConfigOverrideSection.Parameter != null)
                                            {
                                                matchingOverrideParameter = matchingConfigOverrideSection.Parameter.FirstOrDefault <SettingsOverridesTypeSectionParameter>(
                                                    configOverrideSetting => ImageBuilderUtility.Equals(configOverrideSetting.Name, parameter.Name));
                                            }

                                            if (matchingOverrideParameter == null)
                                            {
                                                ImageBuilderUtility.TraceAndThrowValidationErrorWithFileName(
                                                    TraceType,
                                                    settingsFileName,
                                                    StringResources.ImageBuilderError_ParameterNotOverriden,
                                                    parameter.Name);
                                            }
                                        }

                                        if (!String.IsNullOrEmpty(parameter.Type))
                                        {
                                            this.VerifySourceLocation(parameter.Name, "Settings.xml", parameter.Type);
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }

            if (hasEncryptedParameter &&
                RequireCertACLingForUsers(context.ApplicationManifest.Principals) &&
                (context.ApplicationManifest.Certificates == null || context.ApplicationManifest.Certificates.SecretsCertificate == null))
            {
                ImageBuilderUtility.TraceAndThrowValidationErrorWithFileName(
                    TraceType,
                    context.GetApplicationManifestFileName(),
                    StringResources.ImageBuilderError_EncryptedSettingsNoCertInAppManifest);
            }
        }
        private void ValidateServiceManifests(ApplicationTypeContext context)
        {
            IEnumerable <ServiceManifestType> serviceManifestTypes = context.GetServiceManifestTypes();

            // Validates the ServiceManifest
            var duplicateServiceTypeDetector = new DuplicateDetector("ServiceType", "ServiceTypeName");

            foreach (ServiceManifestType serviceManifestType in serviceManifestTypes)
            {
                ImageBuilder.TraceSource.WriteInfo(
                    TraceType,
                    "Validating ServiceManifest. ApplicationTypeName:{0}, ServiceManifestName:{1}, ServiceManifestVersion:{2}",
                    context.ApplicationManifest.ApplicationTypeName,
                    serviceManifestType.Name,
                    serviceManifestType.Version);

                string serviceManifestFileName = context.GetServiceManifestFileName(serviceManifestType.Name);

                if (!ManifestValidatorUtility.IsValidName(serviceManifestType.Name))
                {
                    ImageBuilderUtility.TraceAndThrowValidationErrorWithFileName(
                        TraceType,
                        serviceManifestFileName,
                        StringResources.ImageBuilderError_InvalidName_arg2,
                        "ServiceManifest",
                        serviceManifestType.Name,
                        Path.DirectorySeparatorChar,
                        StringConstants.DoubleDot);
                }

                if (!ManifestValidatorUtility.IsValidVersion(serviceManifestType.Version))
                {
                    ImageBuilderUtility.TraceAndThrowValidationErrorWithFileName(
                        TraceType,
                        serviceManifestFileName,
                        StringResources.ImageBuilderError_InvalidVersion_arg2,
                        "ServiceManifestVersion",
                        serviceManifestType.Version,
                        Path.DirectorySeparatorChar,
                        StringConstants.DoubleDot);
                }

                var implicitServiceTypeCount = 0;
                var normalServiceTypeCount   = 0;
                var hasServiceGroupTypes     = false;

                foreach (object manifestServiceTypeType in serviceManifestType.ServiceTypes)
                {
                    string serviceTypeName      = null;
                    string placementConstraints = null;
                    if (manifestServiceTypeType is ServiceTypeType serviceTypeType)
                    {
                        serviceTypeName      = serviceTypeType.ServiceTypeName;
                        placementConstraints = serviceTypeType.PlacementConstraints;

                        if (IsImplicitServiceType(serviceTypeType))
                        {
                            ++implicitServiceTypeCount;
                        }
                        else
                        {
                            ++normalServiceTypeCount;
                        }
                    }
                    else
                    {
                        hasServiceGroupTypes = true;

                        ServiceGroupTypeType serviceGroupTypeType = (ServiceGroupTypeType)manifestServiceTypeType;
                        serviceTypeName      = serviceGroupTypeType.ServiceGroupTypeName;
                        placementConstraints = serviceGroupTypeType.PlacementConstraints;
                    }

                    this.ValidatePlacementConstraints(serviceManifestFileName, placementConstraints);

                    duplicateServiceTypeDetector.Add(serviceTypeName);
                }

                this.ValidateServiceTypeCombination(
                    serviceManifestFileName,
                    implicitServiceTypeCount,
                    normalServiceTypeCount,
                    hasServiceGroupTypes);

                var activatorCodePackageCount = 0;

                // Validates the CodePackage in the ServiceManifest
                var codePackageDuplicateDetector = new DuplicateDetector("CodePackage", "Name", serviceManifestFileName);
                foreach (CodePackageType codePackageType in serviceManifestType.CodePackage)
                {
                    if (!ManifestValidatorUtility.IsValidName(codePackageType.Name))
                    {
                        ImageBuilderUtility.TraceAndThrowValidationErrorWithFileName(
                            TraceType,
                            serviceManifestFileName,
                            StringResources.ImageBuilderError_InvalidName_arg2,
                            "CodePackage",
                            codePackageType.Name,
                            Path.DirectorySeparatorChar,
                            StringConstants.DoubleDot);
                    }

                    if (!ManifestValidatorUtility.IsValidVersion(codePackageType.Version))
                    {
                        ImageBuilderUtility.TraceAndThrowValidationErrorWithFileName(
                            TraceType,
                            serviceManifestFileName,
                            StringResources.ImageBuilderError_InvalidVersion_arg2,
                            "CodePackageVersion",
                            codePackageType.Version,
                            Path.DirectorySeparatorChar,
                            StringConstants.DoubleDot);
                    }

                    codePackageDuplicateDetector.Add(codePackageType.Name);

                    if (codePackageType.IsActivator)
                    {
                        if (implicitServiceTypeCount > 0)
                        {
                            ImageBuilderUtility.TraceAndThrowValidationErrorWithFileName(
                                TraceType,
                                serviceManifestFileName,
                                StringResources.ImageBuilderError_GuestAppWithActivatorCodePackage);
                        }

                        activatorCodePackageCount++;
                        if (activatorCodePackageCount > 1)
                        {
                            ImageBuilderUtility.TraceAndThrowValidationErrorWithFileName(
                                TraceType,
                                serviceManifestFileName,
                                StringResources.ImageBuilderError_MultipleActivatorCodePackage);
                        }
                    }

                    var codePackageFolderName  = context.BuildLayoutSpecification.GetCodePackageFolder(serviceManifestType.Name, codePackageType.Name);
                    var codePackageArchiveName = context.BuildLayoutSpecification.GetSubPackageArchiveFile(codePackageFolderName);

                    FileLocator fileLocator = null;
                    if (FabricFile.Exists(codePackageArchiveName))
                    {
                        fileLocator = new FileLocator(codePackageArchiveName, true);
                    }
                    else if (FabricDirectory.Exists(codePackageFolderName))
                    {
                        fileLocator = new FileLocator(codePackageFolderName, false);
                    }
                    else
                    {
                        // If the code package doesn't exist, it means it was already provisioned;
                        // validation must have passed at the time of first provisioning
                        continue;
                    }

                    if (codePackageType.SetupEntryPoint != null)
                    {
                        this.ValidateEntryPointPath(serviceManifestFileName, fileLocator, codePackageType.SetupEntryPoint.ExeHost.Program);
                    }

                    if (codePackageType.EntryPoint != null && codePackageType.EntryPoint.Item != null)
                    {
                        if (codePackageType.EntryPoint.Item is DllHostEntryPointType)
                        {
                            this.ValidateEntryPoint(serviceManifestFileName, fileLocator, (DllHostEntryPointType)codePackageType.EntryPoint.Item);
                        }
                        else if (codePackageType.EntryPoint.Item is ExeHostEntryPointType)
                        {
                            this.ValidateEntryPoint(serviceManifestFileName, fileLocator, (ExeHostEntryPointType)codePackageType.EntryPoint.Item);
                        }
                    }
                }

                // Validates the ConfigPackage in the ServiceManifest
                if (serviceManifestType.ConfigPackage != null)
                {
                    DuplicateDetector configPackageDuplicateDetector = new DuplicateDetector("ConfigPackage", "Name", serviceManifestFileName);
                    foreach (ConfigPackageType configPackageType in serviceManifestType.ConfigPackage)
                    {
                        if (!ManifestValidatorUtility.IsValidName(configPackageType.Name))
                        {
                            ImageBuilderUtility.TraceAndThrowValidationErrorWithFileName(
                                TraceType,
                                serviceManifestFileName,
                                StringResources.ImageBuilderError_InvalidName_arg2,
                                "ConfigPackage",
                                configPackageType.Name,
                                Path.DirectorySeparatorChar,
                                StringConstants.DoubleDot);
                        }

                        if (!ManifestValidatorUtility.IsValidVersion(configPackageType.Version))
                        {
                            ImageBuilderUtility.TraceAndThrowValidationErrorWithFileName(
                                TraceType,
                                serviceManifestFileName,
                                StringResources.ImageBuilderError_InvalidVersion_arg2,
                                "ConfigPackageVersion",
                                configPackageType.Version,
                                Path.DirectorySeparatorChar,
                                StringConstants.DoubleDot);
                        }

                        configPackageDuplicateDetector.Add(configPackageType.Name);
                    }
                }

                // Validates the DataPackage in the ServiceManifest
                if (serviceManifestType.DataPackage != null)
                {
                    DuplicateDetector dataPackageDuplicateDetector = new DuplicateDetector("DataPackage", "Name", serviceManifestFileName);
                    foreach (DataPackageType dataPackageType in serviceManifestType.DataPackage)
                    {
                        if (!ManifestValidatorUtility.IsValidName(dataPackageType.Name))
                        {
                            ImageBuilderUtility.TraceAndThrowValidationErrorWithFileName(
                                TraceType,
                                serviceManifestFileName,
                                StringResources.ImageBuilderError_InvalidName_arg2,
                                "DataPackage",
                                dataPackageType.Name,
                                Path.DirectorySeparatorChar,
                                StringConstants.DoubleDot);
                        }

                        if (!ManifestValidatorUtility.IsValidVersion(dataPackageType.Version))
                        {
                            ImageBuilderUtility.TraceAndThrowValidationErrorWithFileName(
                                TraceType,
                                serviceManifestFileName,
                                StringResources.ImageBuilderError_InvalidVersion_arg2,
                                "DataPackageVersion",
                                dataPackageType.Version,
                                Path.DirectorySeparatorChar,
                                StringConstants.DoubleDot);
                        }

                        dataPackageDuplicateDetector.Add(dataPackageType.Name);
                    }
                }

                // Validates the Resources in the ServiceManifest
                if (serviceManifestType.Resources != null && serviceManifestType.Resources.Endpoints != null)
                {
                    DuplicateDetector resourceNameDuplicateDetector = new DuplicateDetector("Endpoint", "Name", serviceManifestFileName);
                    foreach (var endpoint in serviceManifestType.Resources.Endpoints)
                    {
                        if (endpoint.Name.Contains(';') || endpoint.Name.Contains(','))
                        {
                            ImageBuilderUtility.TraceAndThrowValidationErrorWithFileName(
                                TraceType,
                                serviceManifestFileName,
                                StringResources.ImageBuilderError_InvalidName_arg2,
                                "EndppointResource",
                                endpoint.Name,
                                ",",
                                ";");
                        }

                        if (!string.IsNullOrEmpty(endpoint.CodePackageRef))
                        {
                            var matchingCodePackage = serviceManifestType.CodePackage.FirstOrDefault(
                                codePackageType => ImageBuilderUtility.Equals(codePackageType.Name, endpoint.CodePackageRef));

                            if (matchingCodePackage == null)
                            {
                                ImageBuilderUtility.TraceAndThrowValidationErrorWithFileName(
                                    TraceType,
                                    serviceManifestFileName,
                                    StringResources.ImageBuilderError_InvalidRefSameFile,
                                    "CodePackageRef",
                                    endpoint.CodePackageRef,
                                    "EndppointResource",
                                    endpoint.Name,
                                    "CodePackage",
                                    "ServiceManifest");
                            }
                        }

                        resourceNameDuplicateDetector.Add(endpoint.Name);
                    }
                }
            }
        }
Ejemplo n.º 11
0
 public static bool NotEquals(string string1, string string2)
 {
     return(!ImageBuilderUtility.Equals(string1, string2));
 }
Ejemplo n.º 12
0
        public void UpgradeFabric(
            string currentFabricVersion,
            string targetFabricVersion,
#if !DotNetCoreClrLinux && !DotNetCoreClrIOT
            string configurationCsvFilePath,
#endif
            TimeSpan timeout,
            out bool isConfigOnly)
        {
            isConfigOnly = false;

            TimeoutHelper timeoutHelper = new TimeoutHelper(timeout);

            ImageBuilder.TraceSource.WriteInfo(
                TraceType,
                "UpgradeFabric started: CurrentFabricVersion:{0}, TargetFabricVersion:{1}, Timeout:{2}",
                currentFabricVersion,
                targetFabricVersion,
                timeoutHelper.GetRemainingTime());

            // If the current version is invalid (ie. Cluster FabricVersion is not determined)
            // then do not validate
            if (ImageBuilderUtility.Equals(currentFabricVersion, FabricVersion.Invalid.ToString()))
            {
                return;
            }

            FabricVersion currentVersion, targetVersion;

            bool isValid = FabricVersion.TryParse(currentFabricVersion, out currentVersion);

            if (!isValid)
            {
                ImageBuilderUtility.TraceAndThrowValidationError(
                    TraceType,
                    StringResources.ImageBuilderError_InvalidValue,
                    "CurrentFabricVersion",
                    currentFabricVersion);
            }

            isValid = FabricVersion.TryParse(targetFabricVersion, out targetVersion);
            if (!isValid)
            {
                ImageBuilderUtility.TraceAndThrowValidationError(
                    TraceType,
                    StringResources.ImageBuilderError_InvalidValue,
                    "TargetFabricVersion",
                    targetFabricVersion);
            }

            WinFabStoreLayoutSpecification winFabLayout = WinFabStoreLayoutSpecification.Create();
            string currentClusterManifestPath           = winFabLayout.GetClusterManifestFile(currentVersion.ConfigVersion);
            string targetClusterManifestPath            = winFabLayout.GetClusterManifestFile(targetVersion.ConfigVersion);

            ClusterManifestType currentClusterManifest = this.imageStoreWrapper.GetFromStore <ClusterManifestType>(currentClusterManifestPath, timeoutHelper.GetRemainingTime());
            ClusterManifestType targetClusterManifest  = this.imageStoreWrapper.GetFromStore <ClusterManifestType>(targetClusterManifestPath, timeoutHelper.GetRemainingTime());

            timeoutHelper.ThrowIfExpired();

            try
            {
                // todo: Vaishnav to pass node list object loaded from the location
                FabricValidator fabricValidator = FabricValidator.Create(currentClusterManifest, null, new WindowsFabricSettings(currentClusterManifest), DeploymentOperations.Update);
                IEnumerable <KeyValuePair <string, string> > modifiedStaticSettings = fabricValidator.CompareAndAnalyze(targetClusterManifest, null /*Detect changes for any NodeType*/);
                isConfigOnly = !modifiedStaticSettings.Any();
            }
            catch (Exception e)
            {
                ImageBuilderUtility.TraceAndThrowValidationError(
                    TraceType,
                    StringResources.ImageBuilderError_InvalidConfigFabricUpgrade,
                    e.ToString(),
                    currentFabricVersion,
                    targetFabricVersion);
            }

            ImageBuilder.TraceSource.WriteInfo(
                TraceType,
                "UpgradeFabric completed: CurrentFabricVersion:{0}, TargetFabricVersion:{1}, ConfigOnly:{2}",
                currentFabricVersion,
                targetFabricVersion,
                isConfigOnly);
        }