Moved more utilities to docker/future

This commit is contained in:
Solomon Hykes 2013-01-25 11:23:18 -08:00
parent f3ffba7afe
commit 401dd3d8e0
3 changed files with 34 additions and 34 deletions

View file

@ -286,7 +286,7 @@ func (docker *Docker) addContainer(name string, source string, size uint) *Conta
size = fake.RandomContainerSize() size = fake.RandomContainerSize()
} }
c := &Container{ c := &Container{
Id: fake.RandomId(), Id: future.RandomId(),
Name: name, Name: name,
Created: time.Now(), Created: time.Now(),
Source: source, Source: source,
@ -389,7 +389,7 @@ func startCommand(cmd *exec.Cmd, interactive bool) (io.WriteCloser, io.ReadClose
func main() { func main() {
fake.Seed() future.Seed()
flag.Parse() flag.Parse()
docker := New() docker := New()
go func() { 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 { type Docker struct {
containers map[string]*Container containers map[string]*Container
containersByName map[string]*ByDate containersByName map[string]*ByDate
@ -488,26 +480,26 @@ func (c *Container) Run(command string, args []string, stdin io.ReadCloser, stdo
// Reset logs // Reset logs
c.stdoutLog.Reset() c.stdoutLog.Reset()
c.stdinLog.Reset() c.stdinLog.Reset()
c.Running = true
cmd := exec.Command(c.Cmd, c.Args...) cmd := exec.Command(c.Cmd, c.Args...)
cmd_stdin, cmd_stdout, err := startCommand(cmd, true) cmd_stdin, cmd_stdout, err := startCommand(cmd, tty)
// ADD FAKE RANDOM CHANGES
c.FilesChanged = fake.RandomFilesChanged()
c.BytesChanged = fake.RandomBytesChanged()
if err != nil { if err != nil {
return err 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) _, err := io.Copy(io.MultiWriter(stdout, c.stdoutLog), cmd_stdout)
return err return err
}) })
Go(func() error { future.Go(func() error {
_, err := io.Copy(io.MultiWriter(cmd_stdin, c.stdinLog), stdin) _, err := io.Copy(io.MultiWriter(cmd_stdin, c.stdinLog), stdin)
cmd_stdin.Close() cmd_stdin.Close()
stdin.Close() stdin.Close()
return err return err
}) })
wait := Go(func() error { wait := future.Go(func() error {
err := cmd.Wait() err := cmd.Wait()
c.Running = false c.Running = false
return err return err

View file

@ -1,22 +1,12 @@
package fake package fake
import ( import (
"github.com/dotcloud/docker/future"
"bytes" "bytes"
"math/rand" "math/rand"
"time"
"io" "io"
"archive/tar" "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) { func FakeTar() (io.Reader, error) {
content := []byte("Hello world!\n") 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 { func RandomBytesChanged() uint {
return uint(rand.Int31n(24 * 1024 * 1024)) return uint(rand.Int31n(24 * 1024 * 1024))
} }

View file

@ -5,8 +5,14 @@ import (
"io" "io"
"fmt" "fmt"
"time" "time"
"bytes"
"math/rand"
) )
func Seed() {
rand.Seed(time.Now().UTC().UnixNano())
}
func ComputeId(content io.Reader) (string, error) { func ComputeId(content io.Reader) (string, error) {
h := sha256.New() h := sha256.New()
if _, err := io.Copy(h, content); err != nil { 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) 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
}