Skip to content

Commit 58964c9

Browse files
committed
Finish the piping
1 parent d5df133 commit 58964c9

31 files changed

+1660
-1142
lines changed

agent/agent.go

Lines changed: 0 additions & 73 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ import (
1212
"net/http"
1313
"net/netip"
1414
"os"
15-
"os/exec"
1615
"os/user"
1716
"path/filepath"
1817
"sort"
@@ -975,78 +974,6 @@ func (a *agent) runDERPMapSubscriber(ctx context.Context, network *tailnet.Conn)
975974
}
976975
}
977976

978-
func (a *agent) runScript(ctx context.Context, script codersdk.WorkspaceAgentScript) (err error) {
979-
if script.Source == "" {
980-
return nil
981-
}
982-
983-
logger := a.logger.With(slog.F("log_source", script.LogSourceDisplayName))
984-
985-
logger.Info(ctx, "running script", slog.F("script", script.Source))
986-
fileWriter, err := a.filesystem.OpenFile(filepath.Join(a.logDir, fmt.Sprintf("coder-%s-script.log", script.LogSourceDisplayName)), os.O_CREATE|os.O_RDWR, 0o600)
987-
if err != nil {
988-
return xerrors.Errorf("open %s script log file: %w", script.LogSourceDisplayName, err)
989-
}
990-
defer func() {
991-
err := fileWriter.Close()
992-
if err != nil {
993-
logger.Warn(ctx, fmt.Sprintf("close %s script log file", script.LogSourceDisplayName), slog.Error(err))
994-
}
995-
}()
996-
997-
cmdPty, err := a.sshServer.CreateCommand(ctx, script.Source, nil)
998-
if err != nil {
999-
return xerrors.Errorf("%s script: create command: %w", script.LogSourceDisplayName, err)
1000-
}
1001-
cmd := cmdPty.AsExec()
1002-
1003-
send, flushAndClose := agentsdk.LogsSender(script.LogSourceID, a.client.PatchLogs, logger)
1004-
// If ctx is canceled here (or in a writer below), we may be
1005-
// discarding logs, but that's okay because we're shutting down
1006-
// anyway. We could consider creating a new context here if we
1007-
// want better control over flush during shutdown.
1008-
defer func() {
1009-
if err := flushAndClose(ctx); err != nil {
1010-
logger.Warn(ctx, "flush startup logs failed", slog.Error(err))
1011-
}
1012-
}()
1013-
1014-
infoW := agentsdk.StartupLogsWriter(ctx, send, script.LogSourceID, codersdk.LogLevelInfo)
1015-
defer infoW.Close()
1016-
errW := agentsdk.StartupLogsWriter(ctx, send, script.LogSourceID, codersdk.LogLevelError)
1017-
defer errW.Close()
1018-
cmd.Stdout = io.MultiWriter(fileWriter, infoW)
1019-
cmd.Stderr = io.MultiWriter(fileWriter, errW)
1020-
1021-
start := time.Now()
1022-
defer func() {
1023-
end := time.Now()
1024-
execTime := end.Sub(start)
1025-
exitCode := 0
1026-
if err != nil {
1027-
exitCode = 255 // Unknown status.
1028-
var exitError *exec.ExitError
1029-
if xerrors.As(err, &exitError) {
1030-
exitCode = exitError.ExitCode()
1031-
}
1032-
logger.Warn(ctx, fmt.Sprintf("%s script failed", script.LogSourceDisplayName), slog.F("execution_time", execTime), slog.F("exit_code", exitCode), slog.Error(err))
1033-
} else {
1034-
logger.Info(ctx, fmt.Sprintf("%s script completed", script.LogSourceDisplayName), slog.F("execution_time", execTime), slog.F("exit_code", exitCode))
1035-
}
1036-
}()
1037-
1038-
err = cmd.Run()
1039-
if err != nil {
1040-
// cmd.Run does not return a context canceled error, it returns "signal: killed".
1041-
if ctx.Err() != nil {
1042-
return ctx.Err()
1043-
}
1044-
1045-
return xerrors.Errorf("%s script: run: %w", script.LogSourceDisplayName, err)
1046-
}
1047-
return nil
1048-
}
1049-
1050977
func (a *agent) handleReconnectingPTY(ctx context.Context, logger slog.Logger, msg codersdk.WorkspaceAgentReconnectingPTYInit, conn net.Conn) (retErr error) {
1051978
defer conn.Close()
1052979
a.metrics.connectionsTotal.Add(1)

agent/agent_test.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1416,13 +1416,13 @@ func TestAgent_Lifecycle(t *testing.T) {
14161416
agentsdk.Manifest{
14171417
DERPMap: derpMap,
14181418
Scripts: []codersdk.WorkspaceAgentScript{{
1419-
LogSourceDisplayName: "startup",
1420-
Source: "echo 1",
1421-
RunOnStart: true,
1419+
LogPath: "coder-startup-script.log",
1420+
Source: "echo 1",
1421+
RunOnStart: true,
14221422
}, {
1423-
LogSourceDisplayName: "shutdown",
1424-
Source: "echo " + expected,
1425-
RunOnStop: true,
1423+
LogPath: "coder-shutdown-script.log",
1424+
Source: "echo " + expected,
1425+
RunOnStop: true,
14261426
}},
14271427
},
14281428
make(chan *agentsdk.Stats, 50),

agent/agentscripts/agentscripts.go

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ func (r *Runner) Execute(filter func(script codersdk.WorkspaceAgentScript) bool)
9999
eg.Go(func() error {
100100
err := r.run(script)
101101
if err != nil {
102-
return xerrors.Errorf("run agent script %q: %w", script.LogSourceDisplayName, err)
102+
return xerrors.Errorf("run agent script %q: %w", script.LogPath, err)
103103
}
104104
return nil
105105
})
@@ -112,17 +112,25 @@ func (r *Runner) Execute(filter func(script codersdk.WorkspaceAgentScript) bool)
112112
// If the process does not exit after a few seconds, it is forcefully killed.
113113
// This function immediately returns after a timeout, and does not wait for the process to exit.
114114
func (r *Runner) run(script codersdk.WorkspaceAgentScript) error {
115-
logger := r.Logger.With(slog.F("log_source", script.LogSourceDisplayName))
115+
logger := r.Logger.With(slog.F("log_source", script.LogPath))
116116
ctx := r.ctx
117117
logger.Info(ctx, "running agent script", slog.F("script", script.Source))
118-
fileWriter, err := r.Filesystem.OpenFile(filepath.Join(r.LogDir, fmt.Sprintf("coder-%s-script.log", script.LogSourceDisplayName)), os.O_CREATE|os.O_RDWR, 0o600)
118+
119+
logPath := script.LogPath
120+
if logPath == "" {
121+
logPath = fmt.Sprintf("coder-%s-script.log", script.LogSourceID)
122+
}
123+
if !filepath.IsAbs(logPath) {
124+
logPath = filepath.Join(r.LogDir, logPath)
125+
}
126+
fileWriter, err := r.Filesystem.OpenFile(logPath, os.O_CREATE|os.O_RDWR, 0o600)
119127
if err != nil {
120-
return xerrors.Errorf("open %s script log file: %w", script.LogSourceDisplayName, err)
128+
return xerrors.Errorf("open %s script log file: %w", logPath, err)
121129
}
122130
defer func() {
123131
err := fileWriter.Close()
124132
if err != nil {
125-
logger.Warn(ctx, fmt.Sprintf("close %s script log file", script.LogSourceDisplayName), slog.Error(err))
133+
logger.Warn(ctx, fmt.Sprintf("close %s script log file", logPath), slog.Error(err))
126134
}
127135
}()
128136

@@ -136,7 +144,7 @@ func (r *Runner) run(script codersdk.WorkspaceAgentScript) error {
136144

137145
cmdPty, err := r.SSHServer.CreateCommand(ctx, script.Source, nil)
138146
if err != nil {
139-
return xerrors.Errorf("%s script: create command: %w", script.LogSourceDisplayName, err)
147+
return xerrors.Errorf("%s script: create command: %w", logPath, err)
140148
}
141149
cmd = cmdPty.AsExec()
142150

@@ -169,15 +177,15 @@ func (r *Runner) run(script codersdk.WorkspaceAgentScript) error {
169177
if xerrors.As(err, &exitError) {
170178
exitCode = exitError.ExitCode()
171179
}
172-
logger.Warn(ctx, fmt.Sprintf("%s script failed", script.LogSourceDisplayName), slog.F("execution_time", execTime), slog.F("exit_code", exitCode), slog.Error(err))
180+
logger.Warn(ctx, fmt.Sprintf("%s script failed", logPath), slog.F("execution_time", execTime), slog.F("exit_code", exitCode), slog.Error(err))
173181
} else {
174-
logger.Info(ctx, fmt.Sprintf("%s script completed", script.LogSourceDisplayName), slog.F("execution_time", execTime), slog.F("exit_code", exitCode))
182+
logger.Info(ctx, fmt.Sprintf("%s script completed", logPath), slog.F("execution_time", execTime), slog.F("exit_code", exitCode))
175183
}
176184
}()
177185

178186
err = cmd.Start()
179187
if err != nil {
180-
return xerrors.Errorf("%s script: start command: %w", script.LogSourceDisplayName, err)
188+
return xerrors.Errorf("%s script: start command: %w", logPath, err)
181189
}
182190

183191
// timeout stores whether the process timed out then was gracefully killed.
@@ -199,7 +207,7 @@ func (r *Runner) run(script codersdk.WorkspaceAgentScript) error {
199207
cmdDone <- cmd.Wait()
200208
})
201209
if err != nil {
202-
return xerrors.Errorf("%s script: track command goroutine: %w", script.LogSourceDisplayName, err)
210+
return xerrors.Errorf("%s script: track command goroutine: %w", logPath, err)
203211
}
204212
select {
205213
case <-timeout:

agent/agentscripts/agentscripts_test.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,7 @@ func TestExecuteBasic(t *testing.T) {
3131
})
3232
defer runner.Close()
3333
err := runner.Init([]codersdk.WorkspaceAgentScript{{
34-
LogSourceDisplayName: "test",
35-
Source: "echo hello",
34+
Source: "echo hello",
3635
}})
3736
require.NoError(t, err)
3837
require.NoError(t, runner.Execute(func(script codersdk.WorkspaceAgentScript) bool {

coderd/apidoc/docs.go

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

0 commit comments

Comments
 (0)