Skip to content

Commit

Permalink
also refactored out a container module, and got rid of the state comp…
Browse files Browse the repository at this point in the history
…letely
  • Loading branch information
oclaussen committed Apr 8, 2018
1 parent 294f3ab commit c6f54a6
Show file tree
Hide file tree
Showing 8 changed files with 107 additions and 202 deletions.
45 changes: 45 additions & 0 deletions pkg/container/container.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package container

import (
"errors"

"github.com/docker/docker/client"
"golang.org/x/net/context"
)

// Options represents the configuration for running a docker container to
// be used as backdrop.
type Options struct {
Client *client.Client
Image string
Name string
Interactive bool
Interpreter []string
Entrypoint string
Script string
Command []string
Environment []string
Volumes []string
VolumesFrom []string
User string
WorkingDir string
}

// Run runs a docker container as backdrop.
func Run(ctx context.Context, options Options) error {
if options.Client == nil {
return errors.New("client may not be nil")
}

containerID, err := createContainer(ctx, options)
if err != nil {
return err
}

err = uploadEntrypoint(ctx, containerID, options)
if err != nil {
return err
}

return runContainer(ctx, containerID, options)
}
49 changes: 49 additions & 0 deletions pkg/container/create.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package container

import (
"github.com/docker/docker/api/types/container"
"github.com/docker/docker/api/types/network"
"golang.org/x/net/context"
)

func createContainer(ctx context.Context, options Options) (string, error) {
response, err := options.Client.ContainerCreate(
ctx,
&container.Config{
User: options.User,
AttachStdin: true,
AttachStdout: true,
AttachStderr: true,
Tty: true,
OpenStdin: true,
StdinOnce: true,
Env: options.Environment,
Cmd: options.Command,
Image: options.Image,
WorkingDir: options.WorkingDir,
Entrypoint: getEntrypoint(options),
},
&container.HostConfig{
AutoRemove: true,
Binds: options.Volumes,
VolumesFrom: options.VolumesFrom,
},
&network.NetworkingConfig{},
options.Name,
)
if err != nil {
return "", err
}
return response.ID, nil
}

func getEntrypoint(options Options) []string {
entrypoint := []string{"/bin/sh"}
if len(options.Interpreter) > 0 {
entrypoint = options.Interpreter
}
if !options.Interactive {
entrypoint = append(entrypoint, options.Entrypoint)
}
return entrypoint
}
28 changes: 8 additions & 20 deletions pkg/state/entrypoint.go → pkg/container/entrypoint.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package state
package container

import (
"archive/tar"
Expand All @@ -9,19 +9,7 @@ import (
"golang.org/x/net/context"
)

// EnsureEntrypoint makes sure the entrypoint script ist uploaded to the
// container.
func (state *State) EnsureEntrypoint(ctx context.Context) error {
config := state.Config
client, err := state.EnsureClient()
if err != nil {
return err
}
container, err := state.EnsureContainer(ctx)
if err != nil {
return err
}

func uploadEntrypoint(ctx context.Context, containerID string, options Options) error {
reader, writer := io.Pipe()
defer func() {
if err := reader.Close(); err != nil {
Expand All @@ -30,9 +18,9 @@ func (state *State) EnsureEntrypoint(ctx context.Context) error {
}()

go func() {
err := client.CopyToContainer(
err := options.Client.CopyToContainer(
ctx,
container,
containerID,
"/",
reader,
types.CopyToContainerOptions{},
Expand All @@ -43,15 +31,15 @@ func (state *State) EnsureEntrypoint(ctx context.Context) error {
}()

tarWriter := tar.NewWriter(writer)
err = tarWriter.WriteHeader(&tar.Header{
Name: state.Entrypoint,
err := tarWriter.WriteHeader(&tar.Header{
Name: options.Entrypoint,
Mode: 0600,
Size: int64(len(config.Script)),
Size: int64(len(options.Script)),
})
if err != nil {
return err
}
_, err = tarWriter.Write([]byte(state.Config.Script))
_, err = tarWriter.Write([]byte(options.Script))
if err != nil {
return err
}
Expand Down
25 changes: 5 additions & 20 deletions pkg/state/container_run.go → pkg/container/run.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package state
package container

import (
"io"
Expand All @@ -11,23 +11,8 @@ import (
"golang.org/x/net/context"
)

// EnsureRun makes sure the command is performed.
func (state *State) EnsureRun(ctx context.Context) error {
client, err := state.EnsureClient()
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(
func runContainer(ctx context.Context, containerID string, options Options) error {
attach, err := options.Client.ContainerAttach(
ctx,
containerID,
types.ContainerAttachOptions{
Expand Down Expand Up @@ -101,13 +86,13 @@ func (state *State) EnsureRun(ctx context.Context) error {
}
}()

waitChannel, waitErrorChannel := client.ContainerWait(
waitChannel, waitErrorChannel := options.Client.ContainerWait(
ctx,
containerID,
container.WaitConditionRemoved,
)

err = client.ContainerStart(
err = options.Client.ContainerStart(
ctx,
containerID,
types.ContainerStartOptions{},
Expand Down
35 changes: 0 additions & 35 deletions pkg/state/cleanup.go

This file was deleted.

21 changes: 0 additions & 21 deletions pkg/state/client.go

This file was deleted.

58 changes: 0 additions & 58 deletions pkg/state/container_create.go

This file was deleted.

48 changes: 0 additions & 48 deletions pkg/state/state.go

This file was deleted.

0 comments on commit c6f54a6

Please sign in to comment.