mirror of
https://github.com/coder/coder.git
synced 2026-06-03 04:58:23 +00:00
25fb34cabe
This change implements an interface for running `@devcontainers/cli up` and an API endpoint on the agent for triggering recreate for a running devcontainer. A couple of limitations: 1. Synchronous HTTP request, meaning the browser might choose to time it out before it's done => no result/error (and devcontainer cli command probably gets killed via ctx cancel). 2. Logs are only written to agent logs via slog, not as a "script" in the UI. Both 1 and 2 will be improved in future refactors. Fixes coder/internal#481 Fixes coder/internal#482
110 lines
3.6 KiB
Go
110 lines
3.6 KiB
Go
package agentcontainers
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"os"
|
|
"path/filepath"
|
|
"strings"
|
|
|
|
"cdr.dev/slog"
|
|
|
|
"github.com/coder/coder/v2/codersdk"
|
|
)
|
|
|
|
const (
|
|
// DevcontainerLocalFolderLabel is the label that contains the path to
|
|
// the local workspace folder for a devcontainer.
|
|
DevcontainerLocalFolderLabel = "devcontainer.local_folder"
|
|
// DevcontainerConfigFileLabel is the label that contains the path to
|
|
// the devcontainer.json configuration file.
|
|
DevcontainerConfigFileLabel = "devcontainer.config_file"
|
|
)
|
|
|
|
const devcontainerUpScriptTemplate = `
|
|
if ! which devcontainer > /dev/null 2>&1; then
|
|
echo "ERROR: Unable to start devcontainer, @devcontainers/cli is not installed."
|
|
exit 1
|
|
fi
|
|
devcontainer up %s
|
|
`
|
|
|
|
// ExtractAndInitializeDevcontainerScripts extracts devcontainer scripts from
|
|
// the given scripts and devcontainers. The devcontainer scripts are removed
|
|
// from the returned scripts so that they can be run separately.
|
|
//
|
|
// Dev Containers have an inherent dependency on start scripts, since they
|
|
// initialize the workspace (e.g. git clone, npm install, etc). This is
|
|
// important if e.g. a Coder module to install @devcontainer/cli is used.
|
|
func ExtractAndInitializeDevcontainerScripts(
|
|
logger slog.Logger,
|
|
expandPath func(string) (string, error),
|
|
devcontainers []codersdk.WorkspaceAgentDevcontainer,
|
|
scripts []codersdk.WorkspaceAgentScript,
|
|
) (filteredScripts []codersdk.WorkspaceAgentScript, devcontainerScripts []codersdk.WorkspaceAgentScript) {
|
|
ScriptLoop:
|
|
for _, script := range scripts {
|
|
for _, dc := range devcontainers {
|
|
// The devcontainer scripts match the devcontainer ID for
|
|
// identification.
|
|
if script.ID == dc.ID {
|
|
dc = expandDevcontainerPaths(logger, expandPath, dc)
|
|
devcontainerScripts = append(devcontainerScripts, devcontainerStartupScript(dc, script))
|
|
continue ScriptLoop
|
|
}
|
|
}
|
|
|
|
filteredScripts = append(filteredScripts, script)
|
|
}
|
|
|
|
return filteredScripts, devcontainerScripts
|
|
}
|
|
|
|
func devcontainerStartupScript(dc codersdk.WorkspaceAgentDevcontainer, script codersdk.WorkspaceAgentScript) codersdk.WorkspaceAgentScript {
|
|
args := []string{
|
|
"--log-format json",
|
|
fmt.Sprintf("--workspace-folder %q", dc.WorkspaceFolder),
|
|
}
|
|
if dc.ConfigPath != "" {
|
|
args = append(args, fmt.Sprintf("--config %q", dc.ConfigPath))
|
|
}
|
|
cmd := fmt.Sprintf(devcontainerUpScriptTemplate, strings.Join(args, " "))
|
|
script.Script = cmd
|
|
// Disable RunOnStart, scripts have this set so that when devcontainers
|
|
// have not been enabled, a warning will be surfaced in the agent logs.
|
|
script.RunOnStart = false
|
|
return script
|
|
}
|
|
|
|
func expandDevcontainerPaths(logger slog.Logger, expandPath func(string) (string, error), dc codersdk.WorkspaceAgentDevcontainer) codersdk.WorkspaceAgentDevcontainer {
|
|
logger = logger.With(slog.F("devcontainer", dc.Name), slog.F("workspace_folder", dc.WorkspaceFolder), slog.F("config_path", dc.ConfigPath))
|
|
|
|
if wf, err := expandPath(dc.WorkspaceFolder); err != nil {
|
|
logger.Warn(context.Background(), "expand devcontainer workspace folder failed", slog.Error(err))
|
|
} else {
|
|
dc.WorkspaceFolder = wf
|
|
}
|
|
if dc.ConfigPath != "" {
|
|
// Let expandPath handle home directory, otherwise assume relative to
|
|
// workspace folder or absolute.
|
|
if dc.ConfigPath[0] == '~' {
|
|
if cp, err := expandPath(dc.ConfigPath); err != nil {
|
|
logger.Warn(context.Background(), "expand devcontainer config path failed", slog.Error(err))
|
|
} else {
|
|
dc.ConfigPath = cp
|
|
}
|
|
} else {
|
|
dc.ConfigPath = relativePathToAbs(dc.WorkspaceFolder, dc.ConfigPath)
|
|
}
|
|
}
|
|
return dc
|
|
}
|
|
|
|
func relativePathToAbs(workdir, path string) string {
|
|
path = os.ExpandEnv(path)
|
|
if !filepath.IsAbs(path) {
|
|
path = filepath.Join(workdir, path)
|
|
}
|
|
return path
|
|
}
|