MCPcopy Index your code
hub / github.com/coder/coder / Install

Function Install

provisioner/terraform/install.go:37–122  ·  view source on GitHub ↗

Install implements a thread-safe, idempotent Terraform Install operation. nolint:revive // verbose is a control flag that controls the verbosity of the log output.

(ctx context.Context, log slog.Logger, verbose bool, dir string, wantVersion *version.Version, baseUrl string)

Source from the content-addressed store, hash-verified

35//
36//nolint:revive // verbose is a control flag that controls the verbosity of the log output.
37func Install(ctx context.Context, log slog.Logger, verbose bool, dir string, wantVersion *version.Version, baseUrl string) (string, error) {
38 err := os.MkdirAll(dir, 0o750)
39 if err != nil {
40 return "", err
41 }
42
43 // Windows requires a separate lock file.
44 // See https://github.com/pinterest/knox/blob/master/client/flock_windows.go#L64
45 // for precedent.
46 lockFilePath := filepath.Join(dir, "lock")
47 lock := flock.New(lockFilePath)
48 ok, err := lock.TryLockContext(ctx, time.Millisecond*100)
49 if !ok {
50 return "", xerrors.Errorf("could not acquire flock for %v: %w", lockFilePath, err)
51 }
52 defer lock.Close()
53
54 binPath := filepath.Join(dir, product.Terraform.BinaryName())
55
56 hasVersionStr := "nil"
57 hasVersion, err := versionFromBinaryPath(ctx, binPath)
58 if err == nil {
59 hasVersionStr = hasVersion.String()
60 if hasVersion.Equal(wantVersion) {
61 return binPath, err
62 }
63 }
64
65 installer := &releases.ExactVersion{
66 InstallDir: dir,
67 Product: product.Terraform,
68 Version: TerraformVersion,
69 }
70 installer.SetLogger(slog.Stdlib(ctx, log, slog.LevelDebug))
71 if baseUrl != "" {
72 installer.ApiBaseURL = baseUrl
73 }
74
75 logInstall := log.Debug
76 if verbose {
77 logInstall = log.Info
78 }
79
80 logInstall(ctx, "installing terraform",
81 slog.F("prev_version", hasVersionStr),
82 slog.F("dir", dir),
83 slog.F("version", TerraformVersion))
84
85 prolongedInstall := atomic.Bool{}
86 prolongedInstallCtx, prolongedInstallCancel := context.WithCancel(ctx)
87 go func() {
88 seconds := 15
89 select {
90 case <-time.After(time.Duration(seconds) * time.Second):
91 prolongedInstall.Store(true)
92 // We always want to log this at the info level.
93 log.Info(
94 prolongedInstallCtx,

Callers 2

TestInstallFunction · 0.92
ServeFunction · 0.85

Calls 13

versionFromBinaryPathFunction · 0.85
MkdirAllMethod · 0.80
DurationMethod · 0.80
NewMethod · 0.65
CloseMethod · 0.65
SetLoggerMethod · 0.65
ErrorfMethod · 0.45
StringMethod · 0.45
EqualMethod · 0.45
StoreMethod · 0.45
InfoMethod · 0.45
DoneMethod · 0.45

Tested by 1

TestInstallFunction · 0.74