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