2017-03-15 18:28:06 -04:00
|
|
|
package dockerfile
|
|
|
|
|
|
|
|
import (
|
2017-03-22 20:49:39 -04:00
|
|
|
"strconv"
|
|
|
|
"strings"
|
2017-03-15 18:28:06 -04:00
|
|
|
|
|
|
|
"github.com/Sirupsen/logrus"
|
2017-05-05 18:52:11 -04:00
|
|
|
"github.com/docker/docker/api/types/backend"
|
2017-03-22 21:36:08 -04:00
|
|
|
"github.com/docker/docker/api/types/container"
|
2017-03-15 18:28:06 -04:00
|
|
|
"github.com/docker/docker/builder"
|
|
|
|
"github.com/docker/docker/builder/remotecontext"
|
|
|
|
"github.com/pkg/errors"
|
2017-05-05 18:52:11 -04:00
|
|
|
"golang.org/x/net/context"
|
2017-03-15 18:28:06 -04:00
|
|
|
)
|
|
|
|
|
2017-04-13 14:37:32 -04:00
|
|
|
type pathCache interface {
|
|
|
|
Load(key interface{}) (value interface{}, ok bool)
|
|
|
|
Store(key, value interface{})
|
|
|
|
}
|
|
|
|
|
2017-05-05 18:52:11 -04:00
|
|
|
type buildStage struct {
|
|
|
|
id string
|
|
|
|
config *container.Config
|
2017-03-15 18:28:06 -04:00
|
|
|
}
|
|
|
|
|
2017-05-05 18:52:11 -04:00
|
|
|
func newBuildStageFromImage(image builder.Image) *buildStage {
|
|
|
|
return &buildStage{id: image.ImageID(), config: image.RunConfig()}
|
2017-03-15 18:28:06 -04:00
|
|
|
}
|
|
|
|
|
2017-05-05 18:52:11 -04:00
|
|
|
func (b *buildStage) ImageID() string {
|
|
|
|
return b.id
|
2017-03-15 18:28:06 -04:00
|
|
|
}
|
|
|
|
|
2017-05-05 18:52:11 -04:00
|
|
|
func (b *buildStage) RunConfig() *container.Config {
|
|
|
|
return b.config
|
2017-03-15 18:28:06 -04:00
|
|
|
}
|
|
|
|
|
2017-05-05 18:52:11 -04:00
|
|
|
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) {
|
2017-03-22 20:49:39 -04:00
|
|
|
index, err := strconv.Atoi(indexOrName)
|
|
|
|
if err == nil {
|
2017-05-05 18:52:11 -04:00
|
|
|
if err := s.validateIndex(index); err != nil {
|
2017-03-22 20:49:39 -04:00
|
|
|
return nil, err
|
2017-03-15 18:28:06 -04:00
|
|
|
}
|
2017-05-05 18:52:11 -04:00
|
|
|
return s.sequence[index], nil
|
2017-03-15 18:28:06 -04:00
|
|
|
}
|
2017-05-05 18:52:11 -04:00
|
|
|
if im, ok := s.byName[strings.ToLower(indexOrName)]; ok {
|
2017-03-22 20:49:39 -04:00
|
|
|
return im, nil
|
|
|
|
}
|
2017-03-27 21:36:28 -04:00
|
|
|
return nil, nil
|
2017-03-15 18:28:06 -04:00
|
|
|
}
|
|
|
|
|
2017-05-05 18:52:11 -04:00
|
|
|
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")
|
2017-03-22 20:49:39 -04:00
|
|
|
}
|
2017-05-05 18:52:11 -04:00
|
|
|
return errors.New("index out of bounds")
|
2017-03-22 20:49:39 -04:00
|
|
|
}
|
2017-05-05 18:52:11 -04:00
|
|
|
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 {
|
2017-03-22 20:49:39 -04:00
|
|
|
if err := im.unmount(); err != nil {
|
|
|
|
logrus.Error(err)
|
|
|
|
retErr = err
|
2017-03-15 18:28:06 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2017-05-05 18:52:11 -04:00
|
|
|
// TODO: remove getCache/setCache from imageSources
|
|
|
|
func (m *imageSources) getCache(id, path string) (interface{}, bool) {
|
|
|
|
if m.cache != nil {
|
2017-03-22 20:49:39 -04:00
|
|
|
if id == "" {
|
2017-03-15 18:28:06 -04:00
|
|
|
return nil, false
|
|
|
|
}
|
2017-05-05 18:52:11 -04:00
|
|
|
return m.cache.Load(id + path)
|
2017-03-15 18:28:06 -04:00
|
|
|
}
|
|
|
|
return nil, false
|
|
|
|
}
|
|
|
|
|
2017-05-05 18:52:11 -04:00
|
|
|
func (m *imageSources) setCache(id, path string, v interface{}) {
|
|
|
|
if m.cache != nil {
|
|
|
|
m.cache.Store(id+path, v)
|
2017-03-15 18:28:06 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-27 21:36:28 -04:00
|
|
|
// imageMount is a reference to an image that can be used as a builder.Source
|
2017-03-22 20:49:39 -04:00
|
|
|
type imageMount struct {
|
2017-05-05 18:52:11 -04:00
|
|
|
image builder.Image
|
|
|
|
source builder.Source
|
|
|
|
layer builder.ReleaseableLayer
|
2017-03-27 21:36:28 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func newImageMount(image builder.Image, layer builder.ReleaseableLayer) *imageMount {
|
2017-05-05 18:52:11 -04:00
|
|
|
im := &imageMount{image: image, layer: layer}
|
2017-03-27 21:36:28 -04:00
|
|
|
return im
|
2017-03-22 20:49:39 -04:00
|
|
|
}
|
|
|
|
|
2017-05-05 18:52:11 -04:00
|
|
|
func (im *imageMount) Source() (builder.Source, error) {
|
2017-03-20 18:22:29 -04:00
|
|
|
if im.source == nil {
|
2017-05-05 18:52:11 -04:00
|
|
|
if im.layer == nil {
|
2017-03-27 21:36:28 -04:00
|
|
|
return nil, errors.Errorf("empty context")
|
2017-03-22 20:49:39 -04:00
|
|
|
}
|
2017-05-05 18:52:11 -04:00
|
|
|
mountPath, err := im.layer.Mount()
|
2017-03-22 20:49:39 -04:00
|
|
|
if err != nil {
|
2017-05-05 18:52:11 -04:00
|
|
|
return nil, errors.Wrapf(err, "failed to mount %s", im.image.ImageID())
|
2017-03-22 20:49:39 -04:00
|
|
|
}
|
2017-03-27 21:36:28 -04:00
|
|
|
source, err := remotecontext.NewLazyContext(mountPath)
|
2017-03-22 20:49:39 -04:00
|
|
|
if err != nil {
|
2017-03-27 21:36:28 -04:00
|
|
|
return nil, errors.Wrapf(err, "failed to create lazycontext for %s", mountPath)
|
2017-03-22 20:49:39 -04:00
|
|
|
}
|
2017-03-20 18:22:29 -04:00
|
|
|
im.source = source
|
2017-03-22 20:49:39 -04:00
|
|
|
}
|
2017-03-20 18:22:29 -04:00
|
|
|
return im.source, nil
|
2017-03-22 20:49:39 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func (im *imageMount) unmount() error {
|
2017-05-05 13:05:25 -04:00
|
|
|
if im.layer == nil {
|
|
|
|
return nil
|
|
|
|
}
|
2017-03-27 21:36:28 -04:00
|
|
|
if err := im.layer.Release(); err != nil {
|
2017-05-05 18:52:11 -04:00
|
|
|
return errors.Wrapf(err, "failed to unmount previous build image %s", im.image.ImageID())
|
2017-03-22 20:49:39 -04:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2017-05-05 18:52:11 -04:00
|
|
|
func (im *imageMount) Image() builder.Image {
|
|
|
|
return im.image
|
2017-03-22 21:36:08 -04:00
|
|
|
}
|