Skip to content

Commit 84ffc3e

Browse files
authored
Clean up volumes if workspace preparation fails (#1021)
1 parent 0ba39d4 commit 84ffc3e

File tree

2 files changed

+25
-3
lines changed

2 files changed

+25
-3
lines changed

internal/batches/workspace/volume_workspace.go

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,16 +30,22 @@ type dockerVolumeWorkspaceCreator struct {
3030

3131
var _ Creator = &dockerVolumeWorkspaceCreator{}
3232

33-
func (wc *dockerVolumeWorkspaceCreator) Create(ctx context.Context, repo *graphql.Repository, steps []batcheslib.Step, archive repozip.Archive) (Workspace, error) {
33+
func (wc *dockerVolumeWorkspaceCreator) Create(ctx context.Context, repo *graphql.Repository,
34+
steps []batcheslib.Step, archive repozip.Archive) (ws Workspace, err error) {
3435
volume, err := wc.createVolume(ctx)
3536
if err != nil {
3637
return nil, errors.Wrap(err, "creating Docker volume")
3738
}
3839

40+
defer func() {
41+
if err != nil {
42+
deleteVolume(ctx, volume)
43+
}
44+
}()
45+
3946
// Figure out the user that containers will be run as.
4047
ug := docker.UIDGID{}
4148
if len(steps) > 0 {
42-
var err error
4349
img, err := wc.EnsureImage(ctx, steps[0].Container)
4450
if err != nil {
4551
return nil, err
@@ -74,6 +80,10 @@ func (*dockerVolumeWorkspaceCreator) createVolume(ctx context.Context) (string,
7480
return string(bytes.TrimSpace(out)), nil
7581
}
7682

83+
func deleteVolume(ctx context.Context, volume string) error {
84+
return exec.CommandContext(ctx, "docker", "volume", "rm", volume).Run()
85+
}
86+
7787
func (*dockerVolumeWorkspaceCreator) prepareGitRepo(ctx context.Context, w *dockerVolumeWorkspace) error {
7888
script := `#!/bin/sh
7989
@@ -219,7 +229,7 @@ var _ Workspace = &dockerVolumeWorkspace{}
219229

220230
func (w *dockerVolumeWorkspace) Close(ctx context.Context) error {
221231
// Cleanup here is easy: we just get rid of the Docker volume.
222-
return exec.CommandContext(ctx, "docker", "volume", "rm", w.volume).Run()
232+
return deleteVolume(ctx, w.volume)
223233
}
224234

225235
func (w *dockerVolumeWorkspace) DockerRunOpts(ctx context.Context, target string) ([]string, error) {

internal/batches/workspace/volume_workspace_test.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -211,6 +211,10 @@ func TestVolumeWorkspaceCreator(t *testing.T) {
211211
DockerVolumeWorkspaceImage,
212212
"sh", "-c", "touch /work/*; chown -R 0:0 /work",
213213
),
214+
expect.NewGlob(
215+
expect.Behaviour{ExitCode: 0},
216+
"docker", "volume", "rm", volumeID,
217+
),
214218
},
215219
steps: []batcheslib.Step{
216220
{},
@@ -253,6 +257,10 @@ func TestVolumeWorkspaceCreator(t *testing.T) {
253257
DockerVolumeWorkspaceImage,
254258
"sh", "/run.sh",
255259
),
260+
expect.NewGlob(
261+
expect.Behaviour{ExitCode: 0},
262+
"docker", "volume", "rm", volumeID,
263+
),
256264
},
257265
steps: []batcheslib.Step{
258266
{},
@@ -286,6 +294,10 @@ func TestVolumeWorkspaceCreator(t *testing.T) {
286294
DockerVolumeWorkspaceImage,
287295
"sh", "-c", "unzip /tmp/zip; rm /work/*",
288296
),
297+
expect.NewGlob(
298+
expect.Behaviour{ExitCode: 0},
299+
"docker", "volume", "rm", volumeID,
300+
),
289301
},
290302
steps: []batcheslib.Step{
291303
{},

0 commit comments

Comments
 (0)