2014-08-05 01:29:14 -04:00
|
|
|
package graph
|
|
|
|
|
|
|
|
import (
|
2015-04-15 07:43:15 -04:00
|
|
|
"io"
|
2014-08-05 01:29:14 -04:00
|
|
|
"net/http"
|
|
|
|
"net/url"
|
|
|
|
|
2014-09-30 02:23:36 -04:00
|
|
|
"github.com/docker/docker/pkg/archive"
|
2015-03-29 17:17:23 -04:00
|
|
|
"github.com/docker/docker/pkg/httputils"
|
2015-02-24 03:51:46 -05:00
|
|
|
"github.com/docker/docker/pkg/progressreader"
|
2015-03-17 22:18:41 -04:00
|
|
|
"github.com/docker/docker/pkg/streamformatter"
|
2015-02-05 06:26:05 -05:00
|
|
|
"github.com/docker/docker/runconfig"
|
2014-08-05 01:29:14 -04:00
|
|
|
"github.com/docker/docker/utils"
|
|
|
|
)
|
|
|
|
|
2015-07-29 19:45:47 -04:00
|
|
|
// Import imports an image, getting the archived layer data either from
|
|
|
|
// inConfig (if src is "-"), or from a URI specified in src. Progress output is
|
|
|
|
// written to outStream. Repository and tag names can optionally be given in
|
|
|
|
// the repo and tag arguments, respectively.
|
|
|
|
func (s *TagStore) Import(src string, repo string, tag string, inConfig io.ReadCloser, outStream io.Writer, containerConfig *runconfig.Config) error {
|
2014-08-05 01:29:14 -04:00
|
|
|
var (
|
2015-05-12 14:18:54 -04:00
|
|
|
sf = streamformatter.NewJSONStreamFormatter()
|
2015-08-03 21:52:54 -04:00
|
|
|
archive archive.Reader
|
2015-04-16 17:26:33 -04:00
|
|
|
resp *http.Response
|
2014-08-05 01:29:14 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
if src == "-" {
|
2015-07-29 19:45:47 -04:00
|
|
|
archive = inConfig
|
2014-08-05 01:29:14 -04:00
|
|
|
} else {
|
|
|
|
u, err := url.Parse(src)
|
|
|
|
if err != nil {
|
2015-03-25 03:44:12 -04:00
|
|
|
return err
|
2014-08-05 01:29:14 -04:00
|
|
|
}
|
|
|
|
if u.Scheme == "" {
|
|
|
|
u.Scheme = "http"
|
|
|
|
u.Host = src
|
|
|
|
u.Path = ""
|
|
|
|
}
|
2015-07-29 19:45:47 -04:00
|
|
|
outStream.Write(sf.FormatStatus("", "Downloading from %s", u))
|
2015-03-29 17:17:23 -04:00
|
|
|
resp, err = httputils.Download(u.String())
|
2014-08-05 01:29:14 -04:00
|
|
|
if err != nil {
|
2015-03-25 03:44:12 -04:00
|
|
|
return err
|
2014-08-05 01:29:14 -04:00
|
|
|
}
|
2015-02-24 03:51:46 -05:00
|
|
|
progressReader := progressreader.New(progressreader.Config{
|
|
|
|
In: resp.Body,
|
2015-07-29 19:45:47 -04:00
|
|
|
Out: outStream,
|
2015-02-24 03:51:46 -05:00
|
|
|
Formatter: sf,
|
2015-07-23 17:19:58 -04:00
|
|
|
Size: resp.ContentLength,
|
2015-02-24 03:51:46 -05:00
|
|
|
NewLines: true,
|
|
|
|
ID: "",
|
|
|
|
Action: "Importing",
|
|
|
|
})
|
2014-08-05 01:29:14 -04:00
|
|
|
defer progressReader.Close()
|
|
|
|
archive = progressReader
|
|
|
|
}
|
2015-02-05 06:26:05 -05:00
|
|
|
|
2015-07-29 19:45:47 -04:00
|
|
|
img, err := s.graph.Create(archive, "", "", "Imported from "+src, "", nil, containerConfig)
|
2014-08-05 01:29:14 -04:00
|
|
|
if err != nil {
|
2015-03-25 03:44:12 -04:00
|
|
|
return err
|
2014-08-05 01:29:14 -04:00
|
|
|
}
|
|
|
|
// Optionally register the image at REPO/TAG
|
|
|
|
if repo != "" {
|
2015-04-13 22:46:29 -04:00
|
|
|
if err := s.Tag(repo, tag, img.ID, true); err != nil {
|
2015-03-25 03:44:12 -04:00
|
|
|
return err
|
2014-08-05 01:29:14 -04:00
|
|
|
}
|
|
|
|
}
|
2015-07-29 19:45:47 -04:00
|
|
|
outStream.Write(sf.FormatStatus("", img.ID))
|
2014-10-22 09:48:02 -04:00
|
|
|
logID := img.ID
|
|
|
|
if tag != "" {
|
2015-02-26 21:23:50 -05:00
|
|
|
logID = utils.ImageReference(logID, tag)
|
2014-10-22 09:48:02 -04:00
|
|
|
}
|
2015-04-03 18:17:49 -04:00
|
|
|
|
|
|
|
s.eventsService.Log("import", logID, "")
|
2015-03-25 03:44:12 -04:00
|
|
|
return nil
|
2014-08-05 01:29:14 -04:00
|
|
|
}
|