Skip to content

Commit

Permalink
t3c RPM DB check to work with rocky linux 9 (#7866)
Browse files Browse the repository at this point in the history
(cherry picked from commit 7a3e1e0)
  • Loading branch information
jpappa200 authored and rimashah25 committed Jan 24, 2024
1 parent 44ede89 commit 6cb2bca
Show file tree
Hide file tree
Showing 2 changed files with 86 additions and 25 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,7 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/).
- [#7688](https://github.com/apache/trafficcontrol/pull/7688) *Traffic Ops*: Fixed secured parameters being visible when role has proper permissions.
- [#7697](https://github.com/apache/trafficcontrol/pull/7697) *Traffic Ops*: Fixed `iloPassword` and `xmppPassword` checking for priv-level instead of using permissions.
- [#7817](https://github.com/apache/trafficcontrol/pull/7817) *Traffic Control Cache Config (t3c)*: Fixed issue that would cause null ptr panic on client fallback.
- [#7866](https://github.com/apache/trafficcontrol/pull/7866) *Traffic Control Cache Config (t3c)*: Fixed rpm db check to work with rocky linux 9.

### Removed
- [#7808](https://github.com/apache/trafficcontrol/pull/7808) *Traffic Router*: Set SOA `minimum` field to a custom value defined in the `tld.soa.minimum` param, and remove the previously added `dns.negative.caching.ttl` property.
Expand Down
110 changes: 85 additions & 25 deletions cache-config/t3c-apply/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -191,29 +191,6 @@ func directoryExists(dir string) (bool, os.FileInfo) {
return info.IsDir(), info
}

const rpmDir = "/var/lib/rpm"

// verifies the rpm database files. if there is any database corruption
// it will return false
func verifyRpmDB() bool {
exclude := regexp.MustCompile(`(^\.|^__)`)
dbFiles, err := os.ReadDir(rpmDir)
if err != nil {
return false
}
for _, file := range dbFiles {
if exclude.Match([]byte(file.Name())) {
continue
}
cmd := exec.Command("/usr/lib/rpm/rpmdb_verify", rpmDir+"/"+file.Name())
err := cmd.Run()
if err != nil || cmd.ProcessState.ExitCode() > 0 {
return false
}
}
return true
}

// derives the ATS Installation directory from
// the rpm config file list.
func GetTSPackageHome() string {
Expand Down Expand Up @@ -251,6 +228,68 @@ func GetTSPackageHome() string {
return tsHome
}

const (
rpmDBBdb = "bdb"
rpmDBSquLite = "sqlite"
rpmDBUnknown = "unknown"
rpmDBVerifyCmd = "/usr/lib/rpm/rpmdb_verify"
sqliteRpmDbVerifyCmd = "/bin/sqlite3"
rpmDir = "/var/lib/rpm"
sqliteRpmDB = "rpmdb.sqlite"
)

// getRpmDBBackend uses "%_db_backend" macro to get the database type
func getRpmDBBackend() (string, error) {
var outBuf bytes.Buffer
cmd := exec.Command("/bin/rpm", "-E", "%_db_backend")
cmd.Stdout = &outBuf
err := cmd.Run()
if err != nil {
return rpmDBUnknown, err
}
return strings.TrimSpace(outBuf.String()), nil
}

// isSqliteInstalled looks to see if the sqlite3 executable
// is installed which is needed to do the db verify
func isSqliteInstalled() bool {
sqliteUtil := isCommandAvailable("/bin/sqlite3")
return sqliteUtil
}

// verifies the rpm database files. if there is any database corruption
// it will return false
func verifyRpmDB(rpmDir string) bool {
exclude := regexp.MustCompile(`(^\.|^__)`)
dbFiles, err := os.ReadDir(rpmDir)
if err != nil {
return false
}
for _, file := range dbFiles {
if exclude.Match([]byte(file.Name())) {
continue
}
cmd := exec.Command(rpmDBVerifyCmd, rpmDir+"/"+file.Name())
err := cmd.Run()
if err != nil || cmd.ProcessState.ExitCode() > 0 {
return false
}
}
return true
}

// verifySqliteRpmDB runs PRAGMA quick_check
// requires /bin/sqlite3
func verifySqliteRpmDB(sqliteDB string) bool {
args := []string{sqliteDB, `PRAGMA quick_check`}
cmd := exec.Command(sqliteRpmDbVerifyCmd, args...)
err := cmd.Run()
if err != nil || cmd.ProcessState.ExitCode() > 0 {
return false
}
return true
}

func GetCfg(appVersion string, gitRevision string) (Cfg, error) {
var err error
toInfoLog := []string{}
Expand Down Expand Up @@ -499,7 +538,29 @@ If any of the related flags are also set, they override the mode's default behav
os.Setenv("TO_PASS", toPass)
}

rpmDBisOk := verifyRpmDB()
rpmDBisOk := true
rpmDBType, err := getRpmDBBackend()
if err != nil {
toInfoLog = append(toInfoLog, fmt.Sprintf("error getting db type: %s", err.Error()))
rpmDBType = rpmDBUnknown
}

if rpmDBType == rpmDBSquLite {
sqliteUtil := isSqliteInstalled()
toInfoLog = append(toInfoLog, fmt.Sprintf("RPM database is %s", rpmDBSquLite))
if sqliteUtil {
rpmDBisOk = verifySqliteRpmDB(rpmDir + "/" + sqliteRpmDB)
toInfoLog = append(toInfoLog, fmt.Sprintf("RPM database is ok: %t", rpmDBisOk))
} else {
toInfoLog = append(toInfoLog, "/bin/sqlite3 not available, RPM database not checked")
}
} else if rpmDBType == rpmDBBdb {
toInfoLog = append(toInfoLog, fmt.Sprintf("RPM database is %s", rpmDBBdb))
rpmDBisOk = verifyRpmDB(rpmDir)
toInfoLog = append(toInfoLog, fmt.Sprintf("RPM database is ok: %t", rpmDBisOk))
} else {
toInfoLog = append(toInfoLog, fmt.Sprintf("RPM DB type is %s DB check will be skipped", rpmDBUnknown))
}

if *installPackagesPtr && !rpmDBisOk {
if t3cutil.StrToMode(*runModePtr) == t3cutil.ModeBadAss {
Expand All @@ -509,7 +570,6 @@ If any of the related flags are also set, they override the mode's default behav
}
}

toInfoLog = append(toInfoLog, fmt.Sprintf("rpm database is ok: %t", rpmDBisOk))
// set TSHome
var tsHome = ""
if *tsHomePtr != "" {
Expand Down

0 comments on commit 6cb2bca

Please sign in to comment.