@@ -2,10 +2,8 @@ package agentcontainers
2
2
3
3
import (
4
4
"context"
5
- "fmt"
6
5
"os"
7
6
"path/filepath"
8
- "strings"
9
7
10
8
"cdr.dev/slog"
11
9
"github.com/coder/coder/v2/codersdk"
@@ -22,61 +20,6 @@ const (
22
20
DevcontainerDefaultContainerWorkspaceFolder = "/workspaces"
23
21
)
24
22
25
- const devcontainerUpScriptTemplate = `
26
- if ! which devcontainer > /dev/null 2>&1; then
27
- echo "ERROR: Unable to start devcontainer, @devcontainers/cli is not installed or not found in \$PATH." 1>&2
28
- echo "Please install @devcontainers/cli by running \"npm install -g @devcontainers/cli\" or by using the \"devcontainers-cli\" Coder module." 1>&2
29
- exit 1
30
- fi
31
- devcontainer up %s
32
- `
33
-
34
- // ExtractAndInitializeDevcontainerScripts extracts devcontainer scripts from
35
- // the given scripts and devcontainers. The devcontainer scripts are removed
36
- // from the returned scripts so that they can be run separately.
37
- //
38
- // Dev Containers have an inherent dependency on start scripts, since they
39
- // initialize the workspace (e.g. git clone, npm install, etc). This is
40
- // important if e.g. a Coder module to install @devcontainer/cli is used.
41
- func ExtractAndInitializeDevcontainerScripts (
42
- devcontainers []codersdk.WorkspaceAgentDevcontainer ,
43
- scripts []codersdk.WorkspaceAgentScript ,
44
- ) (filteredScripts []codersdk.WorkspaceAgentScript , devcontainerScripts []codersdk.WorkspaceAgentScript ) {
45
- ScriptLoop:
46
- for _ , script := range scripts {
47
- for _ , dc := range devcontainers {
48
- // The devcontainer scripts match the devcontainer ID for
49
- // identification.
50
- if script .ID == dc .ID {
51
- devcontainerScripts = append (devcontainerScripts , devcontainerStartupScript (dc , script ))
52
- continue ScriptLoop
53
- }
54
- }
55
-
56
- filteredScripts = append (filteredScripts , script )
57
- }
58
-
59
- return filteredScripts , devcontainerScripts
60
- }
61
-
62
- func devcontainerStartupScript (dc codersdk.WorkspaceAgentDevcontainer , script codersdk.WorkspaceAgentScript ) codersdk.WorkspaceAgentScript {
63
- args := []string {
64
- "--log-format json" ,
65
- fmt .Sprintf ("--workspace-folder %q" , dc .WorkspaceFolder ),
66
- }
67
- if dc .ConfigPath != "" {
68
- args = append (args , fmt .Sprintf ("--config %q" , dc .ConfigPath ))
69
- }
70
- cmd := fmt .Sprintf (devcontainerUpScriptTemplate , strings .Join (args , " " ))
71
- // Force the script to run in /bin/sh, since some shells (e.g. fish)
72
- // don't support the script.
73
- script .Script = fmt .Sprintf ("/bin/sh -c '%s'" , cmd )
74
- // Disable RunOnStart, scripts have this set so that when devcontainers
75
- // have not been enabled, a warning will be surfaced in the agent logs.
76
- script .RunOnStart = false
77
- return script
78
- }
79
-
80
23
// ExpandAllDevcontainerPaths expands all devcontainer paths in the given
81
24
// devcontainers. This is required by the devcontainer CLI, which requires
82
25
// absolute paths for the workspace folder and config path.
0 commit comments