From ef4e7c2da8f7b77a3c30d8a350b8f9948367c3f1 Mon Sep 17 00:00:00 2001 From: y_uuki Date: Sun, 16 Jul 2017 02:24:54 +0900 Subject: [PATCH] Use Env of docker image insetad of printenv to generate drootenv --- docker/docker.go | 17 +++++++++++++---- docker/docker_test.go | 7 +++++++ docker/testing.go | 15 ++++++++++----- 3 files changed, 30 insertions(+), 9 deletions(-) diff --git a/docker/docker.go b/docker/docker.go index f6bacd9..1e6bbfa 100644 --- a/docker/docker.go +++ b/docker/docker.go @@ -1,20 +1,22 @@ package docker import ( + "fmt" "io" + "strings" "github.com/docker/docker/api/types" "github.com/docker/docker/api/types/container" "github.com/docker/docker/api/types/network" "github.com/docker/docker/client" "github.com/pkg/errors" - "golang.org/x/net/context" // docker/docker don't use 'context' as standard package. - "github.com/yuuki/droot/environ" + "golang.org/x/net/context" // docker/docker don't use 'context' as standard package. ) // dockerAPI is an interface for stub testing. type dockerAPI interface { + ImageInspectWithRaw(ctx context.Context, imageID string) (types.ImageInspect, []byte, error) ContainerCreate(ctx context.Context, config *container.Config, hostConfig *container.HostConfig, networkingConfig *network.NetworkingConfig, containerName string) (container.ContainerCreateCreatedBody, error) ContainerStart(ctx context.Context, containerID string, options types.ContainerStartOptions) error ContainerWait(ctx context.Context, containerID string) (int64, error) @@ -44,10 +46,17 @@ func New() (*Client, error) { func (c *Client) ExportImage(imageID string) (io.ReadCloser, error) { ctx := context.Background() + image, _, err := c.docker.ImageInspectWithRaw(ctx, imageID) + if err != nil { + return nil, errors.Wrapf(err, "Failed to inspect image imageID:%s", imageID) + } + + //Put drootenv file into the filesystem. + cmd := fmt.Sprintf("echo \"%s\" > %s", strings.Join(image.ContainerConfig.Env, "\n"), environ.DROOT_ENV_FILE_PATH) container, err := c.docker.ContainerCreate(ctx, &container.Config{ Image: imageID, - Entrypoint: []string{"/bin/sh"}, // Clear the exising entrypoint - Cmd: []string{"-c", "printenv", ">", environ.DROOT_ENV_FILE_PATH}, + Entrypoint: []string{""}, // Clear the exising entrypoint + Cmd: []string{"/bin/sh", "-c", cmd}, }, nil, nil, "") if err != nil { return nil, errors.Wrapf(err, "Failed to create container imageID:%s", imageID) diff --git a/docker/docker_test.go b/docker/docker_test.go index e7208bb..c155142 100644 --- a/docker/docker_test.go +++ b/docker/docker_test.go @@ -17,6 +17,13 @@ func TestExportImage(t *testing.T) { containerID := "container ID" fakeClient := &fakeDocker{ + FakeImageInspectWithRaw: func(ctx context.Context, imageID string) (types.ImageInspect, []byte, error) { + return types.ImageInspect{ + ContainerConfig: &container.Config{ + Env: []string{"PATH=/usr/bin:/sbin:/bin"}, + }, + }, []byte{}, nil + }, FakeContainerCreate: func(ctx context.Context, config *container.Config, hostConfig *container.HostConfig, networkingConfig *network.NetworkingConfig, containerName string) (container.ContainerCreateCreatedBody, error) { return container.ContainerCreateCreatedBody{ID: containerID}, nil }, diff --git a/docker/testing.go b/docker/testing.go index 8b7a1a7..5ef2fae 100644 --- a/docker/testing.go +++ b/docker/testing.go @@ -11,11 +11,16 @@ import ( type fakeDocker struct { dockerAPI - FakeContainerCreate func(ctx context.Context, config *container.Config, hostConfig *container.HostConfig, networkingConfig *network.NetworkingConfig, containerName string) (container.ContainerCreateCreatedBody, error) - FakeContainerStart func(ctx context.Context, containerID string, options types.ContainerStartOptions) error - FakeContainerWait func(ctx context.Context, containerID string) (int64, error) - FakeContainerExport func(ctx context.Context, containerID string) (io.ReadCloser, error) - FakeContainerRemove func(ctx context.Context, containerID string, options types.ContainerRemoveOptions) error + FakeImageInspectWithRaw func(ctx context.Context, imageID string) (types.ImageInspect, []byte, error) + FakeContainerCreate func(ctx context.Context, config *container.Config, hostConfig *container.HostConfig, networkingConfig *network.NetworkingConfig, containerName string) (container.ContainerCreateCreatedBody, error) + FakeContainerStart func(ctx context.Context, containerID string, options types.ContainerStartOptions) error + FakeContainerWait func(ctx context.Context, containerID string) (int64, error) + FakeContainerExport func(ctx context.Context, containerID string) (io.ReadCloser, error) + FakeContainerRemove func(ctx context.Context, containerID string, options types.ContainerRemoveOptions) error +} + +func (d *fakeDocker) ImageInspectWithRaw(ctx context.Context, imageID string) (types.ImageInspect, []byte, error) { + return d.FakeImageInspectWithRaw(ctx, imageID) } func (d *fakeDocker) ContainerRemove(ctx context.Context, containerID string, options types.ContainerRemoveOptions) error {