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

Method init

provisioner/terraform/executor.go:218–279  ·  view source on GitHub ↗
(ctx, killCtx context.Context, logr logSink)

Source from the content-addressed store, hash-verified

216}
217
218func (e *executor) init(ctx, killCtx context.Context, logr logSink) error {
219 ctx, span := e.server.startTrace(ctx, tracing.FuncName())
220 defer span.End()
221
222 e.mut.Lock()
223 defer e.mut.Unlock()
224
225 // Record lock file checksum before init
226 lockFilePath := e.files.TerraformLockFile()
227 preInitChecksum := checksumFileCRC32(ctx, e.logger, lockFilePath)
228
229 outWriter, doneOut := e.provisionLogWriter(logr)
230 errWriter, doneErr := logWriter(logr, proto.LogLevel_ERROR)
231 defer func() {
232 _ = outWriter.Close()
233 _ = errWriter.Close()
234 <-doneOut
235 <-doneErr
236 }()
237
238 // As a special case, we want to look for the error "text file busy" in the stderr output of
239 // the init command, so we also take a copy of the stderr into an in memory buffer.
240 errBuf := newBufferedWriteCloser(errWriter)
241
242 args := []string{
243 "init",
244 "-no-color",
245 "-input=false",
246 }
247
248 ver, err := e.version(ctx)
249 if err != nil {
250 return xerrors.Errorf("extract version: %w", err)
251 }
252 if ver.GreaterThanOrEqual(version190) {
253 // Added in v1.9.0:
254 args = append(args, "-json")
255 }
256
257 err = e.execWriteOutput(ctx, killCtx, args, e.basicEnv(), outWriter, errBuf)
258 var exitErr *exec.ExitError
259 if xerrors.As(err, &exitErr) {
260 if bytes.Contains(errBuf.b.Bytes(), []byte("text file busy")) {
261 return &textFileBusyError{exitErr: exitErr, stderr: errBuf.b.String()}
262 }
263 }
264 if err != nil {
265 return err
266 }
267
268 // Check if lock file was modified
269 postInitChecksum := checksumFileCRC32(ctx, e.logger, lockFilePath)
270 if preInitChecksum != 0 && postInitChecksum != 0 && preInitChecksum != postInitChecksum {
271 e.logger.Warn(ctx, fmt.Sprintf(".terraform.lock.hcl was modified during init. This means provider hashes "+
272 "are missing for the current platform (%s_%s). Update the lock file with:\n\n"+
273 " terraform providers lock -platform=linux_amd64 -platform=linux_arm64 "+
274 "-platform=darwin_amd64 -platform=darwin_arm64 -platform=windows_amd64\n",
275 runtime.GOOS, runtime.GOARCH),

Callers 1

InitMethod · 0.45

Calls 15

provisionLogWriterMethod · 0.95
versionMethod · 0.95
execWriteOutputMethod · 0.95
basicEnvMethod · 0.95
FuncNameFunction · 0.92
checksumFileCRC32Function · 0.85
newBufferedWriteCloserFunction · 0.85
TerraformLockFileMethod · 0.80
AsMethod · 0.80
logWriterFunction · 0.70
CloseMethod · 0.65
startTraceMethod · 0.45

Tested by

no test coverage detected