Ejemplo n.º 1
0
        /// <summary>
        /// Connects a tag index to a tag in another version.
        /// </summary>
        /// <param name="version1">The first version.</param>
        /// <param name="index1">The tag index in the first version.</param>
        /// <param name="version2">The second version.</param>
        /// <param name="index2">The tag index in the second version.</param>
        public void Add(string version1, int index1, string version2, int index2)
        {
            // Get both version maps, creating them if they don't exist
            VersionMap map1, map2;
            if (!_versionMaps.TryGetValue(version1, out map1))
            {
                map1 = new VersionMap();
                _versionMaps[version1] = map1;
            }
            if (!_versionMaps.TryGetValue(version2, out map2))
            {
                map2 = new VersionMap();
                _versionMaps[version2] = map2;
            }

            // Check if the first index is in the map for the first version.
            // If it is, then we'll get a "global index" which can be used to look it up in other versions.
            // If it isn't, then we need to make a new global index for it.
            var globalIndex = map1.GetGlobalTagIndex(index1);
            if (globalIndex < 0)
            {
                globalIndex = _nextGlobalTagIndex;
                _nextGlobalTagIndex++;
                map1.Add(globalIndex, index1);
            }

            // Connect the global index to the second index in the second version
            map2.Add(globalIndex, index2);
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Connects a tag index to a tag in another version.
        /// </summary>
        /// <param name="version1">The first version.</param>
        /// <param name="index1">The tag index in the first version.</param>
        /// <param name="version2">The second version.</param>
        /// <param name="index2">The tag index in the second version.</param>
        public void Add(string version1, int index1, string version2, int index2)
        {
            // Get both version maps, creating them if they don't exist
            VersionMap map1, map2;

            if (!_versionMaps.TryGetValue(version1, out map1))
            {
                map1 = new VersionMap();
                _versionMaps[version1] = map1;
            }
            if (!_versionMaps.TryGetValue(version2, out map2))
            {
                map2 = new VersionMap();
                _versionMaps[version2] = map2;
            }

            // Check if the first index is in the map for the first version.
            // If it is, then we'll get a "global index" which can be used to look it up in other versions.
            // If it isn't, then we need to make a new global index for it.
            var globalIndex = map1.GetGlobalTagIndex(index1);

            if (globalIndex < 0)
            {
                globalIndex = _nextGlobalTagIndex;
                _nextGlobalTagIndex++;
                map1.Add(globalIndex, index1);
            }

            // Connect the global index to the second index in the second version
            map2.Add(globalIndex, index2);
        }
Ejemplo n.º 3
0
        public void SetVersionsIfVersionIsDeployed(
            VersionMap currentVersionMap,
            VersionMap packagedVersionMap,
            VersionNumber newVersion)
        {
            //var currentVersionMap = JsonConvert.DeserializeObject<Dictionary<string, GlobalVersion>>
            //    (currentHashMapResponse.ResponseContent);
            var versionMap = packagedVersionMap.PackageVersions;

            versionMap
            .Where(x => x.Value.VersionType == VersionType.ServicePackage)
            .Where(x => !x.Value.Hash.Equals(GetGlobalVersion(currentVersionMap.PackageVersions, x.Key).Hash))
            .ForEach(s =>
            {
                SetVersionOnPackage(s, newVersion, versionMap);
            });

            versionMap
            .Where(x => x.Value.VersionType == VersionType.Service)
            .Where(x => !x.Value.Hash.Equals(GetGlobalVersion(currentVersionMap.PackageVersions, x.Key).Hash))
            .ForEach(s =>
            {
                s.Value.Version          = newVersion;
                s.Value.IncludeInPackage = true;
                versionMap[s.Value.ParentRef].Version          = newVersion;
                versionMap[s.Value.ParentRef].IncludeInPackage = true;
            });

            versionMap
            .Where(x => x.Value.VersionType == VersionType.Application)
            .Where(x => !x.Value.Hash.Equals(GetGlobalVersion(currentVersionMap.PackageVersions, x.Key).Hash))
            .ForEach(s =>
            {
                s.Value.Version          = newVersion;
                s.Value.IncludeInPackage = true;
            });

            foreach (var globalVersion in versionMap.Where(x => !x.Value.IncludeInPackage))
            {
                if (currentVersionMap.PackageVersions.ContainsKey(globalVersion.Key))
                {
                    globalVersion.Value.Version = currentVersionMap.PackageVersions[globalVersion.Key].Version;
                }
                else
                {
                    if (globalVersion.Value.VersionType != VersionType.ServicePackage)
                    {
                        continue;
                    }

                    SetVersionOnPackage(globalVersion, newVersion, versionMap);
                }
            }

            if (versionMap.Any(x => x.Value.IncludeInPackage))
            {
                versionMap[Constants.GlobalIdentifier].Version = newVersion;
            }
        }
        public void ItShouldSetVersionOnAllItems()
        {
            // g
            var versions = new VersionMap
            {
                PackageVersions = new Dictionary <string, GlobalVersion>
                {
                    [Constants.GlobalIdentifier] = new GlobalVersion
                    {
                        VersionType = VersionType.Global,
                        Version     = VersionNumber.Default()
                    },
                    ["App"] = new GlobalVersion
                    {
                        VersionType = VersionType.Application,
                        Version     = VersionNumber.Default()
                    },
                    ["Service"] = new GlobalVersion
                    {
                        VersionType = VersionType.Service,
                        Version     = VersionNumber.Default(),
                        ParentRef   = "App"
                    },
                    ["Service-Code"] = new GlobalVersion
                    {
                        VersionType = VersionType.ServicePackage,
                        Version     = VersionNumber.Default(),
                        ParentRef   = "Service"
                    },
                    ["Service-Config"] = new GlobalVersion
                    {
                        VersionType = VersionType.ServicePackage,
                        Version     = VersionNumber.Default(),
                        ParentRef   = "Service"
                    },
                    ["Service-Data"] = new GlobalVersion
                    {
                        VersionType = VersionType.ServicePackage,
                        Version     = VersionNumber.Default(),
                        ParentRef   = "Service"
                    }
                }
            };
            var newVersion = VersionNumber.Create(2, "testhash");

            // w
            _versionService.SetVersionIfNoneIsDeployed(versions, newVersion);

            // t
            foreach (var actual in versions.PackageVersions)
            {
                actual.Value.Version.RollingNumber.Should().Be(2);
                actual.Value.Version.CommitHash.Should().Be("testhash");
                actual.Value.IncludeInPackage.Should().BeTrue();
            }
        }
Ejemplo n.º 5
0
        public async Task PutAsync(VersionMap version)
        {
            var versionNumber = version.PackageVersions[Constants.GlobalIdentifier].Version;

            _log.WriteLine($"Storing version map for {versionNumber}", LogLevel.Info);
            var versionJson = JsonConvert.SerializeObject(version);
            await _blobService
            .SaveFileAsync(versionNumber.FileName, versionJson)
            .ConfigureAwait(false);
        }
Ejemplo n.º 6
0
        /// <summary>
        /// Creates save data from save locations
        /// </summary>
        /// <param name="file">save location</param>
        /// <exception cref="IOException"></exception>
        /// <exception cref="SaveFormatException"></exception>
        internal SaveData(string file)
        {
            _file = file + ".data";
            if (!Directory.Exists(_file))
            {
                Directory.CreateDirectory(_file).Create();
            }
            _partList   = new PartList(_file);
            _versionMap = new VersionMap(_file);

            CheckDataConsistency();
        }
        public async Task PackageApplications(
            VersionMap thingsToPackage,
            Dictionary <string, ServiceFabricApplicationProject> appList,
            Dictionary <string, byte[]> hackedFiles)
        {
            if (_baseConfig.PackageOutputPath.Exists && _baseConfig.CleanOutputFolder)
            {
                try
                {
                    _baseConfig.PackageOutputPath.Delete(true);
                }
                catch (Exception ex)
                {
                    _log.WriteLine($"Problems removing packaging folder. Is something holding file lock?", LogLevel.Error);
                    throw;
                }
            }

            if (!_baseConfig.PackageOutputPath.Exists)
            {
                _baseConfig.PackageOutputPath.Create();
            }

            var applications = thingsToPackage
                               .PackageVersions
                               .Where(x => x.Value.VersionType == VersionType.Application)
                               .Where(x => x.Value.IncludeInPackage);

            foreach (var source in applications)
            {
                var appData = appList[source.Key];

                var applicationPackagePath = appData.GetPackagePath(_baseConfig.PackageOutputPath);
                applicationPackagePath.Create();
                CopyApplicationManifestToPackage(appData, applicationPackagePath);

                var servicesToCopy = thingsToPackage
                                     .PackageVersions
                                     .Where(x => x.Value.VersionType == VersionType.Service)
                                     .Where(x => x.Value.IncludeInPackage)
                                     .Where(x => x.Value.ParentRef.Equals(source.Key))
                                     .ToList();

                await CopyServicesToPackage(servicesToCopy, thingsToPackage.PackageVersions, appData, applicationPackagePath, hackedFiles).ConfigureAwait(false);
            }
        }
Ejemplo n.º 8
0
        public void SetVersionIfNoneIsDeployed(
            VersionMap versions,
            VersionNumber newVersion)
        {
            foreach (var v in versions.PackageVersions)
            {
                v.Value.Version          = newVersion;
                v.Value.IncludeInPackage = true;

                if (v.Value.VersionType != VersionType.Service)
                {
                    continue;
                }

                versions.PackageVersions[v.Value.ParentRef].IncludeInPackage = true;
                versions.PackageVersions[v.Value.ParentRef].Version          = newVersion;
            }

            versions.PackageVersions[Constants.GlobalIdentifier].Version = newVersion;
        }
        public void WhenOnlySomeHasChangedComplex()
        {
            // g
            var newVersion        = VersionNumber.Create(2, "testhash");
            var response          = File.ReadAllText(@"DescribeVersionService\ComplexVersionResponse.json");
            var currentVersionMap = new VersionMap
            {
                PackageVersions = JsonConvert.DeserializeObject <Dictionary <string, GlobalVersion> >(response)
            };

            var versions = new VersionMap
            {
                PackageVersions = new Dictionary <string, GlobalVersion>
                {
                    [Constants.GlobalIdentifier] = new GlobalVersion
                    {
                        VersionType = VersionType.Global,
                        Version     = VersionNumber.Default()
                    },
                    ["App"] = new GlobalVersion
                    {
                        VersionType = VersionType.Application,
                        Version     = VersionNumber.Default()
                    },
                    ["Service"] = new GlobalVersion
                    {
                        VersionType = VersionType.Service,
                        Version     = VersionNumber.Default(),
                        ParentRef   = "App"
                    },
                    ["Service-Code"] = new GlobalVersion
                    {
                        VersionType = VersionType.ServicePackage,
                        Version     = VersionNumber.Default(),
                        ParentRef   = "Service",
                        Hash        = "a",
                        PackageType = PackageType.Code
                    },
                    ["Service-Config"] = new GlobalVersion
                    {
                        VersionType = VersionType.ServicePackage,
                        Version     = VersionNumber.Default(),
                        ParentRef   = "Service",
                        Hash        = "z",
                        PackageType = PackageType.Config
                    },
                    ["App2"] = new GlobalVersion
                    {
                        VersionType = VersionType.Application,
                        Version     = VersionNumber.Default()
                    },
                    ["Service2"] = new GlobalVersion
                    {
                        VersionType = VersionType.Service,
                        Version     = VersionNumber.Default(),
                        ParentRef   = "App2"
                    },
                    ["Service2-Code"] = new GlobalVersion
                    {
                        VersionType = VersionType.ServicePackage,
                        Version     = VersionNumber.Default(),
                        ParentRef   = "Service2",
                        Hash        = "ww",
                        PackageType = PackageType.Code
                    },
                    ["Service2-Config"] = new GlobalVersion
                    {
                        VersionType = VersionType.ServicePackage,
                        Version     = VersionNumber.Default(),
                        ParentRef   = "Service2",
                        Hash        = "zz",
                        PackageType = PackageType.Config
                    }
                }
            };

            // w
            _versionService.SetVersionsIfVersionIsDeployed(currentVersionMap, versions, newVersion);

            // t
            var versionMap = versions.PackageVersions;

            versionMap[Constants.GlobalIdentifier].Version.RollingNumber.Should().Be(2);
            versionMap[Constants.GlobalIdentifier].Version.CommitHash.Should().Be("testhash");
            versionMap["App"].IncludeInPackage.Should().BeTrue();
            versionMap["App"].Version.RollingNumber.Should().Be(2);
            versionMap["App"].Version.CommitHash.Should().Be("testhash");
            versionMap["Service"].IncludeInPackage.Should().BeTrue();
            versionMap["Service"].Version.RollingNumber.Should().Be(2);
            versionMap["Service"].Version.CommitHash.Should().Be("testhash");
            versionMap["Service-Code"].IncludeInPackage.Should().BeFalse();
            versionMap["Service-Code"].Version.RollingNumber.Should().Be(1);
            versionMap["Service-Code"].Version.CommitHash.Should().Be("orghash");
            versionMap["Service-Config"].IncludeInPackage.Should().BeTrue();
            versionMap["Service-Config"].Version.RollingNumber.Should().Be(2);
            versionMap["Service-Config"].Version.CommitHash.Should().Be("testhash");
            versionMap["App2"].IncludeInPackage.Should().BeFalse();
            versionMap["App2"].Version.RollingNumber.Should().Be(1);
            versionMap["App2"].Version.CommitHash.Should().Be("orghash");
            versionMap["Service2"].IncludeInPackage.Should().BeFalse();
            versionMap["Service2"].Version.RollingNumber.Should().Be(1);
            versionMap["Service2"].Version.CommitHash.Should().Be("orghash");
            versionMap["Service2-Code"].IncludeInPackage.Should().BeFalse();
            versionMap["Service2-Code"].Version.RollingNumber.Should().Be(1);
            versionMap["Service2-Code"].Version.CommitHash.Should().Be("orghash");
            versionMap["Service2-Config"].IncludeInPackage.Should().BeFalse();
            versionMap["Service2-Config"].Version.RollingNumber.Should().Be(1);
            versionMap["Service2-Config"].Version.CommitHash.Should().Be("orghash");
        }
        public void WhenAllHasChanged()
        {
            // g
            var newVersion        = VersionNumber.Create(2, "testhash");
            var response          = File.ReadAllText(@"DescribeVersionService\BasicVersionResponse.json");
            var currentVersionMap = new VersionMap
            {
                PackageVersions = JsonConvert.DeserializeObject <Dictionary <string, GlobalVersion> >(response)
            };

            var versions = new VersionMap
            {
                PackageVersions = new Dictionary <string, GlobalVersion>
                {
                    [Constants.GlobalIdentifier] = new GlobalVersion
                    {
                        VersionType = VersionType.Global,
                        Version     = VersionNumber.Default()
                    },
                    ["App"] = new GlobalVersion
                    {
                        VersionType = VersionType.Application,
                        Version     = VersionNumber.Default()
                    },
                    ["Service"] = new GlobalVersion
                    {
                        VersionType = VersionType.Service,
                        Version     = VersionNumber.Default(),
                        ParentRef   = "App",
                        Hash        = "x"
                    },
                    ["Service-Code"] = new GlobalVersion
                    {
                        VersionType = VersionType.ServicePackage,
                        Version     = VersionNumber.Default(),
                        ParentRef   = "Service",
                        Hash        = "y"
                    },
                    ["Service-Config"] = new GlobalVersion
                    {
                        VersionType = VersionType.ServicePackage,
                        Version     = VersionNumber.Default(),
                        ParentRef   = "Service",
                        Hash        = "z"
                    }
                }
            };

            // w
            _versionService.SetVersionsIfVersionIsDeployed(currentVersionMap, versions, newVersion);

            // t
            foreach (var actual in versions.PackageVersions.Where(x => x.Value.VersionType != VersionType.Global))
            {
                actual.Value.Version.RollingNumber.Should().Be(2);
                actual.Value.Version.CommitHash.Should().Be("testhash");
                actual.Value.IncludeInPackage.Should().BeTrue();
            }

            versions.PackageVersions[Constants.GlobalIdentifier].Version.RollingNumber.Should().Be(2);
            versions.PackageVersions[Constants.GlobalIdentifier].Version.CommitHash.Should().Be("testhash");
        }
Ejemplo n.º 11
0
        public async Task RunAsync()
        {
            var sfApplications = await _locator.LocateSfApplications().ConfigureAwait(false);

            await _fabricRemote.Init().ConfigureAwait(false);

            _log.WriteLine("Trying to read app manifest from deployed applications...");
            var deployedApps = await _fabricRemote.GetApplicationManifestsAsync().ConfigureAwait(false);

            var currentVersion = _versionHandler.GetCurrentVersionFromApplications(deployedApps);
            var newVersion     = currentVersion.Increment(_baseConfig.UniqueVersionIdentifier);

            _log.WriteLine($"New version is: {newVersion}", LogLevel.Info);

            var hackData = _hack.FindHackableThings(sfApplications.First());

            var versions = new VersionMap
            {
                PackageVersions = new Dictionary <string, GlobalVersion>
                {
                    [Constants.GlobalIdentifier] = new GlobalVersion
                    {
                        VersionType = VersionType.Global,
                        Version     = currentVersion
                    }
                }
            };

            var currentHashMap = await _versionMapHandler.GetAsync(currentVersion).ConfigureAwait(false);

            var parsedApplications = new Dictionary <string, ServiceFabricApplicationProject>();

            _log.WriteLine("Parsing Service Fabric Applications and computing hashes");
            foreach (var sfApplication in sfApplications)
            {
                var project = _projectHandler.Parse(sfApplication, _baseConfig.SourcePath);
                parsedApplications.Add(project.ApplicationTypeName, project);
                var serviceVersions = await _hasher.Calculate(project, currentVersion).ConfigureAwait(false);

                serviceVersions.ForEach(service => { versions.PackageVersions.Add(service.Key, service.Value); });
            }

            if (_baseConfig.ForcePackageAll || currentHashMap?.PackageVersions == null)
            {
                _log.WriteLine($"Force package all, setting everything to {newVersion}");
                _versionService.SetVersionIfNoneIsDeployed(versions, newVersion);
            }
            else
            {
                _log.WriteLine($"Setting version of changed packages to {newVersion}");
                _versionService.SetVersionsIfVersionIsDeployed(currentHashMap, versions, newVersion);
            }

            _log.WriteLine("Packaging applications", LogLevel.Info);
            await _packager
            .PackageApplications(versions, parsedApplications, hackData)
            .ConfigureAwait(false);

            _log.WriteLine("Updating manifests");
            _manifestReader.Handle(versions, parsedApplications);

            await _versionMapHandler.PutAsync(versions).ConfigureAwait(false);

            var things = versions
                         .PackageVersions
                         .Where(x => x.Value.VersionType == VersionType.Application)
                         .Where(x => x.Value.IncludeInPackage)
                         .Select(x => x.Key)
                         .ToList();

            _scriptCreator.Do(newVersion, things);
        }
        public void Handle(
            VersionMap versions,
            Dictionary <string, ServiceFabricApplicationProject> applications)
        {
            var apps = versions
                       .PackageVersions
                       .Where(x => x.Value.VersionType == VersionType.Application)
                       .Where(x => x.Value.IncludeInPackage)
                       .ToList();

            foreach (var app in apps)
            {
                var appData             = applications[app.Key];
                var appPackagePath      = appData.GetPackagePath(_baseConfig.PackageOutputPath);
                var packagedAppManifest = appData.GetAppManifestTargetFile(appPackagePath);

                var appManifest = _appManifestLoader.Load(packagedAppManifest.FullName);

                CleanAppManifest(appManifest);
                _appManifestHandler.SetGeneralInfo(appManifest, versions.PackageVersions, app.Value);
                _handleEndpointCert.SetEndpointCerts(_packageConfig, appManifest, appData.ApplicationTypeName);
                _handleEnciphermentCert.SetEnciphermentCerts(_packageConfig, appManifest, appData.ApplicationTypeName);

                var guests = _packageConfig.GuestExecutables.Where(x =>
                                                                   x.ApplicationTypeName.Equals(app.Key, StringComparison.CurrentCultureIgnoreCase));

                foreach (var guest in guests)
                {
                    var policies = new Policies();

                    if (guest.GuestRunAs != null)
                    {
                        var runAs = new RunAsPolicy
                        {
                            UserRef        = guest.GuestRunAs.UserName,
                            CodePackageRef = "Code"
                        };

                        var runAsPolicies = new List <RunAsPolicy> {
                            runAs
                        };
                        policies.RunAsPolicy = runAsPolicies;

                        if (appManifest.Principals == null)
                        {
                            appManifest.Principals = new Principals();
                        }
                        if (appManifest.Principals.Users == null)
                        {
                            appManifest.Principals.Users = new Users();
                        }
                        if (appManifest.Principals.Users.User == null)
                        {
                            appManifest.Principals.Users.User = new List <User>();
                        }

                        if (!appManifest.Principals.Users.User.Any(x =>
                                                                   x.Name.Equals(guest.GuestRunAs.UserName, StringComparison.CurrentCultureIgnoreCase)))
                        {
                            var user = new User
                            {
                                Name        = guest.GuestRunAs.UserName,
                                AccountType = guest.GuestRunAs.AccountType
                            };
                            appManifest.Principals.Users.User.Add(user);
                        }
                    }

                    var properServiceKey = $"{appManifest.ApplicationTypeName}-{guest.PackageName}";
                    var serviceVersion   = versions.PackageVersions[properServiceKey].Version.ToString();

                    var serviceManifestRef = new ServiceManifestRef
                    {
                        ServiceManifestName    = guest.PackageName,
                        ServiceManifestVersion = serviceVersion
                    };
                    var serviceImport = new ServiceManifestImport
                    {
                        ServiceManifestRef = serviceManifestRef,
                        ConfigOverrides    = new ConfigOverrides(),
                        Policies           = policies
                    };

                    appManifest.ServiceManifestImports.Add(serviceImport);
                }

                _appManifestLoader.Save(appManifest, packagedAppManifest.FullName);

                var includedServices = versions
                                       .PackageVersions
                                       .Where(x => x.Value.ParentRef.Equals(app.Key))
                                       .Where(x => x.Value.IncludeInPackage)
                                       .ToList();

                foreach (var service in includedServices)
                {
                    var serviceKey  = service.Key.Split('-').Last();
                    var serviceData = appData.Services[serviceKey];

                    var packagedServiceManifest = Path.Combine(appPackagePath.FullName, serviceKey,
                                                               serviceData.ServiceManifestFile);
                    var serviceManifest = _serviceManifestLoader.Load(packagedServiceManifest);

                    _serviceManifestHandler.SetServiceManifestGeneral(serviceManifest, service.Value);
                    SetServiceEndpoints(serviceManifest, appData.ApplicationTypeName, serviceData.ServiceName);

                    _serviceManifestHandler.SetServicePackagesData(serviceManifest, versions.PackageVersions, service.Key);

                    _serviceManifestLoader.Save(serviceManifest, packagedServiceManifest);
                }
            }
        }