diff --git a/CHANGELOG.md b/CHANGELOG.md index 4205c4f8b1..c12dcd8740 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -80,6 +80,7 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/). - [#7204](https://github.com/apache/trafficcontrol/pull/7204) *Traffic Control Cache Config (t3c)* strategies.yaml hash_key only for consistent_hash - [#7277](https://github.com/apache/trafficcontrol/pull/7277) *Traffic Control Cache Config (t3c)* remapdotconfig: remove skip check at mids for nocache/live - [#7282](https://github.com/apache/trafficcontrol/pull/7282) *Traffic Ops* Fixed issue with user getting correctly logged when using an access or bearer token authentication. +- [#7346](https://github.com/apache/trafficcontrol/pull/7346) *Traffic Control Cache Config (t3c)* Fixed issue with stale lock file when using git to track changes. ## [7.0.0] - 2022-07-19 ### Added diff --git a/cache-config/t3c-apply/t3c-apply.go b/cache-config/t3c-apply/t3c-apply.go index 353e24a864..959d9f4a58 100644 --- a/cache-config/t3c-apply/t3c-apply.go +++ b/cache-config/t3c-apply/t3c-apply.go @@ -146,6 +146,22 @@ func Main() int { } if cfg.UseGit == config.UseGitYes || cfg.UseGit == config.UseGitAuto { + //need to see if there is an old lock file laying around. + //older than 5 minutes + const gitMaxLockAgeMinutes = 5 + const gitLock = ".git/index.lock" + gitLockFile := filepath.Join(cfg.TsConfigDir, gitLock) + oldLock, err := util.IsGitLockFileOld(gitLockFile, time.Now(), gitMaxLockAgeMinutes*time.Minute) + if err != nil { + log.Errorln("checking for git lock file: " + err.Error()) + } + if oldLock { + log.Errorf("removing git lock file older than %dm", gitMaxLockAgeMinutes) + err := util.RemoveGitLock(gitLockFile) + if err != nil { + log.Errorf("couldn't remove git lock file: %v", err.Error()) + } + } // commit anything someone else changed when we weren't looking, // with a keyword indicating it wasn't our change if err := util.MakeGitCommitAll(cfg, util.GitChangeNotSelf, true); err != nil { diff --git a/cache-config/t3c-apply/util/gitutil.go b/cache-config/t3c-apply/util/gitutil.go index 3706c3be2a..33d05b0529 100644 --- a/cache-config/t3c-apply/util/gitutil.go +++ b/cache-config/t3c-apply/util/gitutil.go @@ -24,6 +24,7 @@ import ( "errors" "fmt" "io/ioutil" + "os" "os/exec" "strconv" "strings" @@ -201,3 +202,22 @@ func makeGitCommitMsg(cfg config.Cfg, now time.Time, self bool, success bool) st const sep = " " return strings.Join([]string{appStr, selfStr, modeStr, successStr, timeStr}, sep) } + +func IsGitLockFileOld(lockFile string, now time.Time, maxAge time.Duration) (bool, error) { + lockFileInfo, err := os.Stat(lockFile) + if err != nil { + return false, fmt.Errorf("stat returned error: %v on file %v", err, lockFile) + } + if diff := now.Sub(lockFileInfo.ModTime()); diff > maxAge { + return true, nil + } + return false, nil +} + +func RemoveGitLock(lockFile string) error { + err := os.Remove(lockFile) + if err != nil { + return fmt.Errorf("error removing file: %v, %v", lockFile, err.Error()) + } + return nil +}