From 33b628525ce7f56acd5f9075c08c89a8723fa0d5 Mon Sep 17 00:00:00 2001 From: Rob Reynolds Date: Sun, 24 Apr 2016 18:20:13 -0500 Subject: [PATCH] (GH-512) Exit with same code as installer When running native installers, allow choco to exit with the same exit code as the installer. Additionally, detect valid exit codes and do not error when receiving those exit codes. Pass the exit code to the package so that result can be passed back. --- .../helpers/chocolateyScriptRunner.ps1 | 16 +++++++++++++++- .../Install-ChocolateyInstallPackage.ps1 | 8 ++++---- .../functions/Start-ChocolateyProcessAsAdmin.ps1 | 3 +++ .../services/ChocolateyPackageService.cs | 2 +- .../services/PowershellService.cs | 15 ++++++++++++++- 5 files changed, 37 insertions(+), 7 deletions(-) diff --git a/src/chocolatey.resources/helpers/chocolateyScriptRunner.ps1 b/src/chocolatey.resources/helpers/chocolateyScriptRunner.ps1 index 9b8d922a0d..0cc03ce2b7 100644 --- a/src/chocolatey.resources/helpers/chocolateyScriptRunner.ps1 +++ b/src/chocolatey.resources/helpers/chocolateyScriptRunner.ps1 @@ -45,4 +45,18 @@ $ShimGen = Join-Path $chocoTools 'shimgen.exe' $checksumExe = Join-Path $chocoTools 'checksum.exe' Write-Debug "Running `'$packageScript`'"; -& "$packageScript" \ No newline at end of file +& "$packageScript" + +$scriptSuccess = $? + +$exitCode = $LASTEXITCODE +if ($exitCode -eq 0 -and -not $scriptSuccess) { + $exitCode = 1 +} + +if ($env:ChocolateyExitCode -ne $null -and $env:ChocolateyExitCode -ne '') { + $exitCode = $env:ChocolateyExitCode +} + + +Exit $exitCode \ No newline at end of file diff --git a/src/chocolatey.resources/helpers/functions/Install-ChocolateyInstallPackage.ps1 b/src/chocolatey.resources/helpers/functions/Install-ChocolateyInstallPackage.ps1 index d4e87b8e3f..dbb9b4d6da 100644 --- a/src/chocolatey.resources/helpers/functions/Install-ChocolateyInstallPackage.ps1 +++ b/src/chocolatey.resources/helpers/functions/Install-ChocolateyInstallPackage.ps1 @@ -95,16 +95,16 @@ param( $msiArgs = "$msiArgs $silentArgs $additionalInstallArgs"; } - Start-ChocolateyProcessAsAdmin "$msiArgs" 'msiexec' -validExitCodes $validExitCodes + $env:ChocolateyExitCode = Start-ChocolateyProcessAsAdmin "$msiArgs" 'msiexec' -validExitCodes $validExitCodes #Start-Process -FilePath msiexec -ArgumentList $msiArgs -Wait } if ($fileType -like 'exe') { if ($overrideArguments) { - Start-ChocolateyProcessAsAdmin "$additionalInstallArgs" $file -validExitCodes $validExitCodes + $env:ChocolateyExitCode = Start-ChocolateyProcessAsAdmin "$additionalInstallArgs" $file -validExitCodes $validExitCodes write-host "Overriding package arguments with `'$additionalInstallArgs`'"; } else { - Start-ChocolateyProcessAsAdmin "$silentArgs $additionalInstallArgs" $file -validExitCodes $validExitCodes + $env:ChocolateyExitCode = Start-ChocolateyProcessAsAdmin "$silentArgs $additionalInstallArgs" $file -validExitCodes $validExitCodes } } @@ -115,7 +115,7 @@ param( } else { $msuArgs = "$file $silentArgs $additionalInstallArgs" } - Start-ChocolateyProcessAsAdmin "$msuArgs" 'wusa.exe' -validExitCodes $validExitCodes + $env:ChocolateyExitCode = Start-ChocolateyProcessAsAdmin "$msuArgs" 'wusa.exe' -validExitCodes $validExitCodes } write-host "$packageName has been installed." diff --git a/src/chocolatey.resources/helpers/functions/Start-ChocolateyProcessAsAdmin.ps1 b/src/chocolatey.resources/helpers/functions/Start-ChocolateyProcessAsAdmin.ps1 index 2375df4f70..5efb6c731c 100644 --- a/src/chocolatey.resources/helpers/functions/Start-ChocolateyProcessAsAdmin.ps1 +++ b/src/chocolatey.resources/helpers/functions/Start-ChocolateyProcessAsAdmin.ps1 @@ -126,8 +126,11 @@ Elevating Permissions and running [`"$exeToRun`" $wrappedStatements]. This may t Write-Debug "Command [`"$exeToRun`" $wrappedStatements] exited with `'$exitCode`'." if ($validExitCodes -notcontains $exitCode) { + Set-PowerShellExitCode $exitCode throw "Running [`"$exeToRun`" $statements] was not successful. Exit code was '$exitCode'. See log for possible error messages." } Write-Debug "Finishing 'Start-ChocolateyProcessAsAdmin'" + + return $exitCode } diff --git a/src/chocolatey/infrastructure.app/services/ChocolateyPackageService.cs b/src/chocolatey/infrastructure.app/services/ChocolateyPackageService.cs index 5bebc34afc..29ab34f70c 100644 --- a/src/chocolatey/infrastructure.app/services/ChocolateyPackageService.cs +++ b/src/chocolatey/infrastructure.app/services/ChocolateyPackageService.cs @@ -875,7 +875,7 @@ private void ensure_bad_package_path_is_clean(ChocolateyConfiguration config, Pa private void handle_unsuccessful_operation(ChocolateyConfiguration config, PackageResult packageResult, bool movePackageToFailureLocation, bool attemptRollback) { - Environment.ExitCode = 1; + if (Environment.ExitCode == 0 ) Environment.ExitCode = 1; foreach (var message in packageResult.Messages.Where(m => m.MessageType == ResultType.Error)) { diff --git a/src/chocolatey/infrastructure.app/services/PowershellService.cs b/src/chocolatey/infrastructure.app/services/PowershellService.cs index d3204a7b40..97f325b091 100644 --- a/src/chocolatey/infrastructure.app/services/PowershellService.cs +++ b/src/chocolatey/infrastructure.app/services/PowershellService.cs @@ -323,14 +323,27 @@ public bool run_action(ChocolateyConfiguration configuration, PackageResult pack `choco -h` for details."); } + if (result.ExitCode != 0) + { + Environment.ExitCode = result.ExitCode; + packageResult.ExitCode = result.ExitCode; + } + + // 0 - most widely used success exit code + // MSI valid exit codes + // 1605 - (uninstall) - the product is not found, could have already been uninstalled + // 1614 (uninstall) - the product is uninstalled + // 1641 - restart initiated + // 3010 - restart required + var validExitCodes = new List { 0, 1605, 1614, 1641, 3010 }; + if (!validExitCodes.Contains(result.ExitCode)) { failure = true; } if (failure) { - Environment.ExitCode = result.ExitCode; packageResult.Messages.Add(new ResultMessage(ResultType.Error, "Error while running '{0}'.{1} See log for details.".format_with(chocoPowerShellScript, Environment.NewLine))); } packageResult.Messages.Add(new ResultMessage(ResultType.Note, "Ran '{0}'".format_with(chocoPowerShellScript)));