Skip to content

Commit

Permalink
(GH-1151) Option - Stop on first package failure
Browse files Browse the repository at this point in the history
Add option/feature to stop on first package failure. Although it is
normally recommended to let the process continue when one package
fails, sometimes it is best to stop further action from occurring.
  • Loading branch information
ferventcoder committed Mar 19, 2017
1 parent cb6b92c commit 279988d
Show file tree
Hide file tree
Showing 8 changed files with 28 additions and 2 deletions.
1 change: 1 addition & 0 deletions src/chocolatey/infrastructure.app/ApplicationParameters.cs
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,7 @@ public static class Features
public static readonly string ScriptsCheckLastExitCode = "scriptsCheckLastExitCode";
public static readonly string ShowNonElevatedWarnings = "showNonElevatedWarnings";
public static readonly string ShowDownloadProgress = "showDownloadProgress";
public static readonly string StopOnFirstPackageFailure = "stopOnFirstPackageFailure";
}

public static class Messages
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -273,6 +273,7 @@ private static void set_feature_flags(ChocolateyConfiguration config, ConfigFile
config.Features.UseFipsCompliantChecksums = set_feature_flag(ApplicationParameters.Features.UseFipsCompliantChecksums, configFileSettings, defaultEnabled: false, description: "Use FIPS Compliant Checksums - Ensure checksumming done by choco uses FIPS compliant algorithms. Not recommended unless required by FIPS Mode. Enabling on an existing installation could have unintended consequences related to upgrades/uninstalls. Available in 0.9.10+.");
config.Features.ShowNonElevatedWarnings = set_feature_flag(ApplicationParameters.Features.ShowNonElevatedWarnings, configFileSettings, defaultEnabled: true, description: "Show Non-Elevated Warnings - Display non-elevated warnings. Available in 0.10.4+.");
config.Features.ShowDownloadProgress = set_feature_flag(ApplicationParameters.Features.ShowDownloadProgress, configFileSettings, defaultEnabled: true, description: "Show Download Progress - Show download progress percentages in the CLI. Available in 0.10.4+.");
config.Features.StopOnFirstPackageFailure = set_feature_flag(ApplicationParameters.Features.StopOnFirstPackageFailure, configFileSettings, defaultEnabled: false, description: "Stop On First Package Failure - stop running install, upgrade or uninstall on first package failure instead of continuing with others. As this will affect upgrade all, it is normally recommended to leave this off. Available in 0.10.4+.");
config.Features.ScriptsCheckLastExitCode = set_feature_flag(ApplicationParameters.Features.ScriptsCheckLastExitCode, configFileSettings, defaultEnabled: false, description: "Scripts Check $LastExitCode (external commands) - Leave this off unless you absolutely need it while you fix your package scripts to use `throw 'error message'` or `Set-PowerShellExitCode #` instead of `exit #`. This behavior started in 0.9.10 and produced hard to find bugs. If the last external process exits successfully but with an exit code of not zero, this could cause hard to detect package failures. Available in 0.10.3+. Will be removed in 0.11.0.");
config.PromptForConfirmation = !set_feature_flag(ApplicationParameters.Features.AllowGlobalConfirmation, configFileSettings, defaultEnabled: false, description: "Prompt for confirmation in scripts or bypass.");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,10 @@ public virtual void configure_argument_parser(OptionSet optionSet, ChocolateyCon
.Add("usepackagecodes|usepackageexitcodes|use-package-codes|use-package-exit-codes",
"UsePackageExitCodes - Package scripts can provide exit codes. Use those for choco's exit code when non-zero (this value can come from a dependency package). Chocolatey defines valid exit codes as 0, 1605, 1614, 1641, 3010. Overrides the default feature '{0}' set to '{1}'. Available in 0.9.10+.".format_with(ApplicationParameters.Features.UsePackageExitCodes, configuration.Features.UsePackageExitCodes.to_string()),
option => configuration.Features.UsePackageExitCodes = option != null
)
.Add("stoponfirstfailure|stop-on-first-failure|stop-on-first-package-failure",
"Stop On First Package Failure - stop running install, upgrade or uninstall on first package failure instead of continuing with others. Overrides the default feature '{0}' set to '{1}'. Available in 0.10.4+.".format_with(ApplicationParameters.Features.StopOnFirstPackageFailure, configuration.Features.StopOnFirstPackageFailure.to_string()),
option => configuration.Features.StopOnFirstPackageFailure = option != null
)
;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,11 @@ public virtual void configure_argument_parser(OptionSet optionSet, ChocolateyCon
{
configuration.Features.FailOnAutoUninstaller = false;
}
})
})
.Add("stoponfirstfailure|stop-on-first-failure|stop-on-first-package-failure",
"Stop On First Package Failure - stop running install, upgrade or uninstall on first package failure instead of continuing with others. Overrides the default feature '{0}' set to '{1}'. Available in 0.10.4+.".format_with(ApplicationParameters.Features.StopOnFirstPackageFailure, configuration.Features.StopOnFirstPackageFailure.to_string()),
option => configuration.Features.StopOnFirstPackageFailure = option != null
)
;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,12 @@ public virtual void configure_argument_parser(OptionSet optionSet, ChocolateyCon
)
.Add("except=",
"Except - a comma-separated list of package names that should not be upgraded when upgrading 'all'. Defaults to empty. Available in 0.9.10+.",
option => configuration.UpgradeCommand.PackageNamesToSkip = option.remove_surrounding_quotes())
option => configuration.UpgradeCommand.PackageNamesToSkip = option.remove_surrounding_quotes()
)
.Add("stoponfirstfailure|stop-on-first-failure|stop-on-first-package-failure",
"Stop On First Package Failure - stop running install, upgrade or uninstall on first package failure instead of continuing with others. Overrides the default feature '{0}' set to '{1}'. Available in 0.10.4+.".format_with(ApplicationParameters.Features.StopOnFirstPackageFailure, configuration.Features.StopOnFirstPackageFailure.to_string()),
option => configuration.Features.StopOnFirstPackageFailure = option != null
)
;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -356,6 +356,8 @@ public sealed class FeaturesConfiguration
public bool UseFipsCompliantChecksums { get; set; }
public bool ShowNonElevatedWarnings { get; set; }
public bool ShowDownloadProgress { get; set; }
public bool StopOnFirstPackageFailure { get; set; }

//todo remove in 0.11.0
public bool ScriptsCheckLastExitCode { get; set; }
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -421,6 +421,11 @@ public void handle_package_result(PackageResult packageResult, ChocolateyConfigu
this.Log().Error(ChocolateyLoggers.Important, "The {0} of {1} was NOT successful.".format_with(commandName.to_string(), packageResult.Name));
handle_unsuccessful_operation(config, packageResult, movePackageToFailureLocation: true, attemptRollback: true);

if (config.Features.StopOnFirstPackageFailure)
{
throw new ApplicationException("Stopping further execution as {0} has failed {1}.".format_with(packageResult.Name, commandName.to_string()));
}

return;
}

Expand Down
4 changes: 4 additions & 0 deletions src/chocolatey/infrastructure.app/services/NugetService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1217,6 +1217,10 @@ public ConcurrentDictionary<string, PackageResult> uninstall_run(ChocolateyConfi
var result = packageUninstalls.GetOrAdd(packageVersion.Id.to_lower() + "." + packageVersion.Version.to_string(), new PackageResult(packageVersion, _fileSystem.combine_paths(ApplicationParameters.PackagesLocation, packageVersion.Id)));
result.Messages.Add(new ResultMessage(ResultType.Error, logMessage));
if (result.ExitCode == 0) result.ExitCode = 1;
if (config.Features.StopOnFirstPackageFailure)
{
throw new ApplicationException("Stopping further execution as {0} has failed uninstallation".format_with(packageVersion.Id.to_lower()));
}
// do not call continueAction - will result in multiple passes
}
}
Expand Down

0 comments on commit 279988d

Please sign in to comment.