2018-02-07 20:52:47 +00:00
|
|
|
package images // import "github.com/docker/docker/daemon/images"
|
2015-11-18 22:20:54 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"io"
|
|
|
|
"net/http"
|
|
|
|
"net/url"
|
|
|
|
"runtime"
|
2017-02-05 11:39:37 +00:00
|
|
|
"strings"
|
2015-11-18 22:20:54 +00:00
|
|
|
"time"
|
|
|
|
|
2017-01-26 00:54:18 +00:00
|
|
|
"github.com/docker/distribution/reference"
|
2016-09-06 18:18:12 +00:00
|
|
|
"github.com/docker/docker/api/types/container"
|
2016-03-16 23:07:41 +00:00
|
|
|
"github.com/docker/docker/builder/dockerfile"
|
2017-06-01 21:05:44 +00:00
|
|
|
"github.com/docker/docker/builder/remotecontext"
|
2015-11-18 22:20:54 +00:00
|
|
|
"github.com/docker/docker/dockerversion"
|
2018-01-11 19:53:06 +00:00
|
|
|
"github.com/docker/docker/errdefs"
|
2015-11-18 22:20:54 +00:00
|
|
|
"github.com/docker/docker/image"
|
|
|
|
"github.com/docker/docker/layer"
|
2016-02-16 19:19:23 +00:00
|
|
|
"github.com/docker/docker/pkg/archive"
|
2015-11-14 00:59:01 +00:00
|
|
|
"github.com/docker/docker/pkg/progress"
|
2015-11-18 22:20:54 +00:00
|
|
|
"github.com/docker/docker/pkg/streamformatter"
|
2017-01-26 00:54:18 +00:00
|
|
|
"github.com/pkg/errors"
|
2015-11-18 22:20:54 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// ImportImage 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.
|
2018-02-07 20:52:47 +00:00
|
|
|
func (i *ImageService) ImportImage(src string, repository, os string, tag string, msg string, inConfig io.ReadCloser, outStream io.Writer, changes []string) error {
|
2015-11-18 22:20:54 +00:00
|
|
|
var (
|
2016-04-07 21:29:18 +00:00
|
|
|
rc io.ReadCloser
|
|
|
|
resp *http.Response
|
|
|
|
newRef reference.Named
|
2015-11-18 22:20:54 +00:00
|
|
|
)
|
|
|
|
|
2017-08-08 19:43:48 +00:00
|
|
|
// Default the operating system if not supplied.
|
|
|
|
if os == "" {
|
|
|
|
os = runtime.GOOS
|
2017-05-31 18:11:05 +00:00
|
|
|
}
|
|
|
|
|
2016-04-07 21:29:18 +00:00
|
|
|
if repository != "" {
|
|
|
|
var err error
|
2017-01-26 00:54:18 +00:00
|
|
|
newRef, err = reference.ParseNormalizedNamed(repository)
|
2016-04-07 21:29:18 +00:00
|
|
|
if err != nil {
|
2017-11-29 04:09:37 +00:00
|
|
|
return errdefs.InvalidParameter(err)
|
2016-04-07 21:29:18 +00:00
|
|
|
}
|
|
|
|
if _, isCanonical := newRef.(reference.Canonical); isCanonical {
|
2017-11-29 04:09:37 +00:00
|
|
|
return errdefs.InvalidParameter(errors.New("cannot import digest reference"))
|
2016-04-07 21:29:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if tag != "" {
|
|
|
|
newRef, err = reference.WithTag(newRef, tag)
|
|
|
|
if err != nil {
|
2017-11-29 04:09:37 +00:00
|
|
|
return errdefs.InvalidParameter(err)
|
2016-04-07 21:29:18 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-09-19 19:14:46 +00:00
|
|
|
config, err := dockerfile.BuildFromConfig(&container.Config{}, changes, os)
|
2016-03-16 23:07:41 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2015-11-18 22:20:54 +00:00
|
|
|
if src == "-" {
|
2016-02-16 19:19:23 +00:00
|
|
|
rc = inConfig
|
2015-11-18 22:20:54 +00:00
|
|
|
} else {
|
|
|
|
inConfig.Close()
|
2017-02-05 11:39:37 +00:00
|
|
|
if len(strings.Split(src, "://")) == 1 {
|
|
|
|
src = "http://" + src
|
|
|
|
}
|
2015-11-18 22:20:54 +00:00
|
|
|
u, err := url.Parse(src)
|
|
|
|
if err != nil {
|
2017-11-29 04:09:37 +00:00
|
|
|
return errdefs.InvalidParameter(err)
|
2015-11-18 22:20:54 +00:00
|
|
|
}
|
2017-02-05 11:39:37 +00:00
|
|
|
|
2017-06-01 21:05:44 +00:00
|
|
|
resp, err = remotecontext.GetWithStatusError(u.String())
|
2015-11-18 22:20:54 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2017-05-01 18:54:56 +00:00
|
|
|
outStream.Write(streamformatter.FormatStatus("", "Downloading from %s", u))
|
|
|
|
progressOutput := streamformatter.NewJSONProgressOutput(outStream, true)
|
2016-02-16 19:19:23 +00:00
|
|
|
rc = progress.NewProgressReader(resp.Body, progressOutput, resp.ContentLength, "", "Importing")
|
2015-11-18 22:20:54 +00:00
|
|
|
}
|
|
|
|
|
2016-02-16 19:19:23 +00:00
|
|
|
defer rc.Close()
|
2015-11-18 22:20:54 +00:00
|
|
|
if len(msg) == 0 {
|
|
|
|
msg = "Imported from " + src
|
|
|
|
}
|
2016-02-16 19:19:23 +00:00
|
|
|
|
|
|
|
inflatedLayerData, err := archive.DecompressStream(rc)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2018-02-02 22:18:46 +00:00
|
|
|
l, err := i.layerStores[os].Register(inflatedLayerData, "")
|
2015-11-18 22:20:54 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2018-02-02 22:18:46 +00:00
|
|
|
defer layer.ReleaseAndLog(i.layerStores[os], l)
|
2015-11-18 22:20:54 +00:00
|
|
|
|
|
|
|
created := time.Now().UTC()
|
|
|
|
imgConfig, err := json.Marshal(&image.Image{
|
|
|
|
V1Image: image.V1Image{
|
|
|
|
DockerVersion: dockerversion.Version,
|
|
|
|
Config: config,
|
|
|
|
Architecture: runtime.GOARCH,
|
2017-08-08 19:43:48 +00:00
|
|
|
OS: os,
|
2015-11-18 22:20:54 +00:00
|
|
|
Created: created,
|
|
|
|
Comment: msg,
|
|
|
|
},
|
|
|
|
RootFS: &image.RootFS{
|
|
|
|
Type: "layers",
|
|
|
|
DiffIDs: []layer.DiffID{l.DiffID()},
|
|
|
|
},
|
|
|
|
History: []image.History{{
|
|
|
|
Created: created,
|
|
|
|
Comment: msg,
|
|
|
|
}},
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2018-02-02 22:18:46 +00:00
|
|
|
id, err := i.imageStore.Create(imgConfig)
|
2015-11-18 22:20:54 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2015-12-04 21:55:15 +00:00
|
|
|
// FIXME: connect with commit code and call refstore directly
|
2015-11-18 22:20:54 +00:00
|
|
|
if newRef != nil {
|
2018-02-02 22:18:46 +00:00
|
|
|
if err := i.TagImageWithReference(id, newRef); err != nil {
|
2015-11-18 22:20:54 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-02-02 22:18:46 +00:00
|
|
|
i.LogImageEvent(id.String(), id.String(), "import")
|
2017-05-01 18:54:56 +00:00
|
|
|
outStream.Write(streamformatter.FormatStatus("", id.String()))
|
2015-11-18 22:20:54 +00:00
|
|
|
return nil
|
|
|
|
}
|