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

rob 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 d9ef704f3f t3c to look for t3c-generate and other t3c- apps in 
executable directory (#6962)
d9ef704f3f is described below

commit d9ef704f3f7bbb779092c3911f62b3f49e51925a
Author: Brian Olsen <[email protected]>
AuthorDate: Thu Jul 28 08:58:36 2022 -0600

    t3c to look for t3c-generate and other t3c- apps in executable directory 
(#6962)
    
    * t3c to look for t3c-generate and other t3c- apps in executable directory
    
    * convert to calling all programs through t3c
    
    * remove kardianos/osext vendor module
    
    * fix typo in program
    
    * fix logging for t3c request
    
    * remove unnecessary t3cpath formulation
    
    * t3c-apply: revert to using t3c-<> instead of t3c <>
    
    * move request back to t3c request
    
    * restore t3c-check args
    
    * restore using t3c wrapper
---
 CHANGELOG.md                            |   1 +
 cache-config/t3c-apply/config/config.go |   1 -
 cache-config/t3c-apply/torequest/cmd.go | 104 +++++++++++++++++++-------------
 cache-config/t3c-check/t3c-check.go     |   6 +-
 cache-config/t3c/t3c.go                 |   5 +-
 cache-config/t3cutil/t3cutil.go         |  22 +++++++
 6 files changed, 90 insertions(+), 49 deletions(-)

diff --git a/CHANGELOG.md b/CHANGELOG.md
index 253641f88f..9ac611ea77 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -30,6 +30,7 @@ The format is based on [Keep a 
Changelog](http://keepachangelog.com/en/1.0.0/).
 - Added layered profile feature to 4.0 for `GET` 
/deliveryservices/{id}/servers/ and /deliveryservices/{id}/servers/eligible.
 - Change to t3c regex_revalidate so that STALE is no longer explicitly added 
for default revalidate rule for ATS version backwards compatibility.
 - Change to t3c diff to flag a config file for replacement if owner/group 
settings are not `ats` 
[#6879](https://github.com/apache/trafficcontrol/issues/6879).
+- t3c now looks in the executable dir path for t3c- utilities
 
 ### Fixed
 - Fixed TO to default route ID to 0, if it is not present in the request 
context.
diff --git a/cache-config/t3c-apply/config/config.go 
b/cache-config/t3c-apply/config/config.go
index cdcf3e1708..05aa96a0ed 100644
--- a/cache-config/t3c-apply/config/config.go
+++ b/cache-config/t3c-apply/config/config.go
@@ -43,7 +43,6 @@ const DefaultTSConfigDir = 
"/opt/trafficserver/etc/trafficserver"
 
 const (
        StatusDir          = "/var/lib/trafficcontrol-cache-config/status"
-       GenerateCmd        = "/usr/bin/t3c-generate" // TODO don't make 
absolute?
        Chkconfig          = "/sbin/chkconfig"
        Service            = "/sbin/service"
        SystemCtl          = "/bin/systemctl"
diff --git a/cache-config/t3c-apply/torequest/cmd.go 
b/cache-config/t3c-apply/torequest/cmd.go
index fcf347e634..cd8c6df7d0 100644
--- a/cache-config/t3c-apply/torequest/cmd.go
+++ b/cache-config/t3c-apply/torequest/cmd.go
@@ -32,6 +32,7 @@ import (
        golog "log"
        "os"
        "os/exec"
+       "path/filepath"
        "strconv"
        "strings"
        "time"
@@ -43,11 +44,23 @@ import (
        "github.com/apache/trafficcontrol/lib/go-tc"
 )
 
+const (
+       t3cgen       = `t3c generate`
+       t3cupd       = `t3c update`
+       t3cdiff      = `t3c diff`
+       t3cchkrefs   = `t3c check refs`
+       t3cchkreload = `t3c check reload`
+       t3creq       = `t3c request`
+       t3cpreproc   = `t3c preprocess`
+)
+
 type ServerAndConfigs struct {
        ConfigData  json.RawMessage
        ConfigFiles json.RawMessage
 }
 
+var t3cpath string = filepath.Join(t3cutil.InstallDir(), `t3c`)
+
 // generate runs t3c-generate and returns the result.
 func generate(cfg config.Cfg) ([]t3cutil.ATSConfigFile, error) {
        configData, err := requestConfig(cfg)
@@ -55,6 +68,7 @@ func generate(cfg config.Cfg) ([]t3cutil.ATSConfigFile, 
error) {
                return nil, errors.New("requesting: " + err.Error())
        }
        args := []string{
+               `generate`,
                "--dir=" + cfg.TsConfigDir,
        }
 
@@ -85,13 +99,13 @@ func generate(cfg config.Cfg) ([]t3cutil.ATSConfigFile, 
error) {
        args = append(args, 
"--disable-parent-config-comments="+strconv.FormatBool(cfg.DisableParentConfigComments))
        args = append(args, "--use-strategies="+cfg.UseStrategies.String())
 
-       generatedFiles, stdErr, code := t3cutil.DoInput(configData, 
config.GenerateCmd, args...)
+       generatedFiles, stdErr, code := t3cutil.DoInput(configData, t3cpath, 
args...)
        if code != 0 {
-               logSubAppErr(`t3c-generate stdout`, generatedFiles)
-               logSubAppErr(`t3c-generate stderr`, stdErr)
-               return nil, fmt.Errorf("t3c-generate returned non-zero exit 
code %v, see log for output", code)
+               logSubAppErr(t3cgen+` stdout`, generatedFiles)
+               logSubAppErr(t3cgen+` stderr`, stdErr)
+               return nil, fmt.Errorf("%s returned non-zero exit code %v, see 
log for output", t3cgen, code)
        }
-       logSubApp(`t3c-generate`, stdErr)
+       logSubApp(t3cgen, stdErr)
 
        preprocessedBytes, err := preprocess(cfg, configData, generatedFiles)
        if err != nil {
@@ -108,7 +122,7 @@ func generate(cfg config.Cfg) ([]t3cutil.ATSConfigFile, 
error) {
 
 // preprocess takes the to Data from 't3c-request --get-data=config' and the 
generated files from 't3c-generate', passes them to `t3c-preprocess`, and 
returns the result.
 func preprocess(cfg config.Cfg, configData []byte, generatedFiles []byte) 
([]byte, error) {
-       args := []string{}
+       args := []string{`preprocess`}
 
        if cfg.LogLocationErr == log.LogLocationNull {
                args = append(args, "-s")
@@ -120,7 +134,7 @@ func preprocess(cfg config.Cfg, configData []byte, 
generatedFiles []byte) ([]byt
                args = append(args, "-v")
        }
 
-       cmd := exec.Command(`t3c-preprocess`, args...)
+       cmd := exec.Command(t3cpath, args...)
        outbuf := bytes.Buffer{}
        errbuf := bytes.Buffer{}
        cmd.Stdout = &outbuf
@@ -161,11 +175,11 @@ func preprocess(cfg config.Cfg, configData []byte, 
generatedFiles []byte) ([]byt
        stdOut := outbuf.Bytes()
        stdErr := errbuf.Bytes()
        if code != 0 {
-               logSubAppErr(`t3c-preprocess stdout`, stdOut)
-               logSubAppErr(`t3c-preprocess stderr`, stdErr)
-               return nil, fmt.Errorf("t3c-preprocess returned non-zero exit 
code %v, see log for output", code)
+               logSubAppErr(t3cpreproc+` stdout`, stdOut)
+               logSubAppErr(t3cpreproc+` stderr`, stdErr)
+               return nil, fmt.Errorf("%s returned non-zero exit code %v, see 
log for output", t3cpreproc, code)
        }
-       logSubApp(`t3c-preprocess`, stdErr)
+       logSubApp(t3cpreproc, stdErr)
        return stdOut, nil
 }
 
@@ -219,6 +233,7 @@ func getPackages(cfg config.Cfg) ([]Package, error) {
 // Note the statuses are the value to be set, not whether to set the value.
 func sendUpdate(cfg config.Cfg, configApplyTime, revalApplyTime *time.Time, 
configApplyBool, revalApplyBool *bool) error {
        args := []string{
+               `update`,
                "--traffic-ops-timeout-milliseconds=" + 
strconv.FormatInt(int64(cfg.TOTimeoutMS), 10),
                "--traffic-ops-user=" + cfg.TOUser,
                "--traffic-ops-password=" + cfg.TOPass,
@@ -263,14 +278,14 @@ func sendUpdate(cfg config.Cfg, configApplyTime, 
revalApplyTime *time.Time, conf
        if _, used := os.LookupEnv("TO_URL"); !used {
                args = append(args, "--traffic-ops-url="+cfg.TOURL)
        }
-       stdOut, stdErr, code := t3cutil.Do(`t3c-update`, args...)
+       stdOut, stdErr, code := t3cutil.Do(t3cpath, args...)
        if code != 0 {
-               logSubAppErr(`t3c-update stdout`, stdOut)
-               logSubAppErr(`t3c-update stderr`, stdErr)
-               return fmt.Errorf("t3c-update returned non-zero exit code %v, 
see log for output", code)
+               logSubAppErr(t3cupd+` stdout`, stdOut)
+               logSubAppErr(t3cupd+` stderr`, stdErr)
+               return fmt.Errorf("%s returned non-zero exit code %v, see log 
for output", t3cupd, code)
        }
-       logSubApp(`t3c-update`, stdErr)
-       log.Infoln("t3c-update succeeded")
+       logSubApp(t3cupd, stdErr)
+       log.Infoln(t3cupd + " succeeded")
        return nil
 }
 
@@ -280,6 +295,7 @@ func sendUpdate(cfg config.Cfg, configApplyTime, 
revalApplyTime *time.Time, conf
 func diff(cfg config.Cfg, newFile []byte, fileLocation string, reportOnly 
bool, perm os.FileMode, uid int, gid int) (bool, error) {
        diffMsg := ""
        args := []string{
+               `diff`,
                "--file-a=stdin",
                "--file-b=" + fileLocation,
                "--file-mode=" + fmt.Sprintf("%#o", perm),
@@ -287,11 +303,11 @@ func diff(cfg config.Cfg, newFile []byte, fileLocation 
string, reportOnly bool,
                "--file-gid=" + fmt.Sprint(gid),
        }
 
-       stdOut, stdErr, code := t3cutil.DoInput(newFile, `t3c-diff`, args...)
+       stdOut, stdErr, code := t3cutil.DoInput(newFile, t3cpath, args...)
        if code > 1 {
-               return false, fmt.Errorf("t3c-diff returned error code %v 
stdout '%v' stderr '%v'", code, string(stdOut), string(stdErr))
+               return false, fmt.Errorf("%s returned error code %v stdout '%v' 
stderr '%v'", t3cdiff, code, string(stdOut), string(stdErr))
        }
-       logSubApp(`t3c-diff`, stdErr)
+       logSubApp(t3cdiff, stdErr)
 
        if code == 0 {
                diffMsg += fmt.Sprintf("All lines and file permissions match 
TrOps for config file: %s\n", fileLocation)
@@ -343,15 +359,15 @@ func checkRefs(cfg config.Cfg, cfgFile []byte, 
filesAdding []string) error {
                args = append(args, "-v")
        }
 
-       stdOut, stdErr, code := t3cutil.DoInput(cfgFile, `t3c`, args...)
+       stdOut, stdErr, code := t3cutil.DoInput(cfgFile, t3cpath, args...)
 
        if code != 0 {
-               logSubAppErr(`t3c-check-refs stdout`, stdOut)
-               logSubAppErr(`t3c-check-refs stderr`, stdErr)
+               logSubAppErr(t3cchkrefs+` stdout`, stdOut)
+               logSubAppErr(t3cchkrefs+` stderr`, stdErr)
                return fmt.Errorf("%d plugins failed to verify. See log for 
details.", code)
        }
-       logSubApp(`t3c-check-refs stdout`, stdOut)
-       logSubApp(`t3c-check-refs stderr`, stdErr)
+       logSubApp(t3cchkrefs+` stdout`, stdOut)
+       logSubApp(t3cchkrefs+` stderr`, stdErr)
        return nil
 }
 
@@ -373,11 +389,12 @@ func checkCert(c []byte) error {
 
 // checkReload is a helper for the sub-command t3c-check-reload.
 func checkReload(changedConfigFiles []string) (t3cutil.ServiceNeeds, error) {
-       log.Infof("t3c-check-reload calling with changedConfigFiles '%v'\n", 
changedConfigFiles)
+       log.Infof("%s calling with changedConfigFiles '%v'\n", t3cchkreload, 
changedConfigFiles)
 
        changedFiles := []byte(strings.Join(changedConfigFiles, ","))
 
-       cmd := exec.Command(`t3c-check-reload`)
+       args := []string{`check`, `reload`}
+       cmd := exec.Command(t3cpath, args...)
        outBuf := bytes.Buffer{}
        errBuf := bytes.Buffer{}
        cmd.Stdout = &outBuf
@@ -415,16 +432,16 @@ func checkReload(changedConfigFiles []string) 
(t3cutil.ServiceNeeds, error) {
        stdErr := errBuf.Bytes()
 
        if code != 0 {
-               logSubAppErr(`t3c-check-reload stdout`, stdOut)
-               logSubAppErr(`t3c-check-reload stderr`, stdErr)
-               return t3cutil.ServiceNeedsInvalid, 
fmt.Errorf("t3c-check-reload returned error code %d - see log for details.", 
code)
+               logSubAppErr(t3cchkreload+` stdout`, stdOut)
+               logSubAppErr(t3cchkreload+` stderr`, stdErr)
+               return t3cutil.ServiceNeedsInvalid, fmt.Errorf("%s returned 
error code %d - see log for details.", t3cchkreload, code)
        }
 
-       logSubApp(`t3c-check-reload`, stdErr)
+       logSubApp(t3cchkreload, stdErr)
 
        needs := t3cutil.StrToServiceNeeds(strings.TrimSpace(string(stdOut)))
        if needs == t3cutil.ServiceNeedsInvalid {
-               return t3cutil.ServiceNeedsInvalid, 
errors.New("t3c-check-reload returned unknown string '" + string(stdOut) + "'")
+               return t3cutil.ServiceNeedsInvalid, errors.New(t3cchkreload + " 
returned unknown string '" + string(stdOut) + "'")
        }
        return needs, nil
 }
@@ -444,6 +461,7 @@ func requestJSON(cfg config.Cfg, command string, obj 
interface{}) error {
 // request calls t3c-request with the given command, and returns the stdout 
bytes.
 func request(cfg config.Cfg, command string) ([]byte, error) {
        args := []string{
+               `request`,
                "--traffic-ops-insecure=" + strconv.FormatBool(cfg.TOInsecure),
                "--traffic-ops-timeout-milliseconds=" + 
strconv.FormatInt(int64(cfg.TOTimeoutMS), 10),
                "--cache-host-name=" + cfg.CacheHostName,
@@ -469,14 +487,14 @@ func request(cfg config.Cfg, command string) ([]byte, 
error) {
        if _, used := os.LookupEnv("TO_URL"); !used {
                args = append(args, "--traffic-ops-url="+cfg.TOURL)
        }
-       stdOut, stdErr, code := t3cutil.Do(`t3c-request`, args...)
+       stdOut, stdErr, code := t3cutil.Do(t3cpath, args...)
        if code != 0 {
-               logSubAppErr(`t3c-request stdout`, stdOut)
-               logSubAppErr(`t3c-request stderr`, stdErr)
-               return nil, fmt.Errorf("t3c-request returned non-zero exit code 
%v, see log for output", code)
+               logSubAppErr(t3creq+` stdout`, stdOut)
+               logSubAppErr(t3creq+` stderr`, stdErr)
+               return nil, fmt.Errorf("%s returned non-zero exit code %v, see 
log for output", t3creq, code)
        }
 
-       logSubApp(`t3c-request`, stdErr)
+       logSubApp(t3creq, stdErr)
 
        return stdOut, nil
 }
@@ -532,16 +550,16 @@ func requestConfig(cfg config.Cfg) ([]byte, error) {
        stdErr := ([]byte)(nil)
        code := 0
        if len(cacheBts) > 0 {
-               stdOut, stdErr, code = t3cutil.DoInput(cacheBts, `t3c`, args...)
+               stdOut, stdErr, code = t3cutil.DoInput(cacheBts, t3cpath, 
args...)
        } else {
-               stdOut, stdErr, code = t3cutil.Do(`t3c`, args...)
+               stdOut, stdErr, code = t3cutil.Do(t3cpath, args...)
        }
        if code != 0 {
-               logSubAppErr(`t3c-request stdout`, stdOut)
-               logSubAppErr(`t3c-request stderr`, stdErr)
-               return nil, fmt.Errorf("t3c-request returned non-zero exit code 
%v, see log for details", code)
+               logSubAppErr(t3creq+` stdout`, stdOut)
+               logSubAppErr(t3creq+` stderr`, stdErr)
+               return nil, fmt.Errorf("t3c returned non-zero exit code %v, see 
log for details", code)
        }
-       logSubApp(`t3c-request`, stdErr)
+       logSubApp(t3creq, stdErr)
 
        if err := ioutil.WriteFile(t3cutil.ApplyCachePath, stdOut, 0600); err 
!= nil {
                log.Errorln("writing config data to cache failed: " + 
err.Error())
diff --git a/cache-config/t3c-check/t3c-check.go 
b/cache-config/t3c-check/t3c-check.go
index 12327c5ddd..47d69b880c 100644
--- a/cache-config/t3c-check/t3c-check.go
+++ b/cache-config/t3c-check/t3c-check.go
@@ -22,7 +22,7 @@ package main
 import (
        "fmt"
        "os"
-       "os/exec"
+       "path/filepath"
        "syscall" // TODO change to x/unix ?
 
        "github.com/apache/trafficcontrol/cache-config/t3cutil"
@@ -78,8 +78,8 @@ func main() {
        }
 
        app := "t3c-check-" + cmd
-
-       appPath, err := exec.LookPath(app)
+       appPath := filepath.Join(t3cutil.InstallDir(), app)
+       _, err := os.Stat(appPath)
        if err != nil {
                log.Errorf("error finding path to '%s': %s\n", app, err.Error())
                os.Exit(ExitCodeCommandLookupErr)
diff --git a/cache-config/t3c/t3c.go b/cache-config/t3c/t3c.go
index efe56d9861..1488b531bc 100644
--- a/cache-config/t3c/t3c.go
+++ b/cache-config/t3c/t3c.go
@@ -22,7 +22,7 @@ package main
 import (
        "fmt"
        "os"
-       "os/exec"
+       "path/filepath"
        "syscall" // TODO change to x/unix ?
 
        "github.com/apache/trafficcontrol/cache-config/t3cutil"
@@ -84,7 +84,8 @@ func main() {
 
        app := "t3c-" + cmd
 
-       appPath, err := exec.LookPath(app)
+       appPath := filepath.Join(t3cutil.InstallDir(), app)
+       _, err := os.Stat(appPath)
        if err != nil {
                log.Errorf("error finding path to '%s': %s\n", app, err.Error())
                os.Exit(ExitCodeCommandLookupErr)
diff --git a/cache-config/t3cutil/t3cutil.go b/cache-config/t3cutil/t3cutil.go
index 57532d6e05..8073285d9d 100644
--- a/cache-config/t3cutil/t3cutil.go
+++ b/cache-config/t3cutil/t3cutil.go
@@ -29,11 +29,14 @@ import (
        "net/url"
        "os"
        "os/exec"
+       "path/filepath"
        "regexp"
        "sort"
        "strings"
        "syscall"
        "time"
+
+       "github.com/apache/trafficcontrol/lib/go-log"
 )
 
 type ATSConfigFile struct {
@@ -46,6 +49,25 @@ type ATSConfigFile struct {
        Warnings    []string `json:"warnings"`
 }
 
+var installdir string
+
+// initializes InstallDir to executable dir
+// If error, returns "/usr/bin" as default.
+func InstallDir() string {
+       if installdir == "" {
+               execpath, err := os.Executable()
+               if err != nil {
+                       installdir = `/usr/bin`
+                       log.Infof("InstallDir setting to fallback: '%s', %v\n", 
installdir, err)
+               } else {
+                       log.Infof("Executable path is %s", execpath)
+                       installdir = filepath.Dir(execpath)
+               }
+       }
+       log.Infof("Return Installdir '%s'", installdir)
+       return installdir
+}
+
 // ATSConfigFiles implements sort.Interface and sorts by the Location and then 
FileNameOnDisk, i.e. the full file path.
 type ATSConfigFiles []ATSConfigFile
 

Reply via email to