private async Task <ApplicationTypeContext> GetApplicationTypeContextAsync(TimeoutHelper timeoutHelper)
        {
            StoreLayoutSpecification storeLayoutSpecification = StoreLayoutSpecification.Create();

            // Read the the ApplicationManifest from the store
            string applicationManifestFile = storeLayoutSpecification.GetApplicationManifestFile(this.ApplicationTypeName, this.ApplicationTypeVersion);
            ApplicationManifestType applicationManifestType = await this.ImageStoreWrapper.GetFromStoreAsync <ApplicationManifestType>(applicationManifestFile, timeoutHelper.GetRemainingTime());

            ApplicationTypeContext applicationTypeContext = new ApplicationTypeContext(applicationManifestType, string.Empty);

            // Read the Collection of ServiceManifests associated with the ApplicationManifest from the store
            List <Task <ServiceManifest> > taskList = new List <Task <ServiceManifest> >();
            TimeSpan remainingTime = timeoutHelper.GetRemainingTime();

            foreach (ApplicationManifestTypeServiceManifestImport serviceManifestImport in applicationManifestType.ServiceManifestImport)
            {
                taskList.Add(this.GetServiceManifestAsync(serviceManifestImport, storeLayoutSpecification, remainingTime));
            }

            await Task.WhenAll(taskList);

            taskList.ForEach(task => applicationTypeContext.ServiceManifests.Add(task.Result));

            return(applicationTypeContext);
        }
예제 #2
0
        public static void CompareAndFixTargetManifestVersionsForUpgrade(
            ApplicationManifestType currentApplicationManifest,
            ApplicationManifestType targetApplicationManifest,
            IList <ServiceManifestType> currentServiceManifests,
            ref IList <ServiceManifestType> targetServiceManifests)
        {
            // Service manifest imports that exist in both current and target.
            var targetServiceManifestImports = targetApplicationManifest.ServiceManifestImport;
            var commonServiceManifestImports = currentApplicationManifest.ServiceManifestImport.Where(
                ci => targetServiceManifestImports.Any(ti => ti.ServiceManifestRef.ServiceManifestName == ci.ServiceManifestRef.ServiceManifestName));

            List <ServiceManifestType> unchangedServiceManifests = new List <ServiceManifestType>();

            foreach (ApplicationManifestTypeServiceManifestImport currentManifestImport in commonServiceManifestImports)
            {
                var currentManifest = currentServiceManifests.FirstOrDefault(item => item.Name == currentManifestImport.ServiceManifestRef.ServiceManifestName);
                var targetManifest  = targetServiceManifests.FirstOrDefault(item => item.Name == currentManifestImport.ServiceManifestRef.ServiceManifestName);
                // TODO: would codepackage versions be different than manifest version?
                var currentManifestVersion = currentManifest.Version;
                var targetManifestVersion  = targetManifest.Version;

                var equal = ImageBuilderUtility.IsEqual <ServiceManifestType>(
                    ReplaceVersion(currentManifest, "0"), ReplaceVersion(targetManifest, "0"));

                ReplaceVersion(currentManifest, currentManifestVersion);
                ReplaceVersion(targetManifest, targetManifestVersion);
                if (equal)
                {
                    unchangedServiceManifests.Add(currentManifest);
                }
            }

            // Use the unchanged manifests.
            if (unchangedServiceManifests.Count() != 0)
            {
                List <ServiceManifestType> updatedTargetServiceManifests = new List <ServiceManifestType>();
                foreach (var manifest in targetServiceManifests)
                {
                    var unchangedManifest = unchangedServiceManifests.FirstOrDefault(item => item.Name == manifest.Name);
                    if (unchangedManifest != default(ServiceManifestType))
                    {
                        updatedTargetServiceManifests.Add(unchangedManifest);
                        for (int i = 0; i < targetApplicationManifest.ServiceManifestImport.Count(); ++i)
                        {
                            if (targetApplicationManifest.ServiceManifestImport[i].ServiceManifestRef.ServiceManifestName == manifest.Name)
                            {
                                targetApplicationManifest.ServiceManifestImport[i].ServiceManifestRef.ServiceManifestVersion = unchangedManifest.Version;
                                break;
                            }
                        }
                    }
                    else
                    {
                        updatedTargetServiceManifests.Add(manifest);
                    }
                }

                targetServiceManifests = updatedTargetServiceManifests;
            }
        }
예제 #3
0
        private ApplicationManifestTypeServiceManifestImport GetServiceManifestImport(
            ApplicationManifestType applicationManifestType,
            string serviceNamePrefix,
            ComposeServiceTypeDescription serviceTypeDescription,
            out DefaultServicesTypeService defaultServiceEntry,
            out ServiceManifestType serviceManifest)
        {
            IList <PortBindingType> portBindingList;

            serviceManifest = this.CreateServiceManifest(
                serviceNamePrefix,
                applicationManifestType.ApplicationTypeVersion,
                serviceTypeDescription,
                out portBindingList);

            var serviceManifestImport = new ApplicationManifestTypeServiceManifestImport()
            {
                ServiceManifestRef = new ServiceManifestRefType()
                {
                    ServiceManifestName    = serviceManifest.Name,
                    ServiceManifestVersion = serviceManifest.Version
                },
            };

            // Environment variables
            if (serviceTypeDescription.EnvironmentVariables.Count > 0)
            {
                serviceManifestImport.EnvironmentOverrides = new EnvironmentOverridesType[1];

                // one codepackage
                serviceManifestImport.EnvironmentOverrides[0] = this.GetEnvironmentOverrides(serviceTypeDescription,
                                                                                             serviceManifest);
            }

            var servicePackageResourceGovernance = this.GetServicePackageResourceGovernancePolicy(serviceTypeDescription);
            var resourceGovernancePolicies       = this.GetResourceGovernancePolicies(serviceTypeDescription, serviceManifest);

            // Resource governance policy for CP, policy for SP and ContainerHost policy
            serviceManifestImport.Policies = new object[resourceGovernancePolicies.Count + 2];

            int index = 0;

            if (resourceGovernancePolicies.Count != 0)
            {
                resourceGovernancePolicies.ToArray().CopyTo(serviceManifestImport.Policies, index);
                index += resourceGovernancePolicies.Count;
            }

            // Resource governance on SP level
            serviceManifestImport.Policies[index] = servicePackageResourceGovernance;
            ++index;

            // Container Host policies
            serviceManifestImport.Policies[index] = this.GetContainerHostPolicy(serviceTypeDescription, serviceManifest,
                                                                                portBindingList);

            defaultServiceEntry = this.GetDefaultServiceTypeEntry(serviceNamePrefix, serviceTypeDescription);

            return(serviceManifestImport);
        }
예제 #4
0
        public ApplicationTypeContext(ApplicationManifestType appManifestType, string appPath)
        {
            this.ApplicationManifest = appManifestType;
            this.ServiceManifests    = new Collection <ServiceManifest>();

            this.BuildLayoutSpecification = BuildLayoutSpecification.Create();
            this.BuildLayoutSpecification.SetRoot(appPath);

            this.InitializeAndValidateApplicationParameters(appManifestType.Parameters);
        }
예제 #5
0
        public static void GenerateApplicationPackage(
            ApplicationManifestType applicationManifest,
            IList <ServiceManifestType> serviceManifests,
            string packageOutputLocation)
        {
            WriteFile(GetApplicationManifestFilePath(packageOutputLocation), SerializeXml(applicationManifest));

            foreach (var serviceManifest in serviceManifests)
            {
                WriteFile(GetServiceManifestFilePath(packageOutputLocation, serviceManifest.Name), SerializeXml(serviceManifest));
            }
        }
        public static void SaveAppManifest(string v, ApplicationManifestType appManifest)
        {
            if (File.Exists(v))
            {
                File.Delete(v);
            }

            var dir = Path.GetDirectoryName(v);

            if (!Directory.Exists(dir))
            {
                Directory.CreateDirectory(dir);
            }

            using (var stream = new StreamWriter(v))
                appManifestSerializer.Serialize(stream, appManifest);
        }
예제 #7
0
        public static void GenerateApplicationPackage(
            ApplicationManifestType applicationManifest,
            IList <ServiceManifestType> serviceManifests,
            Dictionary <string, Dictionary <string, SettingsType> > settingsType,
            string packageOutputLocation)
        {
            GenerateApplicationPackage(applicationManifest, serviceManifests, packageOutputLocation);

            foreach (var settingsByService in settingsType)
            {
                string serviceManifestName = settingsByService.Key;
                foreach (var settingsTypeByConfig in settingsByService.Value)
                {
                    string configPackageName = settingsTypeByConfig.Key;
                    WriteFile(GetSettingXmlFilePath(packageOutputLocation, serviceManifestName, configPackageName), SerializeXml(settingsTypeByConfig.Value));
                }
            }
        }
        public static ApplicationManifestType SetGitVersion(this ApplicationManifestType appManifest, string baseVersion, IEnumerable <FabricServiceReference> fabricServiceReferences, string configuration, int maxHashLength, bool skipHash)
        {
            DateTime latest = DateTime.MinValue;
            string   version = "", diff = "";

            foreach (var spr in fabricServiceReferences)
            {
                var serviceReferencePath = Path.Combine(Path.GetDirectoryName(spr.ProjectPath), "pkg", configuration);
                var versionFile          = Path.Combine(serviceReferencePath, "Version.txt");
                var diffFile             = Path.Combine(serviceReferencePath, "Diff.txt");

                if (!Directory.Exists(serviceReferencePath) || !File.Exists(versionFile) || !File.Exists(diffFile))
                {
                    continue;
                }

                var commit = File.ReadAllText(versionFile).Split(' ');

                var serviceManifest = FabricSerializers.ServiceManifestFromFile(Path.Combine(serviceReferencePath, "ServiceManifest.xml"));
                appManifest.ServiceManifestImport
                .First(smi => smi.ServiceManifestRef.ServiceManifestName == spr.ServiceManifestName).ServiceManifestRef
                .ServiceManifestVersion = serviceManifest.Version;

                if (long.TryParse(commit[1], out var d))
                {
                    var date = new DateTime(d);
                    if (new DateTime(d) > latest)
                    {
                        version = commit[0];
                        latest  = new DateTime(d);
                    }
                }

                if (!skipHash)
                {
                    diff += File.ReadAllText(diffFile);
                }
            }

            appManifest.ApplicationTypeVersion = VersionHelper.AppendVersion(baseVersion, version, VersionHelper.Hash(diff, maxHashLength));
            return(appManifest);
        }
예제 #9
0
파일: Program.cs 프로젝트: tg123/sfrun
        private static ApplicationManifestType GenerateApplicationManifest(string app, string version)
        {
            var applicationManifest = new ApplicationManifestType();

            applicationManifest.ApplicationTypeName    = $"{app}SFApp";
            applicationManifest.ApplicationTypeVersion = version;

            applicationManifest.ServiceManifestImport = new[]
            {
                new ApplicationManifestTypeServiceManifestImport()
                {
                    ServiceManifestRef =
                        new ServiceManifestRefType()
                    {
                        ServiceManifestName    = $"{app}SFServicePkg",
                        ServiceManifestVersion = version,
                    }
                }
            };


            applicationManifest.DefaultServices = new DefaultServicesType()
            {
                Items = new[]
                {
                    new DefaultServicesTypeService()
                    {
                        Name = $"{app}SFService",
                        Item =
                            new StatelessServiceType()
                        {
                            ServiceTypeName    = $"{app}SFServiceType",
                            InstanceCount      = "-1",
                            SingletonPartition = new ServiceTypeSingletonPartition()
                        }
                    }
                }
            };

            return(applicationManifest);
        }
예제 #10
0
        public static void SortApplicationManifestType(ApplicationManifestType applicationManifest)
        {
            applicationManifest.Parameters            = SortApplicationParameters(applicationManifest.Parameters);
            applicationManifest.ServiceManifestImport = SortServiceManifestImports(applicationManifest.ServiceManifestImport);
            applicationManifest.ServiceTemplates      = SortServiceTemplates(applicationManifest.ServiceTemplates);
            applicationManifest.DefaultServices       = SortDefaultServices(applicationManifest.DefaultServices);

            if (applicationManifest.Principals != null)
            {
                applicationManifest.Principals.Groups = SortGroups(applicationManifest.Principals.Groups);
                applicationManifest.Principals.Users  = SortUsers(applicationManifest.Principals.Users);
            }

            if (applicationManifest.Policies != null)
            {
                if (applicationManifest.Policies.LogCollectionPolicies != null)
                {
                    applicationManifest.Policies.LogCollectionPolicies.LogCollectionPolicy = SortLogCollectionPolicies(applicationManifest.Policies.LogCollectionPolicies.LogCollectionPolicy);
                }
                if (applicationManifest.Policies.SecurityAccessPolicies != null)
                {
                    applicationManifest.Policies.SecurityAccessPolicies.SecurityAccessPolicy = applicationManifest.Policies.SecurityAccessPolicies.SecurityAccessPolicy.OrderBy(
                        securityAccessPolicy => securityAccessPolicy.ResourceRef).ToArray();
                }
            }
            if (applicationManifest.Certificates != null)
            {
                if (applicationManifest.Certificates.SecretsCertificate != null)
                {
                    applicationManifest.Certificates.SecretsCertificate = applicationManifest.Certificates.SecretsCertificate.OrderBy(
                        secretCertificate => secretCertificate.Name).ToArray();
                }
                if (applicationManifest.Certificates.EndpointCertificate != null)
                {
                    applicationManifest.Certificates.EndpointCertificate = applicationManifest.Certificates.EndpointCertificate.OrderBy(
                        endpointCertificate => endpointCertificate.Name).ToArray();
                }
            }
        }
예제 #11
0
        private void CreateManifests(out ApplicationManifestType applicationManifest,
                                     out IList <ServiceManifestType> serviceManifests)
        {
            applicationManifest = new ApplicationManifestType()
            {
                ApplicationTypeName    = this.composeApplicationTypeDescription.ApplicationTypeName,
                ApplicationTypeVersion = this.composeApplicationTypeDescription.ApplicationTypeVersion,
                ServiceManifestImport  =
                    new ApplicationManifestTypeServiceManifestImport[
                        this.composeApplicationTypeDescription.ServiceTypeDescriptions.Count],
                DefaultServices = new DefaultServicesType()
                {
                    Items = new object[this.composeApplicationTypeDescription.ServiceTypeDescriptions.Count]
                }
            };

            var index = 0;

            serviceManifests = new List <ServiceManifestType>();
            foreach (var servicetype in this.composeApplicationTypeDescription.ServiceTypeDescriptions)
            {
                ServiceManifestType        serviceManifest;
                DefaultServicesTypeService defaultService;
                var serviceManifestImport = this.GetServiceManifestImport(
                    applicationManifest,
                    servicetype.Key,
                    servicetype.Value,
                    out defaultService,
                    out serviceManifest);

                applicationManifest.ServiceManifestImport[index] = serviceManifestImport;
                applicationManifest.DefaultServices.Items[index] = defaultService;

                serviceManifests.Add(serviceManifest);
                ++index;
            }
        }
예제 #12
0
        public void LoadParameters()
        {
            Manifest = FabricSerializers.AppManifestFromFile(Path.Combine(PackagePath, "ApplicationManifest.xml"));
            Version  = Manifest.ApplicationTypeVersion;
            var parameters = new Dictionary <string, string>();

            if (Manifest.Parameters != null)
            {
                foreach (var p in Manifest.Parameters)
                {
                    parameters[p.Name] = p.DefaultValue;
                }
            }

            if (!string.IsNullOrWhiteSpace(ParameterFilePath))
            {
                var x = XElement.Load(ParameterFilePath);
                foreach (var p in x.Element(x.Name.Namespace + "Parameters").Elements(x.Name.Namespace + "Parameter"))
                {
                    parameters[p.Attribute("Name").Value] = p.Attribute("Value").Value;
                }
            }

            if (Parameters != null)
            {
                foreach (var p in parameters.Keys.ToList())
                {
                    if (Parameters.ContainsKey(p))
                    {
                        parameters[p] = Parameters[p];
                    }
                }
            }

            Parameters = parameters;
        }
예제 #13
0
 private void LoadExistingAppManifest(string appManifestPath)
 {
     this.existingApplicationManifestContents = Utility.LoadContents(appManifestPath).Trim();
     this.appManifestType = XmlSerializationUtility.Deserialize <ApplicationManifestType>(this.existingApplicationManifestContents);
 }
예제 #14
0
        public static void CompareAndFixTargetManifestVersionsForUpgrade(
            System.Fabric.Common.ImageModel.StoreLayoutSpecification storeLayoutSpecification,
            ImageStoreWrapper destinationImageStoreWrapper,
            TimeoutHelper timeoutHelper,
            Dictionary <string, Dictionary <string, SettingsType> > settingsType,
            string typeName,
            ApplicationManifestType currentApplicationManifest,
            ApplicationManifestType targetApplicationManifest,
            IList <ServiceManifestType> currentServiceManifests,
            ref IList <ServiceManifestType> targetServiceManifests)
        {
            // Service manifest imports that exist in both current and target.
            var targetServiceManifestImports = targetApplicationManifest.ServiceManifestImport;
            var commonServiceManifestImports = currentApplicationManifest.ServiceManifestImport.Where(
                ci => targetServiceManifestImports.Any(ti => ti.ServiceManifestRef.ServiceManifestName == ci.ServiceManifestRef.ServiceManifestName));

            List <ServiceManifestType> unchangedServiceManifests = new List <ServiceManifestType>();

            foreach (ApplicationManifestTypeServiceManifestImport currentManifestImport in commonServiceManifestImports)
            {
                var currentManifest = currentServiceManifests.FirstOrDefault(item => item.Name == currentManifestImport.ServiceManifestRef.ServiceManifestName);
                var targetManifest  = targetServiceManifests.FirstOrDefault(item => item.Name == currentManifestImport.ServiceManifestRef.ServiceManifestName);
                // TODO: would codepackage versions be different than manifest version?
                var currentManifestVersion = currentManifest.Version;
                var targetManifestVersion  = targetManifest.Version;

                bool equal = ImageBuilderUtility.IsEqual <ServiceManifestType>(
                    ReplaceVersion(currentManifest, "0"), ReplaceVersion(targetManifest, "0"));

                ReplaceVersion(currentManifest, currentManifestVersion);
                ReplaceVersion(targetManifest, targetManifestVersion);

                #region Determine if settings have been changed since last version
                if (equal && settingsType != null)
                {
                    string serviceManifestName = currentManifest.Name;

                    // 1. Check if setting count remains the same
                    int newSettingsTypeCount = 0;
                    if (settingsType.ContainsKey(serviceManifestName))
                    {
                        newSettingsTypeCount = settingsType[serviceManifestName].Count(cp => cp.Key != null);
                    }
                    int currentSettingsTypeCount = 0;
                    if (currentManifest.ConfigPackage != null)
                    {
                        currentSettingsTypeCount = currentManifest.ConfigPackage.Count();
                    }

                    if (newSettingsTypeCount != currentSettingsTypeCount)
                    {
                        equal = false;
                    }

                    // 2. Read settings from each configPackage and compare the value
                    if (equal && currentManifest.ConfigPackage != null)
                    {
                        foreach (var configPackage in currentManifest.ConfigPackage)
                        {
                            SettingsType newSettingsType = null;
                            if (settingsType.ContainsKey(serviceManifestName) && settingsType[serviceManifestName].ContainsKey(configPackage.Name))
                            {
                                newSettingsType = settingsType[serviceManifestName][configPackage.Name];
                            }

                            string       configPackageDirectory = storeLayoutSpecification.GetConfigPackageFolder(typeName, serviceManifestName, configPackage.Name, configPackage.Version);
                            string       settingsFile           = storeLayoutSpecification.GetSettingsFile(configPackageDirectory);
                            SettingsType currentSettingsType    = null;
                            if (destinationImageStoreWrapper.DoesContentExists(settingsFile, timeoutHelper.GetRemainingTime()))
                            {
                                currentSettingsType = destinationImageStoreWrapper.GetFromStore <SettingsType>(settingsFile, timeoutHelper.GetRemainingTime());
                            }

                            equal &= CompareSettingsType(currentSettingsType, newSettingsType);
                            if (!equal)
                            {
                                break;
                            }
                        }
                    }

                    // Code pacakge versions not change for settings upgrade
                    if (!equal)
                    {
                        if (targetManifest.CodePackage != null)
                        {
                            for (int i = 0; i < targetManifest.CodePackage.Count(); ++i)
                            {
                                ReplaceVersion(ref targetManifest.CodePackage[i], currentManifestVersion);
                            }
                        }
                    }
                }
                #endregion

                if (equal)
                {
                    unchangedServiceManifests.Add(currentManifest);
                }
            }

            // Use the unchanged manifests.
            if (unchangedServiceManifests.Count() != 0)
            {
                List <ServiceManifestType> updatedTargetServiceManifests = new List <ServiceManifestType>();
                foreach (var manifest in targetServiceManifests)
                {
                    var unchangedManifest = unchangedServiceManifests.FirstOrDefault(item => item.Name == manifest.Name);
                    if (unchangedManifest != default(ServiceManifestType))
                    {
                        updatedTargetServiceManifests.Add(unchangedManifest);
                        for (int i = 0; i < targetApplicationManifest.ServiceManifestImport.Count(); ++i)
                        {
                            if (targetApplicationManifest.ServiceManifestImport[i].ServiceManifestRef.ServiceManifestName == manifest.Name)
                            {
                                targetApplicationManifest.ServiceManifestImport[i].ServiceManifestRef.ServiceManifestVersion = unchangedManifest.Version;
                                break;
                            }
                        }
                    }
                    else
                    {
                        updatedTargetServiceManifests.Add(manifest);
                    }
                }

                targetServiceManifests = updatedTargetServiceManifests;
            }
        }
예제 #15
0
 public void Generate(
     out ApplicationManifestType applicationManifest,
     out IList <ServiceManifestType> serviceManifests)
 {
     this.CreateManifests(out applicationManifest, out serviceManifests);
 }