2014-08-05 00:08:25 -04:00
|
|
|
package graph
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
2015-09-03 22:19:10 -04:00
|
|
|
"fmt"
|
2014-08-05 00:08:25 -04:00
|
|
|
"io"
|
|
|
|
"io/ioutil"
|
|
|
|
"os"
|
2015-05-04 17:56:23 -04:00
|
|
|
"path/filepath"
|
2015-09-03 22:19:10 -04:00
|
|
|
"time"
|
2014-08-05 00:08:25 -04:00
|
|
|
|
2015-03-26 18:22:04 -04:00
|
|
|
"github.com/Sirupsen/logrus"
|
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
|
|
|
)
|
|
|
|
|
2015-07-29 19:45:47 -04:00
|
|
|
// ImageExport exports list of images to a output stream specified in the
|
|
|
|
// config. The exported images are archived into a tar when written to the
|
|
|
|
// output stream. All images with the given tag and all versions containing the
|
|
|
|
// same tag are exported. names is the set of tags to export, and outStream
|
|
|
|
// is the writer which the images are written to.
|
|
|
|
func (s *TagStore) ImageExport(names []string, outStream io.Writer) error {
|
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) {
|
2015-03-26 18:22:04 -04:00
|
|
|
logrus.Debugf("add key [%s:%s]", name, tag)
|
2014-11-11 04:18:22 -05:00
|
|
|
if repo, ok := rootRepoMap[name]; !ok {
|
|
|
|
rootRepoMap[name] = Repository{tag: id}
|
|
|
|
} else {
|
|
|
|
repo[tag] = id
|
|
|
|
}
|
|
|
|
}
|
2015-07-29 19:45:47 -04:00
|
|
|
for _, name := range names {
|
2014-10-06 21:54:52 -04:00
|
|
|
name = registry.NormalizeLocalName(name)
|
2015-03-26 18:22:04 -04:00
|
|
|
logrus.Debugf("Serializing %s", name)
|
2014-05-01 00:26:24 -04:00
|
|
|
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)
|
2015-04-23 15:05:21 -04:00
|
|
|
if err := s.exportImage(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
|
|
|
}
|
2015-04-23 15:05:21 -04:00
|
|
|
if err := s.exportImage(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?
|
2015-04-23 15:05:21 -04:00
|
|
|
if err := s.exportImage(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
|
|
|
}
|
2015-03-26 18:22:04 -04:00
|
|
|
logrus.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 {
|
2015-05-04 17:56:23 -04:00
|
|
|
f, err := os.OpenFile(filepath.Join(tempdir, "repositories"), os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0644)
|
2015-04-20 16:05:48 -04:00
|
|
|
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 03:44:12 -04:00
|
|
|
return err
|
2014-08-05 00:08:25 -04:00
|
|
|
}
|
2015-09-03 22:19:10 -04:00
|
|
|
if err := os.Chtimes(filepath.Join(tempdir, "repositories"), time.Unix(0, 0), time.Unix(0, 0)); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2014-08-05 00:08:25 -04:00
|
|
|
} else {
|
2015-03-26 18:22:04 -04:00
|
|
|
logrus.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()
|
|
|
|
|
2015-07-29 19:45:47 -04:00
|
|
|
if _, err := io.Copy(outStream, fs); err != nil {
|
2015-03-25 03:44:12 -04:00
|
|
|
return err
|
2014-08-05 00:08:25 -04:00
|
|
|
}
|
2015-04-21 09:10:30 -04:00
|
|
|
logrus.Debugf("End export image")
|
2015-03-25 03:44:12 -04:00
|
|
|
return nil
|
2014-08-05 00:08:25 -04:00
|
|
|
}
|
|
|
|
|
2015-04-23 15:05:21 -04:00
|
|
|
func (s *TagStore) exportImage(name, tempdir string) error {
|
2014-08-05 00:08:25 -04:00
|
|
|
for n := name; n != ""; {
|
2015-08-26 17:58:56 -04:00
|
|
|
img, err := s.LookupImage(n)
|
|
|
|
if err != nil || img == nil {
|
|
|
|
return fmt.Errorf("No such image %s", n)
|
|
|
|
}
|
|
|
|
|
2014-08-05 00:08:25 -04:00
|
|
|
// temporary directory
|
2015-05-04 17:56:23 -04:00
|
|
|
tmpImageDir := filepath.Join(tempdir, n)
|
2014-08-05 00:08:25 -04:00
|
|
|
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)
|
|
|
|
|
2015-05-04 17:56:23 -04:00
|
|
|
if err := ioutil.WriteFile(filepath.Join(tmpImageDir, "VERSION"), versionBuf, os.FileMode(0644)); err != nil {
|
2014-08-05 00:08:25 -04:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2015-08-26 17:58:56 -04:00
|
|
|
imageInspectRaw, err := json.Marshal(img)
|
2014-08-05 00:08:25 -04:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2015-08-26 17:58:56 -04:00
|
|
|
|
|
|
|
// serialize json
|
|
|
|
json, err := os.Create(filepath.Join(tmpImageDir, "json"))
|
2015-04-23 15:05:21 -04:00
|
|
|
if err != nil {
|
2014-08-05 00:08:25 -04:00
|
|
|
return err
|
|
|
|
}
|
2015-08-26 17:58:56 -04:00
|
|
|
|
2015-04-23 15:05:21 -04: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 00:08:25 -04:00
|
|
|
|
|
|
|
// serialize filesystem
|
2015-05-04 17:56:23 -04:00
|
|
|
fsTar, err := os.Create(filepath.Join(tmpImageDir, "layer.tar"))
|
2014-08-05 00:08:25 -04:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2015-04-21 13:59:45 -04:00
|
|
|
if err := s.ImageTarLayer(n, fsTar); err != nil {
|
2014-08-05 00:08:25 -04:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2015-09-03 22:19:10 -04:00
|
|
|
for _, fname := range []string{"", "VERSION", "json", "layer.tar"} {
|
|
|
|
if err := os.Chtimes(filepath.Join(tmpImageDir, fname), img.Created, img.Created); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2014-08-05 00:08:25 -04:00
|
|
|
}
|
2015-09-03 22:19:10 -04:00
|
|
|
|
|
|
|
// try again with parent
|
2015-04-21 13:42:06 -04:00
|
|
|
n = img.Parent
|
2014-08-05 00:08:25 -04:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|