Skip to content

Commit

Permalink
(GH-512) Exit with same code as installer
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
ferventcoder committed Apr 24, 2016
1 parent cfa8e2f commit 33b6285
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 7 deletions.
16 changes: 15 additions & 1 deletion src/chocolatey.resources/helpers/chocolateyScriptRunner.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -45,4 +45,18 @@ $ShimGen = Join-Path $chocoTools 'shimgen.exe'
$checksumExe = Join-Path $chocoTools 'checksum.exe'

Write-Debug "Running `'$packageScript`'";
& "$packageScript"
& "$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
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}

Expand All @@ -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."
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Original file line number Diff line number Diff line change
Expand Up @@ -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))
{
Expand Down
15 changes: 14 additions & 1 deletion src/chocolatey/infrastructure.app/services/PowershellService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<int> { 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)));
Expand Down

0 comments on commit 33b6285

Please sign in to comment.