From 07a86ed9c75721fc967d054592221a77b0f87eb1 Mon Sep 17 00:00:00 2001 From: Kyle Carberry Date: Mon, 3 Oct 2022 17:08:20 +0000 Subject: [PATCH] fix: Install Terraform once and only log >=500 Fixes #4302. --- coderd/httpmw/logger.go | 6 +++--- provisioner/terraform/serve.go | 30 +++++++++++++++++++----------- 2 files changed, 22 insertions(+), 14 deletions(-) diff --git a/coderd/httpmw/logger.go b/coderd/httpmw/logger.go index f71dec59e6d7d..7888d72144b31 100644 --- a/coderd/httpmw/logger.go +++ b/coderd/httpmw/logger.go @@ -25,7 +25,7 @@ func Logger(log slog.Logger) func(next http.Handler) http.Handler { next.ServeHTTP(sw, r) // Don't log successful health check requests. - if r.URL.Path == "/api/v2" && sw.Status == 200 { + if r.URL.Path == "/api/v2" && sw.Status == http.StatusOK { return } @@ -37,7 +37,7 @@ func Logger(log slog.Logger) func(next http.Handler) http.Handler { // For status codes 400 and higher we // want to log the response body. - if sw.Status >= 400 { + if sw.Status >= http.StatusInternalServerError { httplog = httplog.With( slog.F("response_body", string(sw.ResponseBody())), ) @@ -47,7 +47,7 @@ func Logger(log slog.Logger) func(next http.Handler) http.Handler { // includes proxy errors etc. It also causes slogtest to fail // instantly without an error message by default. logLevelFn := httplog.Debug - if sw.Status >= 400 { + if sw.Status >= http.StatusInternalServerError { logLevelFn = httplog.Warn } diff --git a/provisioner/terraform/serve.go b/provisioner/terraform/serve.go index b80f8fdfcdb27..139ca5cae15eb 100644 --- a/provisioner/terraform/serve.go +++ b/provisioner/terraform/serve.go @@ -25,6 +25,11 @@ var ( maxTerraformVersion = version.Must(version.NewVersion("1.3.0")) terraformMinorVersionMismatch = xerrors.New("Terraform binary minor version mismatch.") + + installTerraform sync.Once + installTerraformExecPath string + // nolint:errname + installTerraformError error ) const ( @@ -93,18 +98,21 @@ func Serve(ctx context.Context, options *ServeOptions) error { return xerrors.Errorf("absolute binary context canceled: %w", err) } - installer := &releases.ExactVersion{ - InstallDir: options.CachePath, - Product: product.Terraform, - Version: TerraformVersion, - } - installer.SetLogger(slog.Stdlib(ctx, options.Logger, slog.LevelDebug)) - options.Logger.Info(ctx, "installing terraform", slog.F("dir", options.CachePath), slog.F("version", TerraformVersion)) - execPath, err := installer.Install(ctx) - if err != nil { - return xerrors.Errorf("install terraform: %w", err) + // We don't want to install Terraform multiple times! + installTerraform.Do(func() { + installer := &releases.ExactVersion{ + InstallDir: options.CachePath, + Product: product.Terraform, + Version: TerraformVersion, + } + installer.SetLogger(slog.Stdlib(ctx, options.Logger, slog.LevelDebug)) + options.Logger.Debug(ctx, "installing terraform", slog.F("dir", options.CachePath), slog.F("version", TerraformVersion)) + installTerraformExecPath, installTerraformError = installer.Install(ctx) + }) + if installTerraformError != nil { + return xerrors.Errorf("install terraform: %w", installTerraformError) } - options.BinaryPath = execPath + options.BinaryPath = installTerraformExecPath } else { options.BinaryPath = absoluteBinary }