Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add parents to varnish cache #7669

Merged
merged 7 commits into from
Aug 14, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions cache-config/t3c-apply/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,7 @@
Version string
GitRevision string
LocalATSVersion string
CacheType string
}

func (cfg Cfg) AppVersion() string { return t3cutil.VersionStr(AppName, cfg.Version, cfg.GitRevision) }
Expand Down Expand Up @@ -194,23 +195,23 @@

// 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

Check warning on line 206 in cache-config/t3c-apply/config/config.go

View check run for this annotation

Codecov / codecov/patch

cache-config/t3c-apply/config/config.go#L198-L206

Added lines #L198 - L206 were not covered by tests
}
cmd := exec.Command("/usr/lib/rpm/rpmdb_verify", rpmDir+"/"+file.Name())
err := cmd.Run()
if err != nil || cmd.ProcessState.ExitCode() > 0 {
return false
}

Check warning on line 212 in cache-config/t3c-apply/config/config.go

View check run for this annotation

Codecov / codecov/patch

cache-config/t3c-apply/config/config.go#L208-L212

Added lines #L208 - L212 were not covered by tests
}
return true

Check warning on line 214 in cache-config/t3c-apply/config/config.go

View check run for this annotation

Codecov / codecov/patch

cache-config/t3c-apply/config/config.go#L214

Added line #L214 was not covered by tests
}

// derives the ATS Installation directory from
Expand Down Expand Up @@ -277,6 +278,7 @@
defaultClientTLSVersions := getopt.StringLong("default-client-tls-versions", 'V', "", "Comma-delimited list of default TLS versions for Delivery Services with no Parameter, e.g. --default-tls-versions='1.1,1.2,1.3'. If omitted, all versions are enabled.")
maxmindLocationPtr := getopt.StringLong("maxmind-location", 'M', "", "URL of a maxmind gzipped database file, to be installed into the trafficserver etc directory.")
verbosePtr := 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'`)
cache := getopt.StringLong("cache", 'T', "ats", "Cache server type. Generate configuration files for specific cache server type, e.g. 'ats', 'varnish'.")

Check warning on line 281 in cache-config/t3c-apply/config/config.go

View check run for this annotation

Codecov / codecov/patch

cache-config/t3c-apply/config/config.go#L281

Added line #L281 was not covered by tests
const silentFlagName = "silent"
silentPtr := getopt.BoolLong(silentFlagName, '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`)

Expand Down Expand Up @@ -347,11 +349,11 @@
// so we want to log what flags the mode set here, to aid debugging.
// But we can't do that until the loggers are initialized.
modeLogStrs := []string{}
fatalLogStrs := []string{}

Check warning on line 352 in cache-config/t3c-apply/config/config.go

View check run for this annotation

Codecov / codecov/patch

cache-config/t3c-apply/config/config.go#L352

Added line #L352 was not covered by tests
if getopt.IsSet(runModeFlagName) {
runMode := t3cutil.StrToMode(*runModePtr)
if runMode == t3cutil.ModeInvalid {
fatalLogStrs = append(fatalLogStrs, *runModePtr+" is an invalid mode.")

Check warning on line 356 in cache-config/t3c-apply/config/config.go

View check run for this annotation

Codecov / codecov/patch

cache-config/t3c-apply/config/config.go#L356

Added line #L356 was not covered by tests
}
modeLogStrs = append(modeLogStrs, "t3c-apply is running in "+runMode.String()+" mode")
switch runMode {
Expand Down Expand Up @@ -437,7 +439,7 @@
}

if *verbosePtr > 2 {
fatalLogStrs = append(fatalLogStrs, "Too many verbose options. The maximum log verbosity level is 2 (-vv or --verbose=2) for errors (0), warnings (1), and info (2)")

Check warning on line 442 in cache-config/t3c-apply/config/config.go

View check run for this annotation

Codecov / codecov/patch

cache-config/t3c-apply/config/config.go#L442

Added line #L442 was not covered by tests
}

var cacheHostName string
Expand All @@ -446,7 +448,7 @@
} else {
cacheHostName, err = os.Hostname()
if err != nil {
fatalLogStrs = append(fatalLogStrs, "Could not get the hostname from the O.S., please supply a hostname: "+err.Error())

Check warning on line 451 in cache-config/t3c-apply/config/config.go

View check run for this annotation

Codecov / codecov/patch

cache-config/t3c-apply/config/config.go#L451

Added line #L451 was not covered by tests
}
// strings.Split always returns a slice with at least 1 element, so we don't need a len check
cacheHostName = strings.Split(cacheHostName, ".")[0]
Expand All @@ -455,7 +457,7 @@
useGit := StrToUseGitFlag(*useGitStr)

if useGit == UseGitInvalid {
fatalLogStrs = append(fatalLogStrs, "Invalid git flag '"+*useGitStr+"'. Valid options are yes, no, auto.")

Check warning on line 460 in cache-config/t3c-apply/config/config.go

View check run for this annotation

Codecov / codecov/patch

cache-config/t3c-apply/config/config.go#L460

Added line #L460 was not covered by tests
}

retries := *retriesPtr
Expand Down Expand Up @@ -497,17 +499,17 @@
os.Setenv("TO_PASS", toPass)
}

rpmDBisOk := verifyRpmDB()

if *installPackagesPtr && !rpmDBisOk {
if t3cutil.StrToMode(*runModePtr) == t3cutil.ModeBadAss {
fatalLogStrs = append(fatalLogStrs, "RPM database check failed unable to install packages cannot continue in badass mode")
} else {
fatalLogStrs = append(fatalLogStrs, "RPM database check failed unable to install packages cannot continue")
}

Check warning on line 509 in cache-config/t3c-apply/config/config.go

View check run for this annotation

Codecov / codecov/patch

cache-config/t3c-apply/config/config.go#L502-L509

Added lines #L502 - L509 were not covered by tests
}

toInfoLog = append(toInfoLog, fmt.Sprintf("rpm database is ok: %t", rpmDBisOk))

Check warning on line 512 in cache-config/t3c-apply/config/config.go

View check run for this annotation

Codecov / codecov/patch

cache-config/t3c-apply/config/config.go#L512

Added line #L512 was not covered by tests
// set TSHome
var tsHome = ""
if *tsHomePtr != "" {
Expand All @@ -518,13 +520,13 @@
tsHome = os.Getenv("TS_HOME") // check for the environment variable.
if tsHome != "" {
toInfoLog = append(toInfoLog, fmt.Sprintf("set TSHome from TS_HOME environment variable '%s'\n", TSHome))
} else if rpmDBisOk { // check using the config file listing from the rpm package if rpmdb is ok.

Check warning on line 523 in cache-config/t3c-apply/config/config.go

View check run for this annotation

Codecov / codecov/patch

cache-config/t3c-apply/config/config.go#L523

Added line #L523 was not covered by tests
tsHome = GetTSPackageHome()
if tsHome != "" {
toInfoLog = append(toInfoLog, fmt.Sprintf("set TSHome from the RPM config file list '%s'\n", TSHome))
}
} else if tsHome == "" {
toInfoLog = append(toInfoLog, fmt.Sprintf("no override for TSHome was found, using the configured default: '%s'\n", TSHome))

Check warning on line 529 in cache-config/t3c-apply/config/config.go

View check run for this annotation

Codecov / codecov/patch

cache-config/t3c-apply/config/config.go#L528-L529

Added lines #L528 - L529 were not covered by tests
}
}

Expand All @@ -533,6 +535,9 @@
if tsHome != "" {
TSHome = tsHome
tsConfigDir = tsHome + "/etc/trafficserver"
if cache != nil && *cache == "varnish" {
tsConfigDir = tsHome + "/etc/varnish"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It looks like for varnish cache servers, this will cause it to look for configuration files under /etc/trafficserver/etc/varnish (by default I think tsHome is /opt/trafficserver so that winds up being /opt/trafficserver/etc/trafficserver/etc/varnish) - is that really how we want to structure that?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I believe the config dir will be either home + /etc/trafficserver, home + /etc/varnish or if --trafficserver-home flag is not used it will default to /opt/trafficerver/etc/trafficserver and won't go into the if block (which is not good) other than that it will be home + /etc/varnish for varnish case. For example specifying --trafficserver-home flag with /opt/cache will write the config to /opt/cache/etc/varnish and that is what is done in varnish entrypoint.

I think ultimately default home and config dir shouldn't be related to TS. However, this change will affect CIAB, tests, workflows and some other code depending on that. So, Maybe it would be better if it is done in a separate PR?

}

Check warning on line 540 in cache-config/t3c-apply/config/config.go

View check run for this annotation

Codecov / codecov/patch

cache-config/t3c-apply/config/config.go#L538-L540

Added lines #L538 - L540 were not covered by tests
toInfoLog = append(toInfoLog, fmt.Sprintf("TSHome: %s, TSConfigDir: %s\n", TSHome, tsConfigDir))
}

Expand All @@ -540,23 +545,23 @@
if *useLocalATSVersionPtr {
atsVersionStr, err = GetATSVersionStr(tsHome)
if err != nil {
fatalLogStrs = append(fatalLogStrs, "getting local ATS version: "+err.Error())

Check warning on line 548 in cache-config/t3c-apply/config/config.go

View check run for this annotation

Codecov / codecov/patch

cache-config/t3c-apply/config/config.go#L548

Added line #L548 was not covered by tests
}
}
toInfoLog = append(toInfoLog, fmt.Sprintf("ATSVersionStr: '%s'\n", atsVersionStr))

usageStr := "basic usage: t3c-apply --traffic-ops-url=myurl --traffic-ops-user=myuser --traffic-ops-password=mypass --cache-host-name=my-cache"
if strings.TrimSpace(toURL) == "" {
fatalLogStrs = append(fatalLogStrs, "Missing required argument --traffic-ops-url or TO_URL environment variable. "+usageStr)

Check warning on line 555 in cache-config/t3c-apply/config/config.go

View check run for this annotation

Codecov / codecov/patch

cache-config/t3c-apply/config/config.go#L555

Added line #L555 was not covered by tests
}
if strings.TrimSpace(toUser) == "" {
fatalLogStrs = append(fatalLogStrs, "Missing required argument --traffic-ops-user or TO_USER environment variable. "+usageStr)

Check warning on line 558 in cache-config/t3c-apply/config/config.go

View check run for this annotation

Codecov / codecov/patch

cache-config/t3c-apply/config/config.go#L558

Added line #L558 was not covered by tests
}
if strings.TrimSpace(toPass) == "" {
fatalLogStrs = append(fatalLogStrs, "Missing required argument --traffic-ops-password or TO_PASS environment variable. "+usageStr)

Check warning on line 561 in cache-config/t3c-apply/config/config.go

View check run for this annotation

Codecov / codecov/patch

cache-config/t3c-apply/config/config.go#L561

Added line #L561 was not covered by tests
}
if strings.TrimSpace(cacheHostName) == "" {
fatalLogStrs = append(fatalLogStrs, "Missing required argument --cache-host-name. "+usageStr)

Check warning on line 564 in cache-config/t3c-apply/config/config.go

View check run for this annotation

Codecov / codecov/patch

cache-config/t3c-apply/config/config.go#L564

Added line #L564 was not covered by tests
}

toURLParsed, err := url.Parse(toURL)
Expand All @@ -577,7 +582,7 @@
CacheHostName: cacheHostName,
SvcManagement: svcManagement,
Retries: retries,
RpmDBOk: rpmDBisOk,

Check warning on line 585 in cache-config/t3c-apply/config/config.go

View check run for this annotation

Codecov / codecov/patch

cache-config/t3c-apply/config/config.go#L585

Added line #L585 was not covered by tests
ReverseProxyDisable: reverseProxyDisable,
SkipOSCheck: skipOsCheck,
UseStrategies: useStrategies,
Expand Down Expand Up @@ -612,18 +617,19 @@
Version: appVersion,
GitRevision: gitRevision,
LocalATSVersion: atsVersionStr,
CacheType: *cache,

Check warning on line 620 in cache-config/t3c-apply/config/config.go

View check run for this annotation

Codecov / codecov/patch

cache-config/t3c-apply/config/config.go#L620

Added line #L620 was not covered by tests
}

if err = log.InitCfg(cfg); err != nil {
return Cfg{}, errors.New("Initializing loggers: " + err.Error() + "\n")
}

if len(fatalLogStrs) > 0 {
for _, str := range fatalLogStrs {
str = strings.TrimSpace(str)
log.Errorln(str)
}
return Cfg{}, errors.New("fatal error has occurred")

Check warning on line 632 in cache-config/t3c-apply/config/config.go

View check run for this annotation

Codecov / codecov/patch

cache-config/t3c-apply/config/config.go#L627-L632

Added lines #L627 - L632 were not covered by tests
}
for _, str := range modeLogStrs {
str = strings.TrimSpace(str)
Expand Down
8 changes: 4 additions & 4 deletions cache-config/t3c-apply/t3c-apply.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,8 +93,8 @@
var lock util.FileLock
cfg, err := config.GetCfg(Version, GitRevision)
if err != nil {
log.Infoln(err)
log.Errorln(FailureExitMsg)

Check warning on line 97 in cache-config/t3c-apply/t3c-apply.go

View check run for this annotation

Codecov / codecov/patch

cache-config/t3c-apply/t3c-apply.go#L96-L97

Added lines #L96 - L97 were not covered by tests
return ExitCodeConfigError
} else if cfg == (config.Cfg{}) { // user used the --help option
return ExitCodeSuccess
Expand Down Expand Up @@ -220,7 +220,7 @@
}

} else {
syncdsUpdate, err = trops.CheckSyncDSState(metaData)
syncdsUpdate, err = trops.CheckSyncDSState(metaData, cfg)

Check warning on line 223 in cache-config/t3c-apply/t3c-apply.go

View check run for this annotation

Codecov / codecov/patch

cache-config/t3c-apply/t3c-apply.go#L223

Added line #L223 was not covered by tests
if err != nil {
log.Errorln("Checking syncds state: " + err.Error())
return GitCommitAndExit(ExitCodeSyncDSError, FailureExitMsg, cfg, metaData, oldMetaData)
Expand All @@ -241,7 +241,7 @@
} else if rc == 0 {
log.Infoln("updated the remap.config for reloading.")
}
if err := trops.StartServices(&syncdsUpdate, metaData); err != nil {
if err := trops.StartServices(&syncdsUpdate, metaData, cfg); err != nil {

Check warning on line 244 in cache-config/t3c-apply/t3c-apply.go

View check run for this annotation

Codecov / codecov/patch

cache-config/t3c-apply/t3c-apply.go#L244

Added line #L244 was not covered by tests
log.Errorln("failed to start services: " + err.Error())
metaData.PartialSuccess = true
return GitCommitAndExit(ExitCodeServicesError, PostConfigFailureExitMsg, cfg, metaData, oldMetaData)
Expand All @@ -260,7 +260,7 @@
// 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 if cfg.RpmDBOk {

Check warning on line 263 in cache-config/t3c-apply/t3c-apply.go

View check run for this annotation

Codecov / codecov/patch

cache-config/t3c-apply/t3c-apply.go#L263

Added line #L263 was not covered by tests
log.Infoln("======== Start processing packages ========")
err = trops.ProcessPackages()
if err != nil {
Expand All @@ -275,9 +275,9 @@
log.Errorf("Error verifying system services: %s\n", err.Error())
return GitCommitAndExit(ExitCodeServicesError, FailureExitMsg, cfg, metaData, oldMetaData)
}
} else {
log.Warnln("======== RPM DB checks failed, package processing not possible, using installed packages from metadata if available========")
trops.ProcessPackagesWithMetaData(oldMetaData.InstalledPackages)

Check warning on line 280 in cache-config/t3c-apply/t3c-apply.go

View check run for this annotation

Codecov / codecov/patch

cache-config/t3c-apply/t3c-apply.go#L278-L280

Added lines #L278 - L280 were not covered by tests
}

log.Debugf("Preparing to fetch the config files for %s, files: %s, syncdsUpdate: %s\n", cfg.CacheHostName, cfg.Files, syncdsUpdate)
Expand Down Expand Up @@ -311,7 +311,7 @@
}
}

if err := trops.StartServices(&syncdsUpdate, metaData); err != nil {
if err := trops.StartServices(&syncdsUpdate, metaData, cfg); err != nil {

Check warning on line 314 in cache-config/t3c-apply/t3c-apply.go

View check run for this annotation

Codecov / codecov/patch

cache-config/t3c-apply/t3c-apply.go#L314

Added line #L314 was not covered by tests
log.Errorln("failed to start services: " + err.Error())
metaData.PartialSuccess = true
return GitCommitAndExit(ExitCodeServicesError, PostConfigFailureExitMsg, cfg, metaData, oldMetaData)
Expand Down Expand Up @@ -373,7 +373,7 @@
// 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 {
if len(metaData.InstalledPackages) == 0 && oldMetaData != nil {

Check warning on line 376 in cache-config/t3c-apply/t3c-apply.go

View check run for this annotation

Codecov / codecov/patch

cache-config/t3c-apply/t3c-apply.go#L376

Added line #L376 was not covered by tests
metaData.InstalledPackages = oldMetaData.InstalledPackages
}
WriteMetaData(cfg, metaData)
Expand Down
1 change: 1 addition & 0 deletions cache-config/t3c-apply/torequest/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@
args := []string{
`generate`,
"--dir=" + cfg.TsConfigDir,
"--cache=" + cfg.CacheType,

Check warning on line 75 in cache-config/t3c-apply/torequest/cmd.go

View check run for this annotation

Codecov / codecov/patch

cache-config/t3c-apply/torequest/cmd.go#L75

Added line #L75 was not covered by tests
}

if cfg.LogLocationErr == log.LogLocationNull {
Expand Down
30 changes: 20 additions & 10 deletions cache-config/t3c-apply/torequest/torequest.go
Original file line number Diff line number Diff line change
Expand Up @@ -742,7 +742,7 @@

// CheckSyncDSState retrieves and returns the DS Update status from Traffic Ops.
// The metaData is this run's metadata. It must not be nil, and this function may add to it.
func (r *TrafficOpsReq) CheckSyncDSState(metaData *t3cutil.ApplyMetaData) (UpdateStatus, error) {
func (r *TrafficOpsReq) CheckSyncDSState(metaData *t3cutil.ApplyMetaData, cfg config.Cfg) (UpdateStatus, error) {

Check warning on line 745 in cache-config/t3c-apply/torequest/torequest.go

View check run for this annotation

Codecov / codecov/patch

cache-config/t3c-apply/torequest/torequest.go#L745

Added line #L745 was not covered by tests
updateStatus := UpdateTropsNotNeeded
randDispSec := time.Duration(0)
log.Debugln("Checking syncds state.")
Expand Down Expand Up @@ -779,7 +779,7 @@
}
} else if !r.Cfg.IgnoreUpdateFlag {
log.Errorln("no queued update needs to be applied. Running revalidation before exiting.")
r.RevalidateWhileSleeping(metaData)
r.RevalidateWhileSleeping(metaData, cfg)

Check warning on line 782 in cache-config/t3c-apply/torequest/torequest.go

View check run for this annotation

Codecov / codecov/patch

cache-config/t3c-apply/torequest/torequest.go#L782

Added line #L782 was not covered by tests
return UpdateTropsNotNeeded, nil
} else {
log.Errorln("Traffic Ops is signaling that no update is waiting to be applied.")
Expand Down Expand Up @@ -1034,48 +1034,48 @@
return nil
}

func pkgMetaDataToMap(pmd []string) map[string]bool {
pkgMap := map[string]bool{}
for _, pkg := range pmd {
pkgMap[pkg] = true
}
return pkgMap

Check warning on line 1042 in cache-config/t3c-apply/torequest/torequest.go

View check run for this annotation

Codecov / codecov/patch

cache-config/t3c-apply/torequest/torequest.go#L1037-L1042

Added lines #L1037 - L1042 were not covered by tests
}

func pkgMatch(pkgMetaData []string, pk string) bool {
for _, pkg := range pkgMetaData {
if strings.Contains(pk, pkg) {
return true
}

Check warning on line 1049 in cache-config/t3c-apply/torequest/torequest.go

View check run for this annotation

Codecov / codecov/patch

cache-config/t3c-apply/torequest/torequest.go#L1045-L1049

Added lines #L1045 - L1049 were not covered by tests
}
return false

Check warning on line 1051 in cache-config/t3c-apply/torequest/torequest.go

View check run for this annotation

Codecov / codecov/patch

cache-config/t3c-apply/torequest/torequest.go#L1051

Added line #L1051 was not covered by tests

}

// ProcessPackagesWithMetaData will attempt to get installed package data from
// t3c-apply-metadata.json and log the results.
func (r *TrafficOpsReq) ProcessPackagesWithMetaData(packageMetaData []string) error {
pkgs, err := getPackages(r.Cfg)
pkgMdataMap := pkgMetaDataToMap(packageMetaData)
if err != nil {
return fmt.Errorf("getting packages: %w", err)
}
for _, pkg := range pkgs {
fullPackage := pkg.Name + "-" + pkg.Version
if pkgMdataMap[fullPackage] {
log.Infof("package %s is assumed to be installed according to metadata file", fullPackage)
r.Pkgs[fullPackage] = true
} else if pkgMatch(packageMetaData, pkg.Name) {
log.Infof("package %s is assumed to be installed according to metadata, but doesn't match traffic ops pkg", fullPackage)
r.Pkgs[fullPackage] = true
} else {
log.Infof("package %s does not appear to be installed.", pkg.Name+"-"+pkg.Version)
}

Check warning on line 1073 in cache-config/t3c-apply/torequest/torequest.go

View check run for this annotation

Codecov / codecov/patch

cache-config/t3c-apply/torequest/torequest.go#L1057-L1073

Added lines #L1057 - L1073 were not covered by tests
}
return nil

Check warning on line 1075 in cache-config/t3c-apply/torequest/torequest.go

View check run for this annotation

Codecov / codecov/patch

cache-config/t3c-apply/torequest/torequest.go#L1075

Added line #L1075 was not covered by tests
}

func (r *TrafficOpsReq) RevalidateWhileSleeping(metaData *t3cutil.ApplyMetaData) (UpdateStatus, error) {
func (r *TrafficOpsReq) RevalidateWhileSleeping(metaData *t3cutil.ApplyMetaData, cfg config.Cfg) (UpdateStatus, error) {

Check warning on line 1078 in cache-config/t3c-apply/torequest/torequest.go

View check run for this annotation

Codecov / codecov/patch

cache-config/t3c-apply/torequest/torequest.go#L1078

Added line #L1078 was not covered by tests
updateStatus, err := r.CheckRevalidateState(true)
if err != nil {
return updateStatus, err
Expand All @@ -1099,7 +1099,7 @@
t3cutil.WriteActionLog(t3cutil.ActionLogActionUpdateFilesReval, t3cutil.ActionLogStatusSuccess, metaData)
}

if err := r.StartServices(&updateStatus, metaData); err != nil {
if err := r.StartServices(&updateStatus, metaData, cfg); err != nil {

Check warning on line 1102 in cache-config/t3c-apply/torequest/torequest.go

View check run for this annotation

Codecov / codecov/patch

cache-config/t3c-apply/torequest/torequest.go#L1102

Added line #L1102 was not covered by tests
return updateStatus, errors.New("failed to start services: " + err.Error())
}

Expand All @@ -1116,7 +1116,7 @@
// StartServices reloads, restarts, or starts ATS as necessary,
// according to the changed config files and run mode.
// Returns nil on success or any error.
func (r *TrafficOpsReq) StartServices(syncdsUpdate *UpdateStatus, metaData *t3cutil.ApplyMetaData) error {
func (r *TrafficOpsReq) StartServices(syncdsUpdate *UpdateStatus, metaData *t3cutil.ApplyMetaData, cfg config.Cfg) error {

Check warning on line 1119 in cache-config/t3c-apply/torequest/torequest.go

View check run for this annotation

Codecov / codecov/patch

cache-config/t3c-apply/torequest/torequest.go#L1119

Added line #L1119 was not covered by tests
serviceNeeds := t3cutil.ServiceNeedsNothing
if r.Cfg.ServiceAction == t3cutil.ApplyServiceActionFlagRestart {
serviceNeeds = t3cutil.ServiceNeedsRestart
Expand All @@ -1138,13 +1138,17 @@
serviceNeeds = t3cutil.ServiceNeedsReload
}
}
packageName := "trafficserver"
if cfg.CacheType == "varnish" {
packageName = "varnish"
}

Check warning on line 1144 in cache-config/t3c-apply/torequest/torequest.go

View check run for this annotation

Codecov / codecov/patch

cache-config/t3c-apply/torequest/torequest.go#L1141-L1144

Added lines #L1141 - L1144 were not covered by tests

if (serviceNeeds == t3cutil.ServiceNeedsRestart || serviceNeeds == t3cutil.ServiceNeedsReload) && !r.IsPackageInstalled("trafficserver") {
if (serviceNeeds == t3cutil.ServiceNeedsRestart || serviceNeeds == t3cutil.ServiceNeedsReload) && !r.IsPackageInstalled(packageName) {

Check warning on line 1146 in cache-config/t3c-apply/torequest/torequest.go

View check run for this annotation

Codecov / codecov/patch

cache-config/t3c-apply/torequest/torequest.go#L1146

Added line #L1146 was not covered by tests
// TODO try to reload/restart anyway? To allow non-RPM installs?
return errors.New("trafficserver needs " + serviceNeeds.String() + " but is not installed.")
return errors.New(packageName + " needs " + serviceNeeds.String() + " but is not installed.")

Check warning on line 1148 in cache-config/t3c-apply/torequest/torequest.go

View check run for this annotation

Codecov / codecov/patch

cache-config/t3c-apply/torequest/torequest.go#L1148

Added line #L1148 was not covered by tests
}

svcStatus, _, err := util.GetServiceStatus("trafficserver")
svcStatus, _, err := util.GetServiceStatus(packageName)

Check warning on line 1151 in cache-config/t3c-apply/torequest/torequest.go

View check run for this annotation

Codecov / codecov/patch

cache-config/t3c-apply/torequest/torequest.go#L1151

Added line #L1151 was not covered by tests
if err != nil {
return errors.New("getting trafficserver service status: " + err.Error())
}
Expand All @@ -1161,7 +1165,7 @@
if svcStatus != util.SvcRunning {
startStr = "start"
}
if _, err := util.ServiceStart("trafficserver", startStr); err != nil {
if _, err := util.ServiceStart(packageName, startStr); err != nil {

Check warning on line 1168 in cache-config/t3c-apply/torequest/torequest.go

View check run for this annotation

Codecov / codecov/patch

cache-config/t3c-apply/torequest/torequest.go#L1168

Added line #L1168 was not covered by tests
t3cutil.WriteActionLog(t3cutil.ActionLogActionATSRestart, t3cutil.ActionLogStatusFailure, metaData)
return errors.New("failed to restart trafficserver")
}
Expand All @@ -1188,7 +1192,13 @@
log.Errorln("ATS configuration has changed. The new config will be picked up the next time ATS is started.")
} else if serviceNeeds == t3cutil.ServiceNeedsReload {
log.Infoln("ATS configuration has changed, Running 'traffic_ctl config reload' now.")
if _, _, err := util.ExecCommand(config.TSHome+config.TrafficCtl, "config", "reload"); err != nil {
reloadCommand := config.TSHome + config.TrafficCtl
reloadArgs := []string{"config", "reload"}
if cfg.CacheType == "varnish" {
reloadCommand = "varnishreload"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This seems to be relying on varnishreload being in the running user's $PATH - with ATS cache servers we typically install everything under /opt/trafficserver (not that I think that's a good idea, personally) but it seems like that won't work for varnish caches if everything winds up installed under e.g. /opt/varnish. Or, at least, not without some extra work. Is that intentional, or is there some reason why it wouldn't find varnishreload in the same directory as the varnish binary?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is done that way because Varnish currently is installed under root. So, varnishreload is currently installed in user's $PATH. It will be a problem indeed if Varnish is installed under different directory. Should Varnish be installed under different directory?

reloadArgs = []string{}
}
if _, _, err := util.ExecCommand(reloadCommand, reloadArgs...); err != nil {

Check warning on line 1201 in cache-config/t3c-apply/torequest/torequest.go

View check run for this annotation

Codecov / codecov/patch

cache-config/t3c-apply/torequest/torequest.go#L1195-L1201

Added lines #L1195 - L1201 were not covered by tests
t3cutil.WriteActionLog(t3cutil.ActionLogActionATSReload, t3cutil.ActionLogStatusFailure, metaData)

if *syncdsUpdate == UpdateTropsNeeded {
Expand Down
46 changes: 46 additions & 0 deletions cache-config/t3c-generate/cfgfile/varnish.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package cfgfile

/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/

import (
"github.com/apache/trafficcontrol/cache-config/t3c-generate/config"
"github.com/apache/trafficcontrol/cache-config/t3cutil"
"github.com/apache/trafficcontrol/lib/varnishcfg"
)

// GetVarnishConfigs returns varnish configuration files
// TODO: add varnishncsa and hitch configs
func GetVarnishConfigs(toData *t3cutil.ConfigData, cfg config.Cfg) ([]t3cutil.ATSConfigFile, error) {
vclBuilder := varnishcfg.NewVCLBuilder(toData)
vcl, warnings, err := vclBuilder.BuildVCLFile()
logWarnings("Generating varnish configuration files: ", warnings)

configs := make([]t3cutil.ATSConfigFile, 0)
// TODO: should be parameterized and generated from varnishcfg
configs = append(configs, t3cutil.ATSConfigFile{
Name: "default.vcl",
Text: vcl,
Path: cfg.Dir,
ContentType: "text/plain; charset=us-ascii",
LineComment: "//",
Secure: false,
})
return configs, err

Check warning on line 45 in cache-config/t3c-generate/cfgfile/varnish.go

View check run for this annotation

Codecov / codecov/patch

cache-config/t3c-generate/cfgfile/varnish.go#L30-L45

Added lines #L30 - L45 were not covered by tests
}
3 changes: 3 additions & 0 deletions cache-config/t3c-generate/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@
DefaultTLSVersions []atscfg.TLSVersion
Version string
GitRevision string
Cache string
}

func (cfg Cfg) ErrorLog() log.LogLocation { return log.LogLocation(cfg.LogLocationErr) }
Expand All @@ -88,6 +89,7 @@
atsVersion := getopt.StringLong("ats-version", 'a', "", "The ATS version, e.g. 9.1.2-42.abc123.el7.x86_64. If omitted, generation will attempt to get the ATS version from the Server Parameters, and fall back to lib/go-atscfg.DefaultATSVersion")
verbosePtr := 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'`)
silentPtr := 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`)
cache := getopt.StringLong("cache", 'C', "ats", "Cache server type. Generate configuration files for specific cache server type, e.g. 'ats', 'varnish'.")

Check warning on line 92 in cache-config/t3c-generate/config/config.go

View check run for this annotation

Codecov / codecov/patch

cache-config/t3c-generate/config/config.go#L92

Added line #L92 was not covered by tests

const useStrategiesFlagName = "use-strategies"
const defaultUseStrategies = t3cutil.UseStrategiesFlagFalse
Expand Down Expand Up @@ -185,6 +187,7 @@
GitRevision: gitRevision,
UseStrategies: t3cutil.UseStrategiesFlag(*useStrategiesPtr),
GoDirect: *goDirectPtr,
Cache: *cache,

Check warning on line 190 in cache-config/t3c-generate/config/config.go

View check run for this annotation

Codecov / codecov/patch

cache-config/t3c-generate/config/config.go#L190

Added line #L190 was not covered by tests
}
if err := log.InitCfg(cfg); err != nil {
return Cfg{}, errors.New("Initializing loggers: " + err.Error() + "\n")
Expand Down
14 changes: 14 additions & 0 deletions cache-config/t3c-generate/t3c-generate.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,20 @@
os.Exit(config.ExitCodeErrGeneric)
}

if cfg.Cache == "varnish" {
configs, err := cfgfile.GetVarnishConfigs(toData, cfg)
if err != nil {
log.Errorln("Generating varnish config for'" + *toData.Server.HostName + "': " + err.Error())
os.Exit(config.ExitCodeErrGeneric)
}
err = cfgfile.WriteConfigs(configs, os.Stdout)
if err != nil {
log.Errorln("Writing configs for '" + *toData.Server.HostName + "': " + err.Error())
os.Exit(config.ExitCodeErrGeneric)
}
os.Exit(config.ExitCodeSuccess)

Check warning on line 99 in cache-config/t3c-generate/t3c-generate.go

View check run for this annotation

Codecov / codecov/patch

cache-config/t3c-generate/t3c-generate.go#L88-L99

Added lines #L88 - L99 were not covered by tests
}

configs, err := cfgfile.GetAllConfigs(toData, cfg)
if err != nil {
log.Errorln("Getting config for'" + *toData.Server.HostName + "': " + err.Error())
Expand Down
3 changes: 3 additions & 0 deletions infrastructure/cdn-in-a-box/enroller/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,9 @@ COPY ./traffic_ops/toclientlib/ /go/src/github.com/apache/trafficcontrol/traffic
COPY ./traffic_ops/v4-client/ /go/src/github.com/apache/trafficcontrol/traffic_ops/v4-client/
COPY ./infrastructure/cdn-in-a-box/ /go/src/github.com/apache/trafficcontrol/infrastructure/cdn-in-a-box/

# varnishcfg requires t3c for ToData struct and not needed for enroller
RUN rm -rf /go/src/github.com/apache/trafficcontrol/lib/varnishcfg

Comment on lines +45 to +47
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wouldn't worry about it; the enroller is pulling in a lot of things it doesn't strictly need. We can evaluate it if it becomes a problem, and try to clean it up a bit, but varnishcfg is small compared to the rest of the cruft so there's no point worrying about it

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe the comment can be improved, but it is not removed for optimization reasons. if we remove this line it won't be able to build the enroller binary because varnishcfg requires t3c-util package for the ConfigData struct here. So, we will need to copy t3c packages too. t3c also requires some other packages not included with the enroller, so also they will need to be copied. The problem will be in keeping track of all these packages and what they require and any changes in the future. So I Just remove it instead of managing all that when it is not needed. But I believe it might be better to make varnishcfg not depend on t3c-util

WORKDIR /go/src/github.com/apache/trafficcontrol/infrastructure/cdn-in-a-box/enroller
RUN set -o errexit -o nounset; \
go clean; \
Expand Down
63 changes: 63 additions & 0 deletions infrastructure/cdn-in-a-box/varnish/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
# Licensed to the Apache Software Foundation (ASF) under one
# or more contributor license agreements. See the NOTICE file
# distributed with this work for additional information
# regarding copyright ownership. The ASF licenses this file
# to you under the Apache License, Version 2.0 (the
# "License"); you may not use this file except in compliance
# with the License. You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing,
# software distributed under the License is distributed on an
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
# KIND, either express or implied. See the License for the
# specific language governing permissions and limitations
# under the License.

ARG BASE_IMAGE=rockylinux \
RHEL_VERSION=8
FROM ${BASE_IMAGE}:${RHEL_VERSION} AS common-varnish-cache-config-layers
ARG RHEL_VERSION=8
# Makes RHEL_VERSION available at runtime
ENV RHEL_VERSION="$RHEL_VERSION"

RUN dnf module disable varnish -y && yum install -y epel-release

RUN curl -s https://packagecloud.io/install/repositories/varnishcache/varnish73/script.rpm.sh | bash

RUN yum install varnish-7.3.0 -y

RUN dnf install -y bind-utils kyotocabinet-libs initscripts iproute net-tools nmap-ncat gettext autoconf automake libtool gcc-c++ cronie glibc-devel openssl-devel git perl && \
dnf install -y jq logrotate findutils && \
dnf clean all


COPY infrastructure/cdn-in-a-box/varnish/run.sh infrastructure/cdn-in-a-box/traffic_ops/to-access.sh infrastructure/cdn-in-a-box/enroller/server_template.json /

COPY infrastructure/cdn-in-a-box/dns/set-dns.sh \
infrastructure/cdn-in-a-box/dns/insert-self-into-dns.sh \
/usr/local/sbin/


COPY infrastructure/cdn-in-a-box/varnish/systemctl.sh /usr/bin/systemctl
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is it absolutely necessary to use systemd? Doing that in a Docker container is prone to problems and headaches

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since t3c-apply only manages services using systemctl (or service in sysV systems) Traffic Server image and Varnish image both include a script replacing systemctl to start, restart, show status, enable and stop the cache service. There is no actual invoking of systemd, the scripts just mimics the behavior of systemctl regarding services


ARG ORT_RPM=infrastructure/cdn-in-a-box/cache/trafficcontrol-cache-config.rpm
COPY $ORT_RPM /
RUN rpm -Uvh /$(basename $ORT_RPM) &&\
rm /$(basename $ORT_RPM)

COPY infrastructure/cdn-in-a-box/varnish/traffic_ops_ort.crontab /etc/cron.d/traffic_ops_ort-cron-template


CMD /run.sh

FROM common-varnish-cache-config-layers AS mid
ENV CACHE_TYPE=mid
COPY infrastructure/cdn-in-a-box/mid/init.d/ /opt/init.d/

FROM common-varnish-cache-config-layers AS edge
ENV CACHE_TYPE=edge
COPY infrastructure/cdn-in-a-box/edge/init.d/ /opt/init.d/


Loading
Loading