/
volume_workspace.go
352 lines (289 loc) · 10.4 KB
/
volume_workspace.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
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
package workspace
import (
"bytes"
"context"
"crypto/rand"
"encoding/hex"
"fmt"
"os"
"sort"
"strings"
"github.com/sourcegraph/sourcegraph/lib/errors"
batcheslib "github.com/sourcegraph/sourcegraph/lib/batches"
"github.com/sourcegraph/src-cli/internal/batches/docker"
"github.com/sourcegraph/src-cli/internal/batches/graphql"
"github.com/sourcegraph/src-cli/internal/batches/repozip"
"github.com/sourcegraph/src-cli/internal/exec"
"github.com/sourcegraph/src-cli/internal/version"
)
type imageEnsurer func(ctx context.Context, image string) (docker.Image, error)
type dockerVolumeWorkspaceCreator struct {
tempDir string
EnsureImage imageEnsurer
}
var _ Creator = &dockerVolumeWorkspaceCreator{}
func (wc *dockerVolumeWorkspaceCreator) Create(ctx context.Context, repo *graphql.Repository,
steps []batcheslib.Step, archive repozip.Archive) (ws Workspace, err error) {
volume, err := wc.createVolume(ctx)
if err != nil {
return nil, errors.Wrap(err, "creating Docker volume")
}
defer func() {
if err != nil {
deleteVolume(ctx, volume)
}
}()
// Figure out the user that containers will be run as.
ug := docker.UIDGID{}
if len(steps) > 0 {
img, err := wc.EnsureImage(ctx, steps[0].Container)
if err != nil {
return nil, err
}
if ug, err = img.UIDGID(ctx); err != nil {
return nil, errors.Wrap(err, "getting container UID and GID")
}
}
w := &dockerVolumeWorkspace{
tempDir: wc.tempDir,
volume: volume,
uidGid: ug,
}
if err := wc.unzipRepoIntoVolume(ctx, w, archive.Path()); err != nil {
return nil, errors.Wrap(err, "unzipping repo into workspace")
}
if err := wc.copyFilesIntoVolumes(ctx, w, archive.AdditionalFilePaths()); err != nil {
return nil, errors.Wrap(err, "copying additional files into workspace")
}
return w, errors.Wrap(wc.prepareGitRepo(ctx, w), "preparing local git repo")
}
func (*dockerVolumeWorkspaceCreator) createVolume(ctx context.Context) (string, error) {
out, err := exec.CommandContext(ctx, "docker", "volume", "create").CombinedOutput()
if err != nil {
return "", err
}
return string(bytes.TrimSpace(out)), nil
}
func deleteVolume(ctx context.Context, volume string) error {
return exec.CommandContext(ctx, "docker", "volume", "rm", volume).Run()
}
func (*dockerVolumeWorkspaceCreator) prepareGitRepo(ctx context.Context, w *dockerVolumeWorkspace) error {
script := `#!/bin/sh
set -e
set -x
git init
# Note that we don't actually use these anywhere, since we're not creating the
# real commits in this container, but we do need _something_ set to avoid Git
# erroring out.
git config user.name 'Sourcegraph Batch Changes'
git config user.email batch-changes@sourcegraph.com
# --force because we want previously "gitignored" files in the repository
git add --force --all
git commit --quiet --all --allow-empty -m src-action-exec
`
if _, err := w.runScript(ctx, "/work", script); err != nil {
return errors.Wrap(err, "preparing workspace")
}
return nil
}
func (wc *dockerVolumeWorkspaceCreator) unzipRepoIntoVolume(ctx context.Context, w *dockerVolumeWorkspace, zip string) error {
// We want to mount that temporary file into a Docker container that has the
// workspace volume attached, and unzip it into the volume.
// We need to keep a temporary file in the volume before unzipping for the
// permissions to persist because... reasons. Rather than reading the
// potentially large ZIP file, we'll cheat a bit and just assume that if we
// create a file with an appropriately namespaced and random name, it's
// _probably_ OK. If you manage to reliably trigger an archive that has this
// file in it, we'll send you a hoodie or something.
randToken := make([]byte, 16)
if _, err := rand.Read(randToken); err != nil {
return errors.Wrap(err, "generating randomness")
}
dummy := fmt.Sprintf(".batch-change-workspace-placeholder-%s", hex.EncodeToString(randToken))
// So, let's use that to set up the volume.
//
// Theoretically, we could combine this `docker run` and the following one
// into one invocation. Doing so, however, is tricky: we'd have to su within
// the script being run, and Alpine requires a real user account and group;
// just having numeric IDs is insufficient. The logic to make this work is
// complicated enough that it feels brittle, and beyond what should be
// encoded in this function. Running `docker run` twice isn't ideal, but
// should be quick enough in general that it's not a huge concern.
opts := append([]string{
"run",
"--rm",
"--init",
"--workdir", "/work",
}, w.dockerRunOptsWithUser(docker.Root, "/work")...)
opts = append(
opts,
DockerVolumeWorkspaceImage,
"sh", "-c",
fmt.Sprintf("touch /work/%s; chown -R %s /work", dummy, w.uidGid.String()),
)
if out, err := exec.CommandContext(ctx, "docker", opts...).CombinedOutput(); err != nil {
return errors.Wrapf(err, "chown output:\n\n%s\n\n", string(out))
}
// Now we can unzip the archive as the user and clean up the temporary file.
opts = append([]string{
"run",
"--rm",
"--init",
"--workdir", "/work",
"--mount", "type=bind,source=" + zip + ",target=/tmp/zip,ro",
}, w.dockerRunOptsWithUser(w.uidGid, "/work")...)
opts = append(
opts,
DockerVolumeWorkspaceImage,
"sh", "-c",
fmt.Sprintf("unzip /tmp/zip; rm /work/%s", dummy),
)
if out, err := exec.CommandContext(ctx, "docker", opts...).CombinedOutput(); err != nil {
return errors.Wrapf(err, "unzip output:\n\n%s\n\n", string(out))
}
return nil
}
func (wc *dockerVolumeWorkspaceCreator) copyFilesIntoVolumes(ctx context.Context, w *dockerVolumeWorkspace, files map[string]string) error {
if len(files) == 0 {
return nil
}
opts := append([]string{
"run",
"--rm",
"--init",
"--workdir", "/work",
}, w.dockerRunOptsWithUser(w.uidGid, "/work")...)
// We sort these so our tests don't break. Sorry.
var names []string
for name := range files {
names = append(names, name)
}
sort.Strings(names)
var copyCmds []string
for _, name := range names {
localPath := files[name]
opts = append(opts, []string{
"--mount", "type=bind,source=" + localPath + ",target=/tmp/" + name + ",ro",
}...)
copyCmds = append(copyCmds, "cp /tmp/"+name+" /work/"+name)
}
opts = append(
opts,
DockerVolumeWorkspaceImage,
"sh", "-c",
strings.Join(copyCmds, " && ")+";",
)
if out, err := exec.CommandContext(ctx, "docker", opts...).CombinedOutput(); err != nil {
return errors.Wrapf(err, "unzip output:\n\n%s\n\n", string(out))
}
return nil
}
// dockerVolumeWorkspace workspaces are placed on Docker volumes (surprise!),
// and are therefore transparent to the host filesystem. This has performance
// advantages if bind mounts are slow, such as on Docker for Mac, but could make
// debugging harder and is slower when it's time to actually retrieve the diff.
type dockerVolumeWorkspace struct {
tempDir string
volume string
uidGid docker.UIDGID
}
var _ Workspace = &dockerVolumeWorkspace{}
func (w *dockerVolumeWorkspace) Close(ctx context.Context) error {
// Cleanup here is easy: we just get rid of the Docker volume.
return deleteVolume(ctx, w.volume)
}
func (w *dockerVolumeWorkspace) DockerRunOpts(ctx context.Context, target string) ([]string, error) {
return w.dockerRunOptsWithUser(w.uidGid, target), nil
}
func (w *dockerVolumeWorkspace) WorkDir() *string { return nil }
func (w *dockerVolumeWorkspace) Diff(ctx context.Context) ([]byte, error) {
// As of Sourcegraph 3.14 we only support unified diff format.
// That means we need to strip away the `a/` and `/b` prefixes with `--no-prefix`.
// See: https://github.com/sourcegraph/sourcegraph/blob/82d5e7e1562fef6be5c0b17f18631040fd330835/enterprise/internal/campaigns/service.go#L324-L329
//
// Also, we need to add --binary so binary file changes are inlined in the patch.
//
// ATTENTION: When you change the options here, be sure to also update the
// ApplyDiff method accordingly.
script := `#!/bin/sh
set -e
# No set -x here, since we're going to parse the git status output.
git add --all > /dev/null
exec git diff --cached --no-prefix --binary
`
out, err := w.runScript(ctx, "/work", script)
if err != nil {
return nil, errors.Wrapf(err, "git diff:\n\n%s", string(out))
}
return out, nil
}
func (w *dockerVolumeWorkspace) ApplyDiff(ctx context.Context, diff []byte) error {
script := fmt.Sprintf(`#!/bin/sh
set -e
cat <<'EOF' | exec git apply -p0 -
%s
EOF
git add --all > /dev/null
`, string(diff))
out, err := w.runScript(ctx, "/work", script)
if err != nil {
return errors.Wrapf(err, "git apply diff:\n\n%s", string(out))
}
return nil
}
// DockerVolumeWorkspaceImage is the Docker image we'll run our unzip and git
// commands in. This needs to match the name defined in
// .github/workflows/docker.yml.
var DockerVolumeWorkspaceImage = "sourcegraph/src-batch-change-volume-workspace"
func init() {
dockerTag := version.BuildTag
if version.BuildTag == version.DefaultBuildTag {
dockerTag = "latest"
}
DockerVolumeWorkspaceImage = DockerVolumeWorkspaceImage + ":" + dockerTag
}
// runScript is a utility function to mount the given shell script into a Docker
// container started from the dockerWorkspaceImage, then run it and return the
// output.
func (w *dockerVolumeWorkspace) runScript(ctx context.Context, target, script string) ([]byte, error) {
f, err := os.CreateTemp(w.tempDir, "src-run-*")
if err != nil {
return nil, errors.Wrap(err, "creating run script")
}
name := f.Name()
defer os.Remove(name)
if _, err := f.WriteString(script); err != nil {
return nil, errors.Wrap(err, "writing run script")
}
if err := f.Close(); err != nil {
return nil, errors.Wrap(err, "closing run script")
}
// Sidestep any umask issues on the temporary file by always making it
// executable by everyone.
if err := os.Chmod(name, 0755); err != nil {
return nil, errors.Wrap(err, "chmodding run script")
}
common, err := w.DockerRunOpts(ctx, target)
if err != nil {
return nil, errors.Wrap(err, "generating run options")
}
opts := append([]string{
"run",
"--rm",
"--init",
"--workdir", target,
"--mount", "type=bind,source=" + name + ",target=/run.sh,ro",
}, common...)
opts = append(opts, DockerVolumeWorkspaceImage, "sh", "/run.sh")
out, err := exec.CommandContext(ctx, "docker", opts...).CombinedOutput()
if err != nil {
return out, errors.Wrapf(err, "Docker output:\n\n%s\n\n", string(out))
}
return out, nil
}
func (w *dockerVolumeWorkspace) dockerRunOptsWithUser(ug docker.UIDGID, target string) []string {
return []string{
"--user", ug.String(),
"--mount", "type=volume,source=" + w.volume + ",target=" + target,
}
}