From 5f363038215d43cad996236f33f3756436813d19 Mon Sep 17 00:00:00 2001 From: Rob Reynolds Date: Sat, 28 May 2016 12:51:13 -0500 Subject: [PATCH] (GH-198) Set and remove a pending file Once a package install/upgrade is passed from NuGet Core to Chocolatey, we are in a pending state where the package files exist but more work is to be done. Create a pending file for the package that tells Chocolatey that the package is not yet finished installing in case it gets interrupted for some reason. Also ensure that the pending file is cleaned up if the package installation is successful. --- .../ApplicationParameters.cs | 2 + .../services/ChocolateyPackageService.cs | 46 +++++++++++++++++-- 2 files changed, 44 insertions(+), 4 deletions(-) diff --git a/src/chocolatey/infrastructure.app/ApplicationParameters.cs b/src/chocolatey/infrastructure.app/ApplicationParameters.cs index 340e12449f..250e5cbed1 100644 --- a/src/chocolatey/infrastructure.app/ApplicationParameters.cs +++ b/src/chocolatey/infrastructure.app/ApplicationParameters.cs @@ -87,6 +87,8 @@ public static class Environment public static readonly string HashProviderFileTooBig = "UnableToDetectChanges_FileTooBig"; public static readonly string HashProviderFileLocked = "UnableToDetectChanges_FileLocked"; + public static readonly string PackagePendingFileName = ".chocolateyPending"; + public static class Tools { //public static readonly string WebPiCmdExe = _fileSystem.combine_paths(InstallLocation, "nuget.exe"); diff --git a/src/chocolatey/infrastructure.app/services/ChocolateyPackageService.cs b/src/chocolatey/infrastructure.app/services/ChocolateyPackageService.cs index b912cd290b..58b1abf642 100644 --- a/src/chocolatey/infrastructure.app/services/ChocolateyPackageService.cs +++ b/src/chocolatey/infrastructure.app/services/ChocolateyPackageService.cs @@ -271,6 +271,7 @@ public void randomly_notify_about_pro_business(ChocolateyConfiguration config, s public void handle_package_result(PackageResult packageResult, ChocolateyConfiguration config, CommandNameType commandName) { + set_pending(packageResult, config); var pkgInfo = _packageInfoService.get_package_information(packageResult.Package); if (config.AllowMultipleVersions) { @@ -315,10 +316,7 @@ public void handle_package_result(PackageResult packageResult, ChocolateyConfigu _configTransformService.run(packageResult, config); pkgInfo.FilesSnapshot = _filesService.capture_package_files(packageResult, config); - if (packageResult.Success) - { - _shimgenService.install(config, packageResult); - } + if (packageResult.Success) _shimgenService.install(config, packageResult); } else { @@ -345,6 +343,8 @@ public void handle_package_result(PackageResult packageResult, ChocolateyConfigu remove_rollback_if_exists(packageResult); + if (packageResult.Success) remove_pending(packageResult, config); + this.Log().Info(ChocolateyLoggers.Important, " The {0} of {1} was successful.".format_with(commandName.to_string(), packageResult.Name)); } @@ -1054,6 +1054,44 @@ private void remove_rollback_if_exists(PackageResult packageResult) _nugetService.remove_rollback_directory_if_exists(packageResult.Name); } + public void set_pending(PackageResult packageResult, ChocolateyConfiguration config) + { + var packageDirectory = packageResult.InstallLocation; + if (packageDirectory.is_equal_to(ApplicationParameters.InstallLocation) || packageDirectory.is_equal_to(ApplicationParameters.PackagesLocation)) + { + packageResult.Messages.Add( + new ResultMessage( + ResultType.Error, + "Install location is not specific enough, cannot run set package to pending:{0} Erroneous install location captured as '{1}'".format_with(Environment.NewLine, packageResult.InstallLocation) + ) + ); + + return; + } + + var pendingFile = _fileSystem.combine_paths(packageDirectory, ApplicationParameters.PackagePendingFileName); + _fileSystem.write_file(pendingFile, "{0}".format_with(packageResult.Name)); + } + + public void remove_pending(PackageResult packageResult, ChocolateyConfiguration config) + { + var packageDirectory = packageResult.InstallLocation; + if (packageDirectory.is_equal_to(ApplicationParameters.InstallLocation) || packageDirectory.is_equal_to(ApplicationParameters.PackagesLocation)) + { + packageResult.Messages.Add( + new ResultMessage( + ResultType.Error, + "Install location is not specific enough, cannot run set package to pending:{0} Erroneous install location captured as '{1}'".format_with(Environment.NewLine, packageResult.InstallLocation) + ) + ); + + return; + } + + var pendingFile = _fileSystem.combine_paths(packageDirectory, ApplicationParameters.PackagePendingFileName); + if (_fileSystem.file_exists(pendingFile)) _fileSystem.delete_file(pendingFile); + } + private IEnumerable get_environment_before(ChocolateyConfiguration config, bool allowLogging = true) { if (config.Information.PlatformType != PlatformType.Windows) return Enumerable.Empty();