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
WIP use afero
  • Loading branch information
mtojek committed Sep 7, 2023
commit 1b49d80dcdfb9835c7f65ad31b70845dd722eea4
22 changes: 11 additions & 11 deletions provisioner/terraform/cleanup.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,34 +2,34 @@ package terraform

import (
"context"
"io/fs"
"os"
"path/filepath"
"strings"
"time"

"github.com/djherbis/times"
"github.com/spf13/afero"
"golang.org/x/xerrors"

"cdr.dev/slog"
)

// cleanStaleTerraformPlugins browses the Terraform cache directory
// 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/john.doe/Library/Caches/coder/provisioner-1/tf
// /tmp/coder/provisioner-0/tf
func cleanStaleTerraformPlugins(ctx context.Context, cachePath string, now time.Time, logger slog.Logger) error {
func CleanStaleTerraformPlugins(ctx context.Context, cachePath string, fs afero.Fs, now time.Time, logger slog.Logger) error {
cachePath, err := filepath.Abs(cachePath) // sanity check in case the path is e.g. ../../../cache
if err != nil {
return xerrors.Errorf("unable to determine absolute path %q: %w", cachePath, err)
}

// Firstly, check if the cache path exists.
_, err = os.Stat(cachePath)
_, err = fs.Stat(cachePath)
if os.IsNotExist(err) {
return nil
} else if err != nil {
Expand All @@ -56,7 +56,7 @@ func cleanStaleTerraformPlugins(ctx context.Context, cachePath string, now time.

// Review cached Terraform plugins
var pluginPaths []string
err = filepath.Walk(cachePath, func(path string, info fs.FileInfo, err error) error {
err = afero.Walk(fs, cachePath, func(path string, info os.FileInfo, err error) error {
if err != nil {
return err
}
Expand All @@ -76,7 +76,7 @@ func cleanStaleTerraformPlugins(ctx context.Context, cachePath string, now time.
// Identify stale plugins
var stalePlugins []string
for _, pluginPath := range pluginPaths {
accessTime, err := latestAccessTime(pluginPath)
accessTime, err := latestAccessTime(fs, pluginPath)
if err != nil {
return xerrors.Errorf("unable to evaluate latest access time for directory %q: %w", pluginPath, err)
}
Expand All @@ -92,7 +92,7 @@ func cleanStaleTerraformPlugins(ctx context.Context, cachePath string, now time.
// Remove stale plugins
for _, stalePluginPath := range stalePlugins {
// Remove the plugin directory
err = os.RemoveAll(stalePluginPath)
err = fs.RemoveAll(stalePluginPath)
if err != nil {
return xerrors.Errorf("unable to remove stale plugin %q: %w", stalePluginPath, err)
}
Expand All @@ -108,7 +108,7 @@ func cleanStaleTerraformPlugins(ctx context.Context, cachePath string, now time.

wd = filepath.Dir(wd)

files, err := os.ReadDir(wd)
files, err := afero.ReadDir(fs, wd)
if err != nil {
return xerrors.Errorf("unable to read directory content %q: %w", wd, err)
}
Expand All @@ -118,7 +118,7 @@ func cleanStaleTerraformPlugins(ctx context.Context, cachePath string, now time.
}

logger.Debug(ctx, "remove empty directory", slog.F("path", wd))
err = os.Remove(wd)
err = fs.Remove(wd)
if err != nil {
return xerrors.Errorf("unable to remove directory %q: %w", wd, err)
}
Expand All @@ -129,9 +129,9 @@ func cleanStaleTerraformPlugins(ctx context.Context, cachePath string, now time.

// latestAccessTime walks recursively through the directory content, and locates
// the last accessed file.
func latestAccessTime(pluginPath string) (time.Time, error) {
func latestAccessTime(fs afero.Fs, pluginPath string) (time.Time, error) {
var latest time.Time
err := filepath.Walk(pluginPath, func(path string, info fs.FileInfo, err error) error {
err := afero.Walk(fs, pluginPath, func(path string, info os.FileInfo, err error) error {
if err != nil {
return err
}
Expand Down
29 changes: 29 additions & 0 deletions provisioner/terraform/cleanup_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package terraform_test

import (
"context"
"testing"
"time"

"github.com/spf13/afero"

"cdr.dev/slog/sloggers/slogtest"
"github.com/coder/coder/v2/provisioner/terraform"
"github.com/coder/coder/v2/testutil"
)

const (
cachePath = "/tmp/coder/provisioner-0/tf"
)

func TestOnePluginIsStale(t *testing.T) {
ctx, cancel := context.WithTimeout(context.Background(), testutil.WaitShort)
defer cancel()

fs := afero.NewMemMapFs()
now := time.Now()
logger := slogtest.Make(t, nil).Named("cleanup-test")

terraform.CleanStaleTerraformPlugins(ctx, cachePath, fs, now, logger)

}
4 changes: 3 additions & 1 deletion provisioner/terraform/provision.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import (
"strings"
"time"

"github.com/spf13/afero"

"cdr.dev/slog"
"github.com/coder/terraform-provider-coder/provider"

Expand Down Expand Up @@ -91,7 +93,7 @@ func (s *server) Plan(
}
}

err := cleanStaleTerraformPlugins(sess.Context(), "/tmp/coder/provisioner-0/tf", time.Now(), s.logger)
err := CleanStaleTerraformPlugins(sess.Context(), s.cachePath, afero.NewOsFs(), time.Now(), s.logger)
if err != nil {
return provisionersdk.PlanErrorf("unable to clean stale Terraform plugins: %s", err)
}
Expand Down