fix: os.Remove should ignore non existing errors (#4502)

This commit is contained in:
Christian Winther
2024-05-04 21:59:22 +02:00
committed by GitHub
parent d8eb27f583
commit f35b8c69aa
7 changed files with 25 additions and 10 deletions

View File

@@ -12,6 +12,7 @@ import (
version "github.com/hashicorp/go-version"
"github.com/runatlantis/atlantis/server/events/command"
"github.com/runatlantis/atlantis/server/events/models"
"github.com/runatlantis/atlantis/server/utils"
)
// ApplyStepRunner runs `terraform apply`.
@@ -56,7 +57,7 @@ func (a *ApplyStepRunner) Run(ctx command.ProjectContext, extraArgs []string, pa
// If the apply was successful, delete the plan.
if err == nil {
ctx.Log.Info("apply successful, deleting planfile")
if removeErr := os.Remove(planPath); removeErr != nil {
if removeErr := utils.RemoveIgnoreNonExistent(planPath); removeErr != nil {
ctx.Log.Warn("failed to delete planfile after successful apply: %s", removeErr)
}
}
@@ -116,7 +117,6 @@ func (a *ApplyStepRunner) runRemoteApply(
absPlanPath string,
tfVersion *version.Version,
envs map[string]string) (string, error) {
// The planfile contents are needed to ensure that the plan didn't change
// between plan and apply phases.
planfileBytes, err := os.ReadFile(absPlanPath)

View File

@@ -6,6 +6,7 @@ import (
version "github.com/hashicorp/go-version"
"github.com/runatlantis/atlantis/server/events/command"
"github.com/runatlantis/atlantis/server/utils"
)
type importStepRunner struct {
@@ -37,7 +38,7 @@ func (p *importStepRunner) Run(ctx command.ProjectContext, extraArgs []string, p
if err == nil {
if _, planPathErr := os.Stat(planPath); !os.IsNotExist(planPathErr) {
ctx.Log.Info("import successful, deleting planfile")
if removeErr := os.Remove(planPath); removeErr != nil {
if removeErr := utils.RemoveIgnoreNonExistent(planPath); removeErr != nil {
ctx.Log.Warn("failed to delete planfile after successful import: %s", removeErr)
}
}

View File

@@ -1,12 +1,12 @@
package runtime
import (
"os"
"path/filepath"
version "github.com/hashicorp/go-version"
"github.com/runatlantis/atlantis/server/core/runtime/common"
"github.com/runatlantis/atlantis/server/events/command"
"github.com/runatlantis/atlantis/server/utils"
)
// InitStep runs `terraform init`.
@@ -21,14 +21,13 @@ func (i *InitStepRunner) Run(ctx command.ProjectContext, extraArgs []string, pat
terraformLockFileTracked, err := common.IsFileTracked(path, lockFileName)
if err != nil {
ctx.Log.Warn("Error checking if %s is tracked in %s", lockFileName, path)
}
// If .terraform.lock.hcl is not tracked in git and it exists prior to init
// delete it as it probably has been created by a previous run of
// terraform init
if common.FileExists(terraformLockfilePath) && !terraformLockFileTracked {
ctx.Log.Debug("Deleting `%s` that was generated by previous terraform init", terraformLockfilePath)
delErr := os.Remove(terraformLockfilePath)
delErr := utils.RemoveIgnoreNonExistent(terraformLockfilePath)
if delErr != nil {
ctx.Log.Info("Error Deleting `%s`", lockFileName)
}

View File

@@ -6,6 +6,7 @@ import (
version "github.com/hashicorp/go-version"
"github.com/runatlantis/atlantis/server/events/command"
"github.com/runatlantis/atlantis/server/utils"
)
type stateRmStepRunner struct {
@@ -37,7 +38,7 @@ func (p *stateRmStepRunner) Run(ctx command.ProjectContext, extraArgs []string,
if err == nil {
if _, planPathErr := os.Stat(planPath); !os.IsNotExist(planPathErr) {
ctx.Log.Info("state rm successful, deleting planfile")
if removeErr := os.Remove(planPath); removeErr != nil {
if removeErr := utils.RemoveIgnoreNonExistent(planPath); removeErr != nil {
ctx.Log.Warn("failed to delete planfile after successful state rm: %s", removeErr)
}
}

View File

@@ -8,6 +8,7 @@ import (
"github.com/pkg/errors"
"github.com/runatlantis/atlantis/server/core/runtime"
"github.com/runatlantis/atlantis/server/utils"
)
//go:generate pegomock generate --package mocks -o mocks/mock_pending_plan_finder.go PendingPlanFinder
@@ -92,7 +93,7 @@ func (p *DefaultPendingPlanFinder) DeletePlans(pullDir string) error {
return err
}
for _, path := range absPaths {
if err := os.Remove(path); err != nil {
if err := utils.RemoveIgnoreNonExistent(path); err != nil {
return errors.Wrapf(err, "delete plan at %s", path)
}
}

View File

@@ -26,6 +26,7 @@ import (
"github.com/runatlantis/atlantis/server/core/runtime"
"github.com/runatlantis/atlantis/server/events/models"
"github.com/runatlantis/atlantis/server/logging"
"github.com/runatlantis/atlantis/server/utils"
)
const workingDirPrefix = "repos"
@@ -179,7 +180,6 @@ func (w *FileWorkspace) recheckDiverged(logger logging.SimpleLogging, p models.P
cmd.Dir = cloneDir
output, err := cmd.CombinedOutput()
if err != nil {
logger.Warn("getting remote update failed: %s", string(output))
return false
@@ -420,7 +420,7 @@ func (w *FileWorkspace) SetCheckForUpstreamChanges() {
func (w *FileWorkspace) DeletePlan(logger logging.SimpleLogging, r models.Repo, p models.PullRequest, workspace string, projectPath string, projectName string) error {
planPath := filepath.Join(w.cloneDir(r, p, workspace), projectPath, runtime.GetPlanFilename(workspace, projectName))
logger.Info("Deleting plan: " + planPath)
return os.Remove(planPath)
return utils.RemoveIgnoreNonExistent(planPath)
}
// getGitUntrackedFiles returns a list of Git untracked files in the working dir.

13
server/utils/os.go Normal file
View File

@@ -0,0 +1,13 @@
package utils
import "os"
// RemoveIgnoreNonExistent removes a file, ignoring if it doesn't exist.
func RemoveIgnoreNonExistent(file string) error {
err := os.Remove(file)
if err == nil || os.IsNotExist(err) {
return nil
}
return err
}