|
@@ -6,37 +6,29 @@ import (
|
|
|
|
|
|
"github.com/Sirupsen/logrus"
|
|
"github.com/Sirupsen/logrus"
|
|
"github.com/docker/docker/api/types/backend"
|
|
"github.com/docker/docker/api/types/backend"
|
|
- "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"
|
|
|
|
+ dockerimage "github.com/docker/docker/image"
|
|
"github.com/pkg/errors"
|
|
"github.com/pkg/errors"
|
|
"golang.org/x/net/context"
|
|
"golang.org/x/net/context"
|
|
)
|
|
)
|
|
|
|
|
|
type buildStage struct {
|
|
type buildStage struct {
|
|
- id string
|
|
|
|
- config *container.Config
|
|
|
|
|
|
+ id string
|
|
}
|
|
}
|
|
|
|
|
|
-func newBuildStageFromImage(image builder.Image) *buildStage {
|
|
|
|
- return &buildStage{id: image.ImageID(), config: image.RunConfig()}
|
|
|
|
|
|
+func newBuildStage(imageID string) *buildStage {
|
|
|
|
+ return &buildStage{id: imageID}
|
|
}
|
|
}
|
|
|
|
|
|
func (b *buildStage) ImageID() string {
|
|
func (b *buildStage) ImageID() string {
|
|
return b.id
|
|
return b.id
|
|
}
|
|
}
|
|
|
|
|
|
-func (b *buildStage) RunConfig() *container.Config {
|
|
|
|
- return b.config
|
|
|
|
-}
|
|
|
|
-
|
|
|
|
-func (b *buildStage) update(imageID string, runConfig *container.Config) {
|
|
|
|
|
|
+func (b *buildStage) update(imageID string) {
|
|
b.id = imageID
|
|
b.id = imageID
|
|
- b.config = runConfig
|
|
|
|
}
|
|
}
|
|
|
|
|
|
-var _ builder.Image = &buildStage{}
|
|
|
|
-
|
|
|
|
// buildStages tracks each stage of a build so they can be retrieved by index
|
|
// buildStages tracks each stage of a build so they can be retrieved by index
|
|
// or by name.
|
|
// or by name.
|
|
type buildStages struct {
|
|
type buildStages struct {
|
|
@@ -48,12 +40,12 @@ func newBuildStages() *buildStages {
|
|
return &buildStages{byName: make(map[string]*buildStage)}
|
|
return &buildStages{byName: make(map[string]*buildStage)}
|
|
}
|
|
}
|
|
|
|
|
|
-func (s *buildStages) getByName(name string) (builder.Image, bool) {
|
|
|
|
|
|
+func (s *buildStages) getByName(name string) (*buildStage, bool) {
|
|
stage, ok := s.byName[strings.ToLower(name)]
|
|
stage, ok := s.byName[strings.ToLower(name)]
|
|
return stage, ok
|
|
return stage, ok
|
|
}
|
|
}
|
|
|
|
|
|
-func (s *buildStages) get(indexOrName string) (builder.Image, error) {
|
|
|
|
|
|
+func (s *buildStages) get(indexOrName string) (*buildStage, error) {
|
|
index, err := strconv.Atoi(indexOrName)
|
|
index, err := strconv.Atoi(indexOrName)
|
|
if err == nil {
|
|
if err == nil {
|
|
if err := s.validateIndex(index); err != nil {
|
|
if err := s.validateIndex(index); err != nil {
|
|
@@ -78,7 +70,7 @@ func (s *buildStages) validateIndex(i int) error {
|
|
}
|
|
}
|
|
|
|
|
|
func (s *buildStages) add(name string, image builder.Image) error {
|
|
func (s *buildStages) add(name string, image builder.Image) error {
|
|
- stage := newBuildStageFromImage(image)
|
|
|
|
|
|
+ stage := newBuildStage(image.ImageID())
|
|
name = strings.ToLower(name)
|
|
name = strings.ToLower(name)
|
|
if len(name) > 0 {
|
|
if len(name) > 0 {
|
|
if _, ok := s.byName[name]; ok {
|
|
if _, ok := s.byName[name]; ok {
|
|
@@ -90,8 +82,8 @@ func (s *buildStages) add(name string, image builder.Image) error {
|
|
return nil
|
|
return nil
|
|
}
|
|
}
|
|
|
|
|
|
-func (s *buildStages) update(imageID string, runConfig *container.Config) {
|
|
|
|
- s.sequence[len(s.sequence)-1].update(imageID, runConfig)
|
|
|
|
|
|
+func (s *buildStages) update(imageID string) {
|
|
|
|
+ s.sequence[len(s.sequence)-1].update(imageID)
|
|
}
|
|
}
|
|
|
|
|
|
type getAndMountFunc func(string) (builder.Image, builder.ReleaseableLayer, error)
|
|
type getAndMountFunc func(string) (builder.Image, builder.ReleaseableLayer, error)
|
|
@@ -100,6 +92,7 @@ type getAndMountFunc func(string) (builder.Image, builder.ReleaseableLayer, erro
|
|
// all images so they can be unmounted at the end of the build.
|
|
// all images so they can be unmounted at the end of the build.
|
|
type imageSources struct {
|
|
type imageSources struct {
|
|
byImageID map[string]*imageMount
|
|
byImageID map[string]*imageMount
|
|
|
|
+ withoutID []*imageMount
|
|
getImage getAndMountFunc
|
|
getImage getAndMountFunc
|
|
cache pathCache // TODO: remove
|
|
cache pathCache // TODO: remove
|
|
}
|
|
}
|
|
@@ -129,7 +122,7 @@ func (m *imageSources) Get(idOrRef string) (*imageMount, error) {
|
|
return nil, err
|
|
return nil, err
|
|
}
|
|
}
|
|
im := newImageMount(image, layer)
|
|
im := newImageMount(image, layer)
|
|
- m.byImageID[image.ImageID()] = im
|
|
|
|
|
|
+ m.Add(im)
|
|
return im, nil
|
|
return im, nil
|
|
}
|
|
}
|
|
|
|
|
|
@@ -140,9 +133,25 @@ func (m *imageSources) Unmount() (retErr error) {
|
|
retErr = err
|
|
retErr = err
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
+ for _, im := range m.withoutID {
|
|
|
|
+ if err := im.unmount(); err != nil {
|
|
|
|
+ logrus.Error(err)
|
|
|
|
+ retErr = err
|
|
|
|
+ }
|
|
|
|
+ }
|
|
return
|
|
return
|
|
}
|
|
}
|
|
|
|
|
|
|
|
+func (m *imageSources) Add(im *imageMount) {
|
|
|
|
+ switch im.image {
|
|
|
|
+ case nil:
|
|
|
|
+ im.image = &dockerimage.Image{}
|
|
|
|
+ m.withoutID = append(m.withoutID, im)
|
|
|
|
+ default:
|
|
|
|
+ m.byImageID[im.image.ImageID()] = im
|
|
|
|
+ }
|
|
|
|
+}
|
|
|
|
+
|
|
// 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 {
|
|
image builder.Image
|
|
image builder.Image
|
|
@@ -164,7 +173,7 @@ func (im *imageMount) Source() (builder.Source, error) {
|
|
if err != nil {
|
|
if err != nil {
|
|
return nil, errors.Wrapf(err, "failed to mount %s", im.image.ImageID())
|
|
return nil, errors.Wrapf(err, "failed to mount %s", im.image.ImageID())
|
|
}
|
|
}
|
|
- source, err := remotecontext.NewLazyContext(mountPath)
|
|
|
|
|
|
+ source, err := remotecontext.NewLazySource(mountPath)
|
|
if err != nil {
|
|
if err != nil {
|
|
return nil, errors.Wrapf(err, "failed to create lazycontext for %s", mountPath)
|
|
return nil, errors.Wrapf(err, "failed to create lazycontext for %s", mountPath)
|
|
}
|
|
}
|
|
@@ -180,6 +189,7 @@ func (im *imageMount) unmount() error {
|
|
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.image.ImageID())
|
|
return errors.Wrapf(err, "failed to unmount previous build image %s", im.image.ImageID())
|
|
}
|
|
}
|
|
|
|
+ im.layer = nil
|
|
return nil
|
|
return nil
|
|
}
|
|
}
|
|
|
|
|
|
@@ -187,6 +197,10 @@ func (im *imageMount) Image() builder.Image {
|
|
return im.image
|
|
return im.image
|
|
}
|
|
}
|
|
|
|
|
|
|
|
+func (im *imageMount) Layer() builder.ReleaseableLayer {
|
|
|
|
+ return im.layer
|
|
|
|
+}
|
|
|
|
+
|
|
func (im *imageMount) ImageID() string {
|
|
func (im *imageMount) ImageID() string {
|
|
return im.image.ImageID()
|
|
return im.image.ImageID()
|
|
}
|
|
}
|