Add fleetctl run-script command (#13622)

This commit is contained in:
gillespi314
2023-09-05 14:14:09 -05:00
committed by GitHub
parent 113f146c39
commit 37fb4b0dab
15 changed files with 601 additions and 89 deletions
+3 -2
View File
@@ -13,6 +13,7 @@ import (
"time"
"unicode/utf8"
"github.com/fleetdm/fleet/v4/orbit/pkg/constant"
"github.com/fleetdm/fleet/v4/server/fleet"
)
@@ -88,7 +89,7 @@ func (r *Runner) runOne(execID string) (finalErr error) {
return fmt.Errorf("get host script: %w", err)
}
if script.ExitCode.Valid {
if script.ExitCode != nil {
// already a result stored for this execution, skip, it shouldn't be sent
// again by Fleet.
return nil
@@ -119,7 +120,7 @@ func (r *Runner) runOne(execID string) (finalErr error) {
scriptFile := filepath.Join(runDir, "script"+ext)
// the file does not need the executable bit set, it will be executed as
// argument to powershell or /bin/sh.
if err := os.WriteFile(scriptFile, []byte(script.ScriptContents), 0600); err != nil {
if err := os.WriteFile(scriptFile, []byte(script.ScriptContents), constant.DefaultFileMode); err != nil {
return fmt.Errorf("write script file: %w", err)
}
+3 -3
View File
@@ -2,7 +2,6 @@ package scripts
import (
"context"
"database/sql"
"errors"
"fmt"
"io"
@@ -12,6 +11,7 @@ import (
"testing"
"github.com/fleetdm/fleet/v4/server/fleet"
"github.com/fleetdm/fleet/v4/server/ptr"
"github.com/stretchr/testify/require"
)
@@ -124,7 +124,7 @@ func TestRunner(t *testing.T) {
},
{
desc: "script with existing results",
client: &mockClient{scripts: map[string]*fleet.HostScriptResult{"a": {ExitCode: sql.NullInt64{Valid: true}}}},
client: &mockClient{scripts: map[string]*fleet.HostScriptResult{"a": {ExitCode: ptr.Int64(0)}}},
execer: &mockExecCmd{},
enabled: true,
execIDs: []string{"a"},
@@ -133,7 +133,7 @@ func TestRunner(t *testing.T) {
},
{
desc: "multiple errors reported, one get fails, one non-existing",
client: &mockClient{getErr: errFailOnce, scripts: map[string]*fleet.HostScriptResult{"a": {ExitCode: sql.NullInt64{Valid: true}}}},
client: &mockClient{getErr: errFailOnce, scripts: map[string]*fleet.HostScriptResult{"a": {ExitCode: ptr.Int64(0)}}},
execer: &mockExecCmd{},
enabled: true,
execIDs: []string{"a", "b"},