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"
|
|
|
|
|
2015-09-10 22:01:18 +00:00
|
|
|
"github.com/docker/docker/context"
|
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-07-29 23:45:47 +00: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.
|
2015-09-10 22:01:18 +00:00
|
|
|
func (s *TagStore) Import(ctx context.Context, src string, repo string, tag string, msg string, inConfig io.ReadCloser, outStream io.Writer, containerConfig *runconfig.Config) error {
|
2014-08-05 05:29:14 +00:00
|
|
|
var (
|
2015-05-12 18:18:54 +00:00
|
|
|
sf = streamformatter.NewJSONStreamFormatter()
|
2015-08-20 21:20:07 +00:00
|
|
|
archive io.ReadCloser
|
2015-04-16 21:26:33 +00:00
|
|
|
resp *http.Response
|
2014-08-05 05:29:14 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
if src == "-" {
|
2015-07-29 23:45:47 +00:00
|
|
|
archive = inConfig
|
2014-08-05 05:29:14 +00:00
|
|
|
} else {
|
2015-08-20 21:20:07 +00:00
|
|
|
inConfig.Close()
|
2014-08-05 05:29:14 +00:00
|
|
|
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-07-29 23:45:47 +00:00
|
|
|
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-07-29 23:45:47 +00:00
|
|
|
Out: outStream,
|
2015-02-24 08:51:46 +00:00
|
|
|
Formatter: sf,
|
2015-07-23 21:19:58 +00:00
|
|
|
Size: resp.ContentLength,
|
2015-02-24 08:51:46 +00:00
|
|
|
NewLines: true,
|
|
|
|
ID: "",
|
|
|
|
Action: "Importing",
|
|
|
|
})
|
2014-08-05 05:29:14 +00:00
|
|
|
archive = progressReader
|
|
|
|
}
|
2015-02-05 11:26:05 +00:00
|
|
|
|
2015-08-20 21:20:07 +00:00
|
|
|
defer archive.Close()
|
2015-08-20 04:01:50 +00:00
|
|
|
if len(msg) == 0 {
|
|
|
|
msg = "Imported from " + src
|
|
|
|
}
|
|
|
|
|
|
|
|
img, err := s.graph.Create(archive, "", "", msg, "", nil, 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-07-29 23:45:47 +00:00
|
|
|
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
|
|
|
|
2015-09-10 22:01:18 +00:00
|
|
|
s.eventsService.Log(ctx, "import", logID, "")
|
2015-03-25 07:44:12 +00:00
|
|
|
return nil
|
2014-08-05 05:29:14 +00:00
|
|
|
}
|