Add package_dir to pack a spec dir as the main artifact

This commit is contained in:
Ettore Di Giacinto
2020-05-16 21:34:27 +02:00
parent 2803430515
commit 51711dafba
9 changed files with 106 additions and 2 deletions

View File

@@ -357,8 +357,15 @@ func (cs *LuetCompiler) compileWithImage(image, buildertaggedImage, packageImage
var diffs []ArtifactLayer var diffs []ArtifactLayer
var artifact Artifact var artifact Artifact
unpack := p.ImageUnpack()
if !p.ImageUnpack() { // If package_dir was specified in the spec, we want to treat the content of the directory
// as the root of our archive. ImageUnpack is implied to be true. override it
if p.GetPackageDir() != "" {
unpack = true
}
if !unpack {
// we have to get diffs only if spec is not unpacked // we have to get diffs only if spec is not unpacked
diffs, err = cs.Backend.Changes(p.Rel(p.GetPackage().GetFingerPrint()+"-builder.image.tar"), p.Rel(p.GetPackage().GetFingerPrint()+".image.tar")) diffs, err = cs.Backend.Changes(p.Rel(p.GetPackage().GetFingerPrint()+"-builder.image.tar"), p.Rel(p.GetPackage().GetFingerPrint()+".image.tar"))
if err != nil { if err != nil {
@@ -395,7 +402,12 @@ func (cs *LuetCompiler) compileWithImage(image, buildertaggedImage, packageImage
} }
} }
if p.ImageUnpack() { if unpack {
if p.GetPackageDir() != "" {
Info(":tophat: Packing from output dir", p.GetPackageDir())
rootfs = filepath.Join(rootfs, p.GetPackageDir())
}
if len(p.GetIncludes()) > 0 { if len(p.GetIncludes()) > 0 {
// strip from includes // strip from includes

View File

@@ -615,6 +615,61 @@ var _ = Describe("Compiler", func() {
}) })
}) })
Context("Packages which conents are a package folder", func() {
It("Compiles it in parallel", func() {
generalRecipe := tree.NewCompilerRecipe(pkg.NewInMemoryDatabase(false))
err := generalRecipe.Load("../../tests/fixtures/package_dir")
Expect(err).ToNot(HaveOccurred())
Expect(len(generalRecipe.GetDatabase().GetPackages())).To(Equal(2))
compiler := NewLuetCompiler(sd.NewSimpleDockerBackend(), generalRecipe.GetDatabase(), NewDefaultCompilerOptions())
spec, err := compiler.FromPackage(&pkg.DefaultPackage{
Name: "dironly",
Category: "test",
Version: "1.0",
})
Expect(err).ToNot(HaveOccurred())
spec2, err := compiler.FromPackage(&pkg.DefaultPackage{
Name: "dironly_filter",
Category: "test",
Version: "1.0",
})
Expect(err).ToNot(HaveOccurred())
Expect(spec.GetPackage().GetPath()).ToNot(Equal(""))
tmpdir, err := ioutil.TempDir("", "tree")
Expect(err).ToNot(HaveOccurred())
defer os.RemoveAll(tmpdir) // clean up
tmpdir2, err := ioutil.TempDir("", "tree2")
Expect(err).ToNot(HaveOccurred())
defer os.RemoveAll(tmpdir2) // clean up
spec.SetOutputPath(tmpdir)
spec2.SetOutputPath(tmpdir2)
compiler.SetConcurrency(1)
artifacts, errs := compiler.CompileParallel(false, NewLuetCompilationspecs(spec, spec2))
Expect(errs).To(BeNil())
Expect(len(artifacts)).To(Equal(2))
Expect(len(artifacts[0].GetDependencies())).To(Equal(0))
Expect(helpers.Untar(spec.Rel("dironly-test-1.0.package.tar"), tmpdir, false)).ToNot(HaveOccurred())
Expect(helpers.Exists(spec.Rel("test1"))).To(BeTrue())
Expect(helpers.Exists(spec.Rel("test2"))).To(BeTrue())
Expect(helpers.Untar(spec2.Rel("dironly_filter-test-1.0.package.tar"), tmpdir2, false)).ToNot(HaveOccurred())
Expect(helpers.Exists(spec2.Rel("test5"))).To(BeTrue())
Expect(helpers.Exists(spec2.Rel("test6"))).ToNot(BeTrue())
Expect(helpers.Exists(spec2.Rel("artifact42"))).ToNot(BeTrue())
})
})
Context("Compression", func() { Context("Compression", func() {
It("Builds packages in gzip", func() { It("Builds packages in gzip", func() {
generalRecipe := tree.NewCompilerRecipe(pkg.NewInMemoryDatabase(false)) generalRecipe := tree.NewCompilerRecipe(pkg.NewInMemoryDatabase(false))

View File

@@ -168,6 +168,9 @@ type CompilationSpec interface {
GetRetrieve() []string GetRetrieve() []string
CopyRetrieves(dest string) error CopyRetrieves(dest string) error
SetPackageDir(string)
GetPackageDir() string
} }
type CompilationSpecs interface { type CompilationSpecs interface {

View File

@@ -95,6 +95,7 @@ type LuetCompilationSpec struct {
Seed string `json:"seed"` Seed string `json:"seed"`
Package *pkg.DefaultPackage `json:"package"` Package *pkg.DefaultPackage `json:"package"`
SourceAssertion solver.PackagesAssertions `json:"-"` SourceAssertion solver.PackagesAssertions `json:"-"`
PackageDir string `json:"package_dir" yaml:"package_dir"`
Retrieve []string `json:"retrieve"` Retrieve []string `json:"retrieve"`
@@ -123,6 +124,14 @@ func (cs *LuetCompilationSpec) GetPackage() pkg.Package {
return cs.Package return cs.Package
} }
func (cs *LuetCompilationSpec) GetPackageDir() string {
return cs.PackageDir
}
func (cs *LuetCompilationSpec) SetPackageDir(s string) {
cs.PackageDir = s
}
func (cs *LuetCompilationSpec) BuildSteps() []string { func (cs *LuetCompilationSpec) BuildSteps() []string {
return cs.Steps return cs.Steps
} }

View File

@@ -0,0 +1,7 @@
image: "alpine"
prelude:
- mkdir /foo
steps:
- echo artifact5 > /foo/test1
- echo artifact6 > /foo/test2
package_dir: /foo

View File

@@ -0,0 +1,3 @@
category: "test"
name: "dironly"
version: "1.0"

View File

@@ -0,0 +1,11 @@
image: "alpine"
prelude:
- mkdir /foo
- chmod +x generate.sh
steps:
- echo artifact5 > /foo/test5
- echo artifact6 > /foo/test6
- ./generate.sh
package_dir: /foo
includes:
- /test5

View File

@@ -0,0 +1,3 @@
category: "test"
name: "dironly_filter"
version: "1.0"

View File

@@ -0,0 +1 @@
echo generated > /foo/artifact42