|
25 | 25 | maxTerraformVersion = version.Must(version.NewVersion("1.3.0"))
|
26 | 26 |
|
27 | 27 | terraformMinorVersionMismatch = xerrors.New("Terraform binary minor version mismatch.")
|
| 28 | + |
| 29 | + installTerraform sync.Once |
| 30 | + installTerraformExecPath string |
| 31 | + // nolint:errname |
| 32 | + installTerraformError error |
28 | 33 | )
|
29 | 34 |
|
30 | 35 | const (
|
@@ -93,18 +98,21 @@ func Serve(ctx context.Context, options *ServeOptions) error {
|
93 | 98 | return xerrors.Errorf("absolute binary context canceled: %w", err)
|
94 | 99 | }
|
95 | 100 |
|
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) |
106 | 114 | }
|
107 |
| - options.BinaryPath = execPath |
| 115 | + options.BinaryPath = installTerraformExecPath |
108 | 116 | } else {
|
109 | 117 | options.BinaryPath = absoluteBinary
|
110 | 118 | }
|
|
0 commit comments