Add --backend-args

Allow to add arguments to the backend build arguments

Fixes #146
This commit is contained in:
Ettore Di Giacinto
2021-02-22 13:48:26 +01:00
parent 57e19c61e7
commit 749a4cb615
6 changed files with 29 additions and 19 deletions

View File

@@ -72,6 +72,7 @@ Build packages specifying multiple definition trees:
viper.BindPFlag("nodeps", cmd.Flags().Lookup("nodeps")) viper.BindPFlag("nodeps", cmd.Flags().Lookup("nodeps"))
viper.BindPFlag("onlydeps", cmd.Flags().Lookup("onlydeps")) viper.BindPFlag("onlydeps", cmd.Flags().Lookup("onlydeps"))
viper.BindPFlag("values", cmd.Flags().Lookup("values")) viper.BindPFlag("values", cmd.Flags().Lookup("values"))
viper.BindPFlag("backend-args", cmd.Flags().Lookup("backend-args"))
viper.BindPFlag("image-repository", cmd.Flags().Lookup("image-repository")) viper.BindPFlag("image-repository", cmd.Flags().Lookup("image-repository"))
viper.BindPFlag("push", cmd.Flags().Lookup("push")) viper.BindPFlag("push", cmd.Flags().Lookup("push"))
@@ -84,6 +85,7 @@ Build packages specifying multiple definition trees:
LuetCfg.Viper.BindPFlag("solver.rate", cmd.Flags().Lookup("solver-rate")) LuetCfg.Viper.BindPFlag("solver.rate", cmd.Flags().Lookup("solver-rate"))
LuetCfg.Viper.BindPFlag("solver.max_attempts", cmd.Flags().Lookup("solver-attempts")) LuetCfg.Viper.BindPFlag("solver.max_attempts", cmd.Flags().Lookup("solver-attempts"))
LuetCfg.Viper.BindPFlag("general.show_build_output", cmd.Flags().Lookup("live-output")) LuetCfg.Viper.BindPFlag("general.show_build_output", cmd.Flags().Lookup("live-output"))
LuetCfg.Viper.BindPFlag("backend-args", cmd.Flags().Lookup("backend-args"))
}, },
Run: func(cmd *cobra.Command, args []string) { Run: func(cmd *cobra.Command, args []string) {
@@ -109,6 +111,7 @@ Build packages specifying multiple definition trees:
full, _ := cmd.Flags().GetBool("full") full, _ := cmd.Flags().GetBool("full")
concurrent, _ := cmd.Flags().GetBool("solver-concurrent") concurrent, _ := cmd.Flags().GetBool("solver-concurrent")
var results Results var results Results
backendArgs := viper.GetStringSlice("backend-args")
out, _ := cmd.Flags().GetString("output") out, _ := cmd.Flags().GetString("output")
if out != "terminal" { if out != "terminal" {
@@ -180,6 +183,7 @@ Build packages specifying multiple definition trees:
} }
luetCompiler := compiler.NewLuetCompiler(compilerBackend, generalRecipe.GetDatabase(), opts, solverOpts) luetCompiler := compiler.NewLuetCompiler(compilerBackend, generalRecipe.GetDatabase(), opts, solverOpts)
luetCompiler.SetBackendArgs(backendArgs)
luetCompiler.SetConcurrency(concurrency) luetCompiler.SetConcurrency(concurrency)
luetCompiler.SetCompressionType(compiler.CompressionImplementation(compressionType)) luetCompiler.SetCompressionType(compiler.CompressionImplementation(compressionType))
if full { if full {
@@ -297,6 +301,7 @@ func init() {
if err != nil { if err != nil {
Fatal(err) Fatal(err)
} }
buildCmd.Flags().StringSliceP("tree", "t", []string{path}, "Path of the tree to use.") buildCmd.Flags().StringSliceP("tree", "t", []string{path}, "Path of the tree to use.")
buildCmd.Flags().String("backend", "docker", "backend used (docker,img)") buildCmd.Flags().String("backend", "docker", "backend used (docker,img)")
buildCmd.Flags().Bool("privileged", false, "Privileged (Keep permissions)") buildCmd.Flags().Bool("privileged", false, "Privileged (Keep permissions)")
@@ -305,6 +310,7 @@ func init() {
buildCmd.Flags().Bool("all", false, "Build all specfiles in the tree") buildCmd.Flags().Bool("all", false, "Build all specfiles in the tree")
buildCmd.Flags().Bool("full", false, "Build all packages (optimized)") buildCmd.Flags().Bool("full", false, "Build all packages (optimized)")
buildCmd.Flags().String("values", "", "Build values file to interpolate with each package") buildCmd.Flags().String("values", "", "Build values file to interpolate with each package")
buildCmd.Flags().StringSliceP("backend-args", "a", []string{}, "Backend args")
buildCmd.Flags().String("destination", filepath.Join(path, "build"), "Destination folder") buildCmd.Flags().String("destination", filepath.Join(path, "build"), "Destination folder")
buildCmd.Flags().String("compression", "none", "Compression alg: none, gzip, zstd") buildCmd.Flags().String("compression", "none", "Compression alg: none, gzip, zstd")

View File

@@ -74,3 +74,13 @@ func runCommand(cmd *exec.Cmd) error {
return nil return nil
} }
func genBuildCommand(opts compiler.CompilerBackendOptions) []string {
context := opts.Context
if context == "" {
context = "."
}
buildarg := append(opts.BackendArgs, "-f", opts.DockerFileName, "-t", opts.ImageName, context)
return append([]string{"build"}, buildarg...)
}

View File

@@ -44,18 +44,11 @@ func NewSimpleDockerBackend() compiler.CompilerBackend {
// TODO: Missing still: labels, and build args expansion // TODO: Missing still: labels, and build args expansion
func (*SimpleDocker) BuildImage(opts compiler.CompilerBackendOptions) error { func (*SimpleDocker) BuildImage(opts compiler.CompilerBackendOptions) error {
name := opts.ImageName name := opts.ImageName
path := opts.SourcePath
dockerfileName := opts.DockerFileName
context := opts.Context
if context == "" {
context = "."
}
buildarg := []string{"build", "-f", dockerfileName, "-t", name, context}
buildarg := genBuildCommand(opts)
Info(":whale2: Building image " + name) Info(":whale2: Building image " + name)
cmd := exec.Command("docker", buildarg...) cmd := exec.Command("docker", buildarg...)
cmd.Dir = path cmd.Dir = opts.SourcePath
err := runCommand(cmd) err := runCommand(cmd)
if err != nil { if err != nil {
return err return err

View File

@@ -35,20 +35,13 @@ func NewSimpleImgBackend() compiler.CompilerBackend {
// TODO: Missing still: labels, and build args expansion // TODO: Missing still: labels, and build args expansion
func (*SimpleImg) BuildImage(opts compiler.CompilerBackendOptions) error { func (*SimpleImg) BuildImage(opts compiler.CompilerBackendOptions) error {
name := opts.ImageName name := opts.ImageName
path := opts.SourcePath
context := opts.Context
if context == "" { buildarg := genBuildCommand(opts)
context = "."
}
dockerfileName := opts.DockerFileName
buildarg := []string{"build", "-f", dockerfileName, "-t", name, context}
Info(":tea: Building image " + name) Info(":tea: Building image " + name)
cmd := exec.Command("img", buildarg...) cmd := exec.Command("img", buildarg...)
cmd.Dir = path cmd.Dir = opts.SourcePath
err := runCommand(cmd) err := runCommand(cmd)
if err != nil { if err != nil {
return err return err

View File

@@ -51,6 +51,7 @@ type LuetCompiler struct {
CompressionType CompressionImplementation CompressionType CompressionImplementation
Options CompilerOptions Options CompilerOptions
SolverOptions solver.Options SolverOptions solver.Options
BackedArgs []string
} }
func NewLuetCompiler(backend CompilerBackend, db pkg.PackageDatabase, opt *CompilerOptions, solvopts solver.Options) Compiler { func NewLuetCompiler(backend CompilerBackend, db pkg.PackageDatabase, opt *CompilerOptions, solvopts solver.Options) Compiler {
@@ -70,7 +71,9 @@ func NewLuetCompiler(backend CompilerBackend, db pkg.PackageDatabase, opt *Compi
SolverOptions: solvopts, SolverOptions: solvopts,
} }
} }
func (cs *LuetCompiler) SetBackendArgs(args []string) {
cs.BackedArgs = args
}
func (cs *LuetCompiler) SetConcurrency(i int) { func (cs *LuetCompiler) SetConcurrency(i int) {
cs.Concurrency = i cs.Concurrency = i
} }
@@ -381,12 +384,14 @@ func (cs *LuetCompiler) buildPackageImage(image, buildertaggedImage, packageImag
SourcePath: buildDir, SourcePath: buildDir,
DockerFileName: p.GetPackage().GetFingerPrint() + "-builder.dockerfile", DockerFileName: p.GetPackage().GetFingerPrint() + "-builder.dockerfile",
Destination: p.Rel(p.GetPackage().GetFingerPrint() + "-builder.image.tar"), Destination: p.Rel(p.GetPackage().GetFingerPrint() + "-builder.image.tar"),
BackendArgs: cs.BackedArgs,
} }
runnerOpts = CompilerBackendOptions{ runnerOpts = CompilerBackendOptions{
ImageName: packageImage, ImageName: packageImage,
SourcePath: buildDir, SourcePath: buildDir,
DockerFileName: p.GetPackage().GetFingerPrint() + ".dockerfile", DockerFileName: p.GetPackage().GetFingerPrint() + ".dockerfile",
Destination: p.Rel(p.GetPackage().GetFingerPrint() + ".image.tar"), Destination: p.Rel(p.GetPackage().GetFingerPrint() + ".image.tar"),
BackendArgs: cs.BackedArgs,
} }
buildAndPush := func(opts CompilerBackendOptions) error { buildAndPush := func(opts CompilerBackendOptions) error {

View File

@@ -34,6 +34,8 @@ type Compiler interface {
FromDatabase(db pkg.PackageDatabase, minimum bool, dst string) ([]CompilationSpec, error) FromDatabase(db pkg.PackageDatabase, minimum bool, dst string) ([]CompilationSpec, error)
SetBackend(CompilerBackend) SetBackend(CompilerBackend)
GetBackend() CompilerBackend GetBackend() CompilerBackend
SetBackendArgs([]string)
SetCompressionType(t CompressionImplementation) SetCompressionType(t CompressionImplementation)
} }
@@ -43,6 +45,7 @@ type CompilerBackendOptions struct {
DockerFileName string DockerFileName string
Destination string Destination string
Context string Context string
BackendArgs []string
} }
type CompilerOptions struct { type CompilerOptions struct {