2014-11-24 18:28:20 -05:00
|
|
|
// +build linux
|
|
|
|
|
2014-08-05 01:19:23 -04:00
|
|
|
package graph
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"io/ioutil"
|
|
|
|
"os"
|
|
|
|
"path"
|
|
|
|
|
2015-03-26 18:22:04 -04:00
|
|
|
"github.com/Sirupsen/logrus"
|
2014-08-05 01:19:23 -04:00
|
|
|
"github.com/docker/docker/engine"
|
|
|
|
"github.com/docker/docker/image"
|
2014-09-30 02:23:36 -04:00
|
|
|
"github.com/docker/docker/pkg/archive"
|
2014-11-08 10:38:42 -05:00
|
|
|
"github.com/docker/docker/pkg/chrootarchive"
|
2014-08-05 01:19:23 -04: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-03-25 03:44:12 -04:00
|
|
|
func (s *TagStore) CmdLoad(job *engine.Job) error {
|
2014-08-05 01:19:23 -04:00
|
|
|
tmpImageDir, err := ioutil.TempDir("", "docker-import-")
|
|
|
|
if err != nil {
|
2015-03-25 03:44:12 -04:00
|
|
|
return err
|
2014-08-05 01:19:23 -04:00
|
|
|
}
|
|
|
|
defer os.RemoveAll(tmpImageDir)
|
|
|
|
|
|
|
|
var (
|
2015-01-16 19:38:11 -05:00
|
|
|
repoDir = path.Join(tmpImageDir, "repo")
|
2014-08-05 01:19:23 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
if err := os.Mkdir(repoDir, os.ModeDir); err != nil {
|
2015-03-25 03:44:12 -04:00
|
|
|
return err
|
2014-08-05 01:19:23 -04:00
|
|
|
}
|
2014-07-31 03:33:59 -04:00
|
|
|
images, err := s.graph.Map()
|
|
|
|
if err != nil {
|
2015-03-25 03:44:12 -04:00
|
|
|
return err
|
2014-07-31 03:33:59 -04:00
|
|
|
}
|
|
|
|
excludes := make([]string, len(images))
|
|
|
|
i := 0
|
|
|
|
for k := range images {
|
|
|
|
excludes[i] = k
|
|
|
|
i++
|
|
|
|
}
|
2015-01-16 19:38:11 -05:00
|
|
|
if err := chrootarchive.Untar(job.Stdin, repoDir, &archive.TarOptions{ExcludePatterns: excludes}); err != nil {
|
2015-03-25 03:44:12 -04:00
|
|
|
return err
|
2014-08-05 01:19:23 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
dirs, err := ioutil.ReadDir(repoDir)
|
|
|
|
if err != nil {
|
2015-03-25 03:44:12 -04:00
|
|
|
return err
|
2014-08-05 01:19:23 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
for _, d := range dirs {
|
|
|
|
if d.IsDir() {
|
|
|
|
if err := s.recursiveLoad(job.Eng, d.Name(), tmpImageDir); err != nil {
|
2015-03-25 03:44:12 -04:00
|
|
|
return err
|
2014-08-05 01:19:23 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
repositoriesJson, err := ioutil.ReadFile(path.Join(tmpImageDir, "repo", "repositories"))
|
|
|
|
if err == nil {
|
|
|
|
repositories := map[string]Repository{}
|
|
|
|
if err := json.Unmarshal(repositoriesJson, &repositories); err != nil {
|
2015-03-25 03:44:12 -04:00
|
|
|
return err
|
2014-08-05 01:19:23 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
for imageName, tagMap := range repositories {
|
|
|
|
for tag, address := range tagMap {
|
2015-04-01 10:44:40 -04:00
|
|
|
if err := s.SetLoad(imageName, tag, address, true, job.Stdout); err != nil {
|
2015-03-25 03:44:12 -04:00
|
|
|
return err
|
2014-08-05 01:19:23 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else if !os.IsNotExist(err) {
|
2015-03-25 03:44:12 -04:00
|
|
|
return err
|
2014-08-05 01:19:23 -04:00
|
|
|
}
|
|
|
|
|
2015-03-25 03:44:12 -04:00
|
|
|
return nil
|
2014-08-05 01:19:23 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *TagStore) recursiveLoad(eng *engine.Engine, address, tmpImageDir string) error {
|
|
|
|
if err := eng.Job("image_get", address).Run(); err != nil {
|
2015-03-26 18:22:04 -04:00
|
|
|
logrus.Debugf("Loading %s", address)
|
2014-08-05 01:19:23 -04:00
|
|
|
|
|
|
|
imageJson, err := ioutil.ReadFile(path.Join(tmpImageDir, "repo", address, "json"))
|
|
|
|
if err != nil {
|
2015-03-26 18:22:04 -04:00
|
|
|
logrus.Debugf("Error reading json", err)
|
2014-08-05 01:19:23 -04:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
layer, err := os.Open(path.Join(tmpImageDir, "repo", address, "layer.tar"))
|
|
|
|
if err != nil {
|
2015-03-26 18:22:04 -04:00
|
|
|
logrus.Debugf("Error reading embedded tar", err)
|
2014-08-05 01:19:23 -04:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
img, err := image.NewImgJSON(imageJson)
|
|
|
|
if err != nil {
|
2015-03-26 18:22:04 -04:00
|
|
|
logrus.Debugf("Error unmarshalling json", err)
|
2014-08-05 01:19:23 -04:00
|
|
|
return err
|
|
|
|
}
|
2015-03-29 17:17:23 -04:00
|
|
|
if err := image.ValidateID(img.ID); err != nil {
|
2015-03-26 18:22:04 -04:00
|
|
|
logrus.Debugf("Error validating ID: %s", err)
|
2014-11-27 16:55:03 -05:00
|
|
|
return err
|
|
|
|
}
|
2015-02-18 21:45:39 -05: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 18:22:04 -04:00
|
|
|
logrus.Debugf("Image (id: %s) load is already running, waiting: %v", img.ID, err)
|
2015-02-18 21:45:39 -05:00
|
|
|
<-c
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
defer s.poolRemove("pull", "layer:"+img.ID)
|
|
|
|
|
2014-08-05 01:19:23 -04:00
|
|
|
if img.Parent != "" {
|
|
|
|
if !s.graph.Exists(img.Parent) {
|
|
|
|
if err := s.recursiveLoad(eng, img.Parent, tmpImageDir); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2014-10-27 14:00:29 -04:00
|
|
|
if err := s.graph.Register(img, layer); err != nil {
|
2014-08-05 01:19:23 -04:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
2015-03-26 18:22:04 -04:00
|
|
|
logrus.Debugf("Completed processing %s", address)
|
2014-08-05 01:19:23 -04:00
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|