diff --git a/internal/build/container_ops.go b/internal/build/container_ops.go index 9bb2b05c49..1c12c5babb 100644 --- a/internal/build/container_ops.go +++ b/internal/build/container_ops.go @@ -207,92 +207,53 @@ func findMount(info types.ContainerJSON, dst string) (types.MountPoint, error) { return types.MountPoint{}, fmt.Errorf("no matching mount found for %s", dst) } -// WriteProjectMetadata -func WriteProjectMetadata(p string, metadata platform.ProjectMetadata, os string) ContainerOperation { - return func(ctrClient DockerClient, ctx context.Context, containerID string, stdout, stderr io.Writer) error { - buf := &bytes.Buffer{} - err := toml.NewEncoder(buf).Encode(metadata) - if err != nil { - return errors.Wrap(err, "marshaling project metadata") - } +func writeToml(ctrClient DockerClient, ctx context.Context, data interface{}, dstPath string, containerID string, os string, stdout, stderr io.Writer) error { + buf := &bytes.Buffer{} + err := toml.NewEncoder(buf).Encode(data) + if err != nil { + return errors.Wrapf(err, "marshaling data to %s", dstPath) + } - tarBuilder := archive.TarBuilder{} + tarBuilder := archive.TarBuilder{} - tarPath := p - if os == "windows" { - tarPath = paths.WindowsToSlash(p) - } + tarPath := dstPath + if os == "windows" { + tarPath = paths.WindowsToSlash(dstPath) + } - tarBuilder.AddFile(tarPath, 0755, archive.NormalizedDateTime, buf.Bytes()) - reader := tarBuilder.Reader(archive.DefaultTarWriterFactory()) - defer reader.Close() + tarBuilder.AddFile(tarPath, 0755, archive.NormalizedDateTime, buf.Bytes()) + reader := tarBuilder.Reader(archive.DefaultTarWriterFactory()) + defer reader.Close() - if os == "windows" { - dirName := paths.WindowsDir(p) - return copyDirWindows(ctx, ctrClient, containerID, reader, dirName, stdout, stderr) - } + if os == "windows" { + dirName := paths.WindowsDir(dstPath) + return copyDirWindows(ctx, ctrClient, containerID, reader, dirName, stdout, stderr) + } - return ctrClient.CopyToContainer(ctx, containerID, "/", reader, types.CopyToContainerOptions{}) + return ctrClient.CopyToContainer(ctx, containerID, "/", reader, types.CopyToContainerOptions{}) +} + +// WriteProjectMetadata writes a `project-metadata.toml` based on the ProjectMetadata provided to the destination path. +func WriteProjectMetadata(dstPath string, metadata platform.ProjectMetadata, os string) ContainerOperation { + return func(ctrClient DockerClient, ctx context.Context, containerID string, stdout, stderr io.Writer) error { + return writeToml(ctrClient, ctx, metadata, dstPath, containerID, os, stdout, stderr) } } // WriteStackToml writes a `stack.toml` based on the StackMetadata provided to the destination path. func WriteStackToml(dstPath string, stack builder.StackMetadata, os string) ContainerOperation { return func(ctrClient DockerClient, ctx context.Context, containerID string, stdout, stderr io.Writer) error { - buf := &bytes.Buffer{} - err := toml.NewEncoder(buf).Encode(stack) - if err != nil { - return errors.Wrap(err, "marshaling stack metadata") - } - - tarBuilder := archive.TarBuilder{} - - tarPath := dstPath - if os == "windows" { - tarPath = paths.WindowsToSlash(dstPath) - } - - tarBuilder.AddFile(tarPath, 0755, archive.NormalizedDateTime, buf.Bytes()) - reader := tarBuilder.Reader(archive.DefaultTarWriterFactory()) - defer reader.Close() - - if os == "windows" { - dirName := paths.WindowsDir(dstPath) - return copyDirWindows(ctx, ctrClient, containerID, reader, dirName, stdout, stderr) - } - - return ctrClient.CopyToContainer(ctx, containerID, "/", reader, types.CopyToContainerOptions{}) + return writeToml(ctrClient, ctx, stack, dstPath, containerID, os, stdout, stderr) } } // WriteRunToml writes a `run.toml` based on the RunConfig provided to the destination path. func WriteRunToml(dstPath string, runImages []builder.RunImageMetadata, os string) ContainerOperation { + runImageData := builder.RunImages{ + Images: runImages, + } return func(ctrClient DockerClient, ctx context.Context, containerID string, stdout, stderr io.Writer) error { - buf := &bytes.Buffer{} - err := toml.NewEncoder(buf).Encode(builder.RunImages{ - Images: runImages, - }) - if err != nil { - return errors.Wrap(err, "marshaling run metadata") - } - - tarBuilder := archive.TarBuilder{} - - tarPath := dstPath - if os == "windows" { - tarPath = paths.WindowsToSlash(dstPath) - } - - tarBuilder.AddFile(tarPath, 0755, archive.NormalizedDateTime, buf.Bytes()) - reader := tarBuilder.Reader(archive.DefaultTarWriterFactory()) - defer reader.Close() - - if os == "windows" { - dirName := paths.WindowsDir(dstPath) - return copyDirWindows(ctx, ctrClient, containerID, reader, dirName, stdout, stderr) - } - - return ctrClient.CopyToContainer(ctx, containerID, "/", reader, types.CopyToContainerOptions{}) + return writeToml(ctrClient, ctx, runImageData, dstPath, containerID, os, stdout, stderr) } } diff --git a/internal/build/container_ops_test.go b/internal/build/container_ops_test.go index 5ba9b882f3..a83c266f82 100644 --- a/internal/build/container_ops_test.go +++ b/internal/build/container_ops_test.go @@ -646,6 +646,7 @@ drwxr-xr-x 2 123 456 (.*) some-vol `) }) }) + when("#EnsureVolumeAccess", func() { it("changes owner of volume", func() { h.SkipIf(t, osType != "windows", "no-op for linux")