-
Notifications
You must be signed in to change notification settings - Fork 336
/
run.go
236 lines (197 loc) · 6.66 KB
/
run.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
package devcontainer
import (
"context"
"fmt"
"io"
"os"
"os/exec"
"path"
"path/filepath"
"runtime"
"strings"
"github.com/loft-sh/devpod/pkg/devcontainer/config"
"github.com/loft-sh/devpod/pkg/driver"
"github.com/loft-sh/devpod/pkg/driver/drivercreate"
"github.com/loft-sh/devpod/pkg/encoding"
"github.com/loft-sh/devpod/pkg/language"
provider2 "github.com/loft-sh/devpod/pkg/provider"
"github.com/loft-sh/log"
"github.com/pkg/errors"
"github.com/sirupsen/logrus"
)
func NewRunner(agentPath, agentDownloadURL string, workspaceConfig *provider2.AgentWorkspaceInfo, log log.Logger) (*Runner, error) {
driver, err := drivercreate.NewDriver(workspaceConfig, log)
if err != nil {
return nil, err
}
// we use the workspace uid as id to avoid conflicts between container names
return &Runner{
Driver: driver,
AgentPath: agentPath,
AgentDownloadURL: agentDownloadURL,
LocalWorkspaceFolder: workspaceConfig.ContentFolder,
ID: GetRunnerIDFromWorkspace(workspaceConfig.Workspace),
WorkspaceConfig: workspaceConfig,
Log: log,
}, nil
}
type Runner struct {
Driver driver.Driver
WorkspaceConfig *provider2.AgentWorkspaceInfo
AgentPath string
AgentDownloadURL string
LocalWorkspaceFolder string
SubstitutionContext *config.SubstitutionContext
ID string
Log log.Logger
}
type UpOptions struct {
PrebuildRepositories []string
NoBuild bool
ForceBuild bool
Recreate bool
}
func (r *Runner) prepare() (*config.SubstitutedConfig, *WorkspaceConfig, error) {
rawParsedConfig, err := config.ParseDevContainerJSON(r.LocalWorkspaceFolder, r.WorkspaceConfig.Workspace.DevContainerPath)
if err != nil {
return nil, nil, errors.Wrap(err, "parsing devcontainer.json")
} else if rawParsedConfig == nil {
r.Log.Infof("Couldn't find a devcontainer.json")
r.Log.Infof("Try detecting project programming language...")
defaultConfig := language.DefaultConfig(r.LocalWorkspaceFolder, r.Log)
defaultConfig.Origin = path.Join(filepath.ToSlash(r.LocalWorkspaceFolder), ".devcontainer.json")
err = config.SaveDevContainerJSON(defaultConfig)
if err != nil {
return nil, nil, errors.Wrap(err, "write default devcontainer.json")
}
rawParsedConfig = defaultConfig
}
configFile := rawParsedConfig.Origin
// get workspace folder within container
workspace := getWorkspace(r.LocalWorkspaceFolder, r.WorkspaceConfig.Workspace.ID, rawParsedConfig)
r.SubstitutionContext = &config.SubstitutionContext{
DevContainerID: config.GetDevContainerID(config.ListToObject(r.getLabels())),
LocalWorkspaceFolder: r.LocalWorkspaceFolder,
ContainerWorkspaceFolder: workspace.RemoteWorkspaceFolder,
Env: config.ListToObject(os.Environ()),
}
// substitute & load
parsedConfig := &config.DevContainerConfig{}
err = config.Substitute(r.SubstitutionContext, rawParsedConfig, parsedConfig)
if err != nil {
return nil, nil, err
}
if parsedConfig.WorkspaceFolder != "" {
workspace.RemoteWorkspaceFolder = parsedConfig.WorkspaceFolder
}
if parsedConfig.WorkspaceMount != "" {
workspace.WorkspaceMount = parsedConfig.WorkspaceMount
}
parsedConfig.Origin = configFile
return &config.SubstitutedConfig{
Config: parsedConfig,
Raw: rawParsedConfig,
}, &workspace, nil
}
func (r *Runner) Up(ctx context.Context, options UpOptions) (*config.Result, error) {
substitutedConfig, workspace, err := r.prepare()
if err != nil {
return nil, err
}
// run initializeCommand
err = runInitializeCommand(r.LocalWorkspaceFolder, substitutedConfig.Config, r.Log)
if err != nil {
return nil, err
}
// check if its a compose devcontainer.json
var result *config.Result
if isDockerFileConfig(substitutedConfig.Config) || substitutedConfig.Config.Image != "" {
result, err = r.runSingleContainer(ctx, substitutedConfig, workspace.WorkspaceMount, options)
if err != nil {
return nil, err
}
} else if len(substitutedConfig.Config.DockerComposeFile) > 0 {
result, err = r.runDockerCompose(ctx, substitutedConfig, options)
if err != nil {
return nil, err
}
} else {
return nil, fmt.Errorf("dev container config is missing one of \"image\", \"dockerFile\" or \"dockerComposeFile\" properties")
}
// return result
return result, nil
}
func (r *Runner) CommandDevContainer(ctx context.Context, containerId string, user string, command string, stdin io.Reader, stdout io.Writer, stderr io.Writer) error {
return r.Driver.CommandDevContainer(ctx, containerId, user, command, stdin, stdout, stderr)
}
func (r *Runner) FindDevContainer(ctx context.Context) (*config.ContainerDetails, error) {
labels := r.getLabels()
containerDetails, err := r.Driver.FindDevContainer(ctx, labels)
if err != nil {
return nil, errors.Wrap(err, "find dev container")
}
return containerDetails, nil
}
func (r *Runner) getLabels() []string {
return []string{config.DockerIDLabel + "=" + r.ID}
}
func isDockerFileConfig(config *config.DevContainerConfig) bool {
return config.Dockerfile != "" || config.Build.Dockerfile != ""
}
func runInitializeCommand(workspaceFolder string, config *config.DevContainerConfig, log log.Logger) error {
if len(config.InitializeCommand) == 0 {
return nil
}
// should run in shell?
var args []string
if len(config.InitializeCommand) == 1 {
args = []string{"sh", "-c", config.InitializeCommand[0]}
} else {
args = config.InitializeCommand
}
// run the command
log.Infof("Running initializeCommand from devcontainer.json: '%s'", strings.Join(args, " "))
writer := log.Writer(logrus.InfoLevel, false)
defer writer.Close()
cmd := exec.Command(args[0], args[1:]...)
cmd.Stdout = writer
cmd.Stderr = writer
cmd.Dir = workspaceFolder
err := cmd.Run()
if err != nil {
return err
}
return nil
}
type WorkspaceConfig struct {
WorkspaceMount string
RemoteWorkspaceFolder string
}
func getWorkspace(workspaceFolder, workspaceID string, conf *config.DevContainerConfig) WorkspaceConfig {
if conf.WorkspaceMount != "" {
mount := config.ParseMount(conf.WorkspaceMount)
return WorkspaceConfig{
WorkspaceMount: conf.WorkspaceMount,
RemoteWorkspaceFolder: mount.Target,
}
}
containerMountFolder := conf.WorkspaceFolder
if containerMountFolder == "" {
containerMountFolder = "/workspaces/" + workspaceID
}
consistency := ""
if runtime.GOOS != "linux" {
consistency = ",consistency='consistent'"
}
return WorkspaceConfig{
RemoteWorkspaceFolder: containerMountFolder,
WorkspaceMount: fmt.Sprintf("type=bind,source=%s,target=%s%s", workspaceFolder, containerMountFolder, consistency),
}
}
func GetRunnerIDFromWorkspace(workspace *provider2.Workspace) string {
ID := workspace.UID
if encoding.IsLegacyUID(workspace.UID) {
ID = workspace.ID
}
return ID
}