From 55ddcc2215172ff70e17a36c4ffc636b2d2006a3 Mon Sep 17 00:00:00 2001 From: Robert Butts Date: Sat, 4 Dec 2021 11:49:52 -0700 Subject: [PATCH] Fix t3c version in build Fixes the t3c build to properly add the ATC version, changeset, and monotonic version to the t3c version commands, as well as to t3c-generate generated files. Also adds the version flag to apps that were missing it. --- CHANGELOG.md | 1 + cache-config/t3c-apply/README.md | 6 ++ cache-config/t3c-apply/config/config.go | 20 +++++-- cache-config/t3c-apply/t3c-apply.go | 10 +++- cache-config/t3c-check-refs/README.md | 6 ++ cache-config/t3c-check-refs/config/config.go | 19 ++++++- cache-config/t3c-check-refs/t3c-check-refs.go | 10 +++- cache-config/t3c-check-reload/README.md | 9 ++- .../t3c-check-reload/t3c-check-reload.go | 14 +++++ cache-config/t3c-check/README.md | 11 ++++ cache-config/t3c-check/t3c-check.go | 19 ++++++- cache-config/t3c-diff/README.md | 8 ++- cache-config/t3c-diff/t3c-diff.go | 14 +++++ cache-config/t3c-generate/cfgfile/all.go | 3 +- .../t3c-generate/cfgfile/cfgfile_test.go | 4 +- cache-config/t3c-generate/config/config.go | 14 +++-- cache-config/t3c-generate/t3c-generate.go | 12 +++- cache-config/t3c-preprocess/README.md | 14 +++++ cache-config/t3c-preprocess/t3c-preprocess.go | 56 ++++++++++++++++++- cache-config/t3c-request/README.md | 2 + cache-config/t3c-request/config/config.go | 18 +++--- cache-config/t3c-request/t3c-request.go | 12 +++- cache-config/t3c-update/README.md | 22 ++++---- cache-config/t3c-update/config/config.go | 39 +++++++++---- cache-config/t3c-update/t3c-update.go | 16 +++--- cache-config/t3c/README.md | 14 ++++- cache-config/t3c/t3c.go | 19 ++++++- cache-config/t3cutil/t3cutil.go | 17 ++++++ 28 files changed, 345 insertions(+), 64 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 846f16bb28..883735baeb 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -20,6 +20,7 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/). - cache config t3c-apply retrying when another t3c-apply is running. - IMS warnings to Content Invalidation requests in Traffic Portal and documentation. - [#6032](https://github.com/apache/trafficcontrol/issues/6032) Add t3c setting mode 0600 for secure files +- [#6405](https://github.com/apache/trafficcontrol/issues/6405) Added cache config version to all t3c apps and config file headers - Traffic Vault: Added additional flag to TV Riak (Deprecated) Util ### Fixed diff --git a/cache-config/t3c-apply/README.md b/cache-config/t3c-apply/README.md index bd0a42b012..deddfd64fd 100644 --- a/cache-config/t3c-apply/README.md +++ b/cache-config/t3c-apply/README.md @@ -42,6 +42,8 @@ t3c-apply [-2AbceFhIknopsSvW] [-a service-action] [-f \] [-g \< [\-\-help] +[\-\-version] + # DESCRIPTION The t3c-apply command is a transliteration of traffic_ops_ort.pl script to the go language. It is designed to replace the traffic_ops_ort.pl perl script and it is used to apply configuration from Traffic Control, stored in Traffic Ops, to the cache. @@ -94,6 +96,10 @@ Typical usage is to install t3c on the cache machine, and then run it periodical Whether to set the records.config via header to the ATS release from the RPM. Default true. +-E, -\-version + + Print version information and exit. + -f, -\-files=value [all | reval] Which files to generate. If reval, the Traffic diff --git a/cache-config/t3c-apply/config/config.go b/cache-config/t3c-apply/config/config.go index f283e9135f..f0fdc5e6ee 100644 --- a/cache-config/t3c-apply/config/config.go +++ b/cache-config/t3c-apply/config/config.go @@ -35,6 +35,8 @@ import ( "github.com/pborman/getopt/v2" ) +const AppName = "t3c-apply" + var TSHome string = "/opt/trafficserver" const DefaultTSConfigDir = "/opt/trafficserver/etc/trafficserver" @@ -112,8 +114,12 @@ type Cfg struct { IgnoreUpdateFlag bool NoUnsetUpdateFlag bool UpdateIPAllow bool + Version string + GitRevision string } +func (cfg Cfg) AppVersion() string { return t3cutil.VersionStr(AppName, cfg.Version, cfg.GitRevision) } + type UseGitFlag string const ( @@ -213,10 +219,11 @@ func GetTSPackageHome() string { return tsHome } -func GetCfg() (Cfg, error) { +func GetCfg(appVersion string, gitRevision string) (Cfg, error) { var err error toInfoLog := []string{} + version := getopt.BoolLong("version", 'E', "Print version information and exit.") cacheHostNamePtr := getopt.StringLong("cache-host-name", 'H', "", "Host name of the cache to generate config for. Must be the server host name in Traffic Ops, not a URL, and not the FQDN") retriesPtr := getopt.IntLong("num-retries", 'r', 3, "[number] retry connection to Traffic Ops URL [number] times, default is 3") reverseProxyDisablePtr := getopt.BoolLong("reverse-proxy-disable", 'p', "[false | true] bypass the reverse proxy even if one has been configured default is false") @@ -228,7 +235,7 @@ func GetCfg() (Cfg, error) { toPassPtr := getopt.StringLong("traffic-ops-password", 'P', "", "Traffic Ops password. Required. May also be set with the environment variable TO_PASS") tsHomePtr := getopt.StringLong("trafficserver-home", 'R', "", "Trafficserver Package directory. May also be set with the environment variable TS_HOME") dnsLocalBindPtr := getopt.BoolLong("dns-local-bind", 'b', "[true | false] whether to use the server's Service Addresses to set the ATS DNS local bind address") - helpPtr := getopt.BoolLong("help", 'h', "Print usage information and exit") + help := getopt.BoolLong("help", 'h', "Print usage information and exit") useGitStr := getopt.StringLong("git", 'g', "auto", "Create and use a git repo in the config directory. Options are yes, no, and auto. If yes, create and use. If auto, use if it exist. Default is auto.") noCachePtr := getopt.BoolLong("no-cache", 'n', "Whether to not use a cache and make conditional requests to Traffic Ops") syncdsUpdatesIPAllowPtr := getopt.BoolLong("syncds-updates-ipallow", 'S', "Whether syncds mode will update ipallow. This exists because ATS had a bug where reloading after changing ipallow would block everything. Default is false.") @@ -400,10 +407,13 @@ If any of the related flags are also set, they override the mode's default behav toUser := *toUserPtr toPass := *toPassPtr dnsLocalBind := *dnsLocalBindPtr - help := *helpPtr maxmindLocation := *maxmindLocationPtr - if help { + if *version { + cfg := &Cfg{Version: appVersion, GitRevision: gitRevision} + fmt.Println(cfg.AppVersion()) + os.Exit(0) + } else if *help { Usage() return Cfg{}, nil } @@ -515,6 +525,8 @@ If any of the related flags are also set, they override the mode's default behav InstallPackages: *installPackagesPtr, IgnoreUpdateFlag: *ignoreUpdateFlagPtr, NoUnsetUpdateFlag: *noUnsetUpdateFlagPtr, + Version: appVersion, + GitRevision: gitRevision, } if err = log.InitCfg(cfg); err != nil { diff --git a/cache-config/t3c-apply/t3c-apply.go b/cache-config/t3c-apply/t3c-apply.go index b6851453cf..2a26276500 100644 --- a/cache-config/t3c-apply/t3c-apply.go +++ b/cache-config/t3c-apply/t3c-apply.go @@ -31,6 +31,14 @@ import ( "github.com/apache/trafficcontrol/lib/go-log" ) +// Version is the application version. +// This is overwritten by the build with the current project version. +var Version = "0.4" + +// GitRevision is the git revision the application was built from. +// This is overwritten by the build with the current project version. +var GitRevision = "nogit" + // exit codes const ( Success = 0 @@ -66,7 +74,7 @@ const LockFileRetryTimeout = time.Minute func main() { var syncdsUpdate torequest.UpdateStatus var lock util.FileLock - cfg, err := config.GetCfg() + cfg, err := config.GetCfg(Version, GitRevision) if err != nil { fmt.Println(err) os.Exit(ConfigError) diff --git a/cache-config/t3c-check-refs/README.md b/cache-config/t3c-check-refs/README.md index a234ad09e9..10ec64ca86 100644 --- a/cache-config/t3c-check-refs/README.md +++ b/cache-config/t3c-check-refs/README.md @@ -43,6 +43,8 @@ t3c-check-refs [-c directory] [-d location] [-e location] [-f files] [-i locatio [\-\-help] +[\-\-version] + ## DESCRIPTION The t3c-check-refs app will read an ATS formatted plugin.config or remap.config @@ -90,6 +92,10 @@ supplied, t3c-check-refs reads its config file input from stdin. errors are logged. To log warnings, pass '-v'. To log info, pass '-vv'. To omit error logging, see '-s'. +-V, -\-version + + Print version information and exit. + ## EXIT CODES Returns 0 if no missing plugin DSO or config files are found. diff --git a/cache-config/t3c-check-refs/config/config.go b/cache-config/t3c-check-refs/config/config.go index 65fee66a68..9cbbc77404 100644 --- a/cache-config/t3c-check-refs/config/config.go +++ b/cache-config/t3c-check-refs/config/config.go @@ -21,13 +21,18 @@ package config import ( "errors" + "fmt" "os" "strings" + "github.com/apache/trafficcontrol/cache-config/t3cutil" "github.com/apache/trafficcontrol/lib/go-log" + "github.com/pborman/getopt/v2" ) +const AppName = "t3c-check-refs" + type Cfg struct { CommandArgs []string LogLocationDebug string @@ -37,6 +42,8 @@ type Cfg struct { TrafficServerConfigDir string TrafficServerPluginDir string FilesAdding map[string]struct{} + Version string + GitRevision string } var ( @@ -44,6 +51,8 @@ var ( defaultATSPluginDir = "/opt/trafficserver/libexec/trafficserver" ) +func (cfg Cfg) AppVersion() string { return t3cutil.VersionStr(AppName, cfg.Version, cfg.GitRevision) } + func (cfg Cfg) DebugLog() log.LogLocation { return log.LogLocation(cfg.LogLocationDebug) } func (cfg Cfg) ErrorLog() log.LogLocation { return log.LogLocation(cfg.LogLocationError) } func (cfg Cfg) InfoLog() log.LogLocation { return log.LogLocation(cfg.LogLocationInfo) } @@ -57,7 +66,8 @@ func Usage() { } // InitConfig() intializes the configuration variables and loggers. -func InitConfig() (Cfg, error) { +func InitConfig(appVersion string, gitRevision string) (Cfg, error) { + versionPtr := getopt.BoolLong("version", 'V', "Print version information and exit.") atsConfigDirPtr := getopt.StringLong("trafficserver-config-dir", 'c', defaultATSConfigDir, "directory where ATS config files are stored.") atsPluginDirPtr := getopt.StringLong("trafficserver-plugin-dir", 'p', defaultATSPluginDir, "directory where ATS plugins are stored.") filesAdding := getopt.StringLong("files-adding", 'f', "", "comma-delimited list of file names being added, to not fail to verify if they don't already exist.") @@ -69,6 +79,11 @@ func InitConfig() (Cfg, error) { if *helpPtr == true { Usage() + os.Exit(0) + } else if *versionPtr { + cfg := &Cfg{Version: appVersion, GitRevision: gitRevision} + fmt.Println(cfg.AppVersion()) + os.Exit(0) } logLocationError := log.LogLocationStderr @@ -109,6 +124,8 @@ func InitConfig() (Cfg, error) { TrafficServerConfigDir: *atsConfigDirPtr, TrafficServerPluginDir: *atsPluginDirPtr, FilesAdding: filesAddingSet, + Version: appVersion, + GitRevision: gitRevision, } if err := log.InitCfg(cfg); err != nil { diff --git a/cache-config/t3c-check-refs/t3c-check-refs.go b/cache-config/t3c-check-refs/t3c-check-refs.go index 0d29cc6d18..87fa44ab97 100644 --- a/cache-config/t3c-check-refs/t3c-check-refs.go +++ b/cache-config/t3c-check-refs/t3c-check-refs.go @@ -33,6 +33,14 @@ import ( "github.com/apache/trafficcontrol/lib/go-log" ) +// Version is the application version. +// This is overwritten by the build with the current project version. +var Version = "0.4" + +// GitRevision is the git revision the application was built from. +// This is overwritten by the build with the current project version. +var GitRevision = "nogit" + var ( cfg config.Cfg atsPlugins = make(map[string]int) @@ -241,7 +249,7 @@ func main() { pluginErrorCount := 0 var err error - cfg, err = config.InitConfig() + cfg, err = config.InitConfig(Version, GitRevision) if err != nil { fmt.Fprintf(os.Stderr, "ERROR: %s\n", err.Error()) os.Exit(1) diff --git a/cache-config/t3c-check-reload/README.md b/cache-config/t3c-check-reload/README.md index fcce5823c1..54077cfa50 100644 --- a/cache-config/t3c-check-reload/README.md +++ b/cache-config/t3c-check-reload/README.md @@ -43,6 +43,8 @@ t3c-check-reload [\-\-help] +[\-\-version] + # DESCRIPTION The t3c-check-reload app takes json input from stdin. @@ -62,11 +64,16 @@ Possible return values are: # JSON Format {"changed_files":"","installed_plugins":""} + # OPTIONS --h, --help + +-h, -\-help Print usage information and exit +-V, -\-version + + Print version information and exit. # AUTHORS diff --git a/cache-config/t3c-check-reload/t3c-check-reload.go b/cache-config/t3c-check-reload/t3c-check-reload.go index 7be2ec3c28..56ef971a48 100644 --- a/cache-config/t3c-check-reload/t3c-check-reload.go +++ b/cache-config/t3c-check-reload/t3c-check-reload.go @@ -30,16 +30,30 @@ import ( "github.com/pborman/getopt/v2" ) +const AppName = "t3c-check-reload" + +// Version is the application version. +// This is overwritten by the build with the current project version. +var Version = "0.4" + +// GitRevision is the git revision the application was built from. +// This is overwritten by the build with the current project version. +var GitRevision = "nogit" + func main() { // presumably calculated by by t3c-check-refs // TODO remove? The blueprint says t3c/ORT will no longer install packages + version := getopt.BoolLong("version", 'V', "Print version information and exit.") help := getopt.BoolLong("help", 'h', "Print usage information and exit") getopt.Parse() if *help { fmt.Println(usageStr()) os.Exit(0) + } else if *version { + fmt.Println(t3cutil.VersionStr(AppName, Version, GitRevision)) + os.Exit(0) } changedCfg := &ChangedCfg{} diff --git a/cache-config/t3c-check/README.md b/cache-config/t3c-check/README.md index 88d9bab6c2..7df958f043 100644 --- a/cache-config/t3c-check/README.md +++ b/cache-config/t3c-check/README.md @@ -42,6 +42,8 @@ t3c-check \ [\] [\-\-help] +[\-\-version] + # DESCRIPTION The t3c-check application has commands for checking things about new config files, such as @@ -61,6 +63,15 @@ t3c-check-refs Check if a config file's referenced plugins and files are valid +# OPTIONS +-h, -\-help + + Print usage information and exit + +-V, -\-version + + Print version information and exit. + # AUTHORS The t3c application is maintained by Apache Traffic Control project. For help, bug reports, contributing, or anything else, see: diff --git a/cache-config/t3c-check/t3c-check.go b/cache-config/t3c-check/t3c-check.go index 1cf0b940ea..12327c5ddd 100644 --- a/cache-config/t3c-check/t3c-check.go +++ b/cache-config/t3c-check/t3c-check.go @@ -20,14 +20,27 @@ package main */ import ( - "github.com/apache/trafficcontrol/lib/go-log" + "fmt" "os" "os/exec" "syscall" // TODO change to x/unix ? + "github.com/apache/trafficcontrol/cache-config/t3cutil" + "github.com/apache/trafficcontrol/lib/go-log" + "github.com/pborman/getopt/v2" ) +const AppName = "t3c-check" + +// Version is the application version. +// This is overwritten by the build with the current project version. +var Version = "0.4" + +// GitRevision is the git revision the application was built from. +// This is overwritten by the build with the current project version. +var GitRevision = "nogit" + var commands = map[string]struct{}{ "refs": {}, "reload": {}, @@ -42,11 +55,15 @@ const ExitCodeCommandLookupErr = 5 func main() { flagHelp := getopt.BoolLong("help", 'h', "Print usage information and exit") + flagVersion := getopt.BoolLong("version", 'V', "Print version information and exit.") getopt.Parse() log.Init(os.Stderr, os.Stderr, os.Stderr, os.Stderr, os.Stderr) if *flagHelp { log.Errorln(usageStr()) os.Exit(ExitCodeSuccess) + } else if *flagVersion { + fmt.Println(t3cutil.VersionStr(AppName, Version, GitRevision)) + os.Exit(ExitCodeSuccess) } if len(os.Args) < 2 { diff --git a/cache-config/t3c-diff/README.md b/cache-config/t3c-diff/README.md index 955d947670..00eec199cf 100644 --- a/cache-config/t3c-diff/README.md +++ b/cache-config/t3c-diff/README.md @@ -42,6 +42,8 @@ t3c-diff \ \ [\-\-help] +[\-\-version] + # DESCRIPTION The t3c-diff application compares configuration files with semantic context, omitting comments and other semantically irrelevant text. @@ -58,10 +60,14 @@ if the file being created or deleted is semantically empty. # OPTIONS --h, --help +-h, -\-help Print usage info and exit. +-V, -\-version + + Print version information and exit. + # AUTHORS The t3c application is maintained by Apache Traffic Control project. For help, bug reports, contributing, or anything else, see: diff --git a/cache-config/t3c-diff/t3c-diff.go b/cache-config/t3c-diff/t3c-diff.go index 5eae3f95e3..e6f6f5ae59 100644 --- a/cache-config/t3c-diff/t3c-diff.go +++ b/cache-config/t3c-diff/t3c-diff.go @@ -34,8 +34,19 @@ import ( "github.com/pborman/getopt/v2" ) +const AppName = "t3c-diff" + +// Version is the application version. +// This is overwritten by the build with the current project version. +var Version = "0.4" + +// GitRevision is the git revision the application was built from. +// This is overwritten by the build with the current project version. +var GitRevision = "nogit" + func main() { help := getopt.BoolLong("help", 'h', "Print usage info and exit") + version := getopt.BoolLong("version", 'V', "Print version information and exit") lineComment := getopt.StringLong("line_comment", 'l', "#", "Comment symbol") mode := getopt.IntLong("file-mode", 'm', 0644, "file mode default is 644") fa := getopt.StringLong("file-a", 'a', "", "first diff file") @@ -47,6 +58,9 @@ func main() { if *help { log.Errorln(usageStr) os.Exit(0) + } else if *version { + fmt.Println(t3cutil.VersionStr(AppName, Version, GitRevision)) + os.Exit(0) } if len(os.Args) < 3 { diff --git a/cache-config/t3c-generate/cfgfile/all.go b/cache-config/t3c-generate/cfgfile/all.go index c8a6f4658b..f0b54da71d 100644 --- a/cache-config/t3c-generate/cfgfile/all.go +++ b/cache-config/t3c-generate/cfgfile/all.go @@ -36,7 +36,6 @@ import ( // GetAllConfigs gets all config files for cfg.CacheHostName. func GetAllConfigs( toData *t3cutil.ConfigData, - appVersion string, cfg config.Cfg, ) ([]t3cutil.ATSConfigFile, error) { if toData.Server.HostName == nil { @@ -50,7 +49,7 @@ func GetAllConfigs( } genTime := time.Now() - hdrCommentTxt := makeHeaderComment(*toData.Server.HostName, appVersion, toData.TrafficOpsURL, toData.TrafficOpsAddresses, genTime) + hdrCommentTxt := makeHeaderComment(*toData.Server.HostName, cfg.AppVersion(), toData.TrafficOpsURL, toData.TrafficOpsAddresses, genTime) hasSSLMultiCertConfig := false configs := []t3cutil.ATSConfigFile{} diff --git a/cache-config/t3c-generate/cfgfile/cfgfile_test.go b/cache-config/t3c-generate/cfgfile/cfgfile_test.go index 71f5873d1f..286475322f 100644 --- a/cache-config/t3c-generate/cfgfile/cfgfile_test.go +++ b/cache-config/t3c-generate/cfgfile/cfgfile_test.go @@ -91,7 +91,7 @@ func TestGetAllConfigsWriteConfigsDeterministic(t *testing.T) { cfg.Dir = cfgPath cfg.RevalOnly = revalOnly - configs, err := GetAllConfigs(toData, "", cfg) + configs, err := GetAllConfigs(toData, cfg) if err != nil { t.Fatalf("error getting configs: " + err.Error()) } @@ -103,7 +103,7 @@ func TestGetAllConfigsWriteConfigsDeterministic(t *testing.T) { configStr = removeComments(configStr) for i := 0; i < 10; i++ { - configs2, err := GetAllConfigs(toData, "", cfg) + configs2, err := GetAllConfigs(toData, cfg) if err != nil { t.Fatalf("error getting configs2: " + err.Error()) } diff --git a/cache-config/t3c-generate/config/config.go b/cache-config/t3c-generate/config/config.go index 267292370a..8baba8d482 100644 --- a/cache-config/t3c-generate/config/config.go +++ b/cache-config/t3c-generate/config/config.go @@ -26,6 +26,7 @@ import ( "os" "strings" + "github.com/apache/trafficcontrol/cache-config/t3cutil" "github.com/apache/trafficcontrol/lib/go-atscfg" "github.com/apache/trafficcontrol/lib/go-log" "github.com/apache/trafficcontrol/lib/go-tc" @@ -34,8 +35,6 @@ import ( ) const AppName = "t3c-generate" -const Version = "0.3" -const AppVersion = AppName + "/" + Version const ExitCodeSuccess = 0 const ExitCodeErrGeneric = 1 @@ -59,6 +58,8 @@ type Cfg struct { ParentComments bool DefaultEnableH2 bool DefaultTLSVersions []atscfg.TLSVersion + Version string + GitRevision string } func (cfg Cfg) ErrorLog() log.LogLocation { return log.LogLocation(cfg.LogLocationErr) } @@ -67,8 +68,10 @@ func (cfg Cfg) InfoLog() log.LogLocation { return log.LogLocation(cfg.LogLoca func (cfg Cfg) DebugLog() log.LogLocation { return log.LogLocation(cfg.LogLocationDebug) } func (cfg Cfg) EventLog() log.LogLocation { return log.LogLocation(log.LogLocationNull) } // app doesn't use the event logger. +func (cfg Cfg) AppVersion() string { return t3cutil.VersionStr(AppName, cfg.Version, cfg.GitRevision) } + // GetCfg gets the application configuration, from arguments and environment variables. -func GetCfg() (Cfg, error) { +func GetCfg(appVersion string, gitRevision string) (Cfg, error) { version := getopt.BoolLong("version", 'V', "Print version information and exit.") listPlugins := getopt.BoolLong("list-plugins", 'l', "Print the list of plugins.") help := getopt.BoolLong("help", 'h', "Print usage information and exit") @@ -86,7 +89,8 @@ func GetCfg() (Cfg, error) { getopt.Parse() if *version { - fmt.Println(AppName + " v" + Version) + cfg := &Cfg{Version: appVersion, GitRevision: gitRevision} + fmt.Println(cfg.AppVersion()) os.Exit(0) } else if *help { getopt.PrintUsage(os.Stdout) @@ -145,6 +149,8 @@ func GetCfg() (Cfg, error) { ParentComments: !(*disableParentConfigComments), DefaultEnableH2: *defaultEnableH2, DefaultTLSVersions: defaultTLSVersions, + Version: appVersion, + GitRevision: gitRevision, } if err := log.InitCfg(cfg); err != nil { return Cfg{}, errors.New("Initializing loggers: " + err.Error() + "\n") diff --git a/cache-config/t3c-generate/t3c-generate.go b/cache-config/t3c-generate/t3c-generate.go index 7059a576f8..c993fe29d1 100644 --- a/cache-config/t3c-generate/t3c-generate.go +++ b/cache-config/t3c-generate/t3c-generate.go @@ -35,8 +35,16 @@ import ( "github.com/apache/trafficcontrol/lib/go-log" ) +// Version is the application version. +// This is overwritten by the build with the current project version. +var Version = "0.4" + +// GitRevision is the git revision the application was built from. +// This is overwritten by the build with the current project version. +var GitRevision = "nogit" + func main() { - cfg, err := config.GetCfg() + cfg, err := config.GetCfg(Version, GitRevision) if err != nil { fmt.Fprintf(os.Stderr, "Getting config: "+err.Error()+"\n") os.Exit(config.ExitCodeErrGeneric) @@ -80,7 +88,7 @@ func main() { // os.Exit(config.ExitCodeErrGeneric) // } - configs, err := cfgfile.GetAllConfigs(toData, config.AppVersion, cfg) + configs, err := cfgfile.GetAllConfigs(toData, cfg) if err != nil { log.Errorln("Getting config for'" + *toData.Server.HostName + "': " + err.Error()) os.Exit(config.ExitCodeErrGeneric) diff --git a/cache-config/t3c-preprocess/README.md b/cache-config/t3c-preprocess/README.md index 037095af3a..ae93e701cf 100644 --- a/cache-config/t3c-preprocess/README.md +++ b/cache-config/t3c-preprocess/README.md @@ -40,6 +40,10 @@ t3c-preprocess - Traffic Control Cache Configuration preprocessor t3c-preprocess +[\-\-help] + +[\-\-version] + # DESCRIPTION The 't3c-preprocess' app preprocesses generated config files, replacing directives with relevant data. @@ -64,6 +68,16 @@ The following directives will be replaced. These directives may be placed anywhe __RETURN__ is replaced with a newline character, and any whitespace before or after it is removed. +# OPTIONS + +-h, -\-help + + Print usage information and exit + +-V, -\-version + + Print version information and exit. + # AUTHORS The t3c application is maintained by Apache Traffic Control project. For help, bug reports, contributing, or anything else, see: diff --git a/cache-config/t3c-preprocess/t3c-preprocess.go b/cache-config/t3c-preprocess/t3c-preprocess.go index 2c28d6d7d5..6f53bd35b1 100644 --- a/cache-config/t3c-preprocess/t3c-preprocess.go +++ b/cache-config/t3c-preprocess/t3c-preprocess.go @@ -21,7 +21,8 @@ package main import ( "encoding/json" - + "fmt" + "io" "net" "os" "regexp" @@ -33,8 +34,20 @@ import ( "github.com/apache/trafficcontrol/cache-config/t3cutil" "github.com/apache/trafficcontrol/lib/go-atscfg" "github.com/apache/trafficcontrol/lib/go-log" + + "github.com/pborman/getopt/v2" ) +const AppName = "t3c-preprocess" + +// Version is the application version. +// This is overwritten by the build with the current project version. +var Version = "0.4" + +// GitRevision is the git revision the application was built from. +// This is overwritten by the build with the current project version. +var GitRevision = "nogit" + var returnRegex = regexp.MustCompile(`\s*__RETURN__\s*`) func PreprocessConfigFile(server *atscfg.Server, cfgFile string) string { @@ -87,6 +100,37 @@ func PreprocessConfigFile(server *atscfg.Server, cfgFile string) string { } func main() { + flagHelp := getopt.BoolLong("help", 'h', "Print usage information and exit") + flagVersion := getopt.BoolLong("version", 'V', "Print version information and exit.") + flagVerbose := getopt.CounterLong("verbose", 'v', `Log verbosity. Logging is output to stderr. By default, errors are logged. To log warnings, pass '-v'. To log info, pass '-vv'. To omit error logging, see '-s'`) + flagSilent := getopt.BoolLong("silent", 's', `Silent. Errors are not logged, and the 'verbose' flag is ignored. If a fatal error occurs, the return code will be non-zero but no text will be output to stderr`) + + getopt.Parse() + if *flagHelp { + fmt.Println(usageStr()) + os.Exit(0) + } else if *flagVersion { + fmt.Println(t3cutil.VersionStr(AppName, Version, GitRevision)) + os.Exit(0) + } + + logErr := io.WriteCloser(os.Stderr) + logWarn := io.WriteCloser(nil) + logInf := io.WriteCloser(nil) + logDebug := io.WriteCloser(nil) + if *flagSilent { + logErr = io.WriteCloser(nil) + } else { + if *flagVerbose >= 1 { + logWarn = os.Stderr + } + if *flagVerbose >= 2 { + logInf = os.Stderr + logDebug = os.Stderr + } + } + log.Init(nil, logErr, logWarn, logInf, logDebug) + // TODO read log location arguments dataFiles := &DataAndFiles{} if err := json.NewDecoder(os.Stdin).Decode(dataFiles); err != nil { @@ -114,3 +158,13 @@ type DataAndFiles struct { Data t3cutil.ConfigData `json:"data"` Files []t3cutil.ATSConfigFile `json:"files"` } + +func usageStr() string { + return `usage: t3c-preprocess [--help] [--version] + [] + +The 't3c-preprocess' app preprocesses generated config files, replacing directives with relevant data. + +The stdin must be the JSON '{"data": \, "files": \}' where \ is the output of 't3c-request --get-data=config' and \ is the output of 't3c-generate'. +` +} diff --git a/cache-config/t3c-request/README.md b/cache-config/t3c-request/README.md index 1d86e535cf..669ba2ac54 100644 --- a/cache-config/t3c-request/README.md +++ b/cache-config/t3c-request/README.md @@ -42,6 +42,8 @@ t3c-request [-hIprv] [-D \ [\] +t3c \ [\] + +[\-\-help] + +[\-\-version] # DESCRIPTION @@ -51,9 +54,14 @@ The latest version and documentation can be found at https://github.com/apache/t # OPTIONS -\-\-help +-h, -\-help + Prints the synopsis and usage information. +-V, -\-version + + Print the version and exit + # COMMANDS We divide t3c into commands for each independent operation. Each command is its own application and can be called directly or via the t3c app. For example, 't3c apply' or 't3c-apply'. diff --git a/cache-config/t3c/t3c.go b/cache-config/t3c/t3c.go index e59bbd3d78..efe56d9861 100644 --- a/cache-config/t3c/t3c.go +++ b/cache-config/t3c/t3c.go @@ -20,14 +20,27 @@ package main */ import ( - "github.com/apache/trafficcontrol/lib/go-log" + "fmt" "os" "os/exec" "syscall" // TODO change to x/unix ? + "github.com/apache/trafficcontrol/cache-config/t3cutil" + "github.com/apache/trafficcontrol/lib/go-log" + "github.com/pborman/getopt/v2" ) +const AppName = "t3c-check" + +// Version is the application version. +// This is overwritten by the build with the current project version. +var Version = "0.4" + +// GitRevision is the git revision the application was built from. +// This is overwritten by the build with the current project version. +var GitRevision = "nogit" + var commands = map[string]struct{}{ "apply": struct{}{}, "check": struct{}{}, @@ -47,11 +60,15 @@ const ExitCodeCommandLookupErr = 5 func main() { flagHelp := getopt.BoolLong("help", 'h', "Print usage information and exit") + flagVersion := getopt.BoolLong("version", 'V', "Print version information and exit.") getopt.Parse() log.Init(os.Stderr, os.Stderr, os.Stderr, os.Stderr, os.Stderr) if *flagHelp { log.Errorln(usageStr()) os.Exit(ExitCodeSuccess) + } else if *flagVersion { + fmt.Println(t3cutil.VersionStr(AppName, Version, GitRevision)) + os.Exit(ExitCodeSuccess) } if len(os.Args) < 2 { diff --git a/cache-config/t3cutil/t3cutil.go b/cache-config/t3cutil/t3cutil.go index 59dea1f5e9..a1f7e21fd2 100644 --- a/cache-config/t3cutil/t3cutil.go +++ b/cache-config/t3cutil/t3cutil.go @@ -196,3 +196,20 @@ func ValidateURL(u *url.URL) error { } return nil } + +// VersionStr returns a common version string format for all t3c apps. +// The appName is the command itself, e.g. t3c-apply. +// The versionNum is the version number from the build system. It should include the major, minor, git revision, and a monotonically increasing number, e.g. '4.2.1234.abc123'. +func VersionStr(appName string, versionNum string, gitRevision string) string { + if len(gitRevision) > 8 { + gitRevision = gitRevision[:8] + } + return appName + " " + versionNum + ".." + gitRevision +} + +func UserAgentStr(appName string, versionNum string, gitRevision string) string { + if len(gitRevision) > 8 { + gitRevision = gitRevision[:8] + } + return appName + "/" + versionNum + ".." + gitRevision +}