2014-08-05 04:08:25 +00:00
|
|
|
package graph
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"io"
|
|
|
|
"io/ioutil"
|
|
|
|
"os"
|
|
|
|
"path"
|
|
|
|
|
2015-03-26 22:22:04 +00:00
|
|
|
"github.com/Sirupsen/logrus"
|
2014-09-30 06:23:36 +00:00
|
|
|
"github.com/docker/docker/pkg/archive"
|
2014-08-05 04:08:25 +00:00
|
|
|
"github.com/docker/docker/pkg/parsers"
|
2014-10-07 01:54:52 +00:00
|
|
|
"github.com/docker/docker/registry"
|
2014-08-05 04:08:25 +00: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-04-21 13:10:30 +00:00
|
|
|
type ImageExportConfig struct {
|
|
|
|
Names []string
|
|
|
|
Outstream io.Writer
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *TagStore) ImageExport(imageExportConfig *ImageExportConfig) error {
|
|
|
|
|
2014-08-05 04:08:25 +00:00
|
|
|
// get image json
|
|
|
|
tempdir, err := ioutil.TempDir("", "docker-export-")
|
|
|
|
if err != nil {
|
2015-03-25 07:44:12 +00:00
|
|
|
return err
|
2014-08-05 04:08:25 +00:00
|
|
|
}
|
|
|
|
defer os.RemoveAll(tempdir)
|
|
|
|
|
|
|
|
rootRepoMap := map[string]Repository{}
|
2014-11-11 09:18:22 +00:00
|
|
|
addKey := func(name string, tag string, id string) {
|
2015-03-26 22:22:04 +00:00
|
|
|
logrus.Debugf("add key [%s:%s]", name, tag)
|
2014-11-11 09:18:22 +00:00
|
|
|
if repo, ok := rootRepoMap[name]; !ok {
|
|
|
|
rootRepoMap[name] = Repository{tag: id}
|
|
|
|
} else {
|
|
|
|
repo[tag] = id
|
|
|
|
}
|
|
|
|
}
|
2015-04-21 13:10:30 +00:00
|
|
|
for _, name := range imageExportConfig.Names {
|
2014-10-07 01:54:52 +00:00
|
|
|
name = registry.NormalizeLocalName(name)
|
2015-03-26 22:22:04 +00:00
|
|
|
logrus.Debugf("Serializing %s", name)
|
2014-05-01 04:26:24 +00:00
|
|
|
rootRepo := s.Repositories[name]
|
|
|
|
if rootRepo != nil {
|
|
|
|
// this is a base repo name, like 'busybox'
|
2014-11-11 09:18:22 +00:00
|
|
|
for tag, id := range rootRepo {
|
|
|
|
addKey(name, tag, id)
|
2015-04-23 19:05:21 +00:00
|
|
|
if err := s.exportImage(id, tempdir); err != nil {
|
2015-03-25 07:44:12 +00:00
|
|
|
return err
|
2014-05-01 04:26:24 +00:00
|
|
|
}
|
2014-08-05 04:08:25 +00:00
|
|
|
}
|
|
|
|
} else {
|
2014-05-01 04:26:24 +00:00
|
|
|
img, err := s.LookupImage(name)
|
|
|
|
if err != nil {
|
2015-03-25 07:44:12 +00:00
|
|
|
return err
|
2014-08-05 04:08:25 +00:00
|
|
|
}
|
2014-05-01 04:26:24 +00: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 09:18:22 +00:00
|
|
|
addKey(repoName, repoTag, img.ID)
|
2014-05-01 04:26:24 +00:00
|
|
|
}
|
2015-04-23 19:05:21 +00:00
|
|
|
if err := s.exportImage(img.ID, tempdir); err != nil {
|
2015-03-25 07:44:12 +00:00
|
|
|
return err
|
2014-05-01 04:26:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
} else {
|
|
|
|
// this must be an ID that didn't get looked up just right?
|
2015-04-23 19:05:21 +00:00
|
|
|
if err := s.exportImage(name, tempdir); err != nil {
|
2015-03-25 07:44:12 +00:00
|
|
|
return err
|
2014-05-01 04:26:24 +00:00
|
|
|
}
|
|
|
|
}
|
2014-08-05 04:08:25 +00:00
|
|
|
}
|
2015-03-26 22:22:04 +00:00
|
|
|
logrus.Debugf("End Serializing %s", name)
|
2014-08-05 04:08:25 +00:00
|
|
|
}
|
|
|
|
// write repositories, if there is something to write
|
|
|
|
if len(rootRepoMap) > 0 {
|
2015-04-20 20:05:48 +00:00
|
|
|
f, err := os.OpenFile(path.Join(tempdir, "repositories"), os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0644)
|
|
|
|
if err != nil {
|
|
|
|
f.Close()
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if err := json.NewEncoder(f).Encode(rootRepoMap); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if err := f.Close(); err != nil {
|
2015-03-25 07:44:12 +00:00
|
|
|
return err
|
2014-08-05 04:08:25 +00:00
|
|
|
}
|
|
|
|
} else {
|
2015-03-26 22:22:04 +00:00
|
|
|
logrus.Debugf("There were no repositories to write")
|
2014-08-05 04:08:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fs, err := archive.Tar(tempdir, archive.Uncompressed)
|
|
|
|
if err != nil {
|
2015-03-25 07:44:12 +00:00
|
|
|
return err
|
2014-08-05 04:08:25 +00:00
|
|
|
}
|
|
|
|
defer fs.Close()
|
|
|
|
|
2015-04-21 13:10:30 +00:00
|
|
|
if _, err := io.Copy(imageExportConfig.Outstream, fs); err != nil {
|
2015-03-25 07:44:12 +00:00
|
|
|
return err
|
2014-08-05 04:08:25 +00:00
|
|
|
}
|
2015-04-21 13:10:30 +00:00
|
|
|
logrus.Debugf("End export image")
|
2015-03-25 07:44:12 +00:00
|
|
|
return nil
|
2014-08-05 04:08:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// FIXME: this should be a top-level function, not a class method
|
2015-04-23 19:05:21 +00:00
|
|
|
func (s *TagStore) exportImage(name, tempdir string) error {
|
2014-08-05 04:08:25 +00:00
|
|
|
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
|
|
|
|
}
|
2015-04-23 19:05:21 +00:00
|
|
|
imageInspectRaw, err := s.LookupRaw(n)
|
|
|
|
if err != nil {
|
2014-08-05 04:08:25 +00:00
|
|
|
return err
|
|
|
|
}
|
2015-04-23 19:05:21 +00:00
|
|
|
written, err := json.Write(imageInspectRaw)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if written != len(imageInspectRaw) {
|
|
|
|
logrus.Warnf("%d byes should have been written instead %d have been written", written, len(imageInspectRaw))
|
|
|
|
}
|
2014-08-05 04:08:25 +00:00
|
|
|
|
|
|
|
// serialize filesystem
|
|
|
|
fsTar, err := os.Create(path.Join(tmpImageDir, "layer.tar"))
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2015-04-21 17:59:45 +00:00
|
|
|
if err := s.ImageTarLayer(n, fsTar); err != nil {
|
2014-08-05 04:08:25 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// find parent
|
2015-04-21 17:42:06 +00:00
|
|
|
img, err := s.LookupImage(n)
|
|
|
|
if err != nil {
|
2014-08-05 04:08:25 +00:00
|
|
|
return err
|
|
|
|
}
|
2015-04-21 17:42:06 +00:00
|
|
|
n = img.Parent
|
2014-08-05 04:08:25 +00:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|