浏览代码

Factorize the checksums functions

Guillaume J. Charmes 12 年之前
父节点
当前提交
44b33b44aa
共有 3 个文件被更改,包括 65 次插入36 次删除
  1. 24 0
      graph.go
  2. 15 20
      image.go
  3. 26 16
      registry.go

+ 24 - 0
graph.go

@@ -1,6 +1,7 @@
 package docker
 package docker
 
 
 import (
 import (
+	"encoding/json"
 	"fmt"
 	"fmt"
 	"io"
 	"io"
 	"io/ioutil"
 	"io/ioutil"
@@ -300,3 +301,26 @@ func (graph *Graph) Heads() (map[string]*Image, error) {
 func (graph *Graph) imageRoot(id string) string {
 func (graph *Graph) imageRoot(id string) string {
 	return path.Join(graph.Root, id)
 	return path.Join(graph.Root, id)
 }
 }
+
+func (graph *Graph) getStoredChecksums() (map[string]string, error) {
+	checksums := make(map[string]string)
+	// FIXME: Store the checksum in memory
+
+	if checksumDict, err := ioutil.ReadFile(path.Join(graph.Root, "checksums")); err == nil {
+		if err := json.Unmarshal(checksumDict, &checksums); err != nil {
+			return nil, err
+		}
+	}
+	return checksums, nil
+}
+
+func (graph *Graph) storeChecksums(checksums map[string]string) error {
+	checksumJson, err := json.Marshal(checksums)
+	if err != nil {
+		return err
+	}
+	if err := ioutil.WriteFile(path.Join(graph.Root, "checksums"), checksumJson, 0600); err != nil {
+		return err
+	}
+	return nil
+}

+ 15 - 20
image.go

@@ -295,16 +295,12 @@ func (img *Image) Checksum() (string, error) {
 		return "", err
 		return "", err
 	}
 	}
 
 
-	checksumDictPth := path.Join(root, "..", "..", "checksums")
-	checksums := make(map[string]string)
-
-	if checksumDict, err := ioutil.ReadFile(checksumDictPth); err == nil {
-		if err := json.Unmarshal(checksumDict, &checksums); err != nil {
-			return "", err
-		}
-		if checksum, ok := checksums[img.Id]; ok {
-			return checksum, nil
-		}
+	checksums, err := img.graph.getStoredChecksums()
+	if err != nil {
+		return "", err
+	}
+	if checksum, ok := checksums[img.Id]; ok {
+		return checksum, nil
 	}
 	}
 
 
 	layer, err := img.layer()
 	layer, err := img.layer()
@@ -343,24 +339,23 @@ func (img *Image) Checksum() (string, error) {
 	if _, err := io.Copy(h, layerData); err != nil {
 	if _, err := io.Copy(h, layerData); err != nil {
 		return "", err
 		return "", err
 	}
 	}
-
 	hash := "sha256:" + hex.EncodeToString(h.Sum(nil))
 	hash := "sha256:" + hex.EncodeToString(h.Sum(nil))
-	checksums[img.Id] = hash
 
 
 	// Reload the json file to make sure not to overwrite faster sums
 	// Reload the json file to make sure not to overwrite faster sums
 	img.graph.lockSumFile.Lock()
 	img.graph.lockSumFile.Lock()
 	defer img.graph.lockSumFile.Unlock()
 	defer img.graph.lockSumFile.Unlock()
-	if checksumDict, err := ioutil.ReadFile(checksumDictPth); err == nil {
-		if err := json.Unmarshal(checksumDict, &checksums); err != nil {
-			return "", err
-		}
-	}
-	checksumJson, err := json.Marshal(checksums)
+
+	checksums, err = img.graph.getStoredChecksums()
 	if err != nil {
 	if err != nil {
-		return hash, err
+		return "", err
 	}
 	}
-	if err := ioutil.WriteFile(checksumDictPth, checksumJson, 0600); err != nil {
+
+	checksums[img.Id] = hash
+
+	// Dump the checksums to disc
+	if err := img.graph.storeChecksums(checksums); err != nil {
 		return hash, err
 		return hash, err
 	}
 	}
+
 	return hash, nil
 	return hash, nil
 }
 }

+ 26 - 16
registry.go

@@ -578,6 +578,24 @@ func (graph *Graph) PushRepository(stdout io.Writer, remote string, localRepo Re
 	return filteredRepo, nil
 	return filteredRepo, nil
 }
 }
 
 
+// Retrieve the checksum of an image
+// Priority:
+// - Check on the stored checksums
+// - Check if the archive is exists, if it does not, ask the registry
+// - If the archive does exists, process the checksum from it
+// - If the archive does not exists and not found on registry, process checksum from layer
+func (graph *Graph) getChecksum(imageId string) (string, error) {
+	img, err := graph.Get(imageId)
+	if err != nil {
+		return "", err
+	}
+	checksum, err := img.Checksum()
+	if err != nil {
+		return "", err
+	}
+	return checksum, nil
+}
+
 type ImgListJson struct {
 type ImgListJson struct {
 	Id       string `json:"id"`
 	Id       string `json:"id"`
 	Checksum string `json:"checksum,omitempty"`
 	Checksum string `json:"checksum,omitempty"`
@@ -592,7 +610,14 @@ func (graph *Graph) PushRepository(stdout io.Writer, remote string, localRepo Re
 	var imgList []*ImgListJson
 	var imgList []*ImgListJson
 
 
 	for _, id := range localRepo {
 	for _, id := range localRepo {
-		imgList = append(imgList, &ImgListJson{Id: id})
+		checksum, err := graph.getChecksum(id)
+		if err != nil {
+			return err
+		}
+		imgList = append(imgList, &ImgListJson{
+			Id:       id,
+			Checksum: checksum,
+		})
 	}
 	}
 
 
 	imgListJson, err := json.Marshal(imgList)
 	imgListJson, err := json.Marshal(imgList)
@@ -666,21 +691,6 @@ func (graph *Graph) PushRepository(stdout io.Writer, remote string, localRepo Re
 		}
 		}
 	}
 	}
 
 
-	for _, elem := range imgList {
-		img, err := graph.Get(elem.Id)
-		if err != nil {
-			return err
-		}
-		if elem.Checksum, err = img.Checksum(); err != nil {
-			return err
-		}
-	}
-	imgListJson, err = json.Marshal(imgList)
-	if err != nil {
-		return err
-	}
-	Debugf("json sent: %s\n", imgListJson)
-
 	req2, err := http.NewRequest("PUT", INDEX_ENDPOINT+"/repositories/"+remote+"/images", bytes.NewReader(imgListJson))
 	req2, err := http.NewRequest("PUT", INDEX_ENDPOINT+"/repositories/"+remote+"/images", bytes.NewReader(imgListJson))
 	if err != nil {
 	if err != nil {
 		return err
 		return err