From 7e937563c4a1deabf304267932e3d0237d8b23f9 Mon Sep 17 00:00:00 2001 From: Ferran Borreguero Date: Fri, 9 May 2025 11:33:25 +0200 Subject: [PATCH 1/2] Add option to disable logs --- internal/local_runner.go | 43 ++++++++++++++++++++++++---------------- main.go | 5 ++++- 2 files changed, 30 insertions(+), 18 deletions(-) diff --git a/internal/local_runner.go b/internal/local_runner.go index 1a0ac79..74adaa9 100644 --- a/internal/local_runner.go +++ b/internal/local_runner.go @@ -77,6 +77,9 @@ type LocalRunner struct { // labels is the list of labels to apply to each resource being created labels map[string]string + + // logInternally outputs the logs of the service to the artifacts folder + logInternally bool } type task struct { @@ -107,7 +110,7 @@ func newDockerClient() (*client.Client, error) { } // TODO: add a runner config struct -func NewLocalRunner(out *output, manifest *Manifest, overrides map[string]string, interactive bool, bindHostPortsLocally bool, networkName string, labels map[string]string) (*LocalRunner, error) { +func NewLocalRunner(out *output, manifest *Manifest, overrides map[string]string, interactive bool, bindHostPortsLocally bool, networkName string, labels map[string]string, logInternally bool) (*LocalRunner, error) { client, err := newDockerClient() if err != nil { return nil, fmt.Errorf("failed to create docker client: %w", err) @@ -124,23 +127,24 @@ func NewLocalRunner(out *output, manifest *Manifest, overrides map[string]string // Create the concrete instances to run instances := []*instance{} for _, service := range manifest.Services() { - log_output, err := out.LogOutput(service.Name) - if err != nil { - return nil, fmt.Errorf("error getting log output: %w", err) - } - logs := &serviceLogs{ - logRef: log_output, - path: log_output.Name(), - } component := FindComponent(service.ComponentName) if component == nil { return nil, fmt.Errorf("component not found '%s'", service.ComponentName) } instance := &instance{ service: service, - logs: logs, component: component, } + if logInternally { + log_output, err := out.LogOutput(service.Name) + if err != nil { + return nil, fmt.Errorf("error getting log output: %w", err) + } + instance.logs = &serviceLogs{ + logRef: log_output, + path: log_output.Name(), + } + } instances = append(instances, instance) } @@ -213,6 +217,7 @@ func NewLocalRunner(out *output, manifest *Manifest, overrides map[string]string networkName: networkName, instances: instances, labels: labels, + logInternally: logInternally, } if interactive { @@ -872,12 +877,14 @@ func (d *LocalRunner) trackContainerStatusAndLogs() { case events.ActionStart: d.updateTaskStatus(name, taskStatusStarted) - // the container has started, we can track the logs now - go func() { - if err := d.trackLogs(name, event.Actor.ID); err != nil { - log.Warn("error tracking logs", "error", err) - } - }() + if d.logInternally { + // the container has started, we can track the logs now + go func() { + if err := d.trackLogs(name, event.Actor.ID); err != nil { + log.Warn("error tracking logs", "error", err) + } + }() + } case events.ActionDie: d.updateTaskStatus(name, taskStatusDie) log.Info("container died", "name", name) @@ -971,7 +978,9 @@ func (d *LocalRunner) Run() error { // generate the output log file for each service so that it is available after Run is done for _, instance := range d.instances { - d.tasks[instance.service.Name].logs = instance.logs.logRef + if instance.logs != nil { + d.tasks[instance.service.Name].logs = instance.logs.logRef + } } // First start the services that are running in docker-compose diff --git a/main.go b/main.go index 748b234..3719bc5 100644 --- a/main.go +++ b/main.go @@ -28,6 +28,7 @@ var bindExternal bool var withPrometheus bool var networkName string var labels internal.MapStringFlag +var disableLogs bool var rootCmd = &cobra.Command{ Use: "playground", @@ -175,6 +176,8 @@ func main() { recipeCmd.Flags().BoolVar(&withPrometheus, "with-prometheus", false, "whether to gather the Prometheus metrics") recipeCmd.Flags().StringVar(&networkName, "network", "", "network name") recipeCmd.Flags().Var(&labels, "labels", "list of labels to apply to the resources") + recipeCmd.Flags().BoolVar(&disableLogs, "disable-logs", false, "disable logs") + cookCmd.AddCommand(recipeCmd) } @@ -252,7 +255,7 @@ func runIt(recipe internal.Recipe) error { } } - dockerRunner, err := internal.NewLocalRunner(artifacts.Out, svcManager, overrides, interactive, !bindExternal, networkName, labels) + dockerRunner, err := internal.NewLocalRunner(artifacts.Out, svcManager, overrides, interactive, !bindExternal, networkName, labels, !disableLogs) if err != nil { return fmt.Errorf("failed to create docker runner: %w", err) } From 9cc4f5d05735dcf9ea5962986552f854bc7cc009 Mon Sep 17 00:00:00 2001 From: Ferran Borreguero Date: Fri, 9 May 2025 11:34:26 +0200 Subject: [PATCH 2/2] Update README --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index 0d6458c..3a1e7d4 100644 --- a/README.md +++ b/README.md @@ -66,6 +66,7 @@ $ builder-playground cook l1 --latest-fork --output ~/my-builder-testnet --genes - `--dry-run` (bool): Generates the artifacts and manifest but does not deploy anything (also enabled with the `--mise-en-place` flag) - `--log-level` (string): Log level to use (debug, info, warn, error, fatal). Defaults to `info`. - `--labels` (key=val): Custom labels to apply to your deployment. +- `--disable-logs` (bool): Disable the logs for the services. Defaults to `false`. To stop the playground, press `Ctrl+C`.