Skip to content

Commit b6289bb

Browse files
fix: add 'id' to scripts, make timing refer to script id
1 parent 586d88f commit b6289bb

29 files changed

+192
-98
lines changed

agent/agentscripts/agentscripts.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -323,6 +323,7 @@ func (r *Runner) run(ctx context.Context, script codersdk.WorkspaceAgentScript)
323323

324324
_, err = r.scriptCompleted(ctx, &proto.WorkspaceAgentScriptCompletedRequest{
325325
Timing: &proto.Timing{
326+
ScriptId: script.ID[:],
326327
DisplayName: script.DisplayName,
327328
Start: timestamppb.New(start),
328329
End: timestamppb.New(end),
@@ -331,6 +332,10 @@ func (r *Runner) run(ctx context.Context, script codersdk.WorkspaceAgentScript)
331332
BlockedLogin: script.StartBlocksLogin,
332333
},
333334
})
335+
336+
if err != nil {
337+
logger.Error(ctx, fmt.Sprintf("reporting script completed: %s", err.Error()))
338+
}
334339
}()
335340

336341
err = cmd.Start()

agent/proto/agent.pb.go

Lines changed: 94 additions & 75 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

agent/proto/agent.proto

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ message WorkspaceAgentScript {
5454
bool start_blocks_login = 7;
5555
google.protobuf.Duration timeout = 8;
5656
string display_name = 9;
57+
bytes id = 10;
5758
}
5859

5960
message WorkspaceAgentMetadata {
@@ -279,6 +280,7 @@ message Timing {
279280
int32 exit_code = 4;
280281
bool ran_on_start = 5;
281282
bool blocked_login = 6;
283+
bytes script_id = 7;
282284
}
283285

284286
service Agent {

coderd/agentapi/manifest.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -178,6 +178,7 @@ func dbAgentScriptsToProto(scripts []database.WorkspaceAgentScript) []*agentprot
178178

179179
func dbAgentScriptToProto(script database.WorkspaceAgentScript) *agentproto.WorkspaceAgentScript {
180180
return &agentproto.WorkspaceAgentScript{
181+
Id: script.ID[:],
181182
DisplayName: script.DisplayName,
182183
LogSourceId: script.LogSourceID[:],
183184
LogPath: script.LogPath,

coderd/agentapi/scripts.go

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,13 @@ type ScriptsAPI struct {
1818
func (s *ScriptsAPI) ScriptCompleted(ctx context.Context, req *agentproto.WorkspaceAgentScriptCompletedRequest) (*agentproto.WorkspaceAgentScriptCompletedResponse, error) {
1919
res := &agentproto.WorkspaceAgentScriptCompletedResponse{}
2020

21-
_, err := s.Database.InsertWorkspaceAgentScriptTimings(ctx, database.InsertWorkspaceAgentScriptTimingsParams{
22-
AgentID: s.AgentID,
21+
scriptID, err := uuid.FromBytes(req.Timing.ScriptId)
22+
if err != nil {
23+
return nil, xerrors.Errorf("script id from bytes: %w", err)
24+
}
25+
26+
_, err = s.Database.InsertWorkspaceAgentScriptTimings(ctx, database.InsertWorkspaceAgentScriptTimingsParams{
27+
ScriptID: scriptID,
2328
DisplayName: req.Timing.DisplayName,
2429
StartedAt: req.Timing.Start.AsTime(),
2530
EndedAt: req.Timing.End.AsTime(),

coderd/apidoc/docs.go

Lines changed: 4 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

coderd/apidoc/swagger.json

Lines changed: 4 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

coderd/database/dbauthz/dbauthz_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2634,7 +2634,7 @@ func (s *MethodTestSuite) TestSystemFunctions() {
26342634
}))
26352635
s.Run("InsertWorkspaceAgentScriptTimings", s.Subtest(func(db database.Store, check *expects) {
26362636
check.Args(database.InsertWorkspaceAgentScriptTimingsParams{
2637-
AgentID: uuid.New(),
2637+
ScriptID: uuid.New(),
26382638
}).Asserts( /* rbac.ResourceSystem, policy.ActionCreate */ )
26392639
}))
26402640
s.Run("InsertWorkspaceAgentScripts", s.Subtest(func(db database.Store, check *expects) {

coderd/database/dbmem/dbmem.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7838,7 +7838,7 @@ func (q *FakeQuerier) InsertWorkspaceAgentScriptTimings(_ context.Context, arg d
78387838

78397839
//nolint:gosimple // Stop linter suggesting 'arg' should be of type database.WorkspaceAgentScriptTiming
78407840
scriptTiming := database.WorkspaceAgentScriptTiming{
7841-
AgentID: arg.AgentID,
7841+
ScriptID: arg.ScriptID,
78427842
StartedAt: arg.StartedAt,
78437843
EndedAt: arg.EndedAt,
78447844
ExitCode: arg.ExitCode,

coderd/database/dump.sql

Lines changed: 6 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

coderd/database/foreign_key_constraint.go

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
DROP TABLE IF EXISTS workspace_agent_script_timings;
22

33
ALTER TABLE workspace_agent_scripts DROP COLUMN display_name;
4+
ALTER TABLE workspace_agent_scripts DROP COLUMN id;

coderd/database/migrations/000255_workspace_agent_script_timings.up.sql

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
1+
ALTER TABLE workspace_agent_scripts ADD COLUMN id uuid unique not null default gen_random_uuid();
2+
13
CREATE TABLE workspace_agent_script_timings
24
(
3-
agent_id uuid not null references workspace_agents (id) on delete cascade,
5+
script_id uuid not null references workspace_agent_scripts (id) on delete cascade,
46
display_name text not null,
57
started_at timestamp with time zone not null,
68
ended_at timestamp with time zone not null,

coderd/database/models.go

Lines changed: 2 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

coderd/database/queries.sql.go

Lines changed: 14 additions & 9 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

coderd/database/queries/workspaceagents.sql

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -291,7 +291,7 @@ WHERE
291291
-- name: InsertWorkspaceAgentScriptTimings :one
292292
INSERT INTO
293293
workspace_agent_script_timings (
294-
agent_id,
294+
script_id,
295295
display_name,
296296
started_at,
297297
ended_at,

coderd/database/queries/workspacescripts.sql

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
-- name: InsertWorkspaceAgentScripts :many
22
INSERT INTO
3-
workspace_agent_scripts ( workspace_agent_id, created_at, log_source_id, log_path, script, cron, start_blocks_login, run_on_start, run_on_stop, timeout_seconds, display_name )
3+
workspace_agent_scripts ( workspace_agent_id, created_at, log_source_id, log_path, script, cron, start_blocks_login, run_on_start, run_on_stop, timeout_seconds, display_name, id )
44
SELECT
55
@workspace_agent_id :: uuid AS workspace_agent_id,
66
@created_at :: timestamptz AS created_at,
@@ -12,7 +12,8 @@ SELECT
1212
unnest(@run_on_start :: boolean [ ]) AS run_on_start,
1313
unnest(@run_on_stop :: boolean [ ]) AS run_on_stop,
1414
unnest(@timeout_seconds :: integer [ ]) AS timeout_seconds,
15-
unnest(@display_name :: text [ ]) AS display_name
15+
unnest(@display_name :: text [ ]) AS display_name,
16+
unnest(@id :: uuid [ ]) AS id
1617
RETURNING workspace_agent_scripts.*;
1718

1819
-- name: GetWorkspaceAgentScriptsByAgentIDs :many

coderd/database/unique_constraint.go

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

coderd/provisionerdserver/provisionerdserver.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1818,6 +1818,7 @@ func InsertWorkspaceResource(ctx context.Context, db database.Store, jobID uuid.
18181818
logSourceIDs := make([]uuid.UUID, 0, len(prAgent.Scripts))
18191819
logSourceDisplayNames := make([]string, 0, len(prAgent.Scripts))
18201820
logSourceIcons := make([]string, 0, len(prAgent.Scripts))
1821+
scriptIDs := make([]uuid.UUID, 0, len(prAgent.Scripts))
18211822
scriptDisplayName := make([]string, 0, len(prAgent.Scripts))
18221823
scriptLogPaths := make([]string, 0, len(prAgent.Scripts))
18231824
scriptSources := make([]string, 0, len(prAgent.Scripts))
@@ -1831,6 +1832,7 @@ func InsertWorkspaceResource(ctx context.Context, db database.Store, jobID uuid.
18311832
logSourceIDs = append(logSourceIDs, uuid.New())
18321833
logSourceDisplayNames = append(logSourceDisplayNames, script.DisplayName)
18331834
logSourceIcons = append(logSourceIcons, script.Icon)
1835+
scriptIDs = append(scriptIDs, uuid.New())
18341836
scriptDisplayName = append(scriptDisplayName, script.DisplayName)
18351837
scriptLogPaths = append(scriptLogPaths, script.LogPath)
18361838
scriptSources = append(scriptSources, script.Script)
@@ -1864,6 +1866,7 @@ func InsertWorkspaceResource(ctx context.Context, db database.Store, jobID uuid.
18641866
RunOnStart: scriptRunOnStart,
18651867
RunOnStop: scriptRunOnStop,
18661868
DisplayName: scriptDisplayName,
1869+
ID: scriptIDs,
18671870
})
18681871
if err != nil {
18691872
return xerrors.Errorf("insert agent scripts: %w", err)

coderd/workspaceagents.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -969,6 +969,7 @@ func convertScripts(dbScripts []database.WorkspaceAgentScript) []codersdk.Worksp
969969
scripts := make([]codersdk.WorkspaceAgentScript, 0)
970970
for _, dbScript := range dbScripts {
971971
scripts = append(scripts, codersdk.WorkspaceAgentScript{
972+
ID: dbScript.ID,
972973
DisplayName: dbScript.DisplayName,
973974
LogPath: dbScript.LogPath,
974975
LogSourceID: dbScript.LogSourceID,

0 commit comments

Comments
 (0)