Browse code

Implemented checksum computation on image creation (necessary for new push primitive)

shin- authored on 2013/04/20 00:25:55
Showing 3 changed files
... ...
@@ -1,6 +1,7 @@
1 1
 package docker
2 2
 
3 3
 import (
4
+	"crypto/sha256"
4 5
 	"encoding/json"
5 6
 	"fmt"
6 7
 	"github.com/dotcloud/docker/rcli"
... ...
@@ -695,6 +696,18 @@ func (container *Container) ExportRw() (Archive, error) {
695 695
 	return Tar(container.rwPath(), Uncompressed)
696 696
 }
697 697
 
698
+func (container *Container) RwChecksum() (string, error) {
699
+	h := sha256.New()
700
+	rwData, err := container.ExportRw()
701
+	if err != nil {
702
+		return "", err
703
+	}
704
+	if _, err := io.Copy(h, rwData); err != nil {
705
+		return "", err
706
+	}
707
+	return string(h.Sum(nil)), nil
708
+}
709
+
698 710
 func (container *Container) Export() (Archive, error) {
699 711
 	if err := container.EnsureMounted(); err != nil {
700 712
 		return nil, err
... ...
@@ -98,11 +98,13 @@ func (graph *Graph) Create(layerData Archive, container *Container, comment, aut
98 98
 		img.Parent = container.Image
99 99
 		img.Container = container.Id
100 100
 		img.ContainerConfig = *container.Config
101
-		if config == nil {
102
-			if parentImage, err := graph.Get(container.Image); err == nil && parentImage != nil {
103
-				img.Config = parentImage.Config
104
-			}
101
+		// FIXME: If an image is pulled from a raw URL (not created from a container),
102
+		// its checksum will not be computed, which will cause a push to fail
103
+		checksum, err := container.RwChecksum()
104
+		if err != nil {
105
+			return nil, err
105 106
 		}
107
+		img.Checksum = checksum
106 108
 	}
107 109
 	if err := graph.Register(layerData, img); err != nil {
108 110
 		return nil, err
... ...
@@ -18,6 +18,7 @@ import (
18 18
 type Image struct {
19 19
 	Id              string    `json:"id"`
20 20
 	Parent          string    `json:"parent,omitempty"`
21
+	Checksum        string    `json:"checksum,omitempty"`
21 22
 	Comment         string    `json:"comment,omitempty"`
22 23
 	Created         time.Time `json:"created"`
23 24
 	Container       string    `json:"container,omitempty"`