Skip to content
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

t3c remove stale git lock file #7346

Merged
merged 8 commits into from
Feb 10, 2023
Merged
Show file tree
Hide file tree
Changes from 4 commits
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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
13 changes: 13 additions & 0 deletions cache-config/t3c-apply/t3c-apply.go
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,19 @@ func Main() int {
}

if cfg.UseGit == config.UseGitYes || cfg.UseGit == config.UseGitAuto {
//need to see if there is an old lock file laying around.
const gitMaxLockAge = 5
jpappa200 marked this conversation as resolved.
Show resolved Hide resolved
oldLock, err := util.IsGitLockFileOld(cfg, time.Now(), gitMaxLockAge)
if err != nil {
log.Errorln("checking for git lock file: " + err.Error())
}
if oldLock {
log.Errorf("removing git lock file older than %dm", gitMaxLockAge)
err := util.RemoveGitLock(cfg)
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 {
Expand Down
27 changes: 27 additions & 0 deletions cache-config/t3c-apply/util/gitutil.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,9 @@ import (
"errors"
"fmt"
"io/ioutil"
"os"
"os/exec"
"path/filepath"
"strconv"
"strings"
"time"
Expand Down Expand Up @@ -201,3 +203,28 @@ 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)
}

const gitLock = ".git/index.lock"

func IsGitLockFileOld(cfg config.Cfg, now time.Time, maxAge time.Duration) (bool, error) {

lockFile := filepath.Join(cfg.TsConfigDir, gitLock)
oldLock := maxAge * time.Minute
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 > oldLock {
return true, nil
}
return false, nil
}

func RemoveGitLock(cfg config.Cfg) error {
lockFile := filepath.Join(cfg.TsConfigDir, gitLock)
err := os.Remove(lockFile)
if err != nil {
return fmt.Errorf("error removing file: %v, %v", lockFile, err.Error())
}
return nil
}