From 94e2acfc1731b56f29dc7155f2c032aea6f4a249 Mon Sep 17 00:00:00 2001 From: Denis Arh Date: Wed, 31 Jul 2019 16:46:10 +0200 Subject: [PATCH] Add ability to disable script runner calls With script runner disabled, all critical scripts would fail --- compose/internal/service/script_runner.go | 23 +++++++++++++++++++++-- pkg/cli/options/script_runner.go | 2 ++ 2 files changed, 23 insertions(+), 2 deletions(-) diff --git a/compose/internal/service/script_runner.go b/compose/internal/service/script_runner.go index 79281ebce..3ada45998 100644 --- a/compose/internal/service/script_runner.go +++ b/compose/internal/service/script_runner.go @@ -49,13 +49,18 @@ type ( ) // @todo move to opt so all services can use it -func ScriptRunner(c options.ScriptRunnerOpt) (*scriptRunner, error) { - var svc = &scriptRunner{ +func ScriptRunner(c options.ScriptRunnerOpt) (svc *scriptRunner, err error) { + svc = &scriptRunner{ c: c, logger: DefaultLogger.Named("script-runner"), jwtEncoder: auth.DefaultJwtHandler, } + if !c.Enabled { + // Do not connect when script runner is not enabled + return + } + return svc, svc.connect() } @@ -128,6 +133,20 @@ func (svc scriptRunner) Record(ctx context.Context, s Runnable, ns *types.Namesp return nil, errors.New("module not provided") } + if !svc.c.Enabled { + if s.IsCritical() { + // Oh dear, we are in quite a pickle: + // Script runner is disabled but we have critical script to run + return nil, errors.New("script runner disabled") + } + + // Log this + svc.logger.Debug("executing script", zap.Any("record", r)) + + // and pretend like nothing happened + return r, nil + } + svc.logger.Debug("executing script", zap.Any("record", r)) ctx, cancelFn := context.WithTimeout(ctx, time.Second*5) diff --git a/pkg/cli/options/script_runner.go b/pkg/cli/options/script_runner.go index 1b28b134e..77ea6259b 100644 --- a/pkg/cli/options/script_runner.go +++ b/pkg/cli/options/script_runner.go @@ -6,6 +6,7 @@ import ( type ( ScriptRunnerOpt struct { + Enabled bool `env:"SCRIPT_RUNNER_ENABLED"` Addr string `env:"SCRIPT_RUNNER_ADDR"` MaxBackoffDelay time.Duration `env:"SCRIPT_RUNNER_MAX_BACKOFF_DELAY"` Log bool `env:"SCRIPT_RUNNER_LOG"` @@ -14,6 +15,7 @@ type ( func ScriptRunner(pfix string) (o *ScriptRunnerOpt) { o = &ScriptRunnerOpt{ + Enabled: false, Addr: "corredor:80", MaxBackoffDelay: time.Minute, Log: false,