-
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.
- Loading branch information
Showing
5 changed files
with
204 additions
and
0 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,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, | ||
}, | ||
}) | ||
} |
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,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, | ||
}) | ||
} |
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,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 | ||
} |
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,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 | ||
} |
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,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 | ||
} |