From 401dd3d8e02f720e5e3b2a0f337a57af41fa29c4 Mon Sep 17 00:00:00 2001 From: Solomon Hykes Date: Fri, 25 Jan 2013 11:23:18 -0800 Subject: [PATCH] Moved more utilities to docker/future --- dockerd/dockerd.go | 28 ++++++++++------------------ fake/fake.go | 16 ---------------- future/future.go | 24 ++++++++++++++++++++++++ 3 files changed, 34 insertions(+), 34 deletions(-) diff --git a/dockerd/dockerd.go b/dockerd/dockerd.go index fe011b1752..eb1923e7f1 100644 --- a/dockerd/dockerd.go +++ b/dockerd/dockerd.go @@ -286,7 +286,7 @@ func (docker *Docker) addContainer(name string, source string, size uint) *Conta size = fake.RandomContainerSize() } c := &Container{ - Id: fake.RandomId(), + Id: future.RandomId(), Name: name, Created: time.Now(), Source: source, @@ -389,7 +389,7 @@ func startCommand(cmd *exec.Cmd, interactive bool) (io.WriteCloser, io.ReadClose func main() { - fake.Seed() + future.Seed() flag.Parse() docker := New() go func() { @@ -450,14 +450,6 @@ func (docker *Docker) CmdWeb(stdin io.ReadCloser, stdout io.Writer, args ...stri } -func Go(f func() error) chan error { - ch := make(chan error) - go func() { - ch <- f() - }() - return ch -} - type Docker struct { containers map[string]*Container containersByName map[string]*ByDate @@ -488,26 +480,26 @@ func (c *Container) Run(command string, args []string, stdin io.ReadCloser, stdo // Reset logs c.stdoutLog.Reset() c.stdinLog.Reset() - c.Running = true cmd := exec.Command(c.Cmd, c.Args...) - cmd_stdin, cmd_stdout, err := startCommand(cmd, true) - // ADD FAKE RANDOM CHANGES - c.FilesChanged = fake.RandomFilesChanged() - c.BytesChanged = fake.RandomBytesChanged() + cmd_stdin, cmd_stdout, err := startCommand(cmd, tty) if err != nil { return err } - copy_out := Go(func() error { + c.Running = true + // ADD FAKE RANDOM CHANGES + c.FilesChanged = fake.RandomFilesChanged() + c.BytesChanged = fake.RandomBytesChanged() + copy_out := future.Go(func() error { _, err := io.Copy(io.MultiWriter(stdout, c.stdoutLog), cmd_stdout) return err }) - Go(func() error { + future.Go(func() error { _, err := io.Copy(io.MultiWriter(cmd_stdin, c.stdinLog), stdin) cmd_stdin.Close() stdin.Close() return err }) - wait := Go(func() error { + wait := future.Go(func() error { err := cmd.Wait() c.Running = false return err diff --git a/fake/fake.go b/fake/fake.go index df2731a8a3..0e0de77507 100644 --- a/fake/fake.go +++ b/fake/fake.go @@ -1,22 +1,12 @@ package fake import ( - "github.com/dotcloud/docker/future" "bytes" "math/rand" - "time" "io" "archive/tar" - "fmt" ) -func Seed() { - rand.Seed(time.Now().UTC().UnixNano()) -} - -func randomBytes() io.Reader { - return bytes.NewBuffer([]byte(fmt.Sprintf("%x", rand.Int()))) -} func FakeTar() (io.Reader, error) { content := []byte("Hello world!\n") @@ -46,12 +36,6 @@ func WriteFakeTar(dst io.Writer) error { } -func RandomId() string { - id, _ := future.ComputeId(randomBytes()) // can't fail - return id -} - - func RandomBytesChanged() uint { return uint(rand.Int31n(24 * 1024 * 1024)) } diff --git a/future/future.go b/future/future.go index 3b33e7754d..a0efacc03c 100644 --- a/future/future.go +++ b/future/future.go @@ -5,8 +5,14 @@ import ( "io" "fmt" "time" + "bytes" + "math/rand" ) +func Seed() { + rand.Seed(time.Now().UTC().UnixNano()) +} + func ComputeId(content io.Reader) (string, error) { h := sha256.New() if _, err := io.Copy(h, content); err != nil { @@ -37,3 +43,21 @@ func HumanDuration(d time.Duration) string { } return fmt.Sprintf("%d years", d.Hours() / 24 / 365) } + +func randomBytes() io.Reader { + return bytes.NewBuffer([]byte(fmt.Sprintf("%x", rand.Int()))) +} + +func RandomId() string { + id, _ := ComputeId(randomBytes()) // can't fail + return id +} + +func Go(f func() error) chan error { + ch := make(chan error) + go func() { + ch <- f() + }() + return ch +} +