2015-05-11 17:23:32 +00:00
|
|
|
// +build linux windows
|
2014-11-24 23:28:20 +00:00
|
|
|
|
2014-08-05 05:19:23 +00:00
|
|
|
package graph
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
2015-04-21 01:26:15 +00:00
|
|
|
"io"
|
2014-08-05 05:19:23 +00:00
|
|
|
"io/ioutil"
|
|
|
|
"os"
|
2015-05-04 22:14:39 +00:00
|
|
|
"path/filepath"
|
2014-08-05 05:19:23 +00:00
|
|
|
|
2015-03-26 22:22:04 +00:00
|
|
|
"github.com/Sirupsen/logrus"
|
2014-08-05 05:19:23 +00:00
|
|
|
"github.com/docker/docker/image"
|
2014-09-30 06:23:36 +00:00
|
|
|
"github.com/docker/docker/pkg/archive"
|
2014-11-08 15:38:42 +00:00
|
|
|
"github.com/docker/docker/pkg/chrootarchive"
|
2014-08-05 05:19:23 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// Loads a set of images into the repository. This is the complementary of ImageExport.
|
|
|
|
// The input stream is an uncompressed tar ball containing images and metadata.
|
2015-04-23 19:05:21 +00:00
|
|
|
func (s *TagStore) Load(inTar io.ReadCloser, outStream io.Writer) error {
|
2014-08-05 05:19:23 +00:00
|
|
|
tmpImageDir, err := ioutil.TempDir("", "docker-import-")
|
|
|
|
if err != nil {
|
2015-03-25 07:44:12 +00:00
|
|
|
return err
|
2014-08-05 05:19:23 +00:00
|
|
|
}
|
|
|
|
defer os.RemoveAll(tmpImageDir)
|
|
|
|
|
|
|
|
var (
|
2015-05-04 22:14:39 +00:00
|
|
|
repoDir = filepath.Join(tmpImageDir, "repo")
|
2014-08-05 05:19:23 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
if err := os.Mkdir(repoDir, os.ModeDir); err != nil {
|
2015-03-25 07:44:12 +00:00
|
|
|
return err
|
2014-08-05 05:19:23 +00:00
|
|
|
}
|
2015-06-19 15:01:39 +00:00
|
|
|
images := s.graph.Map()
|
2014-07-31 07:33:59 +00:00
|
|
|
excludes := make([]string, len(images))
|
|
|
|
i := 0
|
|
|
|
for k := range images {
|
|
|
|
excludes[i] = k
|
|
|
|
i++
|
|
|
|
}
|
2015-04-23 19:05:21 +00:00
|
|
|
if err := chrootarchive.Untar(inTar, repoDir, &archive.TarOptions{ExcludePatterns: excludes}); err != nil {
|
2015-03-25 07:44:12 +00:00
|
|
|
return err
|
2014-08-05 05:19:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
dirs, err := ioutil.ReadDir(repoDir)
|
|
|
|
if err != nil {
|
2015-03-25 07:44:12 +00:00
|
|
|
return err
|
2014-08-05 05:19:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
for _, d := range dirs {
|
|
|
|
if d.IsDir() {
|
2015-04-23 19:05:21 +00:00
|
|
|
if err := s.recursiveLoad(d.Name(), tmpImageDir); err != nil {
|
2015-03-25 07:44:12 +00:00
|
|
|
return err
|
2014-08-05 05:19:23 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-05-04 22:14:39 +00:00
|
|
|
reposJSONFile, err := os.Open(filepath.Join(tmpImageDir, "repo", "repositories"))
|
2015-04-21 22:47:51 +00:00
|
|
|
if err != nil {
|
|
|
|
if !os.IsNotExist(err) {
|
2015-03-25 07:44:12 +00:00
|
|
|
return err
|
2014-08-05 05:19:23 +00:00
|
|
|
}
|
2015-04-21 22:47:51 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
defer reposJSONFile.Close()
|
2014-08-05 05:19:23 +00:00
|
|
|
|
2015-04-21 22:47:51 +00:00
|
|
|
repositories := map[string]Repository{}
|
|
|
|
if err := json.NewDecoder(reposJSONFile).Decode(&repositories); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
for imageName, tagMap := range repositories {
|
|
|
|
for tag, address := range tagMap {
|
|
|
|
if err := s.SetLoad(imageName, tag, address, true, outStream); err != nil {
|
|
|
|
return err
|
2014-08-05 05:19:23 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-03-25 07:44:12 +00:00
|
|
|
return nil
|
2014-08-05 05:19:23 +00:00
|
|
|
}
|
|
|
|
|
2015-04-23 19:05:21 +00:00
|
|
|
func (s *TagStore) recursiveLoad(address, tmpImageDir string) error {
|
2015-04-21 17:42:06 +00:00
|
|
|
if _, err := s.LookupImage(address); err != nil {
|
2015-03-26 22:22:04 +00:00
|
|
|
logrus.Debugf("Loading %s", address)
|
2014-08-05 05:19:23 +00:00
|
|
|
|
2015-05-04 22:14:39 +00:00
|
|
|
imageJson, err := ioutil.ReadFile(filepath.Join(tmpImageDir, "repo", address, "json"))
|
2014-08-05 05:19:23 +00:00
|
|
|
if err != nil {
|
2015-07-15 19:25:50 +00:00
|
|
|
logrus.Debugf("Error reading json: %v", err)
|
2014-08-05 05:19:23 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2015-05-04 22:14:39 +00:00
|
|
|
layer, err := os.Open(filepath.Join(tmpImageDir, "repo", address, "layer.tar"))
|
2014-08-05 05:19:23 +00:00
|
|
|
if err != nil {
|
2015-07-15 19:25:50 +00:00
|
|
|
logrus.Debugf("Error reading embedded tar: %v", err)
|
2014-08-05 05:19:23 +00:00
|
|
|
return err
|
|
|
|
}
|
2015-07-20 17:57:15 +00:00
|
|
|
img, err := image.NewImgJSON(imageJson)
|
2014-08-05 05:19:23 +00:00
|
|
|
if err != nil {
|
2015-07-15 19:25:50 +00:00
|
|
|
logrus.Debugf("Error unmarshalling json: %v", err)
|
2014-08-05 05:19:23 +00:00
|
|
|
return err
|
|
|
|
}
|
2015-03-29 21:17:23 +00:00
|
|
|
if err := image.ValidateID(img.ID); err != nil {
|
2015-07-15 19:25:50 +00:00
|
|
|
logrus.Debugf("Error validating ID: %v", err)
|
2014-11-27 21:55:03 +00:00
|
|
|
return err
|
|
|
|
}
|
2015-02-19 02:45:39 +00:00
|
|
|
|
|
|
|
// ensure no two downloads of the same layer happen at the same time
|
|
|
|
if c, err := s.poolAdd("pull", "layer:"+img.ID); err != nil {
|
|
|
|
if c != nil {
|
2015-03-26 22:22:04 +00:00
|
|
|
logrus.Debugf("Image (id: %s) load is already running, waiting: %v", img.ID, err)
|
2015-02-19 02:45:39 +00:00
|
|
|
<-c
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
defer s.poolRemove("pull", "layer:"+img.ID)
|
|
|
|
|
2014-08-05 05:19:23 +00:00
|
|
|
if img.Parent != "" {
|
|
|
|
if !s.graph.Exists(img.Parent) {
|
2015-04-23 19:05:21 +00:00
|
|
|
if err := s.recursiveLoad(img.Parent, tmpImageDir); err != nil {
|
2014-08-05 05:19:23 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2014-10-27 18:00:29 +00:00
|
|
|
if err := s.graph.Register(img, layer); err != nil {
|
2014-08-05 05:19:23 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
2015-03-26 22:22:04 +00:00
|
|
|
logrus.Debugf("Completed processing %s", address)
|
2014-08-05 05:19:23 +00:00
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|