diff --git a/pkg/compiler/artifact.go b/pkg/compiler/artifact.go index 2622c9f6..551a823e 100644 --- a/pkg/compiler/artifact.go +++ b/pkg/compiler/artifact.go @@ -19,6 +19,7 @@ import ( "io/ioutil" "os" "path/filepath" + "regexp" "strconv" "strings" "sync" @@ -72,7 +73,7 @@ func worker(i int, wg *sync.WaitGroup, s <-chan CopyJob) { } // ExtractArtifactFromDelta extracts deltas from ArtifactLayer from an image in tar format -func ExtractArtifactFromDelta(src, dst string, layers []ArtifactLayer, concurrency int, keepPerms bool) (Artifact, error) { +func ExtractArtifactFromDelta(src, dst string, layers []ArtifactLayer, concurrency int, keepPerms bool, includes []string) (Artifact, error) { archive, err := ioutil.TempDir(os.TempDir(), "archive") if err != nil { @@ -101,12 +102,39 @@ func ExtractArtifactFromDelta(src, dst string, layers []ArtifactLayer, concurren go worker(i, wg, toCopy) } - for _, l := range layers { - // Consider d.Additions (and d.Changes? - warn at least) only - for _, a := range l.Diffs.Additions { - toCopy <- CopyJob{Src: filepath.Join(src, a.Name), Dst: filepath.Join(archive, a.Name), Artifact: a.Name} + // Handle includes in spec. If specified they filter what gets in the package + if len(includes) > 0 { + var includeRegexp []*regexp.Regexp + for _, i := range includes { + r, e := regexp.Compile(i) + if e != nil { + Warning("Failed compiling regex:", e) + continue + } + includeRegexp = append(includeRegexp, r) + } + for _, l := range layers { + // Consider d.Additions (and d.Changes? - warn at least) only + ADDS: + for _, a := range l.Diffs.Additions { + for _, i := range includeRegexp { + if i.MatchString(a.Name) { + toCopy <- CopyJob{Src: filepath.Join(src, a.Name), Dst: filepath.Join(archive, a.Name), Artifact: a.Name} + continue ADDS + } + } + } + } + } else { + // Otherwise just grab all + for _, l := range layers { + // Consider d.Additions (and d.Changes? - warn at least) only + for _, a := range l.Diffs.Additions { + toCopy <- CopyJob{Src: filepath.Join(src, a.Name), Dst: filepath.Join(archive, a.Name), Artifact: a.Name} + } } } + close(toCopy) wg.Wait() diff --git a/pkg/compiler/artifact_test.go b/pkg/compiler/artifact_test.go index 018aef8b..e07dd5a7 100644 --- a/pkg/compiler/artifact_test.go +++ b/pkg/compiler/artifact_test.go @@ -121,7 +121,7 @@ RUN echo bar > /test2`)) err = b.ExtractRootfs(CompilerBackendOptions{SourcePath: filepath.Join(tmpdir, "output2.tar"), Destination: rootfs}, false) Expect(err).ToNot(HaveOccurred()) - artifact, err := ExtractArtifactFromDelta(rootfs, filepath.Join(tmpdir, "package.tar"), diffs, 2, false) + artifact, err := ExtractArtifactFromDelta(rootfs, filepath.Join(tmpdir, "package.tar"), diffs, 2, false, []string{}) Expect(err).ToNot(HaveOccurred()) Expect(helpers.Exists(filepath.Join(tmpdir, "package.tar"))).To(BeTrue()) err = helpers.Untar(artifact.GetPath(), unpacked, false) diff --git a/pkg/compiler/compiler.go b/pkg/compiler/compiler.go index 4cc18872..2f84517a 100644 --- a/pkg/compiler/compiler.go +++ b/pkg/compiler/compiler.go @@ -180,7 +180,7 @@ func (cs *LuetCompiler) compileWithImage(image, buildertaggedImage, packageImage if err != nil { return nil, errors.Wrap(err, "Could not extract rootfs") } - artifact, err := ExtractArtifactFromDelta(rootfs, p.Rel(p.GetPackage().GetFingerPrint()+".package.tar"), diffs, concurrency, keepPermissions) + artifact, err := ExtractArtifactFromDelta(rootfs, p.Rel(p.GetPackage().GetFingerPrint()+".package.tar"), diffs, concurrency, keepPermissions, p.GetIncludes()) if err != nil { return nil, errors.Wrap(err, "Could not generate deltas") } diff --git a/pkg/compiler/interface.go b/pkg/compiler/interface.go index 9845bf76..96801944 100644 --- a/pkg/compiler/interface.go +++ b/pkg/compiler/interface.go @@ -70,6 +70,7 @@ type ArtifactLayer struct { // CompilationSpec represent a compilation specification derived from a package type CompilationSpec interface { ImageUnpack() bool // tells if the definition is just an image + GetIncludes() []string RenderBuildImage() (string, error) WriteBuildImageDefinition(string) error diff --git a/pkg/compiler/spec.go b/pkg/compiler/spec.go index 42b0caab..10b5a0a9 100644 --- a/pkg/compiler/spec.go +++ b/pkg/compiler/spec.go @@ -31,6 +31,7 @@ type LuetCompilationSpec struct { Package pkg.Package `json:"-"` OutputPath string `json:"-"` // Where the build processfiles go Unpack bool `json:"unpack"` + Includes []string `json:"includes"` } func NewLuetCompilationSpec(b []byte, p pkg.Package) (CompilationSpec, error) { @@ -59,6 +60,10 @@ func (cs *LuetCompilationSpec) GetPreBuildSteps() []string { return cs.Prelude } +func (cs *LuetCompilationSpec) GetIncludes() []string { + return cs.Includes +} + func (cs *LuetCompilationSpec) GetSeedImage() string { return cs.Seed }