Skip to content

Commit

Permalink
Merge pull request containers#4034 from flouthoc/spit-logfile-by-plat…
Browse files Browse the repository at this point in the history
…form

build, multiarch: support splitting build logs for `--platform`
  • Loading branch information
openshift-merge-robot authored Jun 6, 2022
2 parents 13e77e1 + 495add9 commit 44fccd7
Show file tree
Hide file tree
Showing 6 changed files with 59 additions and 1 deletion.
8 changes: 8 additions & 0 deletions cmd/buildah/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -253,6 +253,12 @@ func buildCmd(c *cobra.Command, inputArgs []string, iopts buildOptions) error {
reporter = f
}

if c.Flag("logsplit").Changed {
if !c.Flag("logfile").Changed {
return errors.Errorf("cannot use --logsplit without --logfile")
}
}

store, err := getStore(c)
if err != nil {
return err
Expand Down Expand Up @@ -383,6 +389,8 @@ func buildCmd(c *cobra.Command, inputArgs []string, iopts buildOptions) error {
IgnoreFile: iopts.IgnoreFile,
Labels: iopts.Label,
Layers: layers,
LogFile: iopts.Logfile,
LogSplitByPlatform: iopts.LogSplitByPlatform,
LogRusage: iopts.LogRusage,
Manifest: iopts.Manifest,
MaxPullPushRetries: maxPullPushRetries,
Expand Down
6 changes: 6 additions & 0 deletions define/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,12 @@ type BuildOptions struct {
// Additional tags to add to the image that we write, if we know of a
// way to add them.
AdditionalTags []string
// Logfile specifies if log output is redirected to an external file
// instead of stdout, stderr.
LogFile string
// LogByPlatform tells imagebuildah to split log to different log files
// for each platform if logging to external file was selected.
LogSplitByPlatform bool
// Log is a callback that will print a progress message. If no value
// is supplied, the message will be sent to Err (or os.Stderr, if Err
// is nil) by default.
Expand Down
5 changes: 5 additions & 0 deletions docs/buildah-build.1.md
Original file line number Diff line number Diff line change
Expand Up @@ -398,6 +398,11 @@ environment variable. `export BUILDAH_LAYERS=true`
Log output which would be sent to standard output and standard error to the
specified file instead of to standard output and standard error.

**--logsplit** *bool-value*

If --logfile and --platform is specified following flag allows end-users to split log file for each
platform into different files with naming convention as `${logfile}_${platform-os}_${platform-arch}`.

**--manifest** *listName*

Name of the manifest list to which the built image will be added. Creates the
Expand Down
20 changes: 19 additions & 1 deletion imagebuildah/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -253,7 +253,25 @@ func BuildDockerfiles(ctx context.Context, store storage.Store, options define.B
}
platformOptions.Args = argsCopy
builds.Go(func() error {
thisID, thisRef, err := buildDockerfilesOnce(ctx, store, logger, logPrefix, platformOptions, paths, files)
loggerPerPlatform := logger
if platformOptions.LogFile != "" && platformOptions.LogSplitByPlatform {
logFile := platformOptions.LogFile + "_" + platformOptions.OS + "_" + platformOptions.Architecture
f, err := os.OpenFile(logFile, os.O_CREATE|os.O_TRUNC|os.O_WRONLY, 0600)
if err != nil {
return errors.Wrapf(err, "opening logfile: %q", logFile)
}
defer f.Close()
loggerPerPlatform = logrus.New()
loggerPerPlatform.SetOutput(f)
loggerPerPlatform.SetLevel(logrus.GetLevel())
stdout := f
stderr := f
reporter := f
platformOptions.Out = stdout
platformOptions.ReportWriter = reporter
platformOptions.Err = stderr
}
thisID, thisRef, err := buildDockerfilesOnce(ctx, store, loggerPerPlatform, logPrefix, platformOptions, paths, files)
if err != nil {
return err
}
Expand Down
2 changes: 2 additions & 0 deletions pkg/cli/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ type BudResults struct {
Iidfile string
Label []string
Logfile string
LogSplitByPlatform bool
Manifest string
NoHosts bool
NoCache bool
Expand Down Expand Up @@ -211,6 +212,7 @@ func GetBudFlags(flags *BudResults) pflag.FlagSet {
fs.IntVar(&flags.Jobs, "jobs", 1, "how many stages to run in parallel")
fs.StringArrayVar(&flags.Label, "label", []string{}, "set metadata for an image (default [])")
fs.StringVar(&flags.Logfile, "logfile", "", "log to `file` instead of stdout/stderr")
fs.BoolVar(&flags.LogSplitByPlatform, "logsplit", false, "split logfile to different files for each platform")
fs.Int("loglevel", 0, "NO LONGER USED, flag ignored, and hidden")
if err := fs.MarkHidden("loglevel"); err != nil {
panic(fmt.Sprintf("error marking the loglevel flag as hidden: %v", err))
Expand Down
19 changes: 19 additions & 0 deletions tests/bud.bats
Original file line number Diff line number Diff line change
Expand Up @@ -1700,6 +1700,25 @@ function _test_http() {
test -s ${TEST_SCRATCH_DIR}/logfile
}

@test "bud-logfile-with-split-logfile-by-platform" {
mytmpdir=${TEST_SCRATCH_DIR}/my-dir
mkdir -p $mytmpdir

cat > $mytmpdir/Containerfile << _EOF
FROM alpine
COPY . .
_EOF

rm -f ${TEST_SCRATCH_DIR}/logfile
run_buildah build --logfile ${TEST_SCRATCH_DIR}/logfile --logsplit --platform linux/arm64,linux/amd64 $WITH_POLICY_JSON ${mytmpdir}
run cat ${TEST_SCRATCH_DIR}/logfile_linux_arm64
expect_output --substring "FROM alpine"
expect_output --substring "[linux/arm64]"
run cat ${TEST_SCRATCH_DIR}/logfile_linux_amd64
expect_output --substring "FROM alpine"
expect_output --substring "[linux/amd64]"
}

@test "bud with ARGS" {
_prefetch alpine
target=alpine-image
Expand Down

0 comments on commit 44fccd7

Please sign in to comment.