Skip to content

Commit 098cafb

Browse files
committed
(chocolateyGH-1038) Stop operation on package reboot request
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.
1 parent bde8a9d commit 098cafb

File tree

8 files changed

+161
-49
lines changed

8 files changed

+161
-49
lines changed

src/chocolatey.console/Program.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,7 @@ private static void Main(string[] args)
158158
"chocolatey".Log().Error(ChocolateyLoggers.Important, () => "{0}".format_with(ex.Message));
159159
}
160160

161-
Environment.ExitCode = 1;
161+
if (Environment.ExitCode == 0) Environment.ExitCode = 1;
162162
}
163163
finally
164164
{

src/chocolatey/infrastructure.app/ApplicationParameters.cs

+8-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
// Copyright © 2017 - 2018 Chocolatey Software, Inc
22
// Copyright © 2011 - 2017 RealDimensions Software, LLC
3-
//
3+
//
44
// Licensed under the Apache License, Version 2.0 (the "License");
55
// you may not use this file except in compliance with the License.
66
//
@@ -127,6 +127,12 @@ public static class Environment
127127
/// </summary>
128128
public static readonly bool AllowPrompts = true;
129129

130+
public static class ExitCodes
131+
{
132+
public static readonly int ErrorFailNoActionReboot = 350;
133+
public static readonly int ErrorInstallSuspend = 1604;
134+
}
135+
130136
public static class Tools
131137
{
132138
//public static readonly string WebPiCmdExe = _fileSystem.combine_paths(InstallLocation, "nuget.exe");
@@ -171,6 +177,7 @@ public static class Features
171177
public static readonly string SkipPackageUpgradesWhenNotInstalled = "skipPackageUpgradesWhenNotInstalled";
172178
public static readonly string RemovePackageInformationOnUninstall = "removePackageInformationOnUninstall";
173179
public static readonly string LogWithoutColor = "logWithoutColor";
180+
public static readonly string ExitOnRebootDetected = "exitOnRebootDetected";
174181
}
175182

176183
public static class Messages

src/chocolatey/infrastructure.app/builders/ConfigurationBuilder.cs

+1
Original file line numberDiff line numberDiff line change
@@ -304,6 +304,7 @@ private static void set_feature_flags(ChocolateyConfiguration config, ConfigFile
304304

305305
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.");
306306
config.PromptForConfirmation = !set_feature_flag(ApplicationParameters.Features.AllowGlobalConfirmation, configFileSettings, defaultEnabled: false, description: "Prompt for confirmation in scripts or bypass.");
307+
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));
307308
}
308309

309310
private static bool set_feature_flag(string featureName, ConfigFileSettings configFileSettings, bool defaultEnabled, string description)

src/chocolatey/infrastructure.app/commands/ChocolateyInstallCommand.cs

+15
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,21 @@ public virtual void configure_argument_parser(OptionSet optionSet, ChocolateyCon
155155
"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()),
156156
option => configuration.Features.StopOnFirstPackageFailure = option != null
157157
)
158+
.Add("exitwhenrebootdetected|exit-when-reboot-detected",
159+
"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
160+
(ApplicationParameters.Features.UsePackageExitCodes, ApplicationParameters.ExitCodes.ErrorFailNoActionReboot, ApplicationParameters.ExitCodes.ErrorInstallSuspend, ApplicationParameters.Features.ExitOnRebootDetected, configuration.Features.ExitOnRebootDetected.to_string()),
161+
option => configuration.Features.ExitOnRebootDetected = option != null
162+
)
163+
.Add("ignoredetectedreboot|ignore-detected-reboot",
164+
"Ignore Detected Reboot - Ignore any detected reboots if found. Overrides the default feature '{0}' set to '{1}'. Available in 0.10.12+.".format_with
165+
(ApplicationParameters.Features.ExitOnRebootDetected, configuration.Features.ExitOnRebootDetected.to_string()),
166+
option =>
167+
{
168+
if (option != null)
169+
{
170+
configuration.Features.ExitOnRebootDetected = false;
171+
}
172+
})
158173
;
159174

160175
//todo: package name can be a url / installertype

src/chocolatey/infrastructure.app/commands/ChocolateyUninstallCommand.cs

+15
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,21 @@ public virtual void configure_argument_parser(OptionSet optionSet, ChocolateyCon
118118
"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()),
119119
option => configuration.Features.StopOnFirstPackageFailure = option != null
120120
)
121+
.Add("exitwhenrebootdetected|exit-when-reboot-detected",
122+
"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
123+
(ApplicationParameters.Features.UsePackageExitCodes, ApplicationParameters.ExitCodes.ErrorFailNoActionReboot, ApplicationParameters.ExitCodes.ErrorInstallSuspend, ApplicationParameters.Features.ExitOnRebootDetected, configuration.Features.ExitOnRebootDetected.to_string()),
124+
option => configuration.Features.ExitOnRebootDetected = option != null
125+
)
126+
.Add("ignoredetectedreboot|ignore-detected-reboot",
127+
"Ignore Detected Reboot - Ignore any detected reboots if found. Overrides the default feature '{0}' set to '{1}'. Available in 0.10.12+.".format_with
128+
(ApplicationParameters.Features.ExitOnRebootDetected, configuration.Features.ExitOnRebootDetected.to_string()),
129+
option =>
130+
{
131+
if (option != null)
132+
{
133+
configuration.Features.ExitOnRebootDetected = false;
134+
}
135+
})
121136
;
122137
}
123138

src/chocolatey/infrastructure.app/commands/ChocolateyUpgradeCommand.cs

+15
Original file line numberDiff line numberDiff line change
@@ -195,6 +195,21 @@ public virtual void configure_argument_parser(OptionSet optionSet, ChocolateyCon
195195
{
196196
if (option != null) configuration.Features.UseRememberedArgumentsForUpgrades = false;
197197
})
198+
.Add("exitwhenrebootdetected|exit-when-reboot-detected",
199+
"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
200+
(ApplicationParameters.Features.UsePackageExitCodes, ApplicationParameters.ExitCodes.ErrorFailNoActionReboot, ApplicationParameters.ExitCodes.ErrorInstallSuspend, ApplicationParameters.Features.ExitOnRebootDetected, configuration.Features.ExitOnRebootDetected.to_string()),
201+
option => configuration.Features.ExitOnRebootDetected = option != null
202+
)
203+
.Add("ignoredetectedreboot|ignore-detected-reboot",
204+
"Ignore Detected Reboot - Ignore any detected reboots if found. Overrides the default feature '{0}' set to '{1}'. Available in 0.10.12+.".format_with
205+
(ApplicationParameters.Features.ExitOnRebootDetected, configuration.Features.ExitOnRebootDetected.to_string()),
206+
option =>
207+
{
208+
if (option != null)
209+
{
210+
configuration.Features.ExitOnRebootDetected = false;
211+
}
212+
})
198213
;
199214
}
200215

src/chocolatey/infrastructure.app/configuration/ChocolateyConfiguration.cs

+1
Original file line numberDiff line numberDiff line change
@@ -391,6 +391,7 @@ public sealed class FeaturesConfiguration
391391
public bool IgnoreUnfoundPackagesOnUpgradeOutdated { get; set; }
392392
public bool SkipPackageUpgradesWhenNotInstalled { get; set; }
393393
public bool RemovePackageInformationOnUninstall { get; set; }
394+
public bool ExitOnRebootDetected { get; set; }
394395

395396
//todo remove in 0.11.0
396397
public bool ScriptsCheckLastExitCode { get; set; }

0 commit comments

Comments
 (0)