mirror of
https://github.com/fleetdm/fleet
synced 2026-05-23 08:58:41 +00:00
set exit_code = -1 for scripts that are pending before upgrade to 4.44.0 (#16581)
for https://github.com/fleetdm/fleet/issues/16547 # 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:
parent
dbf53cae6a
commit
db58539c9d
2 changed files with 16 additions and 16 deletions
|
|
@ -49,23 +49,23 @@ func Up_20240126020643(tx *sql.Tx) error {
|
|||
return errors.Wrap(err, "create host_activities table")
|
||||
}
|
||||
|
||||
// Prior to this update, the database didn't differentiate between
|
||||
// "async" and "sync" requests. With Fleet v4.44.0, all async requests
|
||||
// will execute regardless of their pending duration. To avoid
|
||||
// unintended execution of old requests upon server upgrade, these are
|
||||
// now marked as "sync", reflecting their original 5-minute execution
|
||||
// limit.
|
||||
// Force an exit_code for scripts that didn't run previous to this update.
|
||||
// In previous releases, scripts were allowed to be queued only for 5
|
||||
// minutes, since we're removing that restriction, any scripts with a
|
||||
// null exit_code would be unexpectedly sent to the host unless we do
|
||||
// this.
|
||||
const setOldScriptsAsSyncStmt = `
|
||||
UPDATE host_script_results hsr
|
||||
SET
|
||||
sync_request = 1,
|
||||
exit_code = -1,
|
||||
updated_at = hsr.updated_at
|
||||
WHERE
|
||||
user_id IS NULL
|
||||
exit_code IS NULL
|
||||
AND user_id IS NULL
|
||||
AND created_at < CURRENT_TIMESTAMP
|
||||
`
|
||||
if _, err := tx.Exec(setOldScriptsAsSyncStmt); err != nil {
|
||||
return errors.Wrap(err, "set sync_request = 1 for old scripts")
|
||||
return errors.Wrap(err, "set exit_code = -1 for old scripts")
|
||||
}
|
||||
|
||||
return nil
|
||||
|
|
|
|||
|
|
@ -32,24 +32,24 @@ func TestUp_20240126020643(t *testing.T) {
|
|||
// existing host execution request's timestamp hasn't changed (despite
|
||||
// added column, and modified sync_request)
|
||||
type scriptResults struct {
|
||||
CreatedAt time.Time `db:"created_at"`
|
||||
UpdatedAt time.Time `db:"updated_at"`
|
||||
SyncRequest bool `db:"sync_request"`
|
||||
CreatedAt time.Time `db:"created_at"`
|
||||
UpdatedAt time.Time `db:"updated_at"`
|
||||
ExitCode *int `db:"exit_code"`
|
||||
}
|
||||
|
||||
var sr scriptResults
|
||||
err := db.Get(&sr, `SELECT created_at, updated_at, sync_request FROM host_script_results WHERE id = ?`, hsr1)
|
||||
err := db.Get(&sr, `SELECT created_at, updated_at, exit_code FROM host_script_results WHERE id = ?`, hsr1)
|
||||
require.NoError(t, err)
|
||||
assert.Equal(t, minutesAgo, sr.CreatedAt)
|
||||
assert.Equal(t, minutesAgo, sr.UpdatedAt)
|
||||
assert.True(t, sr.SyncRequest)
|
||||
assert.Equal(t, -1, *sr.ExitCode)
|
||||
|
||||
sr = scriptResults{}
|
||||
err = db.Get(&sr, `SELECT created_at, updated_at, sync_request FROM host_script_results WHERE id = ?`, hsr2)
|
||||
err = db.Get(&sr, `SELECT created_at, updated_at, exit_code FROM host_script_results WHERE id = ?`, hsr2)
|
||||
require.NoError(t, err)
|
||||
assert.Equal(t, minutesAgo, sr.CreatedAt)
|
||||
assert.Equal(t, minutesAgo, sr.UpdatedAt)
|
||||
assert.True(t, sr.SyncRequest)
|
||||
assert.Equal(t, 1, *sr.ExitCode)
|
||||
|
||||
// create a new host execution request with user u1 and one with u2
|
||||
hsr3 := execNoErrLastID(t, db, `INSERT INTO host_script_results (host_id, execution_id, script_contents, output, user_id) VALUES (?, ?, ?, ?, ?)`, 1, uuid.NewString(), "echo 'hello'", "", u1)
|
||||
|
|
|
|||
Loading…
Reference in a new issue