fix issues installing software in windows (#19048)

for #19039 and #19041 this:

- fixes the install/remove scripts to read the env variable the proper
way
- truncates output before storing in the databse in case its longer than
MySQL's TEXT size

# Checklist for submitter

If some of the following don't apply, delete the relevant line.

<!-- Note that API documentation changes are now addressed by the
product design team. -->

- [x] Added/updated tests
- [x] Manual QA for all new/changed functionality
This commit is contained in:
Roberto Dip 2024-05-15 19:39:42 -03:00 committed by GitHub
parent ad94dff814
commit d383876a3c
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
10 changed files with 35 additions and 22 deletions

View file

@ -1,4 +1,4 @@
$exeFilePath = "$INSTALLER_PATH"
$exeFilePath = "${env:INSTALLER_PATH}"
# extract the name of the executable to use as the sub-directory name
$exeName = [System.IO.Path]::GetFileName($exeFilePath)

View file

@ -1,7 +1,7 @@
$logFile = "${env:TEMP}/fleet-install-software.log"
$installProcess = Start-Process msiexec.exe `
-ArgumentList "/quiet /norestart /lv ${logFile} /i `"$INSTALLER_PATH`"" `
-ArgumentList "/quiet /norestart /lv ${logFile} /i `"${env:INSTALLER_PATH}`"" `
-PassThru -Verb RunAs -Wait
Get-Content $logFile -Tail 500

View file

@ -1,4 +1,4 @@
$exeFilePath = "$INSTALLER_PATH"
$exeFilePath = "${env:INSTALLER_PATH}"
# extract the name of the executable to use as the sub-directory name
$exeName = [System.IO.Path]::GetFileName($exeFilePath)

View file

@ -1,7 +1,7 @@
$logFile = "${env:TEMP}/fleet-remove-software.log"
$removeProcess = Start-Process msiexec.exe `
-ArgumentList "/quiet /norestart /lv ${logFile} /x `"$INSTALLER_PATH`"" `
-ArgumentList "/quiet /norestart /lv ${logFile} /x `"${env:INSTALLER_PATH}`"" `
-PassThru -Verb RunAs -Wait
Get-Content $logFile -Tail 500

View file

@ -1,4 +1,4 @@
$exeFilePath = "$INSTALLER_PATH"
$exeFilePath = "${env:INSTALLER_PATH}"
# extract the name of the executable to use as the sub-directory name
$exeName = [System.IO.Path]::GetFileName($exeFilePath)

View file

@ -1,7 +1,7 @@
$logFile = "${env:TEMP}/fleet-install-software.log"
$installProcess = Start-Process msiexec.exe `
-ArgumentList "/quiet /norestart /lv ${logFile} /i `"$INSTALLER_PATH`"" `
-ArgumentList "/quiet /norestart /lv ${logFile} /i `"${env:INSTALLER_PATH}`"" `
-PassThru -Verb RunAs -Wait
Get-Content $logFile -Tail 500

View file

@ -1,4 +1,4 @@
$exeFilePath = "$INSTALLER_PATH"
$exeFilePath = "${env:INSTALLER_PATH}"
# extract the name of the executable to use as the sub-directory name
$exeName = [System.IO.Path]::GetFileName($exeFilePath)

View file

@ -1,7 +1,7 @@
$logFile = "${env:TEMP}/fleet-remove-software.log"
$removeProcess = Start-Process msiexec.exe `
-ArgumentList "/quiet /norestart /lv ${logFile} /x `"$INSTALLER_PATH`"" `
-ArgumentList "/quiet /norestart /lv ${logFile} /x `"${env:INSTALLER_PATH}`"" `
-PassThru -Verb RunAs -Wait
Get-Content $logFile -Tail 500

View file

@ -66,6 +66,20 @@ func newHostScriptExecutionRequest(ctx context.Context, request *fleet.HostScrip
return &script, nil
}
func truncateScriptResult(output string) string {
const maxOutputRuneLen = 10000
if len(output) > utf8.UTFMax*maxOutputRuneLen {
// truncate the bytes as we know the output is too long, no point
// converting more bytes than needed to runes.
output = output[len(output)-(utf8.UTFMax*maxOutputRuneLen):]
}
if utf8.RuneCountInString(output) > maxOutputRuneLen {
outputRunes := []rune(output)
output = string(outputRunes[len(outputRunes)-maxOutputRuneLen:])
}
return output
}
func (ds *Datastore) SetHostScriptExecutionResult(ctx context.Context, result *fleet.HostScriptResultPayload) (*fleet.HostScriptResult, error) {
const resultExistsStmt = `
SELECT
@ -101,17 +115,7 @@ func (ds *Datastore) SetHostScriptExecutionResult(ctx context.Context, result *f
host_id = ?
`
const maxOutputRuneLen = 10000
output := result.Output
if len(output) > utf8.UTFMax*maxOutputRuneLen {
// truncate the bytes as we know the output is too long, no point
// converting more bytes than needed to runes.
output = output[len(output)-(utf8.UTFMax*maxOutputRuneLen):]
}
if utf8.RuneCountInString(output) > maxOutputRuneLen {
outputRunes := []rune(output)
output = string(outputRunes[len(outputRunes)-maxOutputRuneLen:])
}
output := truncateScriptResult(result.Output)
var hsr *fleet.HostScriptResult
err := ds.withRetryTxx(ctx, func(tx sqlx.ExtContext) error {

View file

@ -14,6 +14,7 @@ import (
_ "github.com/doug-martin/goqu/v9/dialect/mysql"
"github.com/fleetdm/fleet/v4/server/contexts/ctxerr"
"github.com/fleetdm/fleet/v4/server/fleet"
"github.com/fleetdm/fleet/v4/server/ptr"
"github.com/go-kit/kit/log/level"
"github.com/jmoiron/sqlx"
)
@ -2049,12 +2050,20 @@ func (ds *Datastore) SetHostSoftwareInstallResult(ctx context.Context, result *f
execution_id = ? AND
host_id = ?
`
truncateOutput := func(output *string) *string {
if output != nil {
output = ptr.String(truncateScriptResult(*output))
}
return output
}
res, err := ds.writer(ctx).ExecContext(ctx, stmt,
result.PreInstallConditionOutput,
truncateOutput(result.PreInstallConditionOutput),
result.InstallScriptExitCode,
result.InstallScriptOutput,
truncateOutput(result.InstallScriptOutput),
result.PostInstallScriptExitCode,
result.PostInstallScriptOutput,
truncateOutput(result.PostInstallScriptOutput),
result.InstallUUID,
result.HostID,
)