Compare commits

...

5 Commits

Author SHA1 Message Date
Ettore Di Giacinto
98b01ce00b Tag 0.11.2 2021-02-22 14:46:36 +01:00
Ettore Di Giacinto
749a4cb615 Add --backend-args
Allow to add arguments to the backend build arguments

Fixes #146
2021-02-22 13:49:29 +01:00
Ettore Di Giacinto
57e19c61e7 Start spinner before pulling docker artifacts 2021-02-22 11:54:09 +01:00
Ettore Di Giacinto
5ee1e28b9c Display informative message when pulling docker artifacts 2021-02-22 11:50:52 +01:00
Ettore Di Giacinto
21bd76af9c Uncomplicate runCommand and return command output
Fixes #189
2021-02-22 11:44:46 +01:00
9 changed files with 54 additions and 52 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

@@ -40,7 +40,7 @@ var Verbose bool
var LockedCommands = []string{"install", "uninstall", "upgrade"} var LockedCommands = []string{"install", "uninstall", "upgrade"}
const ( const (
LuetCLIVersion = "0.11.1" LuetCLIVersion = "0.11.2"
LuetEnvPrefix = "LUET" LuetEnvPrefix = "LUET"
) )

View File

@@ -16,7 +16,6 @@
package backend package backend
import ( import (
"fmt"
"os/exec" "os/exec"
"github.com/mudler/luet/pkg/compiler" "github.com/mudler/luet/pkg/compiler"
@@ -49,43 +48,39 @@ func NewBackend(s string) compiler.CompilerBackend {
return compilerBackend return compilerBackend
} }
func runCommand(cmd *exec.Cmd) (string, error) { func runCommand(cmd *exec.Cmd) error {
output := ""
buffered := !config.LuetCfg.GetGeneral().ShowBuildOutput buffered := !config.LuetCfg.GetGeneral().ShowBuildOutput
if buffered {
Spinner(22)
defer SpinnerStop()
}
ans := ""
writer := NewBackendWriter(buffered) writer := NewBackendWriter(buffered)
cmd.Stdout = writer cmd.Stdout = writer
cmd.Stderr = writer cmd.Stderr = writer
if buffered {
Spinner(22)
defer SpinnerStop()
}
err := cmd.Start() err := cmd.Start()
if err != nil { if err != nil {
return "", errors.Wrap(err, "Failed starting build") return errors.Wrap(err, "Failed starting command")
} }
err = cmd.Wait() err = cmd.Wait()
if err != nil { if err != nil {
return "", errors.Wrap(err, "Failed waiting for building command") output = writer.GetCombinedOutput()
return errors.Wrapf(err, "Failed running command: %s", output)
} }
res := cmd.ProcessState.ExitCode() return nil
if res != 0 { }
errMsg := fmt.Sprintf("Failed building image (exiting with %d)", res)
if buffered { func genBuildCommand(opts compiler.CompilerBackendOptions) []string {
errMsg = fmt.Sprintf("Failed building image (exiting with %d): %s", context := opts.Context
res, writer.GetCombinedOutput())
} if context == "" {
context = "."
return "", errors.Wrap(err, errMsg) }
} buildarg := append(opts.BackendArgs, "-f", opts.DockerFileName, "-t", opts.ImageName, context)
return append([]string{"build"}, buildarg...)
if buffered {
ans = writer.GetCombinedOutput()
}
return ans, nil
} }

View File

@@ -44,19 +44,12 @@ 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,21 +35,14 @@ 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

@@ -1,4 +1,5 @@
// Copyright © 2021 Daniele Rondina <geaaru@sabayonlinux.org> // Copyright © 2021 Daniele Rondina <geaaru@sabayonlinux.org>
// Ettore Di Giacinto <mudler@sabayonlinux.org>
// //
// This program is free software; you can redistribute it and/or modify // This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by // it under the terms of the GNU General Public License as published by
@@ -23,21 +24,23 @@ import (
type BackendWriter struct { type BackendWriter struct {
BufferedOutput bool BufferedOutput bool
Buffer bytes.Buffer Buffer *bytes.Buffer
} }
func NewBackendWriter(buffered bool) *BackendWriter { func NewBackendWriter(buffered bool) *BackendWriter {
return &BackendWriter{ return &BackendWriter{
BufferedOutput: buffered, BufferedOutput: buffered,
Buffer: &bytes.Buffer{},
} }
} }
func (b *BackendWriter) Write(p []byte) (int, error) { func (b *BackendWriter) Write(p []byte) (int, error) {
if b.BufferedOutput { if b.BufferedOutput {
return b.Buffer.Write(p) return b.Buffer.Write(p)
} else {
Msg("info", false, false, (string(p)))
} }
Msg("info", false, false, (string(p)))
return len(p), nil return len(p), nil
} }

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 {

View File

@@ -71,6 +71,10 @@ func (c *DockerClient) DownloadArtifact(artifact compiler.Artifact) (compiler.Ar
//var u *url.URL = nil //var u *url.URL = nil
var err error var err error
var temp string var temp string
Spinner(22)
defer SpinnerStop()
var resultingArtifact compiler.Artifact var resultingArtifact compiler.Artifact
artifactName := path.Base(artifact.GetPath()) artifactName := path.Base(artifact.GetPath())
cacheFile := filepath.Join(config.LuetCfg.GetSystem().GetSystemPkgsCacheDirPath(), artifactName) cacheFile := filepath.Join(config.LuetCfg.GetSystem().GetSystemPkgsCacheDirPath(), artifactName)
@@ -102,9 +106,9 @@ func (c *DockerClient) DownloadArtifact(artifact compiler.Artifact) (compiler.Ar
defer os.RemoveAll(temp) defer os.RemoveAll(temp)
for _, uri := range c.RepoData.Urls { for _, uri := range c.RepoData.Urls {
Debug("Downloading artifact", artifactName, "from", uri)
imageName := fmt.Sprintf("%s:%s", uri, artifact.GetCompileSpec().GetPackage().GetFingerPrint()) imageName := fmt.Sprintf("%s:%s", uri, artifact.GetCompileSpec().GetPackage().GetFingerPrint())
Info("Downloading image", imageName)
// imageName := fmt.Sprintf("%s/%s", uri, artifact.GetCompileSpec().GetPackage().GetPackageImageName()) // imageName := fmt.Sprintf("%s/%s", uri, artifact.GetCompileSpec().GetPackage().GetPackageImageName())
err = downloadAndExtractDockerImage(imageName, temp) err = downloadAndExtractDockerImage(imageName, temp)