Skip to content

Commit c0738f3

Browse files
authored
chore: move CODER_ environment variables to CLI options (#167)
1 parent 0423cef commit c0738f3

File tree

4 files changed

+54
-16
lines changed

4 files changed

+54
-16
lines changed

README.md

+3
Original file line numberDiff line numberDiff line change
@@ -293,4 +293,7 @@ On MacOS or Windows systems, we recommend either using a VM or the provided `.de
293293
| `SSL_CERT_BASE64` | | The content of an SSL cert file. This is useful for self-signed certificates. |
294294
| `EXPORT_ENV_FILE` | | Optional file path to a .env file where envbuilder will dump environment variables from devcontainer.json and the built container image. |
295295
| `POST_START_SCRIPT_PATH` | | The path to a script that will be created by envbuilder based on the postStartCommand in devcontainer.json, if any is specified (otherwise the script is not created). If this is set, the specified InitCommand should check for the presence of this script and execute it after successful startup. |
296+
| `CODER_AGENT_URL` | | URL of the Coder deployment. If CODER_AGENT_TOKEN is also set, logs from envbuilder will be forwarded here and will be visible in the workspace build logs. |
297+
| `CODER_AGENT_TOKEN` | | Authentication token for a Coder agent. If this is set, then CODER_AGENT_URL must also be set. |
298+
| `CODER_AGENT_SUBSYSTEM` | | Coder agent subsystems to report when forwarding logs. The envbuilder subsystem is always included. |
296299
<!--- END docsgen --->

cmd/envbuilder/main.go

+9-16
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,9 @@ import (
66
"errors"
77
"fmt"
88
"net/http"
9-
"net/url"
109
"os"
10+
"slices"
11+
"strings"
1112
"time"
1213

1314
"cdr.dev/slog"
@@ -29,18 +30,12 @@ func main() {
2930
Options: options.CLI(),
3031
Handler: func(inv *serpent.Invocation) error {
3132
var sendLogs func(ctx context.Context, log ...agentsdk.Log) error
32-
agentURL := os.Getenv("CODER_AGENT_URL")
33-
agentToken := os.Getenv("CODER_AGENT_TOKEN")
34-
if agentToken != "" {
35-
if agentURL == "" {
33+
if options.CoderAgentToken != "" {
34+
if options.CoderAgentURL == nil {
3635
return errors.New("CODER_AGENT_URL must be set if CODER_AGENT_TOKEN is set")
3736
}
38-
parsed, err := url.Parse(agentURL)
39-
if err != nil {
40-
return err
41-
}
42-
client := agentsdk.New(parsed)
43-
client.SetSessionToken(agentToken)
37+
client := agentsdk.New(options.CoderAgentURL)
38+
client.SetSessionToken(options.CoderAgentToken)
4439
client.SDK.HTTPClient = &http.Client{
4540
Transport: &http.Transport{
4641
TLSClientConfig: &tls.Config{
@@ -56,12 +51,10 @@ func main() {
5651
// If telemetry is enabled in a Coder deployment,
5752
// this will be reported and help us understand
5853
// envbuilder usage.
59-
subsystems := os.Getenv("CODER_AGENT_SUBSYSTEM")
60-
if subsystems != "" {
61-
subsystems += ","
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, ","))
6257
}
63-
subsystems += string(codersdk.AgentSubsystemEnvbuilder)
64-
os.Setenv("CODER_AGENT_SUBSYSTEM", subsystems)
6558
}
6659

6760
options.Logger = func(level codersdk.LogLevel, format string, args ...interface{}) {

options.go

+29
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
package envbuilder
22

33
import (
4+
"net/url"
5+
46
"github.com/coder/coder/v2/codersdk"
57
"github.com/coder/serpent"
68
"github.com/go-git/go-billy/v5"
@@ -44,6 +46,11 @@ type Options struct {
4446
// Filesystem is the filesystem to use for all operations.
4547
// Defaults to the host filesystem.
4648
Filesystem billy.Filesystem
49+
// These options are specifically used when envbuilder
50+
// is invoked as part of a Coder workspace.
51+
CoderAgentURL *url.URL
52+
CoderAgentToken string
53+
CoderAgentSubsystem []string
4754
}
4855

4956
// Generate CLI options for the envbuilder command.
@@ -272,6 +279,28 @@ func (o *Options) CLI() serpent.OptionSet {
272279
"is set, the specified InitCommand should check for the presence of " +
273280
"this script and execute it after successful startup.",
274281
},
282+
{
283+
Flag: "coder-agent-url",
284+
Env: "CODER_AGENT_URL",
285+
Value: serpent.URLOf(o.CoderAgentURL),
286+
Description: "URL of the Coder deployment. If CODER_AGENT_TOKEN is also " +
287+
"set, logs from envbuilder will be forwarded here and will be " +
288+
"visible in the workspace build logs.",
289+
},
290+
{
291+
Flag: "coder-agent-token",
292+
Env: "CODER_AGENT_TOKEN",
293+
Value: serpent.StringOf(&o.CoderAgentToken),
294+
Description: "Authentication token for a Coder agent. If this is set, " +
295+
"then CODER_AGENT_URL must also be set.",
296+
},
297+
{
298+
Flag: "coder-agent-subsystem",
299+
Env: "CODER_AGENT_SUBSYSTEM",
300+
Value: serpent.StringArrayOf(&o.CoderAgentSubsystem),
301+
Description: "Coder agent subsystems to report when forwarding logs. " +
302+
"The envbuilder subsystem is always included.",
303+
},
275304
}
276305
}
277306

testdata/options.golden

+13
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,19 @@ OPTIONS:
2020
The number of days to use cached layers before expiring them. Defaults
2121
to 7 days.
2222

23+
--coder-agent-subsystem string-array, $CODER_AGENT_SUBSYSTEM
24+
Coder agent subsystems to report when forwarding logs. The envbuilder
25+
subsystem is always included.
26+
27+
--coder-agent-token string, $CODER_AGENT_TOKEN
28+
Authentication token for a Coder agent. If this is set, then
29+
CODER_AGENT_URL must also be set.
30+
31+
--coder-agent-url url, $CODER_AGENT_URL
32+
URL of the Coder deployment. If CODER_AGENT_TOKEN is also set, logs
33+
from envbuilder will be forwarded here and will be visible in the
34+
workspace build logs.
35+
2336
--devcontainer-dir string, $DEVCONTAINER_DIR
2437
The path to the folder containing the devcontainer.json file that will
2538
be used to build the workspace and can either be an absolute path or a

0 commit comments

Comments
 (0)