Skip to content

Commit 3ad90cd

Browse files
committed
change terraform download warning to debug log
1 parent 9e8c55f commit 3ad90cd

File tree

4 files changed

+50
-15
lines changed

4 files changed

+50
-15
lines changed

provisioner/terraform/install.go

Lines changed: 38 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,10 @@ package terraform
22

33
import (
44
"context"
5+
"fmt"
56
"os"
67
"path/filepath"
8+
"sync/atomic"
79
"time"
810

911
"github.com/gofrs/flock"
@@ -30,7 +32,9 @@ var (
3032

3133
// Install implements a thread-safe, idempotent Terraform Install
3234
// operation.
33-
func Install(ctx context.Context, log slog.Logger, dir string, wantVersion *version.Version) (string, error) {
35+
//
36+
//nolint:revive // verbose is a control flag that controls the verbosity of the log output.
37+
func Install(ctx context.Context, log slog.Logger, verbose bool, dir string, wantVersion *version.Version) (string, error) {
3438
err := os.MkdirAll(dir, 0o750)
3539
if err != nil {
3640
return "", err
@@ -64,13 +68,37 @@ func Install(ctx context.Context, log slog.Logger, dir string, wantVersion *vers
6468
Version: TerraformVersion,
6569
}
6670
installer.SetLogger(slog.Stdlib(ctx, log, slog.LevelDebug))
67-
log.Debug(
68-
ctx,
69-
"installing terraform",
71+
72+
logInstall := log.Debug
73+
if verbose {
74+
logInstall = log.Info
75+
}
76+
77+
logInstall(ctx, "installing terraform",
7078
slog.F("prev_version", hasVersionStr),
7179
slog.F("dir", dir),
72-
slog.F("version", TerraformVersion),
73-
)
80+
slog.F("version", TerraformVersion))
81+
82+
prolongedInstall := atomic.Bool{}
83+
prolongedInstallCtx, prolongedInstallCancel := context.WithCancel(ctx)
84+
go func() {
85+
seconds := 15
86+
select {
87+
case <-time.After(time.Duration(seconds) * time.Second):
88+
prolongedInstall.Store(true)
89+
// We always want to log this at the info level.
90+
log.Info(
91+
prolongedInstallCtx,
92+
fmt.Sprintf("terraform installation is taking longer than %d seconds, still in progress", seconds),
93+
slog.F("prev_version", hasVersionStr),
94+
slog.F("dir", dir),
95+
slog.F("version", TerraformVersion),
96+
)
97+
case <-prolongedInstallCtx.Done():
98+
return
99+
}
100+
}()
101+
defer prolongedInstallCancel()
74102

75103
path, err := installer.Install(ctx)
76104
if err != nil {
@@ -83,5 +111,9 @@ func Install(ctx context.Context, log slog.Logger, dir string, wantVersion *vers
83111
return "", xerrors.Errorf("%s should be %s", path, binPath)
84112
}
85113

114+
if prolongedInstall.Load() {
115+
log.Info(ctx, "terraform installation complete")
116+
}
117+
86118
return path, nil
87119
}

provisioner/terraform/install_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ func TestInstall(t *testing.T) {
4040
wg.Add(1)
4141
go func() {
4242
defer wg.Done()
43-
p, err := terraform.Install(ctx, log, dir, version)
43+
p, err := terraform.Install(ctx, log, false, dir, version)
4444
assert.NoError(t, err)
4545
paths <- p
4646
}()

provisioner/terraform/serve.go

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,8 @@ type ServeOptions struct {
4141
ExitTimeout time.Duration
4242
}
4343

44-
func absoluteBinaryPath(ctx context.Context, logger slog.Logger) (string, error) {
44+
// nolint:revive // verbose is a control flag that controls the verbosity of the log output.
45+
func absoluteBinaryPath(ctx context.Context, logger slog.Logger, verbose bool) (string, error) {
4546
binaryPath, err := safeexec.LookPath("terraform")
4647
if err != nil {
4748
return "", xerrors.Errorf("Terraform binary not found: %w", err)
@@ -63,7 +64,12 @@ func absoluteBinaryPath(ctx context.Context, logger slog.Logger) (string, error)
6364
return "", xerrors.Errorf("Terraform binary get version failed: %w", err)
6465
}
6566

66-
logger.Info(ctx, "detected terraform version",
67+
logVersion := logger.Debug
68+
if verbose {
69+
logVersion = logger.Info
70+
}
71+
72+
logVersion(ctx, "detected terraform version",
6773
slog.F("installed_version", installedVersion.String()),
6874
slog.F("min_version", minTerraformVersion.String()),
6975
slog.F("max_version", maxTerraformVersion.String()))
@@ -88,7 +94,7 @@ func absoluteBinaryPath(ctx context.Context, logger slog.Logger) (string, error)
8894
// Serve starts a dRPC server on the provided transport speaking Terraform provisioner.
8995
func Serve(ctx context.Context, options *ServeOptions) error {
9096
if options.BinaryPath == "" {
91-
absoluteBinary, err := absoluteBinaryPath(ctx, options.Logger)
97+
absoluteBinary, err := absoluteBinaryPath(ctx, options.Logger, options.ExternalProvisioner)
9298
if err != nil {
9399
// This is an early exit to prevent extra execution in case the context is canceled.
94100
// It generally happens in unit tests since this method is asynchronous and
@@ -97,10 +103,7 @@ func Serve(ctx context.Context, options *ServeOptions) error {
97103
return xerrors.Errorf("absolute binary context canceled: %w", err)
98104
}
99105

100-
options.Logger.Warn(ctx, "no usable terraform binary found, downloading to cache dir",
101-
slog.F("terraform_version", TerraformVersion.String()),
102-
slog.F("cache_dir", options.CachePath))
103-
binPath, err := Install(ctx, options.Logger, options.CachePath, TerraformVersion)
106+
binPath, err := Install(ctx, options.Logger, options.ExternalProvisioner, options.CachePath, TerraformVersion)
104107
if err != nil {
105108
return xerrors.Errorf("install terraform: %w", err)
106109
}

provisioner/terraform/serve_internal_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ func Test_absoluteBinaryPath(t *testing.T) {
8585
}
8686

8787
ctx := testutil.Context(t, testutil.WaitShort)
88-
actualAbsoluteBinary, actualErr := absoluteBinaryPath(ctx, log)
88+
actualAbsoluteBinary, actualErr := absoluteBinaryPath(ctx, log, false)
8989

9090
require.Equal(t, expectedAbsoluteBinary, actualAbsoluteBinary)
9191
if tt.expectedErr == nil {

0 commit comments

Comments
 (0)