Skip to content

Commit 9bc0d06

Browse files
authored
fix: Install Terraform once and only log >=500 (coder#4339)
Fixes coder#4302.
1 parent aa3812f commit 9bc0d06

File tree

2 files changed

+22
-14
lines changed

2 files changed

+22
-14
lines changed

coderd/httpmw/logger.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ func Logger(log slog.Logger) func(next http.Handler) http.Handler {
2525
next.ServeHTTP(sw, r)
2626

2727
// Don't log successful health check requests.
28-
if r.URL.Path == "/api/v2" && sw.Status == 200 {
28+
if r.URL.Path == "/api/v2" && sw.Status == http.StatusOK {
2929
return
3030
}
3131

@@ -37,7 +37,7 @@ func Logger(log slog.Logger) func(next http.Handler) http.Handler {
3737

3838
// For status codes 400 and higher we
3939
// want to log the response body.
40-
if sw.Status >= 400 {
40+
if sw.Status >= http.StatusInternalServerError {
4141
httplog = httplog.With(
4242
slog.F("response_body", string(sw.ResponseBody())),
4343
)
@@ -47,7 +47,7 @@ func Logger(log slog.Logger) func(next http.Handler) http.Handler {
4747
// includes proxy errors etc. It also causes slogtest to fail
4848
// instantly without an error message by default.
4949
logLevelFn := httplog.Debug
50-
if sw.Status >= 400 {
50+
if sw.Status >= http.StatusInternalServerError {
5151
logLevelFn = httplog.Warn
5252
}
5353

provisioner/terraform/serve.go

Lines changed: 19 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,11 @@ var (
2525
maxTerraformVersion = version.Must(version.NewVersion("1.3.0"))
2626

2727
terraformMinorVersionMismatch = xerrors.New("Terraform binary minor version mismatch.")
28+
29+
installTerraform sync.Once
30+
installTerraformExecPath string
31+
// nolint:errname
32+
installTerraformError error
2833
)
2934

3035
const (
@@ -93,18 +98,21 @@ func Serve(ctx context.Context, options *ServeOptions) error {
9398
return xerrors.Errorf("absolute binary context canceled: %w", err)
9499
}
95100

96-
installer := &releases.ExactVersion{
97-
InstallDir: options.CachePath,
98-
Product: product.Terraform,
99-
Version: TerraformVersion,
100-
}
101-
installer.SetLogger(slog.Stdlib(ctx, options.Logger, slog.LevelDebug))
102-
options.Logger.Info(ctx, "installing terraform", slog.F("dir", options.CachePath), slog.F("version", TerraformVersion))
103-
execPath, err := installer.Install(ctx)
104-
if err != nil {
105-
return xerrors.Errorf("install terraform: %w", err)
101+
// We don't want to install Terraform multiple times!
102+
installTerraform.Do(func() {
103+
installer := &releases.ExactVersion{
104+
InstallDir: options.CachePath,
105+
Product: product.Terraform,
106+
Version: TerraformVersion,
107+
}
108+
installer.SetLogger(slog.Stdlib(ctx, options.Logger, slog.LevelDebug))
109+
options.Logger.Debug(ctx, "installing terraform", slog.F("dir", options.CachePath), slog.F("version", TerraformVersion))
110+
installTerraformExecPath, installTerraformError = installer.Install(ctx)
111+
})
112+
if installTerraformError != nil {
113+
return xerrors.Errorf("install terraform: %w", installTerraformError)
106114
}
107-
options.BinaryPath = execPath
115+
options.BinaryPath = installTerraformExecPath
108116
} else {
109117
options.BinaryPath = absoluteBinary
110118
}

0 commit comments

Comments
 (0)