2014-08-05 00:08:25 -04:00
|
|
|
package graph
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
2015-03-25 03:44:12 -04:00
|
|
|
"fmt"
|
2014-08-05 00:08:25 -04:00
|
|
|
"io"
|
|
|
|
"io/ioutil"
|
|
|
|
"os"
|
|
|
|
"path"
|
|
|
|
|
2014-10-24 18:11:48 -04:00
|
|
|
log "github.com/Sirupsen/logrus"
|
2014-08-05 00:08:25 -04:00
|
|
|
"github.com/docker/docker/engine"
|
2014-09-30 02:23:36 -04:00
|
|
|
"github.com/docker/docker/pkg/archive"
|
2014-08-05 00:08:25 -04:00
|
|
|
"github.com/docker/docker/pkg/parsers"
|
2014-10-06 21:54:52 -04:00
|
|
|
"github.com/docker/docker/registry"
|
2014-08-05 00:08:25 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
// CmdImageExport exports all images with the given tag. All versions
|
|
|
|
// containing the same tag are exported. The resulting output is an
|
|
|
|
// uncompressed tar ball.
|
|
|
|
// name is the set of tags to export.
|
|
|
|
// out is the writer where the images are written to.
|
2015-03-25 03:44:12 -04:00
|
|
|
func (s *TagStore) CmdImageExport(job *engine.Job) error {
|
2014-05-01 00:26:24 -04:00
|
|
|
if len(job.Args) < 1 {
|
2015-03-25 03:44:12 -04:00
|
|
|
return fmt.Errorf("Usage: %s IMAGE [IMAGE...]\n", job.Name)
|
2014-08-05 00:08:25 -04:00
|
|
|
}
|
|
|
|
// get image json
|
|
|
|
tempdir, err := ioutil.TempDir("", "docker-export-")
|
|
|
|
if err != nil {
|
2015-03-25 03:44:12 -04:00
|
|
|
return err
|
2014-08-05 00:08:25 -04:00
|
|
|
}
|
|
|
|
defer os.RemoveAll(tempdir)
|
|
|
|
|
|
|
|
rootRepoMap := map[string]Repository{}
|
2014-11-11 04:18:22 -05:00
|
|
|
addKey := func(name string, tag string, id string) {
|
|
|
|
log.Debugf("add key [%s:%s]", name, tag)
|
|
|
|
if repo, ok := rootRepoMap[name]; !ok {
|
|
|
|
rootRepoMap[name] = Repository{tag: id}
|
|
|
|
} else {
|
|
|
|
repo[tag] = id
|
|
|
|
}
|
|
|
|
}
|
2014-05-01 00:26:24 -04:00
|
|
|
for _, name := range job.Args {
|
2014-10-06 21:54:52 -04:00
|
|
|
name = registry.NormalizeLocalName(name)
|
2014-05-01 00:26:24 -04:00
|
|
|
log.Debugf("Serializing %s", name)
|
|
|
|
rootRepo := s.Repositories[name]
|
|
|
|
if rootRepo != nil {
|
|
|
|
// this is a base repo name, like 'busybox'
|
2014-11-11 04:18:22 -05:00
|
|
|
for tag, id := range rootRepo {
|
|
|
|
addKey(name, tag, id)
|
2014-05-01 00:26:24 -04:00
|
|
|
if err := s.exportImage(job.Eng, id, tempdir); err != nil {
|
2015-03-25 03:44:12 -04:00
|
|
|
return err
|
2014-05-01 00:26:24 -04:00
|
|
|
}
|
2014-08-05 00:08:25 -04:00
|
|
|
}
|
|
|
|
} else {
|
2014-05-01 00:26:24 -04:00
|
|
|
img, err := s.LookupImage(name)
|
|
|
|
if err != nil {
|
2015-03-25 03:44:12 -04:00
|
|
|
return err
|
2014-08-05 00:08:25 -04:00
|
|
|
}
|
2014-05-01 00:26:24 -04:00
|
|
|
|
|
|
|
if img != nil {
|
|
|
|
// This is a named image like 'busybox:latest'
|
|
|
|
repoName, repoTag := parsers.ParseRepositoryTag(name)
|
|
|
|
|
|
|
|
// check this length, because a lookup of a truncated has will not have a tag
|
|
|
|
// and will not need to be added to this map
|
|
|
|
if len(repoTag) > 0 {
|
2014-11-11 04:18:22 -05:00
|
|
|
addKey(repoName, repoTag, img.ID)
|
2014-05-01 00:26:24 -04:00
|
|
|
}
|
|
|
|
if err := s.exportImage(job.Eng, img.ID, tempdir); err != nil {
|
2015-03-25 03:44:12 -04:00
|
|
|
return err
|
2014-05-01 00:26:24 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
} else {
|
|
|
|
// this must be an ID that didn't get looked up just right?
|
|
|
|
if err := s.exportImage(job.Eng, name, tempdir); err != nil {
|
2015-03-25 03:44:12 -04:00
|
|
|
return err
|
2014-05-01 00:26:24 -04:00
|
|
|
}
|
|
|
|
}
|
2014-08-05 00:08:25 -04:00
|
|
|
}
|
2014-05-01 00:26:24 -04:00
|
|
|
log.Debugf("End Serializing %s", name)
|
2014-08-05 00:08:25 -04:00
|
|
|
}
|
|
|
|
// write repositories, if there is something to write
|
|
|
|
if len(rootRepoMap) > 0 {
|
|
|
|
rootRepoJson, _ := json.Marshal(rootRepoMap)
|
|
|
|
if err := ioutil.WriteFile(path.Join(tempdir, "repositories"), rootRepoJson, os.FileMode(0644)); err != nil {
|
2015-03-25 03:44:12 -04:00
|
|
|
return err
|
2014-08-05 00:08:25 -04:00
|
|
|
}
|
|
|
|
} else {
|
2014-07-24 16:37:44 -04:00
|
|
|
log.Debugf("There were no repositories to write")
|
2014-08-05 00:08:25 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
fs, err := archive.Tar(tempdir, archive.Uncompressed)
|
|
|
|
if err != nil {
|
2015-03-25 03:44:12 -04:00
|
|
|
return err
|
2014-08-05 00:08:25 -04:00
|
|
|
}
|
|
|
|
defer fs.Close()
|
|
|
|
|
|
|
|
if _, err := io.Copy(job.Stdout, fs); err != nil {
|
2015-03-25 03:44:12 -04:00
|
|
|
return err
|
2014-08-05 00:08:25 -04:00
|
|
|
}
|
2014-05-01 00:26:24 -04:00
|
|
|
log.Debugf("End export job: %s", job.Name)
|
2015-03-25 03:44:12 -04:00
|
|
|
return nil
|
2014-08-05 00:08:25 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// FIXME: this should be a top-level function, not a class method
|
|
|
|
func (s *TagStore) exportImage(eng *engine.Engine, name, tempdir string) error {
|
|
|
|
for n := name; n != ""; {
|
|
|
|
// temporary directory
|
|
|
|
tmpImageDir := path.Join(tempdir, n)
|
|
|
|
if err := os.Mkdir(tmpImageDir, os.FileMode(0755)); err != nil {
|
|
|
|
if os.IsExist(err) {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
var version = "1.0"
|
|
|
|
var versionBuf = []byte(version)
|
|
|
|
|
|
|
|
if err := ioutil.WriteFile(path.Join(tmpImageDir, "VERSION"), versionBuf, os.FileMode(0644)); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// serialize json
|
|
|
|
json, err := os.Create(path.Join(tmpImageDir, "json"))
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
job := eng.Job("image_inspect", n)
|
|
|
|
job.SetenvBool("raw", true)
|
|
|
|
job.Stdout.Add(json)
|
|
|
|
if err := job.Run(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// serialize filesystem
|
|
|
|
fsTar, err := os.Create(path.Join(tmpImageDir, "layer.tar"))
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
job = eng.Job("image_tarlayer", n)
|
|
|
|
job.Stdout.Add(fsTar)
|
|
|
|
if err := job.Run(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// find parent
|
|
|
|
job = eng.Job("image_get", n)
|
|
|
|
info, _ := job.Stdout.AddEnv()
|
|
|
|
if err := job.Run(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
n = info.Get("Parent")
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|