Add 'includes' to CompileSpec

The includes field is an array of regexp used to indicate what include
inside the packag. It can be omitted to default behavior (all)
This commit is contained in:
Ettore Di Giacinto
2019-11-14 17:43:47 +01:00
parent 08944a22ac
commit 0eef18d75c
5 changed files with 41 additions and 7 deletions

View File

@@ -19,6 +19,7 @@ import (
"io/ioutil" "io/ioutil"
"os" "os"
"path/filepath" "path/filepath"
"regexp"
"strconv" "strconv"
"strings" "strings"
"sync" "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 // 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") archive, err := ioutil.TempDir(os.TempDir(), "archive")
if err != nil { if err != nil {
@@ -101,12 +102,39 @@ func ExtractArtifactFromDelta(src, dst string, layers []ArtifactLayer, concurren
go worker(i, wg, toCopy) go worker(i, wg, toCopy)
} }
for _, l := range layers { // Handle includes in spec. If specified they filter what gets in the package
// Consider d.Additions (and d.Changes? - warn at least) only if len(includes) > 0 {
for _, a := range l.Diffs.Additions { var includeRegexp []*regexp.Regexp
toCopy <- CopyJob{Src: filepath.Join(src, a.Name), Dst: filepath.Join(archive, a.Name), Artifact: a.Name} 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) close(toCopy)
wg.Wait() wg.Wait()

View File

@@ -121,7 +121,7 @@ RUN echo bar > /test2`))
err = b.ExtractRootfs(CompilerBackendOptions{SourcePath: filepath.Join(tmpdir, "output2.tar"), Destination: rootfs}, false) err = b.ExtractRootfs(CompilerBackendOptions{SourcePath: filepath.Join(tmpdir, "output2.tar"), Destination: rootfs}, false)
Expect(err).ToNot(HaveOccurred()) 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(err).ToNot(HaveOccurred())
Expect(helpers.Exists(filepath.Join(tmpdir, "package.tar"))).To(BeTrue()) Expect(helpers.Exists(filepath.Join(tmpdir, "package.tar"))).To(BeTrue())
err = helpers.Untar(artifact.GetPath(), unpacked, false) err = helpers.Untar(artifact.GetPath(), unpacked, false)

View File

@@ -180,7 +180,7 @@ func (cs *LuetCompiler) compileWithImage(image, buildertaggedImage, packageImage
if err != nil { if err != nil {
return nil, errors.Wrap(err, "Could not extract rootfs") 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 { if err != nil {
return nil, errors.Wrap(err, "Could not generate deltas") return nil, errors.Wrap(err, "Could not generate deltas")
} }

View File

@@ -70,6 +70,7 @@ type ArtifactLayer struct {
// CompilationSpec represent a compilation specification derived from a package // CompilationSpec represent a compilation specification derived from a package
type CompilationSpec interface { type CompilationSpec interface {
ImageUnpack() bool // tells if the definition is just an image ImageUnpack() bool // tells if the definition is just an image
GetIncludes() []string
RenderBuildImage() (string, error) RenderBuildImage() (string, error)
WriteBuildImageDefinition(string) error WriteBuildImageDefinition(string) error

View File

@@ -31,6 +31,7 @@ type LuetCompilationSpec struct {
Package pkg.Package `json:"-"` Package pkg.Package `json:"-"`
OutputPath string `json:"-"` // Where the build processfiles go OutputPath string `json:"-"` // Where the build processfiles go
Unpack bool `json:"unpack"` Unpack bool `json:"unpack"`
Includes []string `json:"includes"`
} }
func NewLuetCompilationSpec(b []byte, p pkg.Package) (CompilationSpec, error) { func NewLuetCompilationSpec(b []byte, p pkg.Package) (CompilationSpec, error) {
@@ -59,6 +60,10 @@ func (cs *LuetCompilationSpec) GetPreBuildSteps() []string {
return cs.Prelude return cs.Prelude
} }
func (cs *LuetCompilationSpec) GetIncludes() []string {
return cs.Includes
}
func (cs *LuetCompilationSpec) GetSeedImage() string { func (cs *LuetCompilationSpec) GetSeedImage() string {
return cs.Seed return cs.Seed
} }