Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

new(cmd,pkg): support redirect-errors cli option for build subcmd. #17

Merged
merged 2 commits into from
Sep 7, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 8 additions & 6 deletions cmd/build/build_configs.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,18 +18,20 @@ func NewBuildConfigsCmd() *cobra.Command {
flags.Bool("skip-existing", true, "whether to skip the build of drivers existing on S3")
flags.Bool("publish", false, "whether artifacts must be published on S3")
flags.Bool("ignore-errors", false, "whether to ignore build errors and go on looping on config files")
flags.String("aws-profile", "", "aws-profile to be used. Mandatory if publish is enabled.")
flags.String("redirect-errors", "", "redirect build errors to the specified file")
flags.String("aws-profile", "", "aws-profile to be used. Mandatory if publish is enabled")

return cmd
}

func executeConfigs(_ *cobra.Command, _ []string) error {
options := build.Options{
Options: root.LoadRootOptions(),
SkipExisting: viper.GetBool("skip-existing"),
Publish: viper.GetBool("publish"),
IgnoreErrors: viper.GetBool("ignore-errors"),
AwsProfile: viper.GetString("aws-profile"),
Options: root.LoadRootOptions(),
SkipExisting: viper.GetBool("skip-existing"),
Publish: viper.GetBool("publish"),
IgnoreErrors: viper.GetBool("ignore-errors"),
RedirectErrors: viper.GetString("redirect-errors"),
AwsProfile: viper.GetString("aws-profile"),
}

if options.Publish && options.AwsProfile == "" {
Expand Down
2 changes: 1 addition & 1 deletion cmd/cleanup/cleanup_drivers.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ func NewCleanupDriversCmd() *cobra.Command {
}

flags := cmd.Flags()
flags.String("aws-profile", "", "aws-profile to be used. Mandatory.")
flags.String("aws-profile", "", "aws-profile to be used. Mandatory")

_ = cmd.MarkFlagRequired("aws-profile")
return cmd
Expand Down
8 changes: 4 additions & 4 deletions cmd/generate/generate_configs.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,11 @@ func NewGenerateConfigsCmd() *cobra.Command {
Short: "Generate new dbg configs",
Long: `
In auto mode, configs will be generated starting from kernel-crawler output.
In this scenario, target-{distro,kernelrelease,kernelversion} are available to filter to-be-generated configs. Regexes are allowed.
In this scenario, --target-{distro,kernelrelease,kernelversion} are available to filter to-be-generated configs. Regexes are allowed.
Moreover, you can pass special value "load" as target-distro to make the tool automatically fetch latest distro kernel-crawler ran against.
Instead, when auto mode is disabled, the tool is able to generate a single config (for each driver version).
In this scenario, target-{distro,kernelrelease,kernelversion} CANNOT be regexes but must be exact values.
Also, in non-automatic mode, kernelurls driverkit config key will be constructed using driverkit libraries.
In this scenario, --target-{distro,kernelrelease,kernelversion} CANNOT be regexes but must be exact values.
Also, in non-automatic mode, kernelurls will be retrieved using driverkit libraries.
`,
RunE: executeConfigs,
}
Expand All @@ -26,7 +26,7 @@ Also, in non-automatic mode, kernelurls driverkit config key will be constructed
return cmd
}

func executeConfigs(c *cobra.Command, args []string) error {
func executeConfigs(_ *cobra.Command, _ []string) error {
options := generate.Options{
Options: root.LoadRootOptions(),
Auto: viper.GetBool("auto"),
Expand Down
2 changes: 1 addition & 1 deletion cmd/publish/publish_drivers.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ func NewPublishDriversCmd() *cobra.Command {
RunE: executeDrivers,
}
flags := cmd.Flags()
flags.String("aws-profile", "", "aws-profile to be used. Mandatory.")
flags.String("aws-profile", "", "aws-profile to be used. Mandatory")

_ = cmd.MarkFlagRequired("aws-profile")
return cmd
Expand Down
21 changes: 19 additions & 2 deletions pkg/build/build.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package build

import (
"fmt"
"github.com/falcosecurity/driverkit/cmd"
"github.com/falcosecurity/driverkit/pkg/driverbuilder"
"github.com/fededp/dbg-go/pkg/root"
Expand Down Expand Up @@ -31,12 +32,22 @@ func Run(opts Options) error {
client = testClient
}
looper := root.NewFsLooper(root.BuildConfigPath)

var redirectErrorsF *os.File
if opts.RedirectErrors != "" {
redirectErrorsF, err = os.OpenFile(opts.RedirectErrors, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644)
if err != nil {
return err
}
defer redirectErrorsF.Close()
}

return looper.LoopFiltered(opts.Options, "building driver", "config", func(driverVersion, configPath string) error {
return buildConfig(client, opts, driverVersion, configPath)
return buildConfig(client, opts, redirectErrorsF, driverVersion, configPath)
})
}

func buildConfig(client *s3utils.Client, opts Options, driverVersion, configPath string) error {
func buildConfig(client *s3utils.Client, opts Options, redirectErrorsF *os.File, driverVersion, configPath string) error {
logger := slog.With("config", configPath)
configData, err := os.ReadFile(configPath)
if err != nil {
Expand Down Expand Up @@ -76,12 +87,14 @@ func buildConfig(client *s3utils.Client, opts Options, driverVersion, configPath
if ro.Output.Module != "" {
moduleName := filepath.Base(ro.Output.Module)
if client.HeadDriver(opts.Options, driverVersion, moduleName) {
logger.Info("output module already exists inside S3 bucket - skipping", "path", ro.Output.Module)
ro.Output.Module = "" // disable module build
}
}
if ro.Output.Probe != "" {
probeName := filepath.Base(ro.Output.Probe)
if client.HeadDriver(opts.Options, driverVersion, probeName) {
logger.Info("output probe already exists inside S3 bucket - skipping", "path", ro.Output.Probe)
ro.Output.Probe = "" // disable probe build
}
}
Expand All @@ -93,6 +106,10 @@ func buildConfig(client *s3utils.Client, opts Options, driverVersion, configPath

err = driverbuilder.NewDockerBuildProcessor(1000, "").Start(ro.ToBuild())
if err != nil {
if redirectErrorsF != nil {
logLine := fmt.Sprintf("config: %s | error: %s\n", configPath, err.Error())
_, _ = redirectErrorsF.WriteString(logLine)
}
if opts.IgnoreErrors {
logger.Error(err.Error())
return nil // do not break the configs loop, just try the next one
Expand Down
9 changes: 5 additions & 4 deletions pkg/build/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,9 @@ import "github.com/fededp/dbg-go/pkg/root"

type Options struct {
root.Options
SkipExisting bool
Publish bool
IgnoreErrors bool
AwsProfile string
SkipExisting bool
Publish bool
IgnoreErrors bool
RedirectErrors string
AwsProfile string
}
Loading