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()
}
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

View file

@ -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))
}

View file

@ -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
}