2014-08-05 05:29:14 +00:00
|
|
|
package graph
|
|
|
|
|
|
|
|
import (
|
2015-04-15 11:43:15 +00:00
|
|
|
"io"
|
2014-08-05 05:29:14 +00:00
|
|
|
"net/http"
|
|
|
|
"net/url"
|
|
|
|
|
2014-09-30 06:23:36 +00:00
|
|
|
"github.com/docker/docker/pkg/archive"
|
2015-03-29 21:17:23 +00:00
|
|
|
"github.com/docker/docker/pkg/httputils"
|
2015-02-24 08:51:46 +00:00
|
|
|
"github.com/docker/docker/pkg/progressreader"
|
2015-03-18 02:18:41 +00:00
|
|
|
"github.com/docker/docker/pkg/streamformatter"
|
2015-02-05 11:26:05 +00:00
|
|
|
"github.com/docker/docker/runconfig"
|
2014-08-05 05:29:14 +00:00
|
|
|
"github.com/docker/docker/utils"
|
|
|
|
)
|
|
|
|
|
2015-04-15 11:43:15 +00:00
|
|
|
type ImageImportConfig struct {
|
2015-04-16 21:26:33 +00:00
|
|
|
Changes []string
|
|
|
|
InConfig io.ReadCloser
|
|
|
|
OutStream io.Writer
|
|
|
|
ContainerConfig *runconfig.Config
|
2015-04-15 11:43:15 +00:00
|
|
|
}
|
|
|
|
|
2015-04-16 21:26:33 +00:00
|
|
|
func (s *TagStore) Import(src string, repo string, tag string, imageImportConfig *ImageImportConfig) error {
|
2014-08-05 05:29:14 +00:00
|
|
|
var (
|
2015-05-12 18:18:54 +00:00
|
|
|
sf = streamformatter.NewJSONStreamFormatter()
|
2015-04-16 21:26:33 +00:00
|
|
|
archive archive.ArchiveReader
|
|
|
|
resp *http.Response
|
2014-08-05 05:29:14 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
if src == "-" {
|
2015-04-15 11:43:15 +00:00
|
|
|
archive = imageImportConfig.InConfig
|
2014-08-05 05:29:14 +00:00
|
|
|
} else {
|
|
|
|
u, err := url.Parse(src)
|
|
|
|
if err != nil {
|
2015-03-25 07:44:12 +00:00
|
|
|
return err
|
2014-08-05 05:29:14 +00:00
|
|
|
}
|
|
|
|
if u.Scheme == "" {
|
|
|
|
u.Scheme = "http"
|
|
|
|
u.Host = src
|
|
|
|
u.Path = ""
|
|
|
|
}
|
2015-04-15 11:43:15 +00:00
|
|
|
imageImportConfig.OutStream.Write(sf.FormatStatus("", "Downloading from %s", u))
|
2015-03-29 21:17:23 +00:00
|
|
|
resp, err = httputils.Download(u.String())
|
2014-08-05 05:29:14 +00:00
|
|
|
if err != nil {
|
2015-03-25 07:44:12 +00:00
|
|
|
return err
|
2014-08-05 05:29:14 +00:00
|
|
|
}
|
2015-02-24 08:51:46 +00:00
|
|
|
progressReader := progressreader.New(progressreader.Config{
|
|
|
|
In: resp.Body,
|
2015-04-15 11:43:15 +00:00
|
|
|
Out: imageImportConfig.OutStream,
|
2015-02-24 08:51:46 +00:00
|
|
|
Formatter: sf,
|
|
|
|
Size: int(resp.ContentLength),
|
|
|
|
NewLines: true,
|
|
|
|
ID: "",
|
|
|
|
Action: "Importing",
|
|
|
|
})
|
2014-08-05 05:29:14 +00:00
|
|
|
defer progressReader.Close()
|
|
|
|
archive = progressReader
|
|
|
|
}
|
2015-02-05 11:26:05 +00:00
|
|
|
|
2015-04-16 21:26:33 +00:00
|
|
|
img, err := s.graph.Create(archive, "", "", "Imported from "+src, "", nil, imageImportConfig.ContainerConfig)
|
2014-08-05 05:29:14 +00:00
|
|
|
if err != nil {
|
2015-03-25 07:44:12 +00:00
|
|
|
return err
|
2014-08-05 05:29:14 +00:00
|
|
|
}
|
|
|
|
// Optionally register the image at REPO/TAG
|
|
|
|
if repo != "" {
|
2015-04-14 02:46:29 +00:00
|
|
|
if err := s.Tag(repo, tag, img.ID, true); err != nil {
|
2015-03-25 07:44:12 +00:00
|
|
|
return err
|
2014-08-05 05:29:14 +00:00
|
|
|
}
|
|
|
|
}
|
2015-04-15 11:43:15 +00:00
|
|
|
imageImportConfig.OutStream.Write(sf.FormatStatus("", img.ID))
|
2014-10-22 13:48:02 +00:00
|
|
|
logID := img.ID
|
|
|
|
if tag != "" {
|
2015-02-27 02:23:50 +00:00
|
|
|
logID = utils.ImageReference(logID, tag)
|
2014-10-22 13:48:02 +00:00
|
|
|
}
|
2015-04-03 22:17:49 +00:00
|
|
|
|
|
|
|
s.eventsService.Log("import", logID, "")
|
2015-03-25 07:44:12 +00:00
|
|
|
return nil
|
2014-08-05 05:29:14 +00:00
|
|
|
}
|