- Notifications
You must be signed in to change notification settings - Fork909
feat(agent): add devcontainer autostart support#17076
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to ourterms of service andprivacy statement. We’ll occasionally send you account related emails.
Already on GitHub?Sign in to your account
Merged
Uh oh!
There was an error while loading.Please reload this page.
Merged
Changes fromall commits
Commits
Show all changes
15 commits Select commitHold shift + click to select a range
63a2ec1
wip: feat(agent): add devcontainer autostart support
mafredrid7480cb
docs
mafredri9bfdfaf
expand
mafredri304b1a1
devcontainer scripts and timings
mafredrie1048b1
use name, print warning
mafredrid85e9a2
refactor
mafredri7954f8d
tmpl
mafredri234fc25
test
mafredri2d93ee3
add integration test
mafredri9aca1c8
unfoo
mafredri9c1bf01
add agentscripts test for execute option
mafredrief089be
safe assign
mafredri55c5c46
improve logger, fix config path realtive to workspace folder
mafredri4676898
Update agent/agentcontainers/devcontainer.go
mafredrif797b41
skip path separator tests on windows
mafredriFile filter
Filter by extension
Conversations
Failed to load comments.
Loading
Uh oh!
There was an error while loading.Please reload this page.
Jump to
Jump to file
Failed to load files.
Loading
Uh oh!
There was an error while loading.Please reload this page.
Diff view
Diff view
There are no files selected for viewing
50 changes: 34 additions & 16 deletionsagent/agent.go
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
128 changes: 128 additions & 0 deletionsagent/agent_test.go
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1937,6 +1937,134 @@ func TestAgent_ReconnectingPTYContainer(t *testing.T) { | ||
require.ErrorIs(t, tr.ReadUntil(ctx, nil), io.EOF) | ||
} | ||
// This tests end-to-end functionality of auto-starting a devcontainer. | ||
// It runs "devcontainer up" which creates a real Docker container. As | ||
// such, it does not run by default in CI. | ||
// | ||
// You can run it manually as follows: | ||
// | ||
// CODER_TEST_USE_DOCKER=1 go test -count=1 ./agent -run TestAgent_DevcontainerAutostart | ||
func TestAgent_DevcontainerAutostart(t *testing.T) { | ||
mafredri marked this conversation as resolved. Show resolvedHide resolvedUh oh!There was an error while loading.Please reload this page. | ||
t.Parallel() | ||
if os.Getenv("CODER_TEST_USE_DOCKER") != "1" { | ||
t.Skip("Set CODER_TEST_USE_DOCKER=1 to run this test") | ||
} | ||
ctx := testutil.Context(t, testutil.WaitLong) | ||
// Connect to Docker | ||
pool, err := dockertest.NewPool("") | ||
require.NoError(t, err, "Could not connect to docker") | ||
// Prepare temporary devcontainer for test (mywork). | ||
devcontainerID := uuid.New() | ||
tempWorkspaceFolder := t.TempDir() | ||
tempWorkspaceFolder = filepath.Join(tempWorkspaceFolder, "mywork") | ||
t.Logf("Workspace folder: %s", tempWorkspaceFolder) | ||
devcontainerPath := filepath.Join(tempWorkspaceFolder, ".devcontainer") | ||
err = os.MkdirAll(devcontainerPath, 0o755) | ||
require.NoError(t, err, "create devcontainer directory") | ||
devcontainerFile := filepath.Join(devcontainerPath, "devcontainer.json") | ||
err = os.WriteFile(devcontainerFile, []byte(`{ | ||
"name": "mywork", | ||
"image": "busybox:latest", | ||
"cmd": ["sleep", "infinity"] | ||
}`), 0o600) | ||
require.NoError(t, err, "write devcontainer.json") | ||
manifest := agentsdk.Manifest{ | ||
// Set up pre-conditions for auto-starting a devcontainer, the script | ||
// is expected to be prepared by the provisioner normally. | ||
Devcontainers: []codersdk.WorkspaceAgentDevcontainer{ | ||
{ | ||
ID: devcontainerID, | ||
Name: "test", | ||
WorkspaceFolder: tempWorkspaceFolder, | ||
}, | ||
}, | ||
Scripts: []codersdk.WorkspaceAgentScript{ | ||
{ | ||
ID: devcontainerID, | ||
LogSourceID: agentsdk.ExternalLogSourceID, | ||
RunOnStart: true, | ||
Script: "echo this-will-be-replaced", | ||
DisplayName: "Dev Container (test)", | ||
}, | ||
}, | ||
} | ||
// nolint: dogsled | ||
conn, _, _, _, _ := setupAgent(t, manifest, 0, func(_ *agenttest.Client, o *agent.Options) { | ||
o.ExperimentalDevcontainersEnabled = true | ||
}) | ||
t.Logf("Waiting for container with label: devcontainer.local_folder=%s", tempWorkspaceFolder) | ||
var container docker.APIContainers | ||
require.Eventually(t, func() bool { | ||
containers, err := pool.Client.ListContainers(docker.ListContainersOptions{All: true}) | ||
if err != nil { | ||
t.Logf("Error listing containers: %v", err) | ||
return false | ||
} | ||
for _, c := range containers { | ||
t.Logf("Found container: %s with labels: %v", c.ID[:12], c.Labels) | ||
if labelValue, ok := c.Labels["devcontainer.local_folder"]; ok { | ||
if labelValue == tempWorkspaceFolder { | ||
t.Logf("Found matching container: %s", c.ID[:12]) | ||
container = c | ||
return true | ||
} | ||
} | ||
} | ||
return false | ||
}, testutil.WaitSuperLong, testutil.IntervalMedium, "no container with workspace folder label found") | ||
t.Cleanup(func() { | ||
// We can't rely on pool here because the container is not | ||
// managed by it (it is managed by @devcontainer/cli). | ||
err := pool.Client.RemoveContainer(docker.RemoveContainerOptions{ | ||
ID: container.ID, | ||
RemoveVolumes: true, | ||
Force: true, | ||
}) | ||
assert.NoError(t, err, "remove container") | ||
}) | ||
containerInfo, err := pool.Client.InspectContainer(container.ID) | ||
require.NoError(t, err, "inspect container") | ||
t.Logf("Container state: status: %v", containerInfo.State.Status) | ||
require.True(t, containerInfo.State.Running, "container should be running") | ||
ac, err := conn.ReconnectingPTY(ctx, uuid.New(), 80, 80, "", func(opts *workspacesdk.AgentReconnectingPTYInit) { | ||
opts.Container = container.ID | ||
}) | ||
require.NoError(t, err, "failed to create ReconnectingPTY") | ||
defer ac.Close() | ||
// Use terminal reader so we can see output in case somethin goes wrong. | ||
tr := testutil.NewTerminalReader(t, ac) | ||
require.NoError(t, tr.ReadUntil(ctx, func(line string) bool { | ||
return strings.Contains(line, "#") || strings.Contains(line, "$") | ||
}), "find prompt") | ||
wantFileName := "file-from-devcontainer" | ||
wantFile := filepath.Join(tempWorkspaceFolder, wantFileName) | ||
require.NoError(t, json.NewEncoder(ac).Encode(workspacesdk.ReconnectingPTYRequest{ | ||
// NOTE(mafredri): We must use absolute path here for some reason. | ||
Data: fmt.Sprintf("touch /workspaces/mywork/%s; exit\r", wantFileName), | ||
}), "create file inside devcontainer") | ||
// Wait for the connection to close to ensure the touch was executed. | ||
require.ErrorIs(t, tr.ReadUntil(ctx, nil), io.EOF) | ||
_, err = os.Stat(wantFile) | ||
require.NoError(t, err, "file should exist outside devcontainer") | ||
} | ||
func TestAgent_Dial(t *testing.T) { | ||
t.Parallel() | ||
98 changes: 98 additions & 0 deletionsagent/agentcontainers/devcontainer.go
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
package agentcontainers | ||
import ( | ||
"context" | ||
"fmt" | ||
"os" | ||
"path/filepath" | ||
"strings" | ||
"cdr.dev/slog" | ||
"github.com/coder/coder/v2/codersdk" | ||
) | ||
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 { | ||
var args []string | ||
args = append(args, 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)) | ||
mafredri marked this conversation as resolved. Show resolvedHide resolvedUh oh!There was an error while loading.Please reload this page. | ||
} 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 | ||
} |
Oops, something went wrong.
Uh oh!
There was an error while loading.Please reload this page.
Oops, something went wrong.
Uh oh!
There was an error while loading.Please reload this page.
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.