mirror of
https://github.com/coder/coder.git
synced 2026-09-24 15:04:27 +08:00
feat: add support for coder_script (#9584)
* Add basic migrations * Improve schema * Refactor agent scripts into it's own package * Support legacy start and stop script format * Pipe the scripts! * Finish the piping * Fix context usage * It works! * Fix sql query * Fix SQL query * Rename `LogSourceID` -> `SourceID` * Fix the FE * fmt * Rename migrations * Fix log tests * Fix lint err * Fix gen * Fix story type * Rename source to script * Fix schema jank * Uncomment test * Rename proto to TimeoutSeconds * Fix comments * Fix comments * Fix legacy endpoint without specified log_source * Fix non-blocking by default in agent * Fix resources tests * Fix dbfake * Fix resources * Fix linting I think * Add fixtures * fmt * Fix startup script behavior * Fix comments * Fix context * Fix cancel * Fix SQL tests * Fix e2e tests * Interrupt on Windows * Fix agent leaking script process * Fix migrations * Fix stories * Fix duplicate logs appearing * Gen * Fix log location * Fix tests * Fix tests * Fix log output * Show display name in output * Fix print * Return timeout on start context * Gen * Fix fixture * Fix the agent status * Fix startup timeout msg * Fix command using shared context * Fix timeout draining * Change signal type * Add deterministic colors to startup script logs --------- Co-authored-by: Muhammad Atif Ali <atif@coder.com>
This commit is contained in:
co-authored by
Muhammad Atif Ali
parent
dac1375880
commit
1262eef2c0
@@ -1367,39 +1367,23 @@ func InsertWorkspaceResource(ctx context.Context, db database.Store, jobID uuid.
|
||||
}
|
||||
}
|
||||
|
||||
// Set the default in case it was not provided (e.g. echo provider).
|
||||
if prAgent.GetStartupScriptBehavior() == "" {
|
||||
prAgent.StartupScriptBehavior = string(codersdk.WorkspaceAgentStartupScriptBehaviorNonBlocking)
|
||||
}
|
||||
|
||||
agentID := uuid.New()
|
||||
dbAgent, err := db.InsertWorkspaceAgent(ctx, database.InsertWorkspaceAgentParams{
|
||||
ID: agentID,
|
||||
CreatedAt: dbtime.Now(),
|
||||
UpdatedAt: dbtime.Now(),
|
||||
ResourceID: resource.ID,
|
||||
Name: prAgent.Name,
|
||||
AuthToken: authToken,
|
||||
AuthInstanceID: instanceID,
|
||||
Architecture: prAgent.Architecture,
|
||||
EnvironmentVariables: env,
|
||||
Directory: prAgent.Directory,
|
||||
OperatingSystem: prAgent.OperatingSystem,
|
||||
StartupScript: sql.NullString{
|
||||
String: prAgent.StartupScript,
|
||||
Valid: prAgent.StartupScript != "",
|
||||
},
|
||||
ConnectionTimeoutSeconds: prAgent.GetConnectionTimeoutSeconds(),
|
||||
TroubleshootingURL: prAgent.GetTroubleshootingUrl(),
|
||||
MOTDFile: prAgent.GetMotdFile(),
|
||||
StartupScriptBehavior: database.StartupScriptBehavior(prAgent.GetStartupScriptBehavior()),
|
||||
StartupScriptTimeoutSeconds: prAgent.GetStartupScriptTimeoutSeconds(),
|
||||
ShutdownScript: sql.NullString{
|
||||
String: prAgent.ShutdownScript,
|
||||
Valid: prAgent.ShutdownScript != "",
|
||||
},
|
||||
ShutdownScriptTimeoutSeconds: prAgent.GetShutdownScriptTimeoutSeconds(),
|
||||
DisplayApps: convertDisplayApps(prAgent.GetDisplayApps()),
|
||||
ID: agentID,
|
||||
CreatedAt: dbtime.Now(),
|
||||
UpdatedAt: dbtime.Now(),
|
||||
ResourceID: resource.ID,
|
||||
Name: prAgent.Name,
|
||||
AuthToken: authToken,
|
||||
AuthInstanceID: instanceID,
|
||||
Architecture: prAgent.Architecture,
|
||||
EnvironmentVariables: env,
|
||||
Directory: prAgent.Directory,
|
||||
OperatingSystem: prAgent.OperatingSystem,
|
||||
ConnectionTimeoutSeconds: prAgent.GetConnectionTimeoutSeconds(),
|
||||
TroubleshootingURL: prAgent.GetTroubleshootingUrl(),
|
||||
MOTDFile: prAgent.GetMotdFile(),
|
||||
DisplayApps: convertDisplayApps(prAgent.GetDisplayApps()),
|
||||
})
|
||||
if err != nil {
|
||||
return xerrors.Errorf("insert agent: %w", err)
|
||||
@@ -1421,6 +1405,57 @@ func InsertWorkspaceResource(ctx context.Context, db database.Store, jobID uuid.
|
||||
}
|
||||
}
|
||||
|
||||
logSourceIDs := make([]uuid.UUID, 0, len(prAgent.Scripts))
|
||||
logSourceDisplayNames := make([]string, 0, len(prAgent.Scripts))
|
||||
logSourceIcons := make([]string, 0, len(prAgent.Scripts))
|
||||
scriptLogPaths := make([]string, 0, len(prAgent.Scripts))
|
||||
scriptSources := make([]string, 0, len(prAgent.Scripts))
|
||||
scriptCron := make([]string, 0, len(prAgent.Scripts))
|
||||
scriptTimeout := make([]int32, 0, len(prAgent.Scripts))
|
||||
scriptStartBlocksLogin := make([]bool, 0, len(prAgent.Scripts))
|
||||
scriptRunOnStart := make([]bool, 0, len(prAgent.Scripts))
|
||||
scriptRunOnStop := make([]bool, 0, len(prAgent.Scripts))
|
||||
|
||||
for _, script := range prAgent.Scripts {
|
||||
logSourceIDs = append(logSourceIDs, uuid.New())
|
||||
logSourceDisplayNames = append(logSourceDisplayNames, script.DisplayName)
|
||||
logSourceIcons = append(logSourceIcons, script.Icon)
|
||||
scriptLogPaths = append(scriptLogPaths, script.LogPath)
|
||||
scriptSources = append(scriptSources, script.Script)
|
||||
scriptCron = append(scriptCron, script.Cron)
|
||||
scriptTimeout = append(scriptTimeout, script.TimeoutSeconds)
|
||||
scriptStartBlocksLogin = append(scriptStartBlocksLogin, script.StartBlocksLogin)
|
||||
scriptRunOnStart = append(scriptRunOnStart, script.RunOnStart)
|
||||
scriptRunOnStop = append(scriptRunOnStop, script.RunOnStop)
|
||||
}
|
||||
|
||||
_, err = db.InsertWorkspaceAgentLogSources(ctx, database.InsertWorkspaceAgentLogSourcesParams{
|
||||
WorkspaceAgentID: agentID,
|
||||
ID: logSourceIDs,
|
||||
CreatedAt: dbtime.Now(),
|
||||
DisplayName: logSourceDisplayNames,
|
||||
Icon: logSourceIcons,
|
||||
})
|
||||
if err != nil {
|
||||
return xerrors.Errorf("insert agent log sources: %w", err)
|
||||
}
|
||||
|
||||
_, err = db.InsertWorkspaceAgentScripts(ctx, database.InsertWorkspaceAgentScriptsParams{
|
||||
WorkspaceAgentID: agentID,
|
||||
LogSourceID: logSourceIDs,
|
||||
LogPath: scriptLogPaths,
|
||||
CreatedAt: dbtime.Now(),
|
||||
Script: scriptSources,
|
||||
Cron: scriptCron,
|
||||
TimeoutSeconds: scriptTimeout,
|
||||
StartBlocksLogin: scriptStartBlocksLogin,
|
||||
RunOnStart: scriptRunOnStart,
|
||||
RunOnStop: scriptRunOnStop,
|
||||
})
|
||||
if err != nil {
|
||||
return xerrors.Errorf("insert agent scripts: %w", err)
|
||||
}
|
||||
|
||||
for _, app := range prAgent.Apps {
|
||||
slug := app.Slug
|
||||
if slug == "" {
|
||||
|
||||
@@ -1576,7 +1576,6 @@ func TestInsertWorkspaceResource(t *testing.T) {
|
||||
Env: map[string]string{
|
||||
"something": "test",
|
||||
},
|
||||
StartupScript: "value",
|
||||
OperatingSystem: "linux",
|
||||
Architecture: "amd64",
|
||||
Auth: &sdkproto.Agent_Token{
|
||||
@@ -1585,7 +1584,10 @@ func TestInsertWorkspaceResource(t *testing.T) {
|
||||
Apps: []*sdkproto.App{{
|
||||
Slug: "a",
|
||||
}},
|
||||
ShutdownScript: "shutdown",
|
||||
Scripts: []*sdkproto.Script{{
|
||||
DisplayName: "Startup",
|
||||
Icon: "/test.png",
|
||||
}},
|
||||
DisplayApps: &sdkproto.DisplayApps{
|
||||
Vscode: true,
|
||||
PortForwardingHelper: true,
|
||||
@@ -1604,8 +1606,6 @@ func TestInsertWorkspaceResource(t *testing.T) {
|
||||
agent := agents[0]
|
||||
require.Equal(t, "amd64", agent.Architecture)
|
||||
require.Equal(t, "linux", agent.OperatingSystem)
|
||||
require.Equal(t, "value", agent.StartupScript.String)
|
||||
require.Equal(t, "shutdown", agent.ShutdownScript.String)
|
||||
want, err := json.Marshal(map[string]string{
|
||||
"something": "test",
|
||||
})
|
||||
|
||||
Reference in New Issue
Block a user