Skip to content

Commit 942fde6

Browse files
committed
It works!
1 parent 00a4e73 commit 942fde6

File tree

4 files changed

+88
-13
lines changed

4 files changed

+88
-13
lines changed

coderd/provisionerdserver/provisionerdserver.go

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1281,6 +1281,59 @@ func InsertWorkspaceResource(ctx context.Context, db database.Store, jobID uuid.
12811281
}
12821282
}
12831283

1284+
logSourceIDs := make([]uuid.UUID, 0, len(prAgent.Scripts))
1285+
logSourceCreatedAt := make([]time.Time, 0, len(prAgent.Scripts))
1286+
logSourceDisplayNames := make([]string, 0, len(prAgent.Scripts))
1287+
logSourceIcons := make([]string, 0, len(prAgent.Scripts))
1288+
scriptLogPaths := make([]string, 0, len(prAgent.Scripts))
1289+
scriptSources := make([]string, 0, len(prAgent.Scripts))
1290+
scriptCron := make([]string, 0, len(prAgent.Scripts))
1291+
scriptTimeout := make([]int32, 0, len(prAgent.Scripts))
1292+
scriptStartBlocksLogin := make([]bool, 0, len(prAgent.Scripts))
1293+
scriptRunOnStart := make([]bool, 0, len(prAgent.Scripts))
1294+
scriptRunOnStop := make([]bool, 0, len(prAgent.Scripts))
1295+
1296+
for _, script := range prAgent.Scripts {
1297+
logSourceIDs = append(logSourceIDs, uuid.New())
1298+
logSourceCreatedAt = append(logSourceCreatedAt, dbtime.Now())
1299+
logSourceDisplayNames = append(logSourceDisplayNames, script.DisplayName)
1300+
logSourceIcons = append(logSourceIcons, script.Icon)
1301+
scriptLogPaths = append(scriptLogPaths, script.LogPath)
1302+
scriptSources = append(scriptSources, script.Source)
1303+
scriptCron = append(scriptCron, script.Cron)
1304+
scriptTimeout = append(scriptTimeout, script.Timeout)
1305+
scriptStartBlocksLogin = append(scriptStartBlocksLogin, script.StartBlocksLogin)
1306+
scriptRunOnStart = append(scriptRunOnStart, script.RunOnStart)
1307+
scriptRunOnStop = append(scriptRunOnStop, script.RunOnStop)
1308+
}
1309+
1310+
_, err = db.InsertWorkspaceAgentLogSources(ctx, database.InsertWorkspaceAgentLogSourcesParams{
1311+
WorkspaceAgentID: agentID,
1312+
ID: logSourceIDs,
1313+
CreatedAt: logSourceCreatedAt,
1314+
DisplayName: logSourceDisplayNames,
1315+
Icon: logSourceIcons,
1316+
})
1317+
if err != nil {
1318+
return xerrors.Errorf("insert agent log sources: %w", err)
1319+
}
1320+
1321+
_, err = db.InsertWorkspaceAgentScripts(ctx, database.InsertWorkspaceAgentScriptsParams{
1322+
WorkspaceAgentID: agentID,
1323+
LogSourceID: logSourceIDs,
1324+
LogPath: scriptLogPaths,
1325+
CreatedAt: logSourceCreatedAt,
1326+
Source: scriptSources,
1327+
Cron: scriptCron,
1328+
Timeout: scriptTimeout,
1329+
StartBlocksLogin: scriptStartBlocksLogin,
1330+
RunOnStart: scriptRunOnStart,
1331+
RunOnStop: scriptRunOnStop,
1332+
})
1333+
if err != nil {
1334+
return xerrors.Errorf("insert agent scripts: %w", err)
1335+
}
1336+
12841337
for _, app := range prAgent.Apps {
12851338
slug := app.Slug
12861339
if slug == "" {

coderd/provisionerdserver/provisionerdserver_test.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1565,6 +1565,10 @@ func TestInsertWorkspaceResource(t *testing.T) {
15651565
Apps: []*sdkproto.App{{
15661566
Slug: "a",
15671567
}},
1568+
Scripts: []*sdkproto.Script{{
1569+
DisplayName: "Startup",
1570+
Icon: "/test.png",
1571+
}},
15681572
DisplayApps: &sdkproto.DisplayApps{
15691573
Vscode: true,
15701574
PortForwardingHelper: true,

coderd/workspaceagents.go

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -131,12 +131,13 @@ func (api *API) workspaceAgentManifest(rw http.ResponseWriter, r *http.Request)
131131
return nil
132132
})
133133
eg.Go(func() (err error) {
134-
scripts, err = api.Database.GetWorkspaceAgentScriptsByAgentIDs(ctx, []uuid.UUID{workspaceAgent.ID})
135-
return
134+
// nolint:gocritic // This is necessary to fetch agent scripts!
135+
scripts, err = api.Database.GetWorkspaceAgentScriptsByAgentIDs(dbauthz.AsSystemRestricted(ctx), []uuid.UUID{workspaceAgent.ID})
136+
return err
136137
})
137138
eg.Go(func() (err error) {
138139
metadata, err = api.Database.GetWorkspaceAgentMetadata(ctx, workspaceAgent.ID)
139-
return
140+
return err
140141
})
141142
eg.Go(func() (err error) {
142143
resource, err = api.Database.GetWorkspaceResourceByID(ctx, workspaceAgent.ResourceID)
@@ -155,7 +156,7 @@ func (api *API) workspaceAgentManifest(rw http.ResponseWriter, r *http.Request)
155156
if err != nil {
156157
return xerrors.Errorf("getting workspace owner by id: %w", err)
157158
}
158-
return
159+
return err
159160
})
160161
err = eg.Wait()
161162
if err != nil {

coderd/workspacebuilds.go

Lines changed: 26 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import (
1212
"github.com/go-chi/chi/v5"
1313
"github.com/google/uuid"
1414
"golang.org/x/exp/slices"
15+
"golang.org/x/sync/errgroup"
1516
"golang.org/x/xerrors"
1617

1718
"cdr.dev/slog"
@@ -719,16 +720,31 @@ func (api *API) workspaceBuildsData(ctx context.Context, workspaces []database.W
719720
agentIDs = append(agentIDs, agent.ID)
720721
}
721722

722-
// nolint:gocritic // Getting workspace apps by agent IDs is a system function.
723-
apps, err := api.Database.GetWorkspaceAppsByAgentIDs(dbauthz.AsSystemRestricted(ctx), agentIDs)
724-
if err != nil && !errors.Is(err, sql.ErrNoRows) {
725-
return workspaceBuildsData{}, xerrors.Errorf("fetching workspace apps: %w", err)
726-
}
723+
var (
724+
apps []database.WorkspaceApp
725+
scripts []database.WorkspaceAgentScript
726+
logSources []database.WorkspaceAgentLogSource
727+
)
727728

728-
// nolint:gocritic // Getting workspace scripts by agent IDs is a system function.
729-
scripts, err := api.Database.GetWorkspaceAgentScriptsByAgentIDs(dbauthz.AsSystemRestricted(ctx), agentIDs)
730-
if err != nil && !errors.Is(err, sql.ErrNoRows) {
731-
return workspaceBuildsData{}, xerrors.Errorf("fetching workspace agent scripts: %w", err)
729+
var eg errgroup.Group
730+
eg.Go(func() (err error) {
731+
// nolint:gocritic // Getting workspace apps by agent IDs is a system function.
732+
apps, err = api.Database.GetWorkspaceAppsByAgentIDs(dbauthz.AsSystemRestricted(ctx), agentIDs)
733+
return err
734+
})
735+
eg.Go(func() (err error) {
736+
// nolint:gocritic // Getting workspace scripts by agent IDs is a system function.
737+
scripts, err = api.Database.GetWorkspaceAgentScriptsByAgentIDs(dbauthz.AsSystemRestricted(ctx), agentIDs)
738+
return err
739+
})
740+
eg.Go(func() error {
741+
// nolint:gocritic // Getting workspace agent log sources by agent IDs is a system function.
742+
logSources, err = api.Database.GetWorkspaceAgentLogSourcesByAgentIDs(dbauthz.AsSystemRestricted(ctx), agentIDs)
743+
return err
744+
})
745+
err = eg.Wait()
746+
if err != nil {
747+
return workspaceBuildsData{}, err
732748
}
733749

734750
return workspaceBuildsData{
@@ -740,6 +756,7 @@ func (api *API) workspaceBuildsData(ctx context.Context, workspaces []database.W
740756
agents: agents,
741757
apps: apps,
742758
scripts: scripts,
759+
logSources: logSources,
743760
}, nil
744761
}
745762

0 commit comments

Comments
 (0)