From c6e06d8e3eb0e4c68b3ce7f534185cafee0956a8 Mon Sep 17 00:00:00 2001 From: Mikhail Klementev Date: Tue, 23 May 2023 20:46:09 +0000 Subject: [PATCH] feat: multiple commands to run in container --- container/container.go | 15 ++++++++++----- distro/centos/centos.go | 2 +- distro/oraclelinux/oraclelinux.go | 2 +- distro/ubuntu/ubuntu.go | 2 +- kernel/kernel.go | 2 +- pew.go | 4 +++- 6 files changed, 17 insertions(+), 10 deletions(-) diff --git a/container/container.go b/container/container.go index df19cf2..bbcc3ee 100644 --- a/container/container.go +++ b/container/container.go @@ -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 } diff --git a/distro/centos/centos.go b/distro/centos/centos.go index 74aa058..beb87ca 100644 --- a/distro/centos/centos.go +++ b/distro/centos/centos.go @@ -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 } diff --git a/distro/oraclelinux/oraclelinux.go b/distro/oraclelinux/oraclelinux.go index f9673d4..f41a7ca 100644 --- a/distro/oraclelinux/oraclelinux.go +++ b/distro/oraclelinux/oraclelinux.go @@ -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 } diff --git a/distro/ubuntu/ubuntu.go b/distro/ubuntu/ubuntu.go index 302cd80..007e223 100644 --- a/distro/ubuntu/ubuntu.go +++ b/distro/ubuntu/ubuntu.go @@ -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 } diff --git a/kernel/kernel.go b/kernel/kernel.go index f319d87..cfd7fe7 100644 --- a/kernel/kernel.go +++ b/kernel/kernel.go @@ -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 } diff --git a/pew.go b/pew.go index 506084e..7beefd8 100644 --- a/pew.go +++ b/pew.go @@ -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)