1
0

feat: multiple commands to run in container

This commit is contained in:
dump_stack() 2023-05-23 20:46:09 +00:00
parent e302c447f5
commit c6e06d8e3e
Signed by: dump_stack
GPG Key ID: BE44DA8C062D87DC
6 changed files with 17 additions and 10 deletions

View File

@ -250,10 +250,10 @@ func (c Container) build(imagePath string) (output string, err error) {
return
}
func (c Container) Run(workdir string, command string) (output string, err error) {
func (c Container) Run(workdir string, cmds []string) (out string, err error) {
flog := c.Log.With().
Str("workdir", workdir).
Str("command", command).
Str("command", fmt.Sprintf("%v", cmds)).
Logger()
var args []string
@ -268,6 +268,11 @@ func (c Container) Run(workdir string, command string) (output string, err error
args = append(args, "-v", mount)
}
command := "true"
for _, c := range cmds {
command += fmt.Sprintf(" && %s", c)
}
args = append(args, c.name, "bash", "-c")
if workdir != "" {
args = append(args, "cd /work && "+command)
@ -307,7 +312,7 @@ func (c Container) Run(workdir string, command string) (output string, err error
scanner := bufio.NewScanner(stdout)
for scanner.Scan() {
m := scanner.Text()
output += m + "\n"
out += m + "\n"
flog.Trace().Str("stdout", m).Msg("")
}
}()
@ -315,7 +320,7 @@ func (c Container) Run(workdir string, command string) (output string, err error
err = cmd.Wait()
if err != nil {
e := fmt.Sprintf("error `%v` for cmd `%v` with output `%v`",
err, command, output)
err, cmds, out)
err = errors.New(e)
return
}
@ -379,7 +384,7 @@ func (c Container) Kernels() (kernels []config.KernelInfo, err error) {
for _, cmd := range []string{
"find /boot -type f -exec chmod a+r {} \\;",
} {
_, err = c.Run(config.Dir("tmp"), cmd)
_, err = c.Run(config.Dir("tmp"), []string{cmd})
if err != nil {
return
}

View File

@ -47,7 +47,7 @@ func (centos CentOS) Packages() (pkgs []string, err error) {
"| grep -v src " +
"| cut -d ' ' -f 1"
output, err := c.Run(config.Dir("tmp"), cmd)
output, err := c.Run(config.Dir("tmp"), []string{cmd})
if err != nil {
return
}

View File

@ -47,7 +47,7 @@ func (ol OracleLinux) Packages() (pkgs []string, err error) {
"| grep -v src " +
"| cut -d ' ' -f 1"
output, err := c.Run(config.Dir("tmp"), cmd)
output, err := c.Run(config.Dir("tmp"), []string{cmd})
if err != nil {
return
}

View File

@ -51,7 +51,7 @@ func (u Ubuntu) Packages() (pkgs []string, err error) {
"--names-only '^linux-image-[0-9\\.\\-]*-generic$' " +
"| awk '{ print $1 }'"
output, err := c.Run(config.Dir("tmp"), cmd)
output, err := c.Run(config.Dir("tmp"), []string{cmd})
if err != nil {
return
}

View File

@ -172,7 +172,7 @@ func installKernel(sk config.Target, pkgname string, force, headers bool) (err e
cmd += " && cp -r /usr/src /target/usr/"
}
_, err = c.Run("", cmd)
_, err = c.Run("", []string{cmd})
if err != nil {
return
}

4
pew.go
View File

@ -296,7 +296,9 @@ func build(flog zerolog.Logger, tmp string, ka config.Artifact,
log.Fatal().Err(err).Msg("container creation failure")
}
output, err = c.Run(outdir, buildCommand+" && chmod -R 777 /work")
output, err = c.Run(outdir, []string{
buildCommand + " && chmod -R 777 /work",
})
} else {
cmd := exec.Command("bash", "-c", "cd "+outdir+" && "+
buildCommand)