17813: Refactor singularity image loading / caching / conversion
[arvados.git] / lib / crunchrun / docker.go
index a39b754b3d396e4c02c75a518d47940dab9212f7..10a2dd38e8109182d1f41e9f1364a54e8b11d67a 100644 (file)
@@ -11,6 +11,7 @@ import (
        "strings"
        "time"
 
+       "git.arvados.org/arvados.git/sdk/go/arvados"
        dockertypes "github.com/docker/docker/api/types"
        dockercontainer "github.com/docker/docker/api/types/container"
        dockerclient "github.com/docker/docker/client"
@@ -45,12 +46,16 @@ func newDockerExecutor(containerUUID string, logf func(string, ...interface{}),
        }, err
 }
 
-func (e *dockerExecutor) ImageLoaded(imageID string) bool {
+func (e *dockerExecutor) LoadImage(imageID string, container arvados.Container, arvMountPoint string,
+       containerClient *arvados.Client, keepClient IKeepClient) error {
        _, _, err := e.dockerclient.ImageInspectWithRaw(context.TODO(), imageID)
-       return err == nil
-}
+       if err == nil {
+               // already loaded
+               return nil
+       }
+
+       filename := arvMountPoint + "/by_id/" + container.ContainerImage + "/" + imageID + ".tar"
 
-func (e *dockerExecutor) LoadImage(filename string) error {
        f, err := os.Open(filename)
        if err != nil {
                return err
@@ -186,7 +191,7 @@ func (e *dockerExecutor) Wait(ctx context.Context) (int, error) {
        }
 }
 
-func (e *dockerExecutor) startIO(stdin io.ReadCloser, stdout, stderr io.WriteCloser) error {
+func (e *dockerExecutor) startIO(stdin io.Reader, stdout, stderr io.Writer) error {
        resp, err := e.dockerclient.ContainerAttach(context.TODO(), e.containerID, dockertypes.ContainerAttachOptions{
                Stream: true,
                Stdin:  stdin != nil,
@@ -213,8 +218,7 @@ func (e *dockerExecutor) startIO(stdin io.ReadCloser, stdout, stderr io.WriteClo
        return nil
 }
 
-func (e *dockerExecutor) handleStdin(stdin io.ReadCloser, conn io.Writer, closeConn func() error) error {
-       defer stdin.Close()
+func (e *dockerExecutor) handleStdin(stdin io.Reader, conn io.Writer, closeConn func() error) error {
        defer closeConn()
        _, err := io.Copy(conn, stdin)
        if err != nil {
@@ -225,7 +229,7 @@ func (e *dockerExecutor) handleStdin(stdin io.ReadCloser, conn io.Writer, closeC
 
 // Handle docker log protocol; see
 // https://docs.docker.com/engine/reference/api/docker_remote_api_v1.15/#attach-to-a-container
-func (e *dockerExecutor) handleStdoutStderr(stdout, stderr io.WriteCloser, reader io.Reader) error {
+func (e *dockerExecutor) handleStdoutStderr(stdout, stderr io.Writer, reader io.Reader) error {
        header := make([]byte, 8)
        var err error
        for err == nil {
@@ -247,14 +251,6 @@ func (e *dockerExecutor) handleStdoutStderr(stdout, stderr io.WriteCloser, reade
        if err != nil {
                return fmt.Errorf("error copying stdout/stderr from docker: %v", err)
        }
-       err = stdout.Close()
-       if err != nil {
-               return fmt.Errorf("error writing stdout: close: %v", err)
-       }
-       err = stderr.Close()
-       if err != nil {
-               return fmt.Errorf("error writing stderr: close: %v", err)
-       }
        return nil
 }