Exemplo n.º 1
0
        internal static string GetCurrentCodeVersion(string packageLocation)
        {
#if DotNetCoreClrLinux
            string currentAssembly = typeof(Utility).GetTypeInfo().Assembly.Location;
            string versionFile     = Path.Combine(Path.GetDirectoryName(currentAssembly), "ClusterVersion");
            string codeVersion     = File.ReadAllText(versionFile);
            return(codeVersion);
#else
            if (string.IsNullOrEmpty(packageLocation))
            {
                string currentAssembly = typeof(Utility).GetTypeInfo().Assembly.Location;
                string fabricPath      = Path.Combine(Path.GetDirectoryName(currentAssembly), Constants.FabricExe);

                if (!File.Exists(fabricPath))
                {
                    DeployerTrace.WriteWarning("{0} Fabric Path doesn't exist", fabricPath);

                    string codePath = GetFabricCodePath();
                    if (!string.IsNullOrEmpty(codePath))
                    {
                        fabricPath = Path.Combine(codePath, Constants.FabricExe);
                        if (!File.Exists(fabricPath))
                        {
                            DeployerTrace.WriteWarning("{0} Code Path doesn't exist", fabricPath);
                            codePath = null;
                        }
                    }

                    if (string.IsNullOrEmpty(codePath))
                    {
                        string errorMessage = string.Format("Fabric.exe not found in current execution assembly directory. CurrentAssembly : {0}, Fabric Path : {1}", currentAssembly, fabricPath);
                        throw new InvalidOperationException(errorMessage);
                    }
                }
                var versionInfo = System.Diagnostics.FileVersionInfo.GetVersionInfo(fabricPath);
                return(string.Format(CultureInfo.InvariantCulture, Constants.FabricVersions.CodeVersionPattern, versionInfo.ProductMajorPart, versionInfo.ProductMinorPart, versionInfo.ProductBuildPart, versionInfo.ProductPrivatePart));
            }
            else
            {
                return(CabFileOperations.GetCabVersion(packageLocation));
            }
#endif
        }
Exemplo n.º 2
0
        public async Task <ClusterUpgradeCommandParameter> DownloadWindowsUpdate(
            NativeMethods.IUpdate update,
            TimeSpan timeout,
            CancellationToken token)
        {
            if (update == null)
            {
                return(null);
            }

            if (!update.IsDownloaded)
            {
                var downloadOperation = DownloadUpdateAsync(updateSession, update);
                await AwaitForOperationToComplete(downloadOperation, timeout, token, "Download");
            }

            if (update.IsDownloaded)
            {
                string dir  = Path.Combine(Path.GetTempPath(), Path.GetRandomFileName());
                var    info = new DirectoryInfo(dir);
                if (!info.Exists)
                {
                    info.Create();
                }

                update.CopyFromCache(dir, false);
                var cabFilePath = Directory.GetFiles(dir, "*.cab").FirstOrDefault();
                if (cabFilePath != null)
                {
                    var cabVersion = CabFileOperations.GetCabVersion(cabFilePath);
                    return(new ClusterUpgradeCommandParameter()
                    {
                        CodeFilePath = cabFilePath,
                        CodeVersion = cabVersion
                    });
                }
            }

            return(null);
        }
Exemplo n.º 3
0
        public void DeploymentManagerTest_ProductionGoalState()
        {
            Console.WriteLine("test begins: {0}", DateTime.UtcNow);

            string goalStateFilepath = Path.Combine(BaseDir, GoalStateFilename);

            Assert.IsTrue(File.Exists(goalStateFilepath), string.Format("GoalStateFile {0} did not exist.", goalStateFilepath));
            string         goalStateJson = File.ReadAllText(goalStateFilepath);
            GoalStateModel model         = GoalStateModel.GetGoalStateModelFromJson(goalStateJson);

            Assert.IsTrue(model != null, "Goal state JSON could not be deserialized.");
            Assert.IsTrue(model.Packages != null, "Goal state model must have a packages element.");

            if (model.Packages.Count() == 0)
            {
                return;
            }

            // There can only be one goal package
            int countGoalpackages = model.Packages.Where(package => package.IsGoalPackage).Count();

            Assert.IsTrue(countGoalpackages == 1, "Exactly one goal package may be marked as IsGoalPackage in the goal state file.");

            // Validate each package link is reachable
            string errorMessage = string.Empty;

            foreach (PackageDetails package in model.Packages)
            {
                Uri packageUri;
                if (!Uri.TryCreate(package.TargetPackageLocation, UriKind.Absolute, out packageUri))
                {
                    errorMessage = string.Format("Cannot parse packageUri: {0}", package.TargetPackageLocation);
                    Assert.Fail(errorMessage);
                }

                if (!StandaloneUtility.IsUriReachable(packageUri))
                {
                    errorMessage = string.Format("Package uri is not reachable: {0}", packageUri.AbsoluteUri);
                }
            }

            if (!string.IsNullOrEmpty(errorMessage))
            {
                Assert.Fail(errorMessage);
            }

            // Validate that goal state downloaded package is the correct version
            string         packageDropDir = System.Fabric.Common.Helpers.GetNewTempPath();
            PackageDetails goalPackage    = model.Packages.First(package => package.IsGoalPackage);

            Assert.IsTrue(goalPackage != null, "goalPackage null");

            // Validate package downloadable
            Console.WriteLine("download begins for {0}: {1}", goalPackage.Version, DateTime.UtcNow);
            string packageLocalDownloadPath = Path.Combine(packageDropDir, string.Format(Microsoft.ServiceFabric.DeploymentManager.Constants.SFPackageDropNameFormat, goalPackage.Version));
            bool   packageDownloaded        = StandaloneUtility.DownloadPackageAsync(goalPackage.Version, goalPackage.TargetPackageLocation, packageLocalDownloadPath).Result;

            Assert.IsTrue(packageDownloaded, string.Format("Package {0} failed to download from {1}.", goalPackage.Version, goalPackage.TargetPackageLocation));

            // Validate downloaded package matches version of version in goal state file
            string cabVersion = CabFileOperations.GetCabVersion(packageLocalDownloadPath);

            Assert.IsTrue(goalPackage.Version == cabVersion, string.Format("Goal state package version {0} doesn't match downloaded package version {1}.", goalPackage.Version, cabVersion));

            if (Directory.Exists(packageDropDir))
            {
                try
                {
                    FabricDirectory.Delete(packageDropDir, true, true);
                }
                catch { }
            }

            Console.WriteLine("test ends: {0}", DateTime.UtcNow);
        }
 private static bool TryQueryProductVersionFromCab(string fileName, out string productVersion)
 {
     productVersion = CabFileOperations.GetCabVersion(fileName);
     return(!string.IsNullOrWhiteSpace(productVersion));
 }
        // Return true if upgrade started other wise false
        private async Task <bool> OnGettingUpdateCollectionAsync(
            NativeMethods.IUpdateCollection updateCollection,
            FabricUpgradeProgress upgradeProgress,
            CancellationToken token)
        {
            ClusterUpgradeCommandParameter commandParameter = null;
            string updateId = string.Empty;

            if (updateCollection != null)
            {
                NativeMethods.IUpdate updateToDownload = this.GetUpdateToDownload(updateCollection);
                if (updateToDownload != null)
                {
                    Trace.WriteInfo(TraceType, "OnGettingUpdateCollectionAsync: Update to download: {0}", updateToDownload.Identity.UpdateID);
                    commandParameter = await this.packageRetriever.DownloadWindowsUpdate(updateToDownload, this.windowsUpdateApiTimeout, token);

                    updateId = updateToDownload.Identity.UpdateID;
                }
                else
                {
                    Trace.WriteInfo(TraceType, "OnGettingUpdateCollectionAsync: No update found.");
                }
            }
            else
            {
                Trace.WriteInfo(TraceType, "OnGettingUpdateCollectionAsync: update collection is null.");
                if (this.testMode && Directory.Exists(this.testSrcDir))
                {
                    var srcCabFile = Directory.GetFiles(this.testSrcDir, "*.cab", SearchOption.TopDirectoryOnly).FirstOrDefault();
                    if (!string.IsNullOrWhiteSpace(srcCabFile))
                    {
                        Trace.WriteWarning(TraceType, "OnGettingUpdateCollectionAsync: Test cab file {0}", srcCabFile);
                        var dir  = Path.Combine(Path.GetTempPath(), Path.GetRandomFileName());
                        var info = new DirectoryInfo(dir);
                        if (!info.Exists)
                        {
                            info.Create();
                        }

                        updateId = Guid.NewGuid().ToString();
                        var destCabFile = Path.Combine(dir, "test.cab");
                        File.Copy(srcCabFile, destCabFile);
                        Trace.WriteWarning(TraceType, "OnGettingUpdateCollectionAsync: Test dest file {0}", destCabFile);
                        var cabVersion = CabFileOperations.GetCabVersion(destCabFile);
                        Trace.WriteWarning(TraceType, "OnGettingUpdateCollectionAsync: Cab version {0}", cabVersion);
                        commandParameter = new ClusterUpgradeCommandParameter()
                        {
                            CodeFilePath = destCabFile,
                            CodeVersion  = cabVersion
                        };
                    }
                }
            }

            if (commandParameter == null)
            {
                return(false);
            }

            var updatedCommandParameter = await this.UpdateReplicaStoreBeforeUpgradeStartAsync(
                commandParameter,
                upgradeProgress,
                updateId,
                token);

            var upgradeTask = await this.commandProcessor.ClusterUpgradeAsync(updatedCommandParameter, TimeSpan.MaxValue, token).ContinueWith(
                (task) =>
            {
                if (commandParameter != null)
                {
                    DeleteFileDirectory(commandParameter.CodeFilePath);
                    DeleteFileDirectory(commandParameter.ConfigFilePath);
                }

                return(task);
            });

            await upgradeTask;

            return(true);
        }