示例#1
0
 public static InstallManifest FromJsonFile(string path)
 {
     using (StreamReader streamReader = File.OpenText(path))
     {
         return(InstallManifest.FromJson(streamReader));
     }
 }
示例#2
0
        public override bool TryRunInstaller(InstallActionWrapper installActionWrapper, out string error)
        {
            string localError = null;
            int    installerExitCode;
            bool   installSuccessful = true;

            using (ITracer activity = this.tracer.StartActivity(nameof(this.TryRunInstaller), EventLevel.Informational))
            {
                InstallActionInfo currentInstallAction = null;
                try
                {
                    string platformKey = GSDPlatform.Instance.Name;

                    if (!this.TryRecursivelyDeleteInstallerDirectory(out error))
                    {
                        return(false);
                    }

                    if (!this.noVerify)
                    {
                        if (!this.nuGetFeed.VerifyPackage(this.DownloadedPackagePath))
                        {
                            error = "Package signature validation failed. Check the upgrade logs for more details.";
                            activity.RelatedError(error);
                            this.fileSystem.DeleteFile(this.DownloadedPackagePath);
                            return(false);
                        }
                    }

                    this.UnzipPackage();
                    this.installManifest = InstallManifest.FromJsonFile(Path.Combine(this.ExtractedInstallerPath, ContentDirectoryName, InstallManifestFileName));
                    if (!this.installManifest.PlatformInstallManifests.TryGetValue(platformKey, out InstallManifestPlatform platformInstallManifest) ||
                        platformInstallManifest == null)
                    {
                        activity.RelatedError($"Extracted InstallManifest from JSON, but there was no entry for {platformKey}.");
                        error = $"No entry in the manifest for the current platform ({platformKey}). Please verify the upgrade package.";
                        return(false);
                    }

                    activity.RelatedInfo($"Extracted InstallManifest from JSON. InstallActions: {platformInstallManifest.InstallActions.Count}");

                    foreach (InstallActionInfo entry in platformInstallManifest.InstallActions)
                    {
                        currentInstallAction = entry;
                        string installerBasePath = Path.Combine(this.ExtractedInstallerPath, ContentDirectoryName);

                        string args = entry.Args ?? string.Empty;

                        // Replace tokens on args
                        string processedArgs = NuGetUpgrader.ReplaceArgTokens(args, this.UpgradeInstanceId, ProductUpgraderInfo.GetLogDirectoryPath(), $"\"{installerBasePath}\"");

                        activity.RelatedInfo(
                            "Running install action: Name: {0}, Version: {1}, InstallerPath: {2}, Command: {3}, RawArgs: {4}, ProcessedArgs: {5}",
                            entry.Name,
                            entry.Version,
                            entry.InstallerRelativePath ?? string.Empty,
                            entry.Command ?? string.Empty,
                            args,
                            processedArgs);

                        string progressMessage = string.IsNullOrWhiteSpace(entry.Version) ?
                                                 $"Running {entry.Name}" :
                                                 $"Running {entry.Name} (version {entry.Version})";

                        installActionWrapper(
                            () =>
                        {
                            if (!this.dryRun)
                            {
                                if (!string.IsNullOrEmpty(entry.Command))
                                {
                                    this.RunInstaller(entry.Command, processedArgs, out installerExitCode, out localError);
                                }
                                else
                                {
                                    string installerPath = Path.Combine(installerBasePath, entry.InstallerRelativePath);
                                    this.RunInstaller(installerPath, processedArgs, out installerExitCode, out localError);
                                }
                            }
                            else
                            {
                                // We add a sleep here to ensure
                                // the message for this install
                                // action is written to the
                                // console.  Even though the
                                // message is written with a delay
                                // of 0, the messages are not
                                // always written out.  If / when
                                // we can ensure that the message
                                // is written out to console, then
                                // we can remove this sleep.
                                Thread.Sleep(1500);
                                installerExitCode = 0;
                            }

                            installSuccessful = installerExitCode == 0;

                            return(installSuccessful);
                        },
                            progressMessage);

                        if (!installSuccessful)
                        {
                            break;
                        }
                    }
                }
                catch (Exception ex)
                {
                    localError        = ex.Message;
                    installSuccessful = false;
                }

                if (!installSuccessful)
                {
                    string installActionName = string.IsNullOrEmpty(currentInstallAction?.Name) ?
                                               "installer" :
                                               currentInstallAction.Name;

                    error = string.IsNullOrEmpty(localError) ?
                            $"The {installActionName} failed, but no error message was provided by the failing command." :
                            $"The {installActionName} failed with the following error: {localError}";

                    activity.RelatedError($"Could not complete all install actions. The following error was encountered: {error}");
                    return(false);
                }
                else
                {
                    activity.RelatedInfo($"Install actions completed successfully.");
                    error = null;
                    return(true);
                }
            }
        }