You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@trafficcontrol.apache.org by rs...@apache.org on 2023/06/30 16:34:12 UTC

[trafficcontrol] branch master updated: Add package data to t3c-apply-metadata (#7602)

This is an automated email from the ASF dual-hosted git repository.

rshah pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/trafficcontrol.git


The following commit(s) were added to refs/heads/master by this push:
     new f92471635c Add package data to t3c-apply-metadata (#7602)
f92471635c is described below

commit f92471635c9ac35c19c9d089a39201e02fb12074
Author: Joe Pappano <jo...@cable.comcast.com>
AuthorDate: Fri Jun 30 12:34:06 2023 -0400

    Add package data to t3c-apply-metadata (#7602)
    
    * adding installed packages to metadata file
    
    * added changelog entry
    
    * fixes for package data in reval mode
    
    * updated changelog entry
---
 CHANGELOG.md                                       |  1 +
 cache-config/t3c-apply/t3c-apply.go                |  5 +++++
 cache-config/t3c-apply/torequest/torequest.go      | 20 ++++++++++----------
 cache-config/t3c-apply/torequest/torequest_test.go |  4 ++--
 cache-config/t3cutil/t3cutil.go                    | 15 +++++++++++++--
 5 files changed, 31 insertions(+), 14 deletions(-)

diff --git a/CHANGELOG.md b/CHANGELOG.md
index b82cec39ad..8f7e10bb74 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -38,6 +38,7 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/).
 - [#7388](https://github.com/apache/trafficcontrol/pull/7388) *TC go Client* Adds sslkey_expiration methodology in v4 and v5 clients
 - [#7543](https://github.com/apache/trafficcontrol/pull/7543) *Traffic Portal* New Ansible Role to use Traffic Portal v2
 - [#7516](https://github.com/apache/trafficcontrol/pull/7516) *t3c* added command line arg to control go_direct in parent.config
+- [#7602](https://github.com/apache/trafficcontrol/pull/7602) *t3c* added installed package data to t3c-apply-metadata.json
 
 ### Changed
 - [#7584](https://github.com/apache/trafficcontrol/pull/7584) *Documentation* Upgrade Traffic Control Sphinx documentation Makefile OS intelligent.
diff --git a/cache-config/t3c-apply/t3c-apply.go b/cache-config/t3c-apply/t3c-apply.go
index 28cc44962f..bc98fcf23a 100644
--- a/cache-config/t3c-apply/t3c-apply.go
+++ b/cache-config/t3c-apply/t3c-apply.go
@@ -260,6 +260,7 @@ func Main() int {
 	if cfg.Files != t3cutil.ApplyFilesFlagAll {
 		// make sure we got the data necessary to check packages
 		log.Infoln("======== Didn't get all files, no package processing needed or possible ========")
+		metaData.InstalledPackages = oldMetaData.InstalledPackages
 	} else {
 		log.Infoln("======== Start processing packages  ========")
 		err = trops.ProcessPackages()
@@ -267,6 +268,7 @@ func Main() int {
 			log.Errorf("Error processing packages: %s\n", err)
 			return GitCommitAndExit(ExitCodePackagingError, FailureExitMsg, cfg, metaData, oldMetaData)
 		}
+		metaData.InstalledPackages = t3cutil.PackagesToMetaData(trops.Pkgs)
 
 		// check and make sure packages are enabled for startup
 		err = trops.CheckSystemServices()
@@ -369,6 +371,9 @@ func GitCommitAndExit(exitCode int, exitMsg string, cfg config.Cfg, metaData *t3
 	// so add the old files to the new metadata.
 	// This is especially important for reval runs, which don't add all files.
 	metaData.OwnedFilePaths = t3cutil.CombineOwnedFilePaths(metaData, oldMetaData)
+	if len(metaData.InstalledPackages) == 0 {
+		metaData.InstalledPackages = oldMetaData.InstalledPackages
+	}
 	WriteMetaData(cfg, metaData)
 	success := exitCode == ExitCodeSuccess
 	if cfg.UseGit == config.UseGitYes || cfg.UseGit == config.UseGitAuto {
diff --git a/cache-config/t3c-apply/torequest/torequest.go b/cache-config/t3c-apply/torequest/torequest.go
index da779a3ab7..1b6016abf1 100644
--- a/cache-config/t3c-apply/torequest/torequest.go
+++ b/cache-config/t3c-apply/torequest/torequest.go
@@ -63,7 +63,7 @@ type Package struct {
 
 type TrafficOpsReq struct {
 	Cfg     config.Cfg
-	pkgs    map[string]bool // map of packages which are installed, either already installed or newly installed by this run.
+	Pkgs    map[string]bool // map of packages which are installed, either already installed or newly installed by this run.
 	plugins map[string]bool // map of verified plugins
 
 	installedPkgs map[string]struct{} // map of packages which were installed by us.
@@ -192,7 +192,7 @@ func (r *TrafficOpsReq) DumpConfigFiles() {
 func NewTrafficOpsReq(cfg config.Cfg) *TrafficOpsReq {
 	return &TrafficOpsReq{
 		Cfg:           cfg,
-		pkgs:          map[string]bool{},
+		Pkgs:          map[string]bool{},
 		plugins:       map[string]bool{},
 		configFiles:   map[string]*ConfigFile{},
 		installedPkgs: map[string]struct{}{},
@@ -585,7 +585,7 @@ func (r *TrafficOpsReq) CheckSystemServices() error {
 // IsPackageInstalled returns true/false if the named rpm package is installed.
 // the prefix before the version is matched.
 func (r *TrafficOpsReq) IsPackageInstalled(name string) bool {
-	for k, v := range r.pkgs {
+	for k, v := range r.Pkgs {
 		if strings.HasPrefix(k, name) {
 			return v
 		}
@@ -595,17 +595,17 @@ func (r *TrafficOpsReq) IsPackageInstalled(name string) bool {
 	pkgArr, err := util.PackageInfo("pkg-query", name)
 	if err != nil {
 		log.Errorf(`IsPackageInstalled PackageInfo(pkg-query, %v) failed, caching as not installed and returning false! Error: %v\n`, name, err.Error())
-		r.pkgs[name] = false
+		r.Pkgs[name] = false
 		return false
 	}
 	if len(pkgArr) > 0 {
 		pkgAndVersion := pkgArr[0]
 		log.Infof("IsPackageInstalled '%v' found in rpm, adding '%v' to cache", name, pkgAndVersion)
-		r.pkgs[pkgAndVersion] = true
+		r.Pkgs[pkgAndVersion] = true
 		return true
 	}
 	log.Infof("IsPackageInstalled '%v' not found in rpm, adding '%v'=false to cache", name, name)
-	r.pkgs[name] = false
+	r.Pkgs[name] = false
 	return false
 }
 
@@ -923,7 +923,7 @@ func (r *TrafficOpsReq) ProcessPackages() error {
 		if r.Cfg.InstallPackages {
 			if instpkg == fullPackage {
 				log.Infof("%s Currently installed and not marked for removal\n", reqpkg)
-				r.pkgs[fullPackage] = true
+				r.Pkgs[fullPackage] = true
 				continue
 			} else if instpkg != "" { // the installed package needs upgrading.
 				log.Infof("%s Currently installed and marked for removal\n", instpkg)
@@ -953,11 +953,11 @@ func (r *TrafficOpsReq) ProcessPackages() error {
 			// Only check if packages exist and complain if they are wrong.
 			if instpkg == fullPackage {
 				log.Infof("%s Currently installed.\n", reqpkg)
-				r.pkgs[fullPackage] = true
+				r.Pkgs[fullPackage] = true
 				continue
 			} else if instpkg != "" { // the installed package needs upgrading.
 				log.Errorf("%s Wrong version currently installed.\n", instpkg)
-				r.pkgs[instpkg] = true
+				r.Pkgs[instpkg] = true
 			} else {
 				// the required package needs installing.
 				log.Errorf("%s is Not installed.\n", fullPackage)
@@ -1013,7 +1013,7 @@ func (r *TrafficOpsReq) ProcessPackages() error {
 					if err != nil {
 						return errors.New("Unable to install " + pkg + " : " + err.Error())
 					} else if result == true {
-						r.pkgs[pkg] = true
+						r.Pkgs[pkg] = true
 						r.installedPkgs[pkg] = struct{}{}
 						log.Infof("Package %s was installed\n", pkg)
 					}
diff --git a/cache-config/t3c-apply/torequest/torequest_test.go b/cache-config/t3c-apply/torequest/torequest_test.go
index 1fe55e0449..437faef2fb 100644
--- a/cache-config/t3c-apply/torequest/torequest_test.go
+++ b/cache-config/t3c-apply/torequest/torequest_test.go
@@ -86,7 +86,7 @@ func TestUnencodeFilter(t *testing.T) {
 
 func TestIsPackageInstalled(t *testing.T) {
 	trops := NewTrafficOpsReq(testCfg)
-	trops.pkgs["trafficserver"] = true
+	trops.Pkgs["trafficserver"] = true
 
 	if trops.IsPackageInstalled("mouse") {
 		t.Errorf("isPackageInstalled() failed, expected 'false' got 'true'.")
@@ -96,7 +96,7 @@ func TestIsPackageInstalled(t *testing.T) {
 		t.Errorf("isPackageInstalled() failed, expected 'true' got 'false'.")
 	}
 
-	trops.pkgs["trafficserver"] = false
+	trops.Pkgs["trafficserver"] = false
 	if trops.IsPackageInstalled("trafficserver") {
 		t.Errorf("isPackageInstalled() failed, expected 'false' got 'true'.")
 	}
diff --git a/cache-config/t3cutil/t3cutil.go b/cache-config/t3cutil/t3cutil.go
index b59a57fc85..e48985f1e0 100644
--- a/cache-config/t3cutil/t3cutil.go
+++ b/cache-config/t3cutil/t3cutil.go
@@ -300,8 +300,8 @@ type ApplyMetaData struct {
 	// because of --no-unset-reval-flag or --report-only.
 	UnsetRevalFlag bool `json:"unset-reval-flag"`
 
-	// InstalledPackages is which yum packages were installed.
-	// Note this packages actually installed, not what would have been e.g.
+	// InstalledPackages is which yum packages are installed.
+	// Note this packages currently installed, not what would have been e.g.
 	// because of --install-packages=false or --report-only.
 	InstalledPackages []string `json:"installed-packages"`
 
@@ -364,6 +364,17 @@ func (md *ApplyMetaData) Format() ([]byte, error) {
 	return bts, nil
 }
 
+func PackagesToMetaData(pkg map[string]bool) []string {
+	pkgs := []string{}
+	for k, v := range pkg {
+		if v {
+			pkgs = append(pkgs, k)
+		}
+	}
+	sort.Strings(pkgs)
+	return pkgs
+}
+
 // CombineOwnedFilePaths combines the owned file paths of two metadata objects.
 //
 // This is primarily useful when a config run, such as revalidate, adds owned files, but not