Skip to content

Commit 1cc3e9d

Browse files
committed
Push startup script logs from agent
1 parent 66c8ec3 commit 1cc3e9d

File tree

4 files changed

+38
-5
lines changed

4 files changed

+38
-5
lines changed

agent/agent.go

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ import (
2323
"sync"
2424
"time"
2525

26+
"github.com/ammario/prefixsuffix"
2627
"github.com/armon/circbuf"
2728
"github.com/gliderlabs/ssh"
2829
"github.com/google/uuid"
@@ -76,6 +77,7 @@ type Client interface {
7677
PostLifecycle(ctx context.Context, state agentsdk.PostLifecycleRequest) error
7778
PostAppHealth(ctx context.Context, req agentsdk.PostAppHealthsRequest) error
7879
PostStartup(ctx context.Context, req agentsdk.PostStartupRequest) error
80+
InsertOrUpdateStartupLogs(ctx context.Context, req agentsdk.InsertOrUpdateStartupLogsRequest) error
7981
}
8082

8183
func New(options Options) io.Closer {
@@ -617,13 +619,26 @@ func (a *agent) runStartupScript(ctx context.Context, script string) error {
617619
}
618620

619621
a.logger.Info(ctx, "running startup script", slog.F("script", script))
620-
writer, err := a.filesystem.OpenFile(filepath.Join(a.logDir, "coder-startup-script.log"), os.O_CREATE|os.O_RDWR, 0o600)
622+
623+
fileWriter, err := a.filesystem.OpenFile(filepath.Join(a.logDir, "coder-startup-script.log"), os.O_CREATE|os.O_RDWR, 0o600)
621624
if err != nil {
622625
return xerrors.Errorf("open startup script log file: %w", err)
623626
}
624627
defer func() {
625-
_ = writer.Close()
628+
_ = fileWriter.Close()
629+
}()
630+
631+
saver := &prefixsuffix.Saver{N: 512 << 10}
632+
writer := io.MultiWriter(saver, fileWriter)
633+
defer func() {
634+
err := a.client.InsertOrUpdateStartupLogs(ctx, agentsdk.InsertOrUpdateStartupLogsRequest{
635+
Output: string(saver.Bytes()),
636+
})
637+
if err != nil {
638+
a.logger.Error(ctx, "upload startup logs", slog.Error(err))
639+
}
626640
}()
641+
627642
cmd, err := a.createCommand(ctx, script, nil)
628643
if err != nil {
629644
return xerrors.Errorf("create command: %w", err)

agent/agent_test.go

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -667,9 +667,9 @@ func TestAgent_StartupScript(t *testing.T) {
667667
if runtime.GOOS == "windows" {
668668
t.Skip("This test doesn't work on Windows for some reason...")
669669
}
670-
content := "output"
670+
content := "output\n"
671671
//nolint:dogsled
672-
_, _, _, fs := setupAgent(t, agentsdk.Metadata{
672+
_, client, _, fs := setupAgent(t, agentsdk.Metadata{
673673
StartupScript: "echo " + content,
674674
}, 0)
675675
var gotContent string
@@ -694,7 +694,8 @@ func TestAgent_StartupScript(t *testing.T) {
694694
gotContent = string(content)
695695
return true
696696
}, testutil.WaitShort, testutil.IntervalMedium)
697-
require.Equal(t, content, strings.TrimSpace(gotContent))
697+
require.Equal(t, content, gotContent)
698+
require.Equal(t, content, client.getLogs())
698699
}
699700

700701
func TestAgent_Lifecycle(t *testing.T) {
@@ -1229,6 +1230,7 @@ type client struct {
12291230
mu sync.Mutex // Protects following.
12301231
lifecycleStates []codersdk.WorkspaceAgentLifecycle
12311232
startup agentsdk.PostStartupRequest
1233+
logs agentsdk.InsertOrUpdateStartupLogsRequest
12321234
}
12331235

12341236
func (c *client) Metadata(_ context.Context) (agentsdk.Metadata, error) {
@@ -1313,6 +1315,19 @@ func (c *client) PostStartup(_ context.Context, startup agentsdk.PostStartupRequ
13131315
return nil
13141316
}
13151317

1318+
func (c *client) getLogs() string {
1319+
c.mu.Lock()
1320+
defer c.mu.Unlock()
1321+
return c.logs.Output
1322+
}
1323+
1324+
func (c *client) InsertOrUpdateStartupLogs(_ context.Context, logs agentsdk.InsertOrUpdateStartupLogsRequest) error {
1325+
c.mu.Lock()
1326+
defer c.mu.Unlock()
1327+
c.logs = logs
1328+
return nil
1329+
}
1330+
13161331
// tempDirUnixSocket returns a temporary directory that can safely hold unix
13171332
// sockets (probably).
13181333
//

go.mod

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ require (
5959
cloud.google.com/go/compute/metadata v0.2.1
6060
github.com/AlecAivazis/survey/v2 v2.3.5
6161
github.com/adrg/xdg v0.4.0
62+
github.com/ammario/prefixsuffix v0.0.0-20200405191514-5a0456bf2cfd
6263
github.com/andybalholm/brotli v1.0.4
6364
github.com/armon/circbuf v0.0.0-20190214190532-5111143e8da2
6465
github.com/awalterschulze/gographviz v2.0.3+incompatible

go.sum

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -190,6 +190,8 @@ github.com/alexbrainman/sspi v0.0.0-20210105120005-909beea2cc74/go.mod h1:cEWa1L
190190
github.com/alexflint/go-filemutex v0.0.0-20171022225611-72bdc8eae2ae/go.mod h1:CgnQgUtFrFz9mxFNtED3jI5tLDjKlOM+oUF/sTk6ps0=
191191
github.com/alexflint/go-filemutex v1.1.0/go.mod h1:7P4iRhttt/nUvUOrYIhcpMzv2G6CY9UnI16Z+UJqRyk=
192192
github.com/alexkohler/prealloc v1.0.0/go.mod h1:VetnK3dIgFBBKmg0YnD9F9x6Icjd+9cvfHR56wJVlKE=
193+
github.com/ammario/prefixsuffix v0.0.0-20200405191514-5a0456bf2cfd h1:WOzjyD34+0vVw3wzE7js8Yvzo08ljzvK1jG6wL8elVU=
194+
github.com/ammario/prefixsuffix v0.0.0-20200405191514-5a0456bf2cfd/go.mod h1:VM1c/0Tl3O26UkHMbU32VFqLwLvi2FA40b6s5vPOpoo=
193195
github.com/andreyvit/diff v0.0.0-20170406064948-c7f18ee00883 h1:bvNMNQO63//z+xNgfBlViaCIJKLlCJ6/fmUseuG0wVQ=
194196
github.com/andybalholm/brotli v1.0.2/go.mod h1:loMXtMfwqflxFJPmdbJO0a3KNoPuLBgiu3qAvBg8x/Y=
195197
github.com/andybalholm/brotli v1.0.3/go.mod h1:fO7iG3H7G2nSZ7m0zPUDn85XEX2GTukHGRSepvi9Eig=

0 commit comments

Comments
 (0)