Skip to content

Commit

Permalink
some more organizing files
Browse files Browse the repository at this point in the history
  • Loading branch information
oclaussen committed Apr 5, 2018
1 parent 9dfec6c commit 2e107af
Show file tree
Hide file tree
Showing 5 changed files with 204 additions and 0 deletions.
30 changes: 30 additions & 0 deletions container/create.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package container

import (
"github.com/oclaussen/dodo/config"
docker "github.com/fsouza/go-dockerclient"
)

func CreateContainer(client *docker.Client, image string, config *config.CommandConfig) (*docker.Container, error) {
return client.CreateContainer(docker.CreateContainerOptions{
Name: config.ContainerName,
Config: &docker.Config{
User: config.User,
Env: config.Environment, // TODO: support env_file
Cmd: []string{}, // TODO: command
Image: image,
WorkingDir: config.WorkingDir,
Entrypoint: []string{"/bin/sh"}, // TODO: entrypoint
AttachStdin: true,
AttachStdout: true,
AttachStderr: true,
Tty: true,
OpenStdin: true,
StdinOnce: true,
},
HostConfig: &docker.HostConfig{
Binds: []string{}, // TODO: bind mounts
VolumesFrom: config.VolumesFrom,
},
})
}
13 changes: 13 additions & 0 deletions container/remove.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package container

import (
docker "github.com/fsouza/go-dockerclient"
)

func RemoveContainer(client *docker.Client, container *docker.Container) error {
return client.RemoveContainer(docker.RemoveContainerOptions{
ID: container.ID,
RemoveVolumes: true,
Force: true,
})
}
41 changes: 41 additions & 0 deletions container/run.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package container

import (
"os"

"github.com/docker/docker/pkg/term"
docker "github.com/fsouza/go-dockerclient"
)

func RunContainer(client *docker.Client, container *docker.Container) error {
_, err := client.AttachToContainerNonBlocking(docker.AttachToContainerOptions{
Container: container.ID,
InputStream: os.Stdin,
OutputStream: os.Stdout,
ErrorStream: os.Stderr,
RawTerminal: true,
Stream: true,
Stdin: true,
Stdout: true,
Stderr: true,
})
if err != nil {
return err
}

inFd, _ := term.GetFdInfo(os.Stdin)
state, err := term.SetRawTerminal(inFd)
if err != nil {
return err
}
defer term.RestoreTerminal(inFd, state)

err = client.StartContainer(container.ID, nil)
_, err = client.WaitContainer(container.ID)
// TODO: handle exit code
if err != nil {
return err
}

return nil
}
69 changes: 69 additions & 0 deletions image/build.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
package image

import (
"io"
"os"
"encoding/json"
"errors"

"github.com/oclaussen/dodo/config"
"github.com/docker/docker/api/types"
"github.com/docker/docker/pkg/jsonmessage"
"github.com/docker/docker/pkg/term"
docker "github.com/fsouza/go-dockerclient"
)

func BuildImage(client *docker.Client, config *config.CommandConfig) (string, error) {
args := []docker.BuildArg{}
for key, value := range config.Build.Args {
args = append(args, docker.BuildArg{Name: key, Value: *value})
}

authConfigs, err := docker.NewAuthConfigurationsFromDockerCfg()
if err != nil {
return "", err
}

rpipe, wpipe := io.Pipe()
defer rpipe.Close()

image := ""
aux := func(auxJSON *json.RawMessage) {
var result types.BuildResult
// TODO: handle parse error
if err := json.Unmarshal(*auxJSON, &result); err == nil {
image = result.ID
}
}

errChan := make(chan error)
go func() {
outFd, isTerminal := term.GetFdInfo(os.Stdout)
errChan <- jsonmessage.DisplayJSONMessagesStream(rpipe, os.Stdout, outFd, isTerminal, aux)
}()

err = client.BuildImage(docker.BuildImageOptions{
Dockerfile: config.Build.Dockerfile,
NoCache: false, // TODO no cache mode
CacheFrom: []string{}, // TODO implement cache_from
SuppressOutput: false, // TODO: quiet mode
Pull: true, // TODO: force pull option
RmTmpContainer: true,
RawJSONStream: true,
OutputStream: wpipe,
AuthConfigs: *authConfigs,
ContextDir: config.Build.Context,
BuildArgs: args,
})

wpipe.Close()
if err != nil {
<-errChan
return "", err
}

if image == "" {
return "", errors.New("Build complete, but the server did not send an image id.")
}
return image, <-errChan
}
51 changes: 51 additions & 0 deletions image/pull.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package image

import (
"io"
"os"

"github.com/oclaussen/dodo/config"
"github.com/docker/docker/pkg/jsonmessage"
"github.com/docker/docker/pkg/term"
"github.com/docker/distribution/reference"
docker "github.com/fsouza/go-dockerclient"
)

func PullImage(client *docker.Client, config *config.CommandConfig) (string, error) {
// TODO: validate that the image is actually normalized named
ref, err := reference.ParseNormalizedNamed(config.Image)
if err != nil {
return "", err
}
tagged := reference.TagNameOnly(ref).(reference.Tagged)

authConfigs, err := docker.NewAuthConfigurationsFromDockerCfg()
if err != nil {
return "", err
}
authConfig := authConfigs.Configs[reference.Domain(ref)]

rpipe, wpipe := io.Pipe()
defer rpipe.Close()

errChan := make(chan error)
go func() {
outFd, isTerminal := term.GetFdInfo(os.Stdout)
errChan <- jsonmessage.DisplayJSONMessagesStream(rpipe, os.Stdout, outFd, isTerminal, nil)
}()

err = client.PullImage(docker.PullImageOptions{
Repository: ref.Name(),
Tag: tagged.Tag(),
OutputStream: wpipe,
RawJSONStream: true,
}, authConfig)

wpipe.Close()
if err != nil {
<-errChan
return "", err
}

return tagged.String(), <-errChan
}

0 comments on commit 2e107af

Please sign in to comment.