Skip to content

Commit 9a0f335

Browse files
committed
feat!: use agentsdk.LogSender instead of deprecated method, remove manually vendored codersdk
1 parent 6cb9954 commit 9a0f335

22 files changed

+862
-891
lines changed

.github/workflows/ci.yaml

+3
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,9 @@ jobs:
3939
with:
4040
go-version: "~1.22"
4141

42+
- name: Download Go modules
43+
run: go mod download
44+
4245
- name: Lint
4346
run: make -j lint
4447

Makefile

+1-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ lint: lint/go lint/shellcheck
1616
.PHONY: lint/go
1717
lint/go: $(GO_SRC_FILES)
1818
go install github.com/golangci/golangci-lint/cmd/golangci-lint@$(GOLANGCI_LINT_VERSION)
19-
golangci-lint run
19+
golangci-lint run --timeout=10m
2020

2121
.PHONY: lint/shellcheck
2222
lint/shellcheck: $(SHELL_SRC_FILES)

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -388,4 +388,5 @@ On MacOS or Windows systems, we recommend either using a VM or the provided `.de
388388
| `--coder-agent-subsystem` | `CODER_AGENT_SUBSYSTEM` | | Coder agent subsystems to report when forwarding logs. The envbuilder subsystem is always included. |
389389
| `--push-image` | `ENVBUILDER_PUSH_IMAGE` | | Push the built image to a remote registry. This option forces a reproducible build. |
390390
| `--get-cached-image` | `ENVBUILDER_GET_CACHED_IMAGE` | | Print the digest of the cached image, if available. Exits with an error if not found. |
391+
| `--verbose` | `ENVBUILDER_VERBOSE` | | Enable verbose logging. |
391392
<!--- END docsgen --->

cmd/envbuilder/main.go

+20-46
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,16 @@
11
package main
22

33
import (
4-
"context"
5-
"crypto/tls"
64
"errors"
75
"fmt"
8-
"net/http"
96
"net/url"
107
"os"
118
"slices"
129
"strings"
13-
"time"
1410

15-
"cdr.dev/slog"
11+
"github.com/coder/coder/v2/codersdk"
1612
"github.com/coder/envbuilder"
17-
"github.com/coder/envbuilder/internal/notcodersdk"
13+
"github.com/coder/envbuilder/internal/eblog"
1814
"github.com/coder/serpent"
1915

2016
// *Never* remove this. Certificates are not bundled as part
@@ -38,58 +34,36 @@ func envbuilderCmd() serpent.Command {
3834
Use: "envbuilder",
3935
Options: options.CLI(),
4036
Handler: func(inv *serpent.Invocation) error {
41-
var sendLogs func(ctx context.Context, log ...notcodersdk.Log) error
42-
if options.CoderAgentToken != "" {
43-
if options.CoderAgentURL == "" {
37+
options.Logger = eblog.New(os.Stderr, options.Verbose)
38+
if options.CoderAgentURL != "" {
39+
if options.CoderAgentToken == "" {
4440
return errors.New("CODER_AGENT_URL must be set if CODER_AGENT_TOKEN is set")
4541
}
4642
u, err := url.Parse(options.CoderAgentURL)
4743
if err != nil {
4844
return fmt.Errorf("unable to parse CODER_AGENT_URL as URL: %w", err)
4945
}
50-
client := notcodersdk.New(u)
51-
client.SetSessionToken(options.CoderAgentToken)
52-
client.HTTPClient = &http.Client{
53-
Transport: &http.Transport{
54-
TLSClientConfig: &tls.Config{
55-
InsecureSkipVerify: options.Insecure,
56-
},
57-
},
58-
}
59-
var flushAndClose func(ctx context.Context) error
60-
// nolint: staticcheck // FIXME: https://github.com/coder/envbuilder/issues/260
61-
sendLogs, flushAndClose = notcodersdk.LogsSender(notcodersdk.ExternalLogSourceID, client.PatchLogs, slog.Logger{})
62-
defer func() {
63-
_ = flushAndClose(inv.Context())
64-
}()
65-
66-
// This adds the envbuilder subsystem.
67-
// If telemetry is enabled in a Coder deployment,
68-
// this will be reported and help us understand
69-
// envbuilder usage.
70-
if !slices.Contains(options.CoderAgentSubsystem, string(notcodersdk.AgentSubsystemEnvbuilder)) {
71-
options.CoderAgentSubsystem = append(options.CoderAgentSubsystem, string(notcodersdk.AgentSubsystemEnvbuilder))
72-
_ = os.Setenv("CODER_AGENT_SUBSYSTEM", strings.Join(options.CoderAgentSubsystem, ","))
73-
}
74-
}
75-
76-
options.Logger = func(level notcodersdk.LogLevel, format string, args ...interface{}) {
77-
output := fmt.Sprintf(format, args...)
78-
_, _ = fmt.Fprintln(inv.Stderr, output)
79-
if sendLogs != nil {
80-
if err := sendLogs(inv.Context(), notcodersdk.Log{
81-
CreatedAt: time.Now(),
82-
Output: output,
83-
Level: level,
84-
}); err != nil {
85-
_, _ = fmt.Fprintf(inv.Stderr, "failed to send logs: %s\n", err.Error())
46+
coderLog, closeLogs, err := eblog.Coder(inv.Context(), u, options.CoderAgentToken)
47+
if err == nil {
48+
options.Logger = eblog.Wrap(options.Logger, coderLog)
49+
defer closeLogs()
50+
// This adds the envbuilder subsystem.
51+
// If telemetry is enabled in a Coder deployment,
52+
// this will be reported and help us understand
53+
// envbuilder usage.
54+
if !slices.Contains(options.CoderAgentSubsystem, string(codersdk.AgentSubsystemEnvbuilder)) {
55+
options.CoderAgentSubsystem = append(options.CoderAgentSubsystem, string(codersdk.AgentSubsystemEnvbuilder))
56+
_ = os.Setenv("CODER_AGENT_SUBSYSTEM", strings.Join(options.CoderAgentSubsystem, ","))
8657
}
58+
} else {
59+
// Failure to log to Coder should cause a fatal error.
60+
options.Logger(eblog.LevelError, "unable to send logs to Coder: %s", err.Error())
8761
}
8862
}
8963

9064
err := envbuilder.Run(inv.Context(), options)
9165
if err != nil {
92-
options.Logger(notcodersdk.LogLevelError, "error: %s", err)
66+
options.Logger(eblog.LevelError, "error: %s", err)
9367
}
9468
return err
9569
},

cmd/envbuilder/main_test.go

-84
This file was deleted.

0 commit comments

Comments
 (0)