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"
|
|
|
|
|
2014-10-24 18:11:48 -04:00
|
|
|
log "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-11-27 16:55:03 -05:00
|
|
|
"github.com/docker/docker/utils"
|
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.
|
|
|
|
func (s *TagStore) CmdLoad(job *engine.Job) engine.Status {
|
|
|
|
tmpImageDir, err := ioutil.TempDir("", "docker-import-")
|
|
|
|
if err != nil {
|
|
|
|
return job.Error(err)
|
|
|
|
}
|
|
|
|
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 {
|
|
|
|
return job.Error(err)
|
|
|
|
}
|
2014-07-31 03:33:59 -04:00
|
|
|
images, err := s.graph.Map()
|
|
|
|
if err != nil {
|
|
|
|
return job.Error(err)
|
|
|
|
}
|
|
|
|
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 {
|
2014-08-05 01:19:23 -04:00
|
|
|
return job.Error(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
dirs, err := ioutil.ReadDir(repoDir)
|
|
|
|
if err != nil {
|
|
|
|
return job.Error(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, d := range dirs {
|
|
|
|
if d.IsDir() {
|
|
|
|
if err := s.recursiveLoad(job.Eng, d.Name(), tmpImageDir); err != nil {
|
|
|
|
return job.Error(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
repositoriesJson, err := ioutil.ReadFile(path.Join(tmpImageDir, "repo", "repositories"))
|
|
|
|
if err == nil {
|
|
|
|
repositories := map[string]Repository{}
|
|
|
|
if err := json.Unmarshal(repositoriesJson, &repositories); err != nil {
|
|
|
|
return job.Error(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
for imageName, tagMap := range repositories {
|
|
|
|
for tag, address := range tagMap {
|
|
|
|
if err := s.Set(imageName, tag, address, true); err != nil {
|
|
|
|
return job.Error(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else if !os.IsNotExist(err) {
|
|
|
|
return job.Error(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return engine.StatusOK
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *TagStore) recursiveLoad(eng *engine.Engine, address, tmpImageDir string) error {
|
|
|
|
if err := eng.Job("image_get", address).Run(); err != nil {
|
2014-07-24 16:37:44 -04:00
|
|
|
log.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 {
|
2014-07-24 16:37:44 -04:00
|
|
|
log.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 {
|
2014-07-24 16:37:44 -04:00
|
|
|
log.Debugf("Error reading embedded tar", err)
|
2014-08-05 01:19:23 -04:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
img, err := image.NewImgJSON(imageJson)
|
|
|
|
if err != nil {
|
2014-07-24 16:37:44 -04:00
|
|
|
log.Debugf("Error unmarshalling json", err)
|
2014-08-05 01:19:23 -04:00
|
|
|
return err
|
|
|
|
}
|
2014-11-27 16:55:03 -05:00
|
|
|
if err := utils.ValidateID(img.ID); err != nil {
|
|
|
|
log.Debugf("Error validating ID: %s", err)
|
|
|
|
return err
|
|
|
|
}
|
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
|
|
|
|
}
|
|
|
|
}
|
2014-07-24 16:37:44 -04:00
|
|
|
log.Debugf("Completed processing %s", address)
|
2014-08-05 01:19:23 -04:00
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|