mirror of
https://github.com/coder/coder.git
synced 2026-09-24 15:04:27 +08:00
feat(agent): add script data dir for binaries and files (#12205)
The agent is extended with a `--script-data-dir` flag, defaulting to the OS temp dir. This dir is used for storing `coder-script-data/bin` and `coder-script/[script uuid]`. The former is a place for all scripts to place executable binaries that will be available by other scripts, SSH sessions, etc. The latter is a place for the script to store files. Since we default to OS temp dir, files are ephemeral by default. In the future, we may consider adding new env vars or changing the default storage location. Workspace startup speed could potentially benefit from scripts being able to skip steps that require downloading software. We may also extend this with more env variables (e.g. persistent storage in HOME). Fixes #11131
This commit is contained in:
+26
-5
@@ -66,6 +66,7 @@ type Options struct {
|
||||
Filesystem afero.Fs
|
||||
LogDir string
|
||||
TempDir string
|
||||
ScriptDataDir string
|
||||
ExchangeToken func(ctx context.Context) (string, error)
|
||||
Client Client
|
||||
ReconnectingPTYTimeout time.Duration
|
||||
@@ -112,9 +113,19 @@ func New(options Options) Agent {
|
||||
if options.LogDir == "" {
|
||||
if options.TempDir != os.TempDir() {
|
||||
options.Logger.Debug(context.Background(), "log dir not set, using temp dir", slog.F("temp_dir", options.TempDir))
|
||||
} else {
|
||||
options.Logger.Debug(context.Background(), "using log dir", slog.F("log_dir", options.LogDir))
|
||||
}
|
||||
options.LogDir = options.TempDir
|
||||
}
|
||||
if options.ScriptDataDir == "" {
|
||||
if options.TempDir != os.TempDir() {
|
||||
options.Logger.Debug(context.Background(), "script data dir not set, using temp dir", slog.F("temp_dir", options.TempDir))
|
||||
} else {
|
||||
options.Logger.Debug(context.Background(), "using script data dir", slog.F("script_data_dir", options.ScriptDataDir))
|
||||
}
|
||||
options.ScriptDataDir = options.TempDir
|
||||
}
|
||||
if options.ExchangeToken == nil {
|
||||
options.ExchangeToken = func(ctx context.Context) (string, error) {
|
||||
return "", nil
|
||||
@@ -152,6 +163,7 @@ func New(options Options) Agent {
|
||||
filesystem: options.Filesystem,
|
||||
logDir: options.LogDir,
|
||||
tempDir: options.TempDir,
|
||||
scriptDataDir: options.ScriptDataDir,
|
||||
lifecycleUpdate: make(chan struct{}, 1),
|
||||
lifecycleReported: make(chan codersdk.WorkspaceAgentLifecycle, 1),
|
||||
lifecycleStates: []agentsdk.PostLifecycleRequest{{State: codersdk.WorkspaceAgentLifecycleCreated}},
|
||||
@@ -183,6 +195,7 @@ type agent struct {
|
||||
filesystem afero.Fs
|
||||
logDir string
|
||||
tempDir string
|
||||
scriptDataDir string
|
||||
// ignorePorts tells the api handler which ports to ignore when
|
||||
// listing all listening ports. This is helpful to hide ports that
|
||||
// are used by the agent, that the user does not care about.
|
||||
@@ -249,11 +262,12 @@ func (a *agent) init(ctx context.Context) {
|
||||
}
|
||||
a.sshServer = sshSrv
|
||||
a.scriptRunner = agentscripts.New(agentscripts.Options{
|
||||
LogDir: a.logDir,
|
||||
Logger: a.logger,
|
||||
SSHServer: sshSrv,
|
||||
Filesystem: a.filesystem,
|
||||
PatchLogs: a.client.PatchLogs,
|
||||
LogDir: a.logDir,
|
||||
DataDirBase: a.scriptDataDir,
|
||||
Logger: a.logger,
|
||||
SSHServer: sshSrv,
|
||||
Filesystem: a.filesystem,
|
||||
PatchLogs: a.client.PatchLogs,
|
||||
})
|
||||
// Register runner metrics. If the prom registry is nil, the metrics
|
||||
// will not report anywhere.
|
||||
@@ -954,6 +968,13 @@ func (a *agent) updateCommandEnv(current []string) (updated []string, err error)
|
||||
envs[k] = v
|
||||
}
|
||||
|
||||
// Prepend the agent script bin directory to the PATH
|
||||
// (this is where Coder modules place their binaries).
|
||||
if _, ok := envs["PATH"]; !ok {
|
||||
envs["PATH"] = os.Getenv("PATH")
|
||||
}
|
||||
envs["PATH"] = fmt.Sprintf("%s%c%s", a.scriptRunner.ScriptBinDir(), filepath.ListSeparator, envs["PATH"])
|
||||
|
||||
for k, v := range envs {
|
||||
updated = append(updated, fmt.Sprintf("%s=%s", k, v))
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user