Skip to content

Commit 48ca345

Browse files
committed
add script output to gitops
1 parent 9d8e6e4 commit 48ca345

3 files changed

Lines changed: 83 additions & 0 deletions

File tree

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
- Added output to GitOps for scripts, indicating how many would or be applied when running.

cmd/fleetctl/fleetctl/gitops_test.go

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6609,3 +6609,72 @@ software:
66096609
assert.Contains(t, err.Error(), "macos_manual_agent_install")
66106610
})
66116611
}
6612+
6613+
func TestGitOpsScriptsLogging(t *testing.T) {
6614+
// Cannot run t.Parallel() because SetupFullGitOpsPremiumServer sets env vars.
6615+
ds, _, _ := testing_utils.SetupFullGitOpsPremiumServer(t)
6616+
6617+
// Echo back a ScriptResponse for each input script so the real-run no-team
6618+
// log line reflects the count of scripts sent to the server. (In dry-run,
6619+
// the service short-circuits before reaching this mock.)
6620+
ds.BatchSetScriptsFunc = func(ctx context.Context, tmID *uint, scripts []*fleet.Script) ([]fleet.ScriptResponse, error) {
6621+
responses := make([]fleet.ScriptResponse, len(scripts))
6622+
for i, s := range scripts {
6623+
responses[i] = fleet.ScriptResponse{ID: uint(i + 1), Name: s.Name, TeamID: s.TeamID} //nolint:gosec // dismiss G115
6624+
}
6625+
return responses, nil
6626+
}
6627+
6628+
// No labels — keep the test focused on scripts.
6629+
ds.GetLabelSpecsFunc = func(ctx context.Context, filter fleet.TeamFilter) ([]*fleet.LabelSpec, error) {
6630+
return nil, nil
6631+
}
6632+
6633+
tmpDir := t.TempDir()
6634+
scriptPath := filepath.Join(tmpDir, "script.sh")
6635+
require.NoError(t, os.WriteFile(scriptPath, []byte(`echo "hello"`), 0o644))
6636+
6637+
globalPath := filepath.Join(tmpDir, "global.yml")
6638+
require.NoError(t, os.WriteFile(globalPath, []byte(`
6639+
controls:
6640+
scripts:
6641+
- path: ./script.sh
6642+
queries:
6643+
policies:
6644+
agent_options:
6645+
org_settings:
6646+
server_settings:
6647+
server_url: $FLEET_SERVER_URL
6648+
org_info:
6649+
contact_url: https://example.com/contact
6650+
org_name: $ORG_NAME
6651+
secrets:
6652+
- secret: globalSecret
6653+
software:
6654+
`), 0o644))
6655+
6656+
teamPath := filepath.Join(tmpDir, "team.yml")
6657+
require.NoError(t, os.WriteFile(teamPath, []byte(`
6658+
name: $TEST_TEAM_NAME
6659+
team_settings:
6660+
secrets:
6661+
- secret: team-secret
6662+
agent_options:
6663+
controls:
6664+
scripts:
6665+
- path: ./script.sh
6666+
policies:
6667+
queries:
6668+
software:
6669+
`), 0o644))
6670+
6671+
// Dry run.
6672+
logs := RunAppForTest(t, []string{"gitops", "-f", globalPath, "-f", teamPath, "--dry-run"})
6673+
assert.Contains(t, logs, "[+] would've applied 1 script\n")
6674+
assert.Contains(t, logs, fmt.Sprintf("[+] would've applied 1 script for fleet %s\n", teamName))
6675+
6676+
// Real run.
6677+
logs = RunAppForTest(t, []string{"gitops", "-f", globalPath, "-f", teamPath})
6678+
assert.Contains(t, logs, "[+] applied 1 script\n")
6679+
assert.Contains(t, logs, fmt.Sprintf("[+] applying 1 script for fleet %s\n", teamName))
6680+
}

server/service/client.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -664,6 +664,12 @@ func (c *Client) ApplyGroup(
664664
return nil, nil, nil, nil, fmt.Errorf("applying scripts for unassigned hosts: %w", err)
665665
}
666666
teamsScripts["No team"] = noTeamScripts
667+
668+
if opts.DryRun {
669+
logfn(dryRunAppliedFormat, numberWithPluralization(len(scriptPayloads), "script", "scripts"))
670+
} else {
671+
logfn(appliedFormat, numberWithPluralization(len(noTeamScripts), "script", "scripts"))
672+
}
667673
}
668674

669675
rules, err := extractAppCfgYaraRules(specs.AppConfig)
@@ -1066,6 +1072,13 @@ func (c *Client) ApplyGroup(
10661072
return nil, nil, nil, nil, fmt.Errorf("applying scripts for fleet %q: %w", tmName, err)
10671073
}
10681074
teamsScripts[tmName] = scriptResponses
1075+
if opts.DryRun {
1076+
// We split here on dry-run to capture the number we want to apply
1077+
logfn(format, numberWithPluralization(len(scripts), "script", "scripts"), tmName)
1078+
} else {
1079+
// vs. the number that actually got applied and returned by the server
1080+
logfn(format, numberWithPluralization(len(scriptResponses), "script", "scripts"), tmName)
1081+
}
10691082
}
10701083
}
10711084
if len(tmSoftwarePackagesPayloads) > 0 {

0 commit comments

Comments
 (0)