Skip to content

feat: clean stale provisioner files #9545

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 27 commits into from
Sep 11, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Stub for cleanStaleTerraformPlugins
  • Loading branch information
mtojek committed Sep 6, 2023
commit e2c71f87a7589fa4e3d4f381d40adc254a3753d7
29 changes: 27 additions & 2 deletions provisioner/terraform/provision.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,10 +89,13 @@ func (s *server) Plan(
}
}

// TODO Clean stale TF files
err := cleanStaleTerraformPlugins(sess.Context(), s.cachePath, time.Now(), s.logger)
if err != nil {
return provisionersdk.PlanErrorf("unable to clean stale Terraform plugins: %s", err)
}

s.logger.Debug(ctx, "running initialization")
err := e.init(ctx, killCtx, sess)
err = e.init(ctx, killCtx, sess)
if err != nil {
s.logger.Debug(ctx, "init failed", slog.Error(err))
return provisionersdk.PlanErrorf("initialize terraform: %s", err)
Expand Down Expand Up @@ -241,3 +244,25 @@ func logTerraformEnvVars(sink logSink) {
}
}
}

// cleanStaleTerraformPlugins browses the Terraform cache directory
// and remove stale plugins that haven't been used for a while.
//
// Additionally, it sweeps empty, old directory trees.
//
// Sample cachePath: /Users/<username>/Library/Caches/coder/provisioner-<N>/tf
func cleanStaleTerraformPlugins(ctx context.Context, cachePath string, now time.Time, logger slog.Logger) error {
// Review cached Terraform plugins
// TODO

// Identify stale plugins
// TODO

// Remove stale plugins
// TODO

// Maintain the directory tree
// TODO

return nil
}
5 changes: 4 additions & 1 deletion provisionersdk/session.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ func (p *protoServer) Session(stream proto.DRPCProvisioner_SessionStream) error

err := cleanStaleSessions(s.Context(), p.opts.WorkDirectory, time.Now(), s.Logger)
if err != nil {
return xerrors.Errorf("clean stale sessions %q: %w", s.WorkDirectory, err)
return xerrors.Errorf("unable to clean stale sessions %q: %w", s.WorkDirectory, err)
}

s.WorkDirectory = filepath.Join(p.opts.WorkDirectory, sessionDir(sessID))
Expand Down Expand Up @@ -327,6 +327,9 @@ func (r *request[R, C]) do() (C, error) {
}
}

// cleanStaleSessions browses the work directory searching for stale session
// directories. Coder provisioner is supposed to remove them once after finishing the provisioning,
// but there is a risk of keeping them in case of a failure.
func cleanStaleSessions(ctx context.Context, workDirectory string, now time.Time, logger slog.Logger) error {
entries, err := os.ReadDir(workDirectory)
if err != nil {
Expand Down