-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
replace go-dockerclient with the official one
- Loading branch information
Showing
11 changed files
with
505 additions
and
295 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
package state | ||
|
||
import ( | ||
"github.com/docker/docker/api/types" | ||
"golang.org/x/net/context" | ||
) | ||
|
||
func (state *State) EnsureCleanup(ctx context.Context) { | ||
if state.ContainerID == "" { | ||
return | ||
} | ||
client, err := state.EnsureClient(ctx) | ||
if err != nil { | ||
return | ||
} | ||
config, err := state.EnsureConfig(ctx) | ||
if err != nil { | ||
return | ||
} | ||
if config.Remove != nil && !*config.Remove { | ||
return | ||
} | ||
|
||
client.ContainerRemove( | ||
ctx, | ||
state.ContainerID, | ||
types.ContainerRemoveOptions{ | ||
RemoveVolumes: true, | ||
RemoveLinks: true, | ||
Force: true, | ||
}, | ||
) | ||
state.ContainerID = "" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,17 +1,18 @@ | ||
package state | ||
|
||
import ( | ||
docker "github.com/fsouza/go-dockerclient" | ||
"github.com/docker/docker/client" | ||
"golang.org/x/net/context" | ||
) | ||
|
||
func (state *state) ensureClient() error { | ||
func (state *State) EnsureClient(ctx context.Context) (*client.Client, error) { | ||
if state.Client != nil { | ||
return nil | ||
return state.Client, nil | ||
} | ||
client, err := docker.NewClientFromEnv() | ||
client, err := client.NewEnvClient() | ||
if err != nil { | ||
return err | ||
return nil, err | ||
} | ||
state.Client = client | ||
return nil | ||
return client, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
package state | ||
|
||
import ( | ||
"github.com/docker/docker/api/types/container" | ||
"github.com/docker/docker/api/types/network" | ||
"golang.org/x/net/context" | ||
) | ||
|
||
func (state *State) EnsureContainer(ctx context.Context) (string, error) { | ||
if state.ContainerID != "" { | ||
return state.ContainerID, nil | ||
} | ||
client, err := state.EnsureClient(ctx) | ||
if err != nil { | ||
return "", err | ||
} | ||
config, err := state.EnsureConfig(ctx) | ||
if err != nil { | ||
return "", err | ||
} | ||
image, err := state.EnsureImage(ctx) | ||
if err != nil { | ||
return "", err | ||
} | ||
|
||
response, err := client.ContainerCreate( | ||
ctx, | ||
&container.Config{ | ||
User: config.User, | ||
AttachStdin: true, | ||
AttachStdout: true, | ||
AttachStderr: true, | ||
Tty: true, | ||
OpenStdin: true, | ||
StdinOnce: true, | ||
Env: config.Environment, | ||
Cmd: state.Options.Arguments, | ||
Image: image, | ||
WorkingDir: config.WorkingDir, | ||
Entrypoint: state.getEntrypoint(), | ||
}, | ||
&container.HostConfig{ | ||
AutoRemove: true, | ||
Binds: config.Volumes, | ||
VolumesFrom: config.VolumesFrom, | ||
}, | ||
&network.NetworkingConfig{}, | ||
config.ContainerName, | ||
) | ||
if err != nil { | ||
return "", err | ||
} | ||
state.ContainerID = response.ID | ||
return state.ContainerID, nil | ||
} | ||
|
||
func (state *State) getEntrypoint() []string { | ||
entrypoint := []string{"/bin/sh"} | ||
if len(state.Config.Interpreter) > 0 { | ||
entrypoint = state.Config.Interpreter | ||
} | ||
if !state.Options.Interactive { | ||
entrypoint = append(entrypoint, state.Entrypoint) | ||
} | ||
return entrypoint | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,115 @@ | ||
package state | ||
|
||
import ( | ||
"io" | ||
"os" | ||
|
||
"github.com/docker/docker/api/types" | ||
"github.com/docker/docker/api/types/container" | ||
"github.com/docker/docker/pkg/term" | ||
"golang.org/x/net/context" | ||
) | ||
|
||
func (state *State) EnsureRun(ctx context.Context) error { | ||
client, err := state.EnsureClient(ctx) | ||
if err != nil { | ||
return err | ||
} | ||
containerID, err := state.EnsureContainer(ctx) | ||
if err != nil { | ||
return err | ||
} | ||
defer state.EnsureCleanup(ctx) | ||
err = state.EnsureEntrypoint(ctx) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
attach, err := client.ContainerAttach( | ||
ctx, | ||
containerID, | ||
types.ContainerAttachOptions{ | ||
Stream: true, | ||
Stdin: true, | ||
Stdout: true, | ||
Stderr: true, | ||
Logs: true, | ||
}, | ||
) | ||
if err != nil { | ||
return err | ||
} | ||
defer attach.Close() | ||
|
||
streamErrorChannel := make(chan error, 1) | ||
go func() { | ||
inFd, _ := term.GetFdInfo(os.Stdin) | ||
inState, err := term.SetRawTerminal(inFd) | ||
if err != nil { | ||
streamErrorChannel <- err | ||
return | ||
} | ||
defer term.RestoreTerminal(inFd, inState) | ||
|
||
outFd, _ := term.GetFdInfo(os.Stdout) | ||
outState, err := term.SetRawTerminal(outFd) | ||
if err != nil { | ||
streamErrorChannel <- err | ||
return | ||
} | ||
defer term.RestoreTerminal(outFd, outState) | ||
|
||
outputDone := make(chan error) | ||
go func() { | ||
_, err := io.Copy(os.Stdout, attach.Reader) | ||
outputDone <- err | ||
}() | ||
|
||
inputDone := make(chan struct{}) | ||
go func() { | ||
io.Copy(attach.Conn, os.Stdin) | ||
attach.CloseWrite() | ||
close(inputDone) | ||
}() | ||
|
||
select { | ||
case err := <-outputDone: | ||
streamErrorChannel <- err | ||
case <-inputDone: | ||
select { | ||
case err := <-outputDone: | ||
streamErrorChannel <- err | ||
case <-ctx.Done(): | ||
streamErrorChannel <- ctx.Err() | ||
} | ||
case <-ctx.Done(): | ||
streamErrorChannel <- ctx.Err() | ||
} | ||
}() | ||
|
||
waitChannel, waitErrorChannel := client.ContainerWait( | ||
ctx, | ||
containerID, | ||
container.WaitConditionRemoved, | ||
) | ||
|
||
err = client.ContainerStart( | ||
ctx, | ||
containerID, | ||
types.ContainerStartOptions{}, | ||
) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
if err := <-streamErrorChannel; err != nil { | ||
return err | ||
} | ||
|
||
select { | ||
case _ = <-waitChannel: | ||
return nil | ||
case err := <-waitErrorChannel: | ||
return err | ||
} | ||
} |
Oops, something went wrong.