This repository has been archived by the owner on Apr 28, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 36
/
Copy pathproject.go
309 lines (253 loc) · 6.91 KB
/
project.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
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
package main
import (
"context"
"fmt"
"os"
"path/filepath"
"strings"
"time"
"github.com/docker/docker/api/types"
"golang.org/x/xerrors"
"go.coder.com/flog"
"go.coder.com/sail/internal/browserapp"
"go.coder.com/sail/internal/codeserver"
"go.coder.com/sail/internal/dockutil"
"go.coder.com/sail/internal/xexec"
)
type projectStatus string
const (
on projectStatus = "on"
off projectStatus = "off"
)
// project represents a sail project.
type project struct {
conf config
repo repo
}
func (p *project) pathName() string {
return strings.TrimSuffix(p.repo.Path, ".git")
}
func (p *project) localDir() string {
hostHomeDir, err := os.UserHomeDir()
if err != nil {
panic(err)
}
path := strings.TrimSuffix(p.repo.Path, ".git")
projectDir := filepath.Join(p.conf.ProjectRoot, path)
projectDir = resolvePath(hostHomeDir, projectDir)
return projectDir
}
func (p *project) dockerfilePath() string {
return filepath.Join(p.localDir(), ".sail", "Dockerfile")
}
// clone clones a git repository to dir.
func clone(r repo, dir string) error {
uri := r.CloneURI()
cmd := xexec.Fmt("git clone %v %v", uri, dir)
xexec.Attach(cmd)
err := cmd.Run()
if err != nil {
return xerrors.Errorf("failed to clone '%s' to '%s': %w", uri, dir, err)
}
return nil
}
func isContainerNotFoundError(err error) bool {
if err == nil {
return false
}
return strings.Contains(err.Error(), "No such container")
}
func (p *project) cntExists() (bool, error) {
cli := dockerClient()
defer cli.Close()
_, err := cli.ContainerInspect(context.Background(), p.cntName())
if err != nil {
if isContainerNotFoundError(err) {
return false, nil
}
return false, xerrors.Errorf("failed to inspect %v: %w", p.cntName(), err)
}
return true, nil
}
func (p *project) running() (bool, error) {
cli := dockerClient()
defer cli.Close()
cnt, err := cli.ContainerInspect(context.Background(), p.cntName())
if err != nil {
return false, xerrors.Errorf("failed to get container %v: %v", p.cntName(), err)
}
return cnt.State.Running, nil
}
func (p *project) requireRunning() {
running, err := p.running()
if err != nil {
flog.Fatal("%v", err)
}
if !running {
flog.Fatal("container %v is not running", p.cntName())
}
}
// ensureDir ensures that a project directory exists or creates
// one if it doesn't exist.
func (p *project) ensureDir() error {
err := os.MkdirAll(p.localDir(), 0750)
if err != nil {
return xerrors.Errorf("failed to make project dir %v: %w", p.localDir(), err)
}
// If the git directory exists, don't bother re-downloading the project.
gitDir := filepath.Join(p.localDir(), ".git")
_, err = os.Stat(gitDir)
if err == nil {
return nil
}
return clone(p.repo, p.localDir())
}
// buildImage finds the `.sail/Dockerfile` in the project directory
// and builds it. It sets the sail base image label on the image
// so the runner can use it when creating the container.
func (p *project) buildImage() (string, bool, error) {
const relPath = ".sail/Dockerfile"
path := filepath.Join(p.localDir(), relPath)
_, err := os.Stat(path)
if err != nil {
if !os.IsNotExist(err) {
return "", false, xerrors.Errorf("failed to stat %v: %w", path, err)
}
return "", false, nil
}
// Docker image names must be completely lowercase.
imageID := strings.ToLower(p.repo.DockerName())
cmdStr := fmt.Sprintf("docker build --network=host -t %v -f %v %v --label %v=%v",
imageID, path, p.localDir(), baseImageLabel, imageID,
)
flog.Info("running %v", cmdStr)
cmd := xexec.Fmt(cmdStr)
xexec.Attach(cmd)
err = cmd.Run()
if err != nil {
return "", false, xerrors.Errorf("failed to build: %w", err)
}
return imageID, true, nil
}
func fmtImage(img string) string {
return fmt.Sprintf("codercom/ubuntu-dev-%s:latest", img)
}
// defaultRepoImage returns a base image suitable for development with the
// repo's language. If the repo language isn't able to be determined, this
// returns the default image from the sail config.
func (p *project) defaultRepoImage() string {
lang := p.repo.language()
switch strings.ToLower(lang) {
case "go":
return fmtImage("go")
case "javascript", "typescript":
return fmtImage("node12")
case "python":
return fmtImage("python3.7")
case "c", "c++":
return fmtImage("gcc8")
case "java":
return fmtImage("openjdk12")
case "ruby":
return fmtImage("ruby2.6")
default:
return p.conf.DefaultImage
}
}
func ensureImage(image string) error {
flog.Info("ensuring image %v exists", image)
cmd := xexec.Fmt("docker pull %s", image)
xexec.Attach(cmd)
return cmd.Run()
}
func (p *project) cntName() string {
return p.repo.DockerName()
}
// containerDir returns the directory of which the project is mounted within the container.
func (p *project) containerDir() (string, error) {
client := dockerClient()
defer client.Close()
cnt, err := client.ContainerInspect(context.Background(), p.cntName())
if err != nil {
return "", err
}
dir, ok := cnt.Config.Labels[projectDirLabel]
if !ok {
return "", xerrors.Errorf("no %v label", projectDirLabel)
}
return dir, nil
}
// proxyAddr returns the address of the sail proxy for the container.
func (p *project) proxyURL() (string, error) {
return proxyURL(p.cntName())
}
func proxyURL(cntName string) (string, error) {
client := dockerClient()
defer client.Close()
cnt, err := client.ContainerInspect(context.Background(), cntName)
if err != nil {
return "", err
}
proxyURL, ok := cnt.Config.Labels[proxyURLLabel]
if !ok {
return "", xerrors.Errorf("no %v label", proxyURLLabel)
}
return proxyURL, nil
}
func (p *project) readCodeServerLog() ([]byte, error) {
cmd := xexec.Fmt("docker logs %v", p.cntName())
out, err := cmd.CombinedOutput()
if err != nil {
return nil, xerrors.Errorf("failed to cat %v: %w", containerLogPath, err)
}
return out, nil
}
// waitOnline waits until code-server has bound to it's port.
func (p *project) waitOnline() error {
cli := dockerClient()
defer cli.Close()
ctx, cancel := context.WithTimeout(context.Background(), time.Second*10)
defer cancel()
for ctx.Err() == nil {
cnt, err := cli.ContainerInspect(ctx, p.cntName())
if err != nil {
return err
}
if !cnt.State.Running {
return xerrors.Errorf("container %v not running", p.cntName())
}
_, err = codeserver.PID(p.cntName())
if err == nil {
return nil
}
time.Sleep(time.Millisecond * 100)
}
return ctx.Err()
}
func (p *project) open() error {
cli := dockerClient()
defer cli.Close()
err := cli.ContainerStart(context.Background(), p.cntName(), types.ContainerStartOptions{})
if err != nil {
return xerrors.Errorf("failed to start container: %w", err)
}
u, err := p.proxyURL()
if err != nil {
return err
}
if os.Getenv("DISPLAY") == "" {
flog.Info("please visit %v", u)
return nil
}
flog.Info("opening %v", u)
err = browserapp.Open(u)
if err != nil {
return err
}
return nil
}
func (p *project) delete() error {
cli := dockerClient()
defer cli.Close()
return dockutil.StopRemove(context.Background(), cli, p.cntName())
}