Skip to content

Commit

Permalink
Clean up volumes if workspace preparation fails
Browse files Browse the repository at this point in the history
  • Loading branch information
lowjoel committed Aug 22, 2023
1 parent b9e783a commit 970f0d3
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 3 deletions.
16 changes: 13 additions & 3 deletions internal/batches/workspace/volume_workspace.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,16 +30,22 @@ type dockerVolumeWorkspaceCreator struct {

var _ Creator = &dockerVolumeWorkspaceCreator{}

func (wc *dockerVolumeWorkspaceCreator) Create(ctx context.Context, repo *graphql.Repository, steps []batcheslib.Step, archive repozip.Archive) (Workspace, error) {
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 {
var err error
img, err := wc.EnsureImage(ctx, steps[0].Container)
if err != nil {
return nil, err
Expand Down Expand Up @@ -74,6 +80,10 @@ func (*dockerVolumeWorkspaceCreator) createVolume(ctx context.Context) (string,
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
Expand Down Expand Up @@ -219,7 +229,7 @@ var _ Workspace = &dockerVolumeWorkspace{}

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

func (w *dockerVolumeWorkspace) DockerRunOpts(ctx context.Context, target string) ([]string, error) {
Expand Down
12 changes: 12 additions & 0 deletions internal/batches/workspace/volume_workspace_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,10 @@ func TestVolumeWorkspaceCreator(t *testing.T) {
DockerVolumeWorkspaceImage,
"sh", "-c", "touch /work/*; chown -R 0:0 /work",
),
expect.NewGlob(
expect.Behaviour{ExitCode: 0},
"docker", "volume", "rm", volumeID,
),
},
steps: []batcheslib.Step{
{},
Expand Down Expand Up @@ -253,6 +257,10 @@ func TestVolumeWorkspaceCreator(t *testing.T) {
DockerVolumeWorkspaceImage,
"sh", "/run.sh",
),
expect.NewGlob(
expect.Behaviour{ExitCode: 0},
"docker", "volume", "rm", volumeID,
),
},
steps: []batcheslib.Step{
{},
Expand Down Expand Up @@ -286,6 +294,10 @@ func TestVolumeWorkspaceCreator(t *testing.T) {
DockerVolumeWorkspaceImage,
"sh", "-c", "unzip /tmp/zip; rm /work/*",
),
expect.NewGlob(
expect.Behaviour{ExitCode: 0},
"docker", "volume", "rm", volumeID,
),
},
steps: []batcheslib.Step{
{},
Expand Down

0 comments on commit 970f0d3

Please sign in to comment.