|
@@ -5,10 +5,12 @@ import (
|
|
"strings"
|
|
"strings"
|
|
|
|
|
|
"github.com/Sirupsen/logrus"
|
|
"github.com/Sirupsen/logrus"
|
|
|
|
+ "github.com/docker/docker/api/types/backend"
|
|
"github.com/docker/docker/api/types/container"
|
|
"github.com/docker/docker/api/types/container"
|
|
"github.com/docker/docker/builder"
|
|
"github.com/docker/docker/builder"
|
|
"github.com/docker/docker/builder/remotecontext"
|
|
"github.com/docker/docker/builder/remotecontext"
|
|
"github.com/pkg/errors"
|
|
"github.com/pkg/errors"
|
|
|
|
+ "golang.org/x/net/context"
|
|
)
|
|
)
|
|
|
|
|
|
type pathCache interface {
|
|
type pathCache interface {
|
|
@@ -16,66 +18,128 @@ type pathCache interface {
|
|
Store(key, value interface{})
|
|
Store(key, value interface{})
|
|
}
|
|
}
|
|
|
|
|
|
-// imageContexts is a helper for stacking up built image rootfs and reusing
|
|
|
|
-// them as contexts
|
|
|
|
-type imageContexts struct {
|
|
|
|
- list []*imageMount
|
|
|
|
- byName map[string]*imageMount
|
|
|
|
- cache pathCache
|
|
|
|
|
|
+type buildStage struct {
|
|
|
|
+ id string
|
|
|
|
+ config *container.Config
|
|
}
|
|
}
|
|
|
|
|
|
-func (ic *imageContexts) add(name string, im *imageMount) error {
|
|
|
|
- if len(name) > 0 {
|
|
|
|
- if ic.byName == nil {
|
|
|
|
- ic.byName = make(map[string]*imageMount)
|
|
|
|
- }
|
|
|
|
- if _, ok := ic.byName[name]; ok {
|
|
|
|
- return errors.Errorf("duplicate name %s", name)
|
|
|
|
- }
|
|
|
|
- ic.byName[name] = im
|
|
|
|
- }
|
|
|
|
- ic.list = append(ic.list, im)
|
|
|
|
- return nil
|
|
|
|
|
|
+func newBuildStageFromImage(image builder.Image) *buildStage {
|
|
|
|
+ return &buildStage{id: image.ImageID(), config: image.RunConfig()}
|
|
}
|
|
}
|
|
|
|
|
|
-func (ic *imageContexts) update(imageID string, runConfig *container.Config) {
|
|
|
|
- ic.list[len(ic.list)-1].update(imageID, runConfig)
|
|
|
|
|
|
+func (b *buildStage) ImageID() string {
|
|
|
|
+ return b.id
|
|
}
|
|
}
|
|
|
|
|
|
-func (ic *imageContexts) validate(i int) error {
|
|
|
|
- if i < 0 || i >= len(ic.list)-1 {
|
|
|
|
- if i == len(ic.list)-1 {
|
|
|
|
- return errors.New("refers to current build stage")
|
|
|
|
- }
|
|
|
|
- return errors.New("index out of bounds")
|
|
|
|
- }
|
|
|
|
- return nil
|
|
|
|
|
|
+func (b *buildStage) RunConfig() *container.Config {
|
|
|
|
+ return b.config
|
|
}
|
|
}
|
|
|
|
|
|
-func (ic *imageContexts) getMount(indexOrName string) (*imageMount, error) {
|
|
|
|
|
|
+func (b *buildStage) update(imageID string, runConfig *container.Config) {
|
|
|
|
+ b.id = imageID
|
|
|
|
+ b.config = runConfig
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+var _ builder.Image = &buildStage{}
|
|
|
|
+
|
|
|
|
+// buildStages tracks each stage of a build so they can be retrieved by index
|
|
|
|
+// or by name.
|
|
|
|
+type buildStages struct {
|
|
|
|
+ sequence []*buildStage
|
|
|
|
+ byName map[string]*buildStage
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+func newBuildStages() *buildStages {
|
|
|
|
+ return &buildStages{byName: make(map[string]*buildStage)}
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+func (s *buildStages) getByName(name string) (builder.Image, bool) {
|
|
|
|
+ stage, ok := s.byName[strings.ToLower(name)]
|
|
|
|
+ return stage, ok
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+func (s *buildStages) get(indexOrName string) (builder.Image, error) {
|
|
index, err := strconv.Atoi(indexOrName)
|
|
index, err := strconv.Atoi(indexOrName)
|
|
if err == nil {
|
|
if err == nil {
|
|
- if err := ic.validate(index); err != nil {
|
|
|
|
|
|
+ if err := s.validateIndex(index); err != nil {
|
|
return nil, err
|
|
return nil, err
|
|
}
|
|
}
|
|
- return ic.list[index], nil
|
|
|
|
|
|
+ return s.sequence[index], nil
|
|
}
|
|
}
|
|
- if im, ok := ic.byName[strings.ToLower(indexOrName)]; ok {
|
|
|
|
|
|
+ if im, ok := s.byName[strings.ToLower(indexOrName)]; ok {
|
|
return im, nil
|
|
return im, nil
|
|
}
|
|
}
|
|
return nil, nil
|
|
return nil, nil
|
|
}
|
|
}
|
|
|
|
|
|
-func (ic *imageContexts) unmount() (retErr error) {
|
|
|
|
- for _, iml := range append([][]*imageMount{}, ic.list, ic.implicitMounts) {
|
|
|
|
- for _, im := range iml {
|
|
|
|
- if err := im.unmount(); err != nil {
|
|
|
|
- logrus.Error(err)
|
|
|
|
- retErr = err
|
|
|
|
- }
|
|
|
|
|
|
+func (s *buildStages) validateIndex(i int) error {
|
|
|
|
+ if i < 0 || i >= len(s.sequence)-1 {
|
|
|
|
+ if i == len(s.sequence)-1 {
|
|
|
|
+ return errors.New("refers to current build stage")
|
|
}
|
|
}
|
|
|
|
+ return errors.New("index out of bounds")
|
|
}
|
|
}
|
|
- for _, im := range ic.byName {
|
|
|
|
|
|
+ return nil
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+func (s *buildStages) add(name string, image builder.Image) error {
|
|
|
|
+ stage := newBuildStageFromImage(image)
|
|
|
|
+ name = strings.ToLower(name)
|
|
|
|
+ if len(name) > 0 {
|
|
|
|
+ if _, ok := s.byName[name]; ok {
|
|
|
|
+ return errors.Errorf("duplicate name %s", name)
|
|
|
|
+ }
|
|
|
|
+ s.byName[name] = stage
|
|
|
|
+ }
|
|
|
|
+ s.sequence = append(s.sequence, stage)
|
|
|
|
+ return nil
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+func (s *buildStages) update(imageID string, runConfig *container.Config) {
|
|
|
|
+ s.sequence[len(s.sequence)-1].update(imageID, runConfig)
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+type getAndMountFunc func(string) (builder.Image, builder.ReleaseableLayer, error)
|
|
|
|
+
|
|
|
|
+// imageSources mounts images and provides a cache for mounted images. It tracks
|
|
|
|
+// all images so they can be unmounted at the end of the build.
|
|
|
|
+type imageSources struct {
|
|
|
|
+ byImageID map[string]*imageMount
|
|
|
|
+ getImage getAndMountFunc
|
|
|
|
+ cache pathCache // TODO: remove
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+func newImageSources(ctx context.Context, options builderOptions) *imageSources {
|
|
|
|
+ getAndMount := func(idOrRef string) (builder.Image, builder.ReleaseableLayer, error) {
|
|
|
|
+ return options.Backend.GetImageAndReleasableLayer(ctx, idOrRef, backend.GetImageAndLayerOptions{
|
|
|
|
+ ForcePull: options.Options.PullParent,
|
|
|
|
+ AuthConfig: options.Options.AuthConfigs,
|
|
|
|
+ Output: options.ProgressWriter.Output,
|
|
|
|
+ })
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ return &imageSources{
|
|
|
|
+ byImageID: make(map[string]*imageMount),
|
|
|
|
+ getImage: getAndMount,
|
|
|
|
+ }
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+func (m *imageSources) Get(idOrRef string) (*imageMount, error) {
|
|
|
|
+ if im, ok := m.byImageID[idOrRef]; ok {
|
|
|
|
+ return im, nil
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ image, layer, err := m.getImage(idOrRef)
|
|
|
|
+ if err != nil {
|
|
|
|
+ return nil, err
|
|
|
|
+ }
|
|
|
|
+ im := newImageMount(image, layer)
|
|
|
|
+ m.byImageID[image.ImageID()] = im
|
|
|
|
+ return im, nil
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+func (m *imageSources) Unmount() (retErr error) {
|
|
|
|
+ for _, im := range m.byImageID {
|
|
if err := im.unmount(); err != nil {
|
|
if err := im.unmount(); err != nil {
|
|
logrus.Error(err)
|
|
logrus.Error(err)
|
|
retErr = err
|
|
retErr = err
|
|
@@ -84,47 +148,43 @@ func (ic *imageContexts) unmount() (retErr error) {
|
|
return
|
|
return
|
|
}
|
|
}
|
|
|
|
|
|
-// TODO: remove getCache/setCache from imageContexts
|
|
|
|
-func (ic *imageContexts) getCache(id, path string) (interface{}, bool) {
|
|
|
|
- if ic.cache != nil {
|
|
|
|
|
|
+// TODO: remove getCache/setCache from imageSources
|
|
|
|
+func (m *imageSources) getCache(id, path string) (interface{}, bool) {
|
|
|
|
+ if m.cache != nil {
|
|
if id == "" {
|
|
if id == "" {
|
|
return nil, false
|
|
return nil, false
|
|
}
|
|
}
|
|
- return ic.cache.Load(id + path)
|
|
|
|
|
|
+ return m.cache.Load(id + path)
|
|
}
|
|
}
|
|
return nil, false
|
|
return nil, false
|
|
}
|
|
}
|
|
|
|
|
|
-func (ic *imageContexts) setCache(id, path string, v interface{}) {
|
|
|
|
- if ic.cache != nil {
|
|
|
|
- ic.cache.Store(id+path, v)
|
|
|
|
|
|
+func (m *imageSources) setCache(id, path string, v interface{}) {
|
|
|
|
+ if m.cache != nil {
|
|
|
|
+ m.cache.Store(id+path, v)
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
// imageMount is a reference to an image that can be used as a builder.Source
|
|
// imageMount is a reference to an image that can be used as a builder.Source
|
|
type imageMount struct {
|
|
type imageMount struct {
|
|
- id string
|
|
|
|
- source builder.Source
|
|
|
|
- runConfig *container.Config
|
|
|
|
- layer builder.ReleaseableLayer
|
|
|
|
|
|
+ image builder.Image
|
|
|
|
+ source builder.Source
|
|
|
|
+ layer builder.ReleaseableLayer
|
|
}
|
|
}
|
|
|
|
|
|
func newImageMount(image builder.Image, layer builder.ReleaseableLayer) *imageMount {
|
|
func newImageMount(image builder.Image, layer builder.ReleaseableLayer) *imageMount {
|
|
- im := &imageMount{layer: layer}
|
|
|
|
- if image != nil {
|
|
|
|
- im.update(image.ImageID(), image.RunConfig())
|
|
|
|
- }
|
|
|
|
|
|
+ im := &imageMount{image: image, layer: layer}
|
|
return im
|
|
return im
|
|
}
|
|
}
|
|
|
|
|
|
-func (im *imageMount) context() (builder.Source, error) {
|
|
|
|
|
|
+func (im *imageMount) Source() (builder.Source, error) {
|
|
if im.source == nil {
|
|
if im.source == nil {
|
|
- if im.id == "" || im.layer == nil {
|
|
|
|
|
|
+ if im.layer == nil {
|
|
return nil, errors.Errorf("empty context")
|
|
return nil, errors.Errorf("empty context")
|
|
}
|
|
}
|
|
- mountPath, err := im.layer.Mount(im.id)
|
|
|
|
|
|
+ mountPath, err := im.layer.Mount()
|
|
if err != nil {
|
|
if err != nil {
|
|
- return nil, errors.Wrapf(err, "failed to mount %s", im.id)
|
|
|
|
|
|
+ return nil, errors.Wrapf(err, "failed to mount %s", im.image.ImageID())
|
|
}
|
|
}
|
|
source, err := remotecontext.NewLazyContext(mountPath)
|
|
source, err := remotecontext.NewLazyContext(mountPath)
|
|
if err != nil {
|
|
if err != nil {
|
|
@@ -140,20 +200,11 @@ func (im *imageMount) unmount() error {
|
|
return nil
|
|
return nil
|
|
}
|
|
}
|
|
if err := im.layer.Release(); err != nil {
|
|
if err := im.layer.Release(); err != nil {
|
|
- return errors.Wrapf(err, "failed to unmount previous build image %s", im.id)
|
|
|
|
|
|
+ return errors.Wrapf(err, "failed to unmount previous build image %s", im.image.ImageID())
|
|
}
|
|
}
|
|
return nil
|
|
return nil
|
|
}
|
|
}
|
|
|
|
|
|
-func (im *imageMount) update(imageID string, runConfig *container.Config) {
|
|
|
|
- im.id = imageID
|
|
|
|
- im.runConfig = runConfig
|
|
|
|
-}
|
|
|
|
-
|
|
|
|
-func (im *imageMount) ImageID() string {
|
|
|
|
- return im.id
|
|
|
|
-}
|
|
|
|
-
|
|
|
|
-func (im *imageMount) RunConfig() *container.Config {
|
|
|
|
- return im.runConfig
|
|
|
|
|
|
+func (im *imageMount) Image() builder.Image {
|
|
|
|
+ return im.image
|
|
}
|
|
}
|