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

zrhoffman 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 ac165a9238 t3c will not clear update flag in error condition (#7696)
ac165a9238 is described below

commit ac165a9238bada703353228dd041a4e8b142f579
Author: Joe Pappano <[email protected]>
AuthorDate: Wed Aug 9 18:03:17 2023 -0400

    t3c will not clear update flag in error condition (#7696)
    
    * added .lua to plugin and remap checks
    
    * removed deprecated io/ioutil and added cache config failure message
    
    * added support for config file audit failures and removed deprecated 
io/ioutil
    
    * added change log entry
---
 CHANGELOG.md                                  |  1 +
 cache-config/t3c-apply/t3c-apply.go           | 12 +++++---
 cache-config/t3c-apply/torequest/torequest.go | 42 ++++++++++++++++++---------
 cache-config/t3c-check-refs/t3c-check-refs.go |  8 ++---
 4 files changed, 42 insertions(+), 21 deletions(-)

diff --git a/CHANGELOG.md b/CHANGELOG.md
index cf9ab93930..8dd14d625a 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -71,6 +71,7 @@ The format is based on [Keep a 
Changelog](http://keepachangelog.com/en/1.0.0/).
 - [##7605](https://github.com/apache/trafficcontrol/pull/#7605) *Traffic Ops* 
Fixes `cachegroups_request_comments` v5 apis to respond with `RFC3339` 
date/time Format.
 - [#7621](https://github.com/apache/trafficcontrol/pull/7621) *Traffic Ops* 
Use ID token for OAuth authentication, not Access Token
 - [#7694](https://github.com/apache/trafficcontrol/pull/7694) *t3c*, *Traffic 
Control Health Client* Upgrade to ATS 9.2
+- [#7966](https://github.com/apache/trafficcontrol/pull/7696) *t3c* will no 
longer clear update flag when config failure occurs and will also give a cache 
config error msg on exit. 
 
 ### Fixed
 - [#4393](https://github.com/apache/trafficcontrol/issues/4393) *Traffic Ops* 
Fixed the error code and alert structure when TO is queried for a delivery 
service with no ssl keys.
diff --git a/cache-config/t3c-apply/t3c-apply.go 
b/cache-config/t3c-apply/t3c-apply.go
index 24e1e8e91a..32692822b6 100644
--- a/cache-config/t3c-apply/t3c-apply.go
+++ b/cache-config/t3c-apply/t3c-apply.go
@@ -22,7 +22,6 @@ package main
 import (
        "encoding/json"
        "errors"
-       "io/ioutil"
        "os"
        "path/filepath"
        "strings"
@@ -75,6 +74,7 @@ const LockFilePath = "/var/run/t3c.lock"
 const LockFileRetryInterval = time.Second
 const LockFileRetryTimeout = time.Minute
 
+const CacheConfigFailureExitMsg = `CACHE CONFIG FAILURE`
 const FailureExitMsg = `CRITICAL FAILURE, ABORTING`
 const PostConfigFailureExitMsg = `CRITICAL FAILURE AFTER SETTING CONFIG, 
ABORTING`
 const SuccessExitMsg = `SUCCESS`
@@ -346,7 +346,11 @@ func Main() int {
        }
 
        metaData.Succeeded = true
-       return GitCommitAndExit(ExitCodeSuccess, SuccessExitMsg, cfg, metaData, 
oldMetaData)
+       if syncdsUpdate == torequest.UpdateTropsFailed {
+               return GitCommitAndExit(ExitCodeSuccess, 
CacheConfigFailureExitMsg, cfg, metaData, oldMetaData)
+       } else {
+               return GitCommitAndExit(ExitCodeSuccess, SuccessExitMsg, cfg, 
metaData, oldMetaData)
+       }
 }
 
 func LogPanic(f func() int) (exitCode int) {
@@ -450,7 +454,7 @@ func WriteMetaData(cfg config.Cfg, metaData 
*t3cutil.ApplyMetaData) {
 
        metaDataFilePath := GetMetaDataFilePath(cfg)
 
-       if err := ioutil.WriteFile(metaDataFilePath, bts, MetaDataFileMode); 
err != nil {
+       if err := os.WriteFile(metaDataFilePath, bts, MetaDataFileMode); err != 
nil {
                log.Errorln("writing metadata file '" + metaDataFilePath + "': 
" + err.Error())
                return
        }
@@ -459,7 +463,7 @@ func WriteMetaData(cfg config.Cfg, metaData 
*t3cutil.ApplyMetaData) {
 func LoadMetaData(cfg config.Cfg) (*t3cutil.ApplyMetaData, error) {
        metaDataFilePath := GetMetaDataFilePath(cfg)
 
-       bts, err := ioutil.ReadFile(metaDataFilePath)
+       bts, err := os.ReadFile(metaDataFilePath)
        if err != nil {
                return nil, errors.New("reading metadata file '" + 
metaDataFilePath + "': " + err.Error())
        }
diff --git a/cache-config/t3c-apply/torequest/torequest.go 
b/cache-config/t3c-apply/torequest/torequest.go
index 5298091f6a..3d6d7e34c9 100644
--- a/cache-config/t3c-apply/torequest/torequest.go
+++ b/cache-config/t3c-apply/torequest/torequest.go
@@ -22,7 +22,6 @@ package torequest
 import (
        "errors"
        "fmt"
-       "io/ioutil"
        "os"
        "os/user"
        "path/filepath"
@@ -102,6 +101,7 @@ type ConfigFile struct {
        TropsBackup       string // location to backup the TrafficOps Version
        AuditComplete     bool   // audit is complete
        AuditFailed       bool   // audit failed
+       AuditError        string // Error generated when AuditFailed is true
        ChangeApplied     bool   // a change has been applied
        ChangeNeeded      bool   // change required
        PreReqFailed      bool   // failed plugin prerequiste check
@@ -208,22 +208,33 @@ func (r *TrafficOpsReq) checkConfigFile(cfg *ConfigFile, 
filesAdding []string) e
        }
 
        if cfg.Dir == "" {
+               cfg.AuditFailed = true
                return errors.New("No location information for " + cfg.Name)
        }
        // return if audit has already been done.
-       if cfg.AuditComplete == true {
+       if cfg.AuditComplete {
                return nil
        }
 
        if !util.MkDirWithOwner(cfg.Dir, r.Cfg.ReportOnly, &cfg.Uid, &cfg.Gid) {
+               cfg.AuditFailed = true
                return errors.New("Unable to create the directory '" + cfg.Dir 
+ " for " + "'" + cfg.Name + "'")
        }
 
        log.Debugf("======== Start processing config file: %s ========\n", 
cfg.Name)
 
+       if cfg.Name == "50-ats.rules" {
+               err := r.processUdevRules(cfg)
+               if err != nil {
+                       cfg.AuditFailed = true
+                       return errors.New("unable to process udev rules in '" + 
cfg.Name + "': " + err.Error())
+               }
+       }
+
        if cfg.Name == "remap.config" {
                err := r.processRemapOverrides(cfg)
                if err != nil {
+                       cfg.AuditFailed = true
                        return err
                }
        }
@@ -232,6 +243,7 @@ func (r *TrafficOpsReq) checkConfigFile(cfg *ConfigFile, 
filesAdding []string) e
        if cfg.Name == "remap.config" || cfg.Name == "plugin.config" {
                if err := checkRefs(r.Cfg, cfg.Body, filesAdding); err != nil {
                        r.configFileWarnings[cfg.Name] = 
append(r.configFileWarnings[cfg.Name], "failed to verify '"+cfg.Name+"': 
"+err.Error())
+                       cfg.AuditFailed = true
                        return errors.New("failed to verify '" + cfg.Name + "': 
" + err.Error())
                }
                log.Infoln("Successfully verified plugins used by '" + cfg.Name 
+ "'")
@@ -251,18 +263,12 @@ func (r *TrafficOpsReq) checkConfigFile(cfg *ConfigFile, 
filesAdding []string) e
        changeNeeded, err := diff(r.Cfg, cfg.Body, cfg.Path, r.Cfg.ReportOnly, 
cfg.Perm, cfg.Uid, cfg.Gid)
 
        if err != nil {
+               cfg.AuditFailed = true
                return errors.New("getting diff: " + err.Error())
        }
        cfg.ChangeNeeded = changeNeeded
        cfg.AuditComplete = true
 
-       if cfg.Name == "50-ats.rules" {
-               err := r.processUdevRules(cfg)
-               if err != nil {
-                       return errors.New("unable to process udev rules in '" + 
cfg.Name + "': " + err.Error())
-               }
-       }
-
        log.Infof("======== End processing config file: %s for service: %s 
========\n", cfg.Name, cfg.Service)
        return nil
 }
@@ -418,7 +424,7 @@ func (r *TrafficOpsReq) processUdevRules(cfg *ConfigFile) 
error {
                        }
                }
        }
-       fs, err := ioutil.ReadDir("/proc/fs/ext4")
+       fs, err := os.ReadDir("/proc/fs/ext4")
        if err != nil {
                log.Errorln("unable to read /proc/fs/ext4, cannot audit disks 
for filesystem usage.")
        } else {
@@ -811,11 +817,12 @@ func (r *TrafficOpsReq) CheckReloadRestart(data 
[]FileRestartData) RestartData {
 // ProcessConfigFiles processes all config files retrieved from Traffic Ops.
 func (r *TrafficOpsReq) ProcessConfigFiles(metaData *t3cutil.ApplyMetaData) 
(UpdateStatus, error) {
        var updateStatus UpdateStatus = UpdateTropsNotNeeded
+       var auditErrors []string
 
        log.Infoln(" ======== Start processing config files ========")
 
        filesAdding := []string{} // list of file names being added, needed for 
verification.
-       for fileName, _ := range r.configFiles {
+       for fileName := range r.configFiles {
                filesAdding = append(filesAdding, fileName)
        }
 
@@ -841,6 +848,7 @@ func (r *TrafficOpsReq) ProcessConfigFiles(metaData 
*t3cutil.ApplyMetaData) (Upd
                err := r.checkConfigFile(cfg, filesAdding)
                if err != nil {
                        log.Errorln(err)
+                       r.configFiles[cfg.Name].AuditError = err.Error()
                }
        }
 
@@ -857,11 +865,11 @@ func (r *TrafficOpsReq) ProcessConfigFiles(metaData 
*t3cutil.ApplyMetaData) (Upd
                        !cfg.AuditFailed {
 
                        changesRequired++
-                       if cfg.Name == "plugin.config" && 
r.configFiles["remap.config"].PreReqFailed == true {
+                       if cfg.Name == "plugin.config" && 
r.configFiles["remap.config"].PreReqFailed {
                                updateStatus = UpdateTropsFailed
                                log.Errorln("plugin.config changed however, 
prereqs failed for remap.config so I am skipping updates for plugin.config")
                                continue
-                       } else if cfg.Name == "remap.config" && 
r.configFiles["plugin.config"].PreReqFailed == true {
+                       } else if cfg.Name == "remap.config" && 
r.configFiles["plugin.config"].PreReqFailed {
                                updateStatus = UpdateTropsFailed
                                log.Errorln("remap.config changed however, 
prereqs failed for plugin.config so I am skipping updates for remap.config")
                                continue
@@ -876,9 +884,17 @@ func (r *TrafficOpsReq) ProcessConfigFiles(metaData 
*t3cutil.ApplyMetaData) (Upd
                                }
                                shouldRestartReload.ReloadRestart = 
append(shouldRestartReload.ReloadRestart, *reData)
                        }
+               } else if cfg.AuditFailed {
+                       auditErrors = append(auditErrors, cfg.AuditError)
+                       log.Warnf("audit failed for config file: %v Error: %s", 
cfg.Name, cfg.AuditError)
+                       updateStatus = UpdateTropsFailed
                }
        }
 
+       if updateStatus == UpdateTropsFailed {
+               return UpdateTropsFailed, errors.New(strings.Join(auditErrors, 
"\n"))
+       }
+
        r.RestartData = r.CheckReloadRestart(shouldRestartReload.ReloadRestart)
 
        if 0 < len(r.changedFiles) {
diff --git a/cache-config/t3c-check-refs/t3c-check-refs.go 
b/cache-config/t3c-check-refs/t3c-check-refs.go
index 92c11426ad..835719bca0 100644
--- a/cache-config/t3c-check-refs/t3c-check-refs.go
+++ b/cache-config/t3c-check-refs/t3c-check-refs.go
@@ -111,11 +111,11 @@ func checkConfigLine(line string, lineNumber int, 
filesAdding map[string]struct{
                                        }
                                }
                        } else if strings.HasPrefix(fields[ii], "@pparam") {
-                               // any plugin parameters that end in '.config | 
.cfg | .txt | yml | .yaml'
+                               // any plugin parameters that end in '.config | 
.cfg | .txt | yml | .yaml | .lua'
                                // are assumed to be configuration files and 
are checked that they
                                // exist in the filesystem at the absolute 
location in the name
                                // or relative to the ATS configuration files 
directory.
-                               m := 
regexp.MustCompile(`^*(\.config|\.cfg|\.txt|\.yml|\.yaml)+`)
+                               m := 
regexp.MustCompile(`^*(\.config|\.cfg|\.txt|\.yml|\.yaml|\.lua)+`)
                                sa := strings.Split(fields[ii], "=")
                                if len(sa) != 2 && len(sa) != 3 {
                                        log.Errorf("malformed @pparam 
definition in remap.config on line '%d': %v\n", lineNumber, fields)
@@ -159,11 +159,11 @@ func checkConfigLine(line string, lineNumber int, 
filesAdding map[string]struct{
                        }
                }
                // Check the arguments in a plugin.config file for possible 
plugin config files.
-               // Any plugin argument that ends in '.config | .cfg | .txt | 
.yml | .yaml' are
+               // Any plugin argument that ends in '.config | .cfg | .txt | 
.yml | .yaml | .lua' are
                // assumed to be configuration files and are checked that they
                // exist in the filesystem at the absolute location in the name
                // or relative to the ATS configuration files directory.
-               m := 
regexp.MustCompile(`([^=]+\.config$|[^=]\.cfg$|[^=]+\.txt$|[^=]+\.yml$|[^=]+\.yaml$)`)
+               m := 
regexp.MustCompile(`([^=]+\.config$|[^=]\.cfg$|[^=]+\.txt$|[^=]+\.yml$|[^=]+\.yaml$|[^=]+\.lua$)`)
                for ii := 1; ii < length; ii++ {
                        param := strings.TrimSpace(fields[ii])
                        cfg := m.FindStringSubmatch(param)

Reply via email to