Skip to content

Commit

Permalink
(chocolateyGH-1038) Stop operation on package reboot request
Browse files Browse the repository at this point in the history
There are now parameters and a feature flag which will allow
halting/overriding the install/upgrade/uninstall of a package, when
a reboot request is returned from one of it's dependencies.

When this occurs, an ApplicationException will be thrown, along with
a specific exit code, either 350 (pending reboot discovered prior to
running), or 1604 (some work completed prior to restart request) will be
returned.  This could then be inspected to decide when a reboot should
actually be performed, before continuing with the remainder of the
installation.
  • Loading branch information
gep13 committed Mar 9, 2019
1 parent 36af792 commit 2863cd1
Show file tree
Hide file tree
Showing 8 changed files with 161 additions and 49 deletions.
2 changes: 1 addition & 1 deletion src/chocolatey.console/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ private static void Main(string[] args)
"chocolatey".Log().Error(ChocolateyLoggers.Important, () => "{0}".format_with(ex.Message));
}

Environment.ExitCode = 1;
if (Environment.ExitCode == 0) Environment.ExitCode = 1;
}
finally
{
Expand Down
9 changes: 8 additions & 1 deletion src/chocolatey/infrastructure.app/ApplicationParameters.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// Copyright © 2017 - 2018 Chocolatey Software, Inc
// Copyright © 2011 - 2017 RealDimensions Software, LLC
//
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
//
Expand Down Expand Up @@ -127,6 +127,12 @@ public static class Environment
/// </summary>
public static readonly bool AllowPrompts = true;

public static class ExitCodes
{
public static readonly int ErrorFailNoActionReboot = 350;
public static readonly int ErrorInstallSuspend = 1604;
}

public static class Tools
{
//public static readonly string WebPiCmdExe = _fileSystem.combine_paths(InstallLocation, "nuget.exe");
Expand Down Expand Up @@ -170,6 +176,7 @@ public static class Features
public static readonly string IgnoreUnfoundPackagesOnUpgradeOutdated = "ignoreUnfoundPackagesOnUpgradeOutdated";
public static readonly string RemovePackageInformationOnUninstall = "removePackageInformationOnUninstall";
public static readonly string LogWithoutColor = "logWithoutColor";
public static readonly string ExitOnRebootDetected = "exitOnRebootDetected";
}

public static class Messages
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -303,6 +303,7 @@ private static void set_feature_flags(ChocolateyConfiguration config, ConfigFile

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.");
config.Features.ExitOnRebootDetected = set_feature_flag(ApplicationParameters.Features.ExitOnRebootDetected, configFileSettings, defaultEnabled: false, description: "Exit On Reboot Detected - Stop running install, upgrade, or uninstall when a reboot request is detected. Requires '{0}' feature to be turned on. Will exit with either {1} or {2}. When it exits with {1}, it means pending reboot discovered prior to running operation. When it exits with {2}, it means some work completed prior to reboot request being detected. As this will affect upgrade all, it is normally recommended to leave this off. Available in 0.10.12+.".format_with(ApplicationParameters.Features.ExitOnRebootDetected, ApplicationParameters.ExitCodes.ErrorFailNoActionReboot, ApplicationParameters.ExitCodes.ErrorInstallSuspend));
}

private static bool set_feature_flag(string featureName, ConfigFileSettings configFileSettings, bool defaultEnabled, string description)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,21 @@ public virtual void configure_argument_parser(OptionSet optionSet, ChocolateyCon
"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
)
.Add("exitwhenrebootdetected|exit-when-reboot-detected",
"Exit When Reboot Detected - Stop running install, upgrade, or uninstall when a reboot request is detected. Requires '{0}' feature to be turned on. Will exit with either {1} or {2}. Overrides the default feature '{3}' set to '{4}'. Available in 0.10.12+.".format_with
(ApplicationParameters.Features.UsePackageExitCodes, ApplicationParameters.ExitCodes.ErrorFailNoActionReboot, ApplicationParameters.ExitCodes.ErrorInstallSuspend, ApplicationParameters.Features.ExitOnRebootDetected, configuration.Features.ExitOnRebootDetected.to_string()),
option => configuration.Features.ExitOnRebootDetected = option != null
)
.Add("ignoredetectedreboot|ignore-detected-reboot",
"Ignore Detected Reboot - Ignore any detected reboots if found. Overrides the default feature '{0}' set to '{1}'. Available in 0.10.12+.".format_with
(ApplicationParameters.Features.ExitOnRebootDetected, configuration.Features.ExitOnRebootDetected.to_string()),
option =>
{
if (option != null)
{
configuration.Features.ExitOnRebootDetected = false;
}
})
;

//todo: package name can be a url / installertype
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,21 @@ public virtual void configure_argument_parser(OptionSet optionSet, ChocolateyCon
"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
)
.Add("exitwhenrebootdetected|exit-when-reboot-detected",
"Exit When Reboot Detected - Stop running install, upgrade, or uninstall when a reboot request is detected. Requires '{0}' feature to be turned on. Will exit with either {1} or {2}. Overrides the default feature '{3}' set to '{4}'. Available in 0.10.12+.".format_with
(ApplicationParameters.Features.UsePackageExitCodes, ApplicationParameters.ExitCodes.ErrorFailNoActionReboot, ApplicationParameters.ExitCodes.ErrorInstallSuspend, ApplicationParameters.Features.ExitOnRebootDetected, configuration.Features.ExitOnRebootDetected.to_string()),
option => configuration.Features.ExitOnRebootDetected = option != null
)
.Add("ignoredetectedreboot|ignore-detected-reboot",
"Ignore Detected Reboot - Ignore any detected reboots if found. Overrides the default feature '{0}' set to '{1}'. Available in 0.10.12+.".format_with
(ApplicationParameters.Features.ExitOnRebootDetected, configuration.Features.ExitOnRebootDetected.to_string()),
option =>
{
if (option != null)
{
configuration.Features.ExitOnRebootDetected = false;
}
})
;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,21 @@ public virtual void configure_argument_parser(OptionSet optionSet, ChocolateyCon
{
if (option != null) configuration.Features.UseRememberedArgumentsForUpgrades = false;
})
.Add("exitwhenrebootdetected|exit-when-reboot-detected",
"Exit When Reboot Detected - Stop running install, upgrade, or uninstall when a reboot request is detected. Requires '{0}' feature to be turned on. Will exit with either {1} or {2}. Overrides the default feature '{3}' set to '{4}'. Available in 0.10.12+.".format_with
(ApplicationParameters.Features.UsePackageExitCodes, ApplicationParameters.ExitCodes.ErrorFailNoActionReboot, ApplicationParameters.ExitCodes.ErrorInstallSuspend, ApplicationParameters.Features.ExitOnRebootDetected, configuration.Features.ExitOnRebootDetected.to_string()),
option => configuration.Features.ExitOnRebootDetected = option != null
)
.Add("ignoredetectedreboot|ignore-detected-reboot",
"Ignore Detected Reboot - Ignore any detected reboots if found. Overrides the default feature '{0}' set to '{1}'. Available in 0.10.12+.".format_with
(ApplicationParameters.Features.ExitOnRebootDetected, configuration.Features.ExitOnRebootDetected.to_string()),
option =>
{
if (option != null)
{
configuration.Features.ExitOnRebootDetected = false;
}
})
;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -390,6 +390,7 @@ public sealed class FeaturesConfiguration
public bool UseRememberedArgumentsForUpgrades { get; set; }
public bool IgnoreUnfoundPackagesOnUpgradeOutdated { get; set; }
public bool RemovePackageInformationOnUninstall { get; set; }
public bool ExitOnRebootDetected { get; set; }

//todo remove in 0.11.0
public bool ScriptsCheckLastExitCode { get; set; }
Expand Down
Loading

0 comments on commit 2863cd1

Please sign in to comment.