mirror of
https://github.com/moby/moby.git
synced 2022-11-09 12:21:53 -05:00
moved GenerateId() to the graph package
This commit is contained in:
parent
d7c5d060c4
commit
0208b6accd
4 changed files with 22 additions and 32 deletions
|
@ -9,6 +9,7 @@ import (
|
||||||
"github.com/dotcloud/docker/future"
|
"github.com/dotcloud/docker/future"
|
||||||
"github.com/dotcloud/docker/rcli"
|
"github.com/dotcloud/docker/rcli"
|
||||||
"io"
|
"io"
|
||||||
|
"math/rand"
|
||||||
"net/http"
|
"net/http"
|
||||||
"net/url"
|
"net/url"
|
||||||
"path"
|
"path"
|
||||||
|
@ -795,7 +796,7 @@ func (srv *Server) CmdRun(stdin io.ReadCloser, stdout io.Writer, args ...string)
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewServer() (*Server, error) {
|
func NewServer() (*Server, error) {
|
||||||
future.Seed()
|
rand.Seed(time.Now().UTC().UnixNano())
|
||||||
// if err != nil {
|
// if err != nil {
|
||||||
// return nil, err
|
// return nil, err
|
||||||
// }
|
// }
|
||||||
|
|
|
@ -4,7 +4,6 @@ import (
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
"github.com/dotcloud/docker/future"
|
|
||||||
"github.com/dotcloud/docker/graph"
|
"github.com/dotcloud/docker/graph"
|
||||||
"github.com/kr/pty"
|
"github.com/kr/pty"
|
||||||
"io"
|
"io"
|
||||||
|
@ -66,8 +65,8 @@ type NetworkSettings struct {
|
||||||
}
|
}
|
||||||
|
|
||||||
func GenerateId() string {
|
func GenerateId() string {
|
||||||
future.Seed()
|
return graph.GenerateId() // Re-use the same code to generate container and image IDs
|
||||||
return future.RandomId()
|
// (this might change when image Ids become content-based)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (container *Container) Cmd() *exec.Cmd {
|
func (container *Container) Cmd() *exec.Cmd {
|
||||||
|
|
|
@ -1,35 +1,10 @@
|
||||||
package future
|
package future
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"bytes"
|
|
||||||
"crypto/sha256"
|
|
||||||
"fmt"
|
"fmt"
|
||||||
"io"
|
"io"
|
||||||
"math/rand"
|
|
||||||
"time"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
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 {
|
|
||||||
return "", err
|
|
||||||
}
|
|
||||||
return fmt.Sprintf("%x", h.Sum(nil)[:8]), nil
|
|
||||||
}
|
|
||||||
|
|
||||||
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 {
|
func Go(f func() error) chan error {
|
||||||
ch := make(chan error)
|
ch := make(chan error)
|
||||||
go func() {
|
go func() {
|
||||||
|
|
|
@ -1,10 +1,13 @@
|
||||||
package graph
|
package graph
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"bytes"
|
||||||
|
"crypto/sha256"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"fmt"
|
"fmt"
|
||||||
"github.com/dotcloud/docker/future"
|
"io"
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
|
"math/rand"
|
||||||
"os"
|
"os"
|
||||||
"path"
|
"path"
|
||||||
"strings"
|
"strings"
|
||||||
|
@ -157,8 +160,20 @@ func ValidateId(id string) error {
|
||||||
}
|
}
|
||||||
|
|
||||||
func GenerateId() string {
|
func GenerateId() string {
|
||||||
future.Seed()
|
// FIXME: don't seed every time
|
||||||
return future.RandomId()
|
rand.Seed(time.Now().UTC().UnixNano())
|
||||||
|
randomBytes := bytes.NewBuffer([]byte(fmt.Sprintf("%x", rand.Int())))
|
||||||
|
id, _ := ComputeId(randomBytes) // can't fail
|
||||||
|
return id
|
||||||
|
}
|
||||||
|
|
||||||
|
// ComputeId reads from `content` until EOF, then returns a SHA of what it read, as a string.
|
||||||
|
func ComputeId(content io.Reader) (string, error) {
|
||||||
|
h := sha256.New()
|
||||||
|
if _, err := io.Copy(h, content); err != nil {
|
||||||
|
return "", err
|
||||||
|
}
|
||||||
|
return fmt.Sprintf("%x", h.Sum(nil)[:8]), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// Image includes convenience proxy functions to its graph
|
// Image includes convenience proxy functions to its graph
|
||||||
|
|
Loading…
Add table
Reference in a new issue