Skip to content

Commit

Permalink
replace go-dockerclient with the official one
Browse files Browse the repository at this point in the history
  • Loading branch information
oclaussen committed Apr 5, 2018
1 parent 272168f commit 1b76aab
Show file tree
Hide file tree
Showing 11 changed files with 505 additions and 295 deletions.
34 changes: 34 additions & 0 deletions state/cleanup.go
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 = ""
}
13 changes: 7 additions & 6 deletions state/client.go
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
}
13 changes: 7 additions & 6 deletions state/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"path/filepath"

"github.com/oclaussen/dodo/config"
"golang.org/x/net/context"
"gopkg.in/yaml.v2"
)

Expand All @@ -22,26 +23,26 @@ var (
}
)

func (state *state) ensureConfig() error {
func (state *State) EnsureConfig(ctx context.Context) (*config.BackdropConfig, error) {
if state.Config != nil {
return nil
return state.Config, nil
}
if state.Options.Filename != "" {
config, err := findConfigInFile(state.Name, state.Options.Filename)
if err != nil {
return err
return nil, err
}
state.Options.UpdateConfiguration(config)
state.Config = config
return nil
return config, nil
}
config, err := findConfigAnywhere(state.Name)
if err != nil {
return err
return nil, err
}
state.Options.UpdateConfiguration(config)
state.Config = config
return nil
return config, nil
}

func findConfigDirectories() ([]string, error) {
Expand Down
77 changes: 0 additions & 77 deletions state/container.go

This file was deleted.

66 changes: 66 additions & 0 deletions state/container_create.go
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
}
115 changes: 115 additions & 0 deletions state/container_run.go
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
}
}

0 comments on commit 1b76aab

Please sign in to comment.