Skip to content

Commit

Permalink
Merge pull request #6 from crowley-io/develop
Browse files Browse the repository at this point in the history
Release 0.1.1-rc2
  • Loading branch information
LE ROUX Thomas committed Feb 1, 2016
2 parents 6ad7309 + 37489d1 commit a77a5de
Show file tree
Hide file tree
Showing 19 changed files with 171 additions and 247 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
/pack
/crowley-pack_*

/examples/go-hello/go-hello
/examples/node-hello/app.tar.gz
5 changes: 4 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,7 @@ clean:
install: pack
install -o root -g root -m 0755 pack /usr/local/bin/crowley-pack

.PHONY: clean
artifacts:
gox -osarch="linux/amd64" -output="crowley-pack_{{.OS}}-{{.Arch}}"

.PHONY: clean artifacts install test style lint pack
7 changes: 1 addition & 6 deletions compose/compose.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (
)

// Compose create a new image.
func Compose(client docker.Docker, configuration *configuration.Configuration) error {
func Compose(client docker.Docker, stream docker.LogStream, configuration *configuration.Configuration) error {

if err := configuration.Validate(); err != nil {
return err
Expand All @@ -32,7 +32,6 @@ func Compose(client docker.Docker, configuration *configuration.Configuration) e
NoCache: noCache,
}

stream := docker.NewLogStream()
err = client.Build(option, stream)

if newid := client.ImageID(name); newid != id {
Expand All @@ -42,9 +41,5 @@ func Compose(client docker.Docker, configuration *configuration.Configuration) e
}
}

if err3 := stream.Close(); err3 != nil && err == nil {
err = err3
}

return err
}
19 changes: 12 additions & 7 deletions compose/compose_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,11 @@ func TestCompose(t *testing.T) {
t.FailNow()
}

wireMock(d, o, nil)
ls := docker.NewLogStream()

err := Compose(d, c)
wireMock(d, ls, o, nil)

err := Compose(d, ls, c)

d.AssertExpectations(t)
assert.Nil(t, err)
Expand All @@ -35,8 +37,9 @@ func TestComposeWithConfigurationError(t *testing.T) {

c := &configuration.Configuration{}
d := &mocks.DockerMock{}
ls := docker.NewLogStream()

err := Compose(d, c)
err := Compose(d, ls, c)

d.AssertExpectations(t)
assert.NotNil(t, err)
Expand All @@ -55,18 +58,20 @@ func TestComposeOnError(t *testing.T) {
t.FailNow()
}

wireMock(d, o, e)
ls := docker.NewLogStream()

wireMock(d, ls, o, e)

err := Compose(d, c)
err := Compose(d, ls, c)

d.AssertExpectations(t)
assert.NotNil(t, err)
assert.Equal(t, e, err)

}

func wireMock(m *mocks.DockerMock, o docker.BuildOptions, err error) {
m.On("Build", o, docker.NewLogStream()).Return(err)
func wireMock(m *mocks.DockerMock, ls docker.LogStream, o docker.BuildOptions, err error) {
m.On("Build", o, ls).Return(err)
m.On("ImageID", o.Name).Return("26913aba19ca").Twice()
}

Expand Down
1 change: 1 addition & 0 deletions configuration/configuration.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ type Install struct {
Command string
Environment []string
Volumes []string
Links []string
}

// Compose contains compose runtime's configuration.
Expand Down
16 changes: 10 additions & 6 deletions docker/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,11 @@ import (

// Build options
const (
quiet = false
rm = true
forceRm = true
buildQuiet = false
buildRm = true
buildForceRm = true
buildMemory = 0
buildMemswap = 0
)

// BuildOptions contains the build configuration for the docker daemon.
Expand All @@ -29,13 +31,15 @@ func buildImageOptions(option BuildOptions, stream LogStream) api.BuildImageOpti
opts := api.BuildImageOptions{
Name: option.Name,
NoCache: option.NoCache,
SuppressOutput: quiet,
RmTmpContainer: rm,
ForceRmTmpContainer: forceRm,
SuppressOutput: buildQuiet,
RmTmpContainer: buildRm,
ForceRmTmpContainer: buildForceRm,
Pull: option.Pull,
OutputStream: stream.Decoder,
ContextDir: option.Directory,
RawJSONStream: rawJSONStream,
Memory: buildMemory,
Memswap: buildMemswap,
}

if auth := getAuth(); auth != nil {
Expand Down
10 changes: 5 additions & 5 deletions docker/json.go → docker/decoder.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,14 @@ type message struct {
ID string `json:"id,omitempty"`
}

func newStreamDecoderWrapper(out io.Writer, err io.Writer) *streamDecoderWrapper {
func newStreamDecoderWrapper(out io.Writer, err io.Writer, tty bool) io.WriteCloser {

reader, writer := io.Pipe()
decoder := json.NewDecoder(reader)

wrapper := &streamDecoderWrapper{
out: out, err: err, decoder: decoder,
reader: reader, writer: writer, tty: false,
reader: reader, writer: writer, tty: tty,
}

go wrapper.Flush()
Expand Down Expand Up @@ -135,10 +135,10 @@ func (w streamDecoderWrapper) Flush() {

if err := w.decoder.Decode(&m); err != nil {
if err != io.EOF {
fmt.Fprint(w.getErrWriter(), err)
fmt.Fprintln(w.getErrWriter(), err)
}
if err := w.reader.Close; err != nil {
fmt.Fprint(w.getErrWriter(), err)
if err := w.reader.Close(); err != nil {
fmt.Fprintln(w.getErrWriter(), err)
}
return
}
Expand Down
6 changes: 4 additions & 2 deletions docker/log.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,22 @@ import (
"io"
"os"

"github.com/andrew-d/go-termutil"
api "github.com/fsouza/go-dockerclient"
)

const (
rawJSONStream = true
)

// LogStream contains two io.Writer for respectively, stdout and stderr.
// LogStream contains two io.Writer for respectively, stdout and stderr, and also a JSON Decoder for the Docker API.
type LogStream struct {
Out io.Writer
Err io.Writer
Decoder io.WriteCloser
}

// Close will closes the JSON Decoder.
func (l LogStream) Close() error {
return l.Decoder.Close()
}
Expand All @@ -31,7 +33,7 @@ func (d docker) Logs(id string, stream LogStream) error {
func NewLogStream() LogStream {
out := os.Stdout
err := os.Stderr
decoder := newStreamDecoderWrapper(out, err)
decoder := newStreamDecoderWrapper(out, err, termutil.Isatty(out.Fd()))
return LogStream{Out: out, Err: err, Decoder: decoder}
}

Expand Down
22 changes: 15 additions & 7 deletions docker/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"fmt"
"strings"

parser "github.com/crowley-io/docker-parser"
api "github.com/fsouza/go-dockerclient"
)

Expand All @@ -25,12 +26,18 @@ type RunOptions struct {
Command string
Env []string
Volumes []string
Links []string
}

// See Docker interface
func (d docker) Run(option RunOptions, stream LogStream) (int, error) {

if err := d.client.PullImage(pullImageOptions(option, stream), pullAuthConfiguration(option)); err != nil {
r, err := parser.Parse(option.Image)
if err != nil {
return 0, err
}

if err := d.client.PullImage(pullImageOptions(r.Remote(), stream), pullAuthConfiguration(option)); err != nil {
return 0, err
}

Expand Down Expand Up @@ -66,9 +73,13 @@ func (d docker) Run(option RunOptions, stream LogStream) (int, error) {

}

func pullImageOptions(option RunOptions, stream LogStream) api.PullImageOptions {
func pullAuthConfiguration(option RunOptions) api.AuthConfiguration {
return getAuthWithImage(option.Image)
}

func pullImageOptions(remote string, stream LogStream) api.PullImageOptions {
return api.PullImageOptions{
Repository: option.Image,
Repository: remote,
OutputStream: stream.Decoder,
RawJSONStream: rawJSONStream,
}
Expand All @@ -89,6 +100,7 @@ func createContainerOptions(option RunOptions) api.CreateContainerOptions {
HostConfig: &api.HostConfig{
NetworkMode: hostNetworkMode,
Binds: option.Volumes,
Links: option.Links,
},
}
}
Expand All @@ -100,7 +112,3 @@ func removeContainerOptions(id string) api.RemoveContainerOptions {
Force: forceRemove,
}
}

func pullAuthConfiguration(option RunOptions) api.AuthConfiguration {
return getAuthWithImage(option.Image)
}
6 changes: 0 additions & 6 deletions install/env.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,12 +53,6 @@ func home() (h string) {
return
}

// Return user's working directory
func pwd() string {
p, _ := os.Getwd()
return p
}

func expandEnv(list []string) []string {

var env []string
Expand Down
9 changes: 0 additions & 9 deletions install/env_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,15 +38,6 @@ func TestHome(t *testing.T) {

}

func TestPwd(t *testing.T) {

p := pwd()

assert.NotEmpty(t, p)
assert.True(t, pathExist(p), "pwd path doesn't exist")

}

func TestGetEnv(t *testing.T) {

uid, gid := whoami()
Expand Down
9 changes: 3 additions & 6 deletions install/install.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (
)

// Install run compile instructions with a Docker container.
func Install(client docker.Docker, configuration *configuration.Configuration) error {
func Install(client docker.Docker, stream docker.LogStream, configuration *configuration.Configuration) error {

if err := configuration.Validate(); err != nil {
return err
Expand All @@ -34,21 +34,18 @@ func Install(client docker.Docker, configuration *configuration.Configuration) e
output := configuration.Install.Output
image := configuration.Install.Image
command := configuration.Install.Command
links := configuration.Install.Links

option := docker.RunOptions{
Image: image,
Command: command,
Env: env,
Volumes: volumes,
Links: links,
}

stream := docker.NewLogStream()
exit, err := client.Run(option, stream)

if err2 := stream.Close(); err2 != nil && err == nil {
err = err2
}

if err != nil {
return err
}
Expand Down
Loading

0 comments on commit a77a5de

Please sign in to comment.