Skip to content

Commit

Permalink
fix issue with dovetailing images
Browse files Browse the repository at this point in the history
  • Loading branch information
Daniel van Gils committed May 30, 2017
1 parent 8b11b67 commit b1c571c
Show file tree
Hide file tree
Showing 6 changed files with 46 additions and 24 deletions.
14 changes: 7 additions & 7 deletions build/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ func (b *Builder) StartBuild() error {
b.Conf.Logger.Debugf("Building %d steps", len(b.Build.Steps))
for i, levels := range b.Build.buildLevels {
for j, s := range levels {
b.Conf.Logger.Debugf("Step %d - step_name %s: name = '%s'", i + j, s.Label, s.Name)
b.Conf.Logger.Debugf("Step %d - %s, image-name = '%s'", i + j + 1, s.Label, b.uniqueStepName(&s))
}
}

Expand All @@ -122,7 +122,7 @@ func (b *Builder) StartBuild() error {
for j, s := range levels {
b.wg.Add(1)
go func(st Step) {
b.Conf.Logger.Debugf("Step %d - Parallel build for %s", i + j, st.Name)
b.Conf.Logger.Debugf("Step %d - Build for %s", i + j + 1, st.Name)
defer b.wg.Done()

err := b.BuildStep(&st, i+j)
Expand Down Expand Up @@ -158,7 +158,7 @@ func (b *Builder) StartBuild() error {
for i, levels := range b.Build.buildLevels {
for j, s := range levels {
if (j == len(levels)-1) && (i != len(b.Build.buildLevels)-1) {
b.Conf.Logger.Debugf("Step %d - Removing unwanted image %s", i+j, b.uniqueStepName(&s))
b.Conf.Logger.Debugf("Step %d - Removing unwanted image %s", i + j + 1, b.uniqueStepName(&s))
rmiOptions := docker.RemoveImageOptions{Force: b.Conf.FroceRmImages, NoPrune: b.Conf.NoPruneRmImages}
err := b.docker.RemoveImageExtended(b.uniqueStepName(&s), rmiOptions)
if err != nil {
Expand Down Expand Up @@ -221,7 +221,7 @@ func (b *Builder) uniqueStepName(step *Step) string {

// BuildStep builds a single step
func (b *Builder) BuildStep(step *Step, step_number int) error {
b.Conf.Logger.Noticef("Step %d - Building %s from context '%s'", step_number, step.Name, b.Conf.Workdir)
b.Conf.Logger.Noticef("Step %d - Building %s from context '%s'", step_number + 1, step.Name, b.Conf.Workdir)
// fix the Dockerfile
err := b.replaceFromField(step, step_number)
if err != nil {
Expand All @@ -234,7 +234,7 @@ func (b *Builder) BuildStep(step *Step, step_number int) error {
}
// call Docker to build the Dockerfile (from the parsed file)

b.Conf.Logger.Infof("Step %d - Building the %s image from %s", step_number, b.uniqueStepName(step), b.uniqueDockerfile(step))
b.Conf.Logger.Infof("Step %d - Building the %s image from %s", step_number + 1, b.uniqueStepName(step), b.uniqueDockerfile(step))
opts := docker.BuildImageOptions{
Name: b.uniqueStepName(step),
Dockerfile: b.uniqueDockerfileName(step),
Expand Down Expand Up @@ -549,7 +549,7 @@ func (b *Builder) BuildStep(step *Step, step_number int) error {
// this replaces the FROM field in the Dockerfile to one with the previous step's unique name
// it stores the parsed result Dockefile in uniqueSessionName file
func (b *Builder) replaceFromField(step *Step, step_number int) error {
b.Conf.Logger.Noticef("Step %d - Parsing and converting '%s'", step_number, step.Dockerfile)
b.Conf.Logger.Noticef("Step %d - Parsing and converting '%s'", step_number + 1, step.Dockerfile)

rwc, err := os.Open(path.Join(b.Conf.Workdir, step.Dockerfile))
if err != nil {
Expand Down Expand Up @@ -580,7 +580,7 @@ func (b *Builder) replaceFromField(step *Step, step_number int) error {
buffer = fromTag.ReplaceAll(buffer, []byte("FROM " + uniqueStepName))
}

b.Conf.Logger.Debugf("Step %d - Writing the new Dockerfile into '%s'", step_number, b.uniqueDockerfile(step))
b.Conf.Logger.Debugf("Step %d - Writing the new Dockerfile into '%s'", step_number + 1, b.uniqueDockerfile(step))
err = ioutil.WriteFile(b.uniqueDockerfile(step), buffer, 0644)
if err != nil {
return err
Expand Down
34 changes: 17 additions & 17 deletions build/manifest.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ type Step struct {
Label string
Dockerfile string
Artifacts []Artifact
Manifest Manifest
Manifest *Manifest
Cleanup *Cleanup
DependsOn []*Step
Command string
Expand Down Expand Up @@ -121,28 +121,28 @@ func LoadBuildFromFile(config *configuration.Config) (*Manifest, error) {
}

func (n *namespace) convertToBuild(version string) (*Manifest, error) {
r := Manifest{
manifest := Manifest{
SecretProviders: make(map[string]secrets.SecretProvider),
}
r.SecretProviders["file"] = &secrets.FileProvider{}
r.SecretProviders["env"] = &secrets.EnvProvider{}
manifest.SecretProviders["file"] = &secrets.FileProvider{}
manifest.SecretProviders["env"] = &secrets.EnvProvider{}


r.IsPrivileged = false
r.Steps = []Step{}
manifest.IsPrivileged = false
manifest.Steps = []Step{}

for name, s := range n.BuildConfig.Steps {
convertedStep := Step{}

convertedStep.Manifest = r
convertedStep.Manifest = &manifest
convertedStep.Dockerfile = s.Dockerfile
convertedStep.Name = s.Name
convertedStep.Label = name
convertedStep.Artifacts = []Artifact{}
convertedStep.Command = s.Command
if s.Cleanup != nil && !n.Config.NoSquash {
convertedStep.Cleanup = &Cleanup{Commands: s.Cleanup.Commands}
r.IsPrivileged = true
manifest.IsPrivileged = true
} else {
convertedStep.Cleanup = &Cleanup{}
}
Expand All @@ -162,7 +162,7 @@ func (n *namespace) convertToBuild(version string) (*Manifest, error) {
return nil, fmt.Errorf("Unsupported type '%s'", s.Type)
}

r.SecretProviders[s.Type].RegisterSecret(name, s.Value)
manifest.SecretProviders[s.Type].RegisterSecret(name, s.Value)

convertedStep.Secrets = append(convertedStep.Secrets, convertedSecret)
}
Expand All @@ -185,40 +185,40 @@ func (n *namespace) convertToBuild(version string) (*Manifest, error) {
}

// is it unique?
for _, s := range r.Steps {
for _, s := range manifest.Steps {
if s.Name == convertedStep.Name {
return nil, fmt.Errorf("Step name '%s' is not unique", convertedStep.Name)
}
}

r.Steps = append(r.Steps, convertedStep)
manifest.Steps = append(manifest.Steps, convertedStep)
}

// now that we have the Manifest built from the file, we can resolve dependencies
for idx, step := range r.Steps {
for idx, step := range manifest.Steps {
bStep := n.BuildConfig.Steps[step.Label]

for _, d := range bStep.DependsOn {
convertedStep, err := r.FindStepByLabel(d)
convertedStep, err := manifest.FindStepByLabel(d)
if err != nil {
return nil, err
}
if convertedStep == nil {
return nil, fmt.Errorf("can't find step %s", d)
}

r.Steps[idx].DependsOn = append(r.Steps[idx].DependsOn, convertedStep)
manifest.Steps[idx].DependsOn = append(manifest.Steps[idx].DependsOn, convertedStep)
}
}

// build the dependency tree
bl, err := r.serviceOrder(r.Steps)
bl, err := manifest.serviceOrder(manifest.Steps)
if err != nil {
return nil, err
}
r.buildLevels = bl
manifest.buildLevels = bl

return &r, nil
return &manifest, nil
}

func (m *Manifest) getStepsByLevel(level int) ([]Step, error) {
Expand Down
2 changes: 2 additions & 0 deletions examples/uid_nested/Dockerfile.base
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
FROM alpine:3.5
RUN echo 'the base'
2 changes: 2 additions & 0 deletions examples/uid_nested/Dockerfile.compile
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
FROM base
RUN echo 'compiling'
2 changes: 2 additions & 0 deletions examples/uid_nested/Dockerfile.runtime
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
FROM base
RUN echo 'runtime'
16 changes: 16 additions & 0 deletions examples/uid_nested/build.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
build:
version: 2016-03-14
steps:
base:
name: base
dockerfile: Dockerfile.base
compilation:
name: compilation
dockerfile: Dockerfile.compile
depends_on:
- base
runtime:
name: runtime
dockerfile: Dockerfile.runtime
depends_on:
- compilation

0 comments on commit b1c571c

Please sign in to comment.