Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support More Docker Usage #87

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 8 additions & 3 deletions system/docker/contract.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ package docker

import (
"fmt"
"strings"

"github.com/docker/docker/api/types"
"github.com/docker/docker/api/types/container"
"github.com/docker/docker/api/types/mount"
Expand All @@ -10,7 +12,6 @@ import (
"github.com/viant/toolbox"
"github.com/viant/toolbox/secret"
"github.com/viant/toolbox/url"
"strings"
)

//RunRequest represents a docker runAdapter request
Expand All @@ -24,6 +25,7 @@ type RunRequest struct {
Ports map[string]string `description:"publish a container’s port(s) to the host, docker -p option"`
Workdir string `description:"working directory inside the container, docker -w option"`
Reuse bool `description:"reuse existing container if exists, otherwise always removes"`
Foreground bool `description:"wait for container to stop"`
Cmd []string
Entrypoint []string
types.ContainerCreateConfig `json:",inline" yaml:",inline"`
Expand Down Expand Up @@ -118,7 +120,10 @@ type StatusResponse struct {
}

//StartRequest start request
type StartRequest StatusRequest
type StartRequest struct {
StatusRequest
Foreground bool
}

//StartResponse represents docker start response
type StartResponse StopResponse
Expand Down Expand Up @@ -310,7 +315,7 @@ func (r *RemoveRequest) AsStatusRequest() *StatusRequest {

//StatusRequest returns status request
func (r *StartRequest) AsStatusRequest() *StatusRequest {
result := StatusRequest(*r)
result := StatusRequest((*r).StatusRequest)
return &result
}

Expand Down
58 changes: 51 additions & 7 deletions system/docker/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,21 @@ import (
"archive/tar"
"bytes"
"fmt"
"io"
"io/ioutil"
"log"
"path"
"strings"

"github.com/docker/docker/api/types"
"github.com/docker/docker/api/types/container"
"github.com/docker/docker/client"
"github.com/docker/docker/pkg/stdcopy"
"github.com/go-errors/errors"
"github.com/viant/endly"
"github.com/viant/endly/model/msg"
"github.com/viant/toolbox"
"github.com/viant/toolbox/url"
"io"
"io/ioutil"
"log"
"path"
"strings"
)

const (
Expand Down Expand Up @@ -111,7 +114,10 @@ func (s *service) run(context *endly.Context, request *RunRequest) (*RunResponse
return response, nil
}
_, err := s.start(context, &StartRequest{
IDs: []string{containerInfo.ID},
StatusRequest: StatusRequest{
IDs: []string{containerInfo.ID},
},
Foreground: request.Foreground,
})
return response, err
}
Expand Down Expand Up @@ -146,7 +152,10 @@ func (s *service) run(context *endly.Context, request *RunRequest) (*RunResponse
return nil, err
}
response.ContainerID = createResponse.ID
startRequest := &StartRequest{IDs: []string{createResponse.ID}}
startRequest := &StartRequest{
StatusRequest: StatusRequest{IDs: []string{createResponse.ID}},
Foreground: request.Foreground,
}

if _, err := s.start(context, startRequest); err != nil {
return nil, err
Expand Down Expand Up @@ -414,7 +423,42 @@ func (s *service) start(context *endly.Context, request *StartRequest) (*StartRe
if err = runAdapter(context, startRequest, nil); err != nil {
return nil, err
}

if !request.Foreground {
continue
}

err = func() error {
attachRequest := &ContainerAttachRequest{
Container: candidate.ID,
ContainerAttachOptions: types.ContainerAttachOptions{
Stream: true,
Stdout: true,
Stderr: true,
},
}

resp := new(types.HijackedResponse)
err := runAdapter(context, attachRequest, resp)
if err != nil {
return err
}

defer resp.Close()
stdout := new(strings.Builder)
stderr := new(strings.Builder)
_, err = stdcopy.StdCopy(stdout, stderr, resp.Reader)
context.Publish(msg.NewStdoutEvent("run", stdout.String()))
context.Publish(msg.NewStdoutEvent("run:stderr", stderr.String()))
return err

}()

if err != nil {
return nil, err
}
}

return response, nil
}

Expand Down