Skip to content

Commit

Permalink
Remove t3c unused code (#6413)
Browse files Browse the repository at this point in the history
* Remove t3c unused variable

* Remove t3c duplicate fields

* Change t3c func to return struct

CheckReloadRestart was returning bools which were exactly the values
of the RestartData struct. Changed to the struct.
  • Loading branch information
rob05c authored Dec 14, 2021
1 parent e70ac05 commit e67c967
Showing 1 changed file with 35 additions and 43 deletions.
78 changes: 35 additions & 43 deletions cache-config/t3c-apply/torequest/torequest.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,21 +61,21 @@ type TrafficOpsReq struct {
pluginPkgs map[string]struct{} // map of packages
changedFiles []string // list of config files which were changed

configFiles map[string]*ConfigFile
TrafficCtlReload bool // a traffic_ctl_reload is required
SysCtlReload bool // a reload of the sysctl.conf is required
NtpdRestart bool // ntpd needs restarting
TeakdRestart bool // a restart of teakd is required
TrafficServerRestart bool // a trafficserver restart is required
RemapConfigReload bool // remap.config should be reloaded
unixTimeStr string // unix time string at program startup.
configFiles map[string]*ConfigFile

RestartData
}

type ShouldReloadRestart struct {
ReloadRestart []RestartData
ReloadRestart []FileRestartData
}

type FileRestartData struct {
Name string
RestartData
}

type RestartData struct {
Name string
TrafficCtlReload bool // a traffic_ctl_reload is required
SysCtlReload bool // a reload of the sysctl.conf is required
NtpdRestart bool // ntpd needs restarting
Expand Down Expand Up @@ -180,16 +180,13 @@ func (r *TrafficOpsReq) DumpConfigFiles() {

// NewTrafficOpsReq returns a new TrafficOpsReq object.
func NewTrafficOpsReq(cfg config.Cfg) *TrafficOpsReq {
unixTimeString := strconv.FormatInt(time.Now().Unix(), 10)

return &TrafficOpsReq{
Cfg: cfg,
pkgs: map[string]bool{},
plugins: map[string]bool{},
configFiles: map[string]*ConfigFile{},
installedPkgs: map[string]struct{}{},
pluginPkgs: map[string]struct{}{},
unixTimeStr: unixTimeString,
}
}

Expand Down Expand Up @@ -475,12 +472,12 @@ func (r *TrafficOpsReq) readCfgFile(cfg *ConfigFile, dir string) ([]byte, error)
const configFileTempSuffix = `.tmp`

// replaceCfgFile replaces an ATS configuration file with one from Traffic Ops.
func (r *TrafficOpsReq) replaceCfgFile(cfg *ConfigFile) (*RestartData, error) {
func (r *TrafficOpsReq) replaceCfgFile(cfg *ConfigFile) (*FileRestartData, error) {
if r.Cfg.ReportOnly ||
(r.Cfg.Files != t3cutil.ApplyFilesFlagAll && r.Cfg.Files != t3cutil.ApplyFilesFlagReval) {
log.Infof("You elected not to replace %s with the version from Traffic Ops.\n", cfg.Name)
cfg.ChangeApplied = false
return &RestartData{Name: cfg.Name}, nil
return &FileRestartData{Name: cfg.Name}, nil
}

tmpFileName := cfg.Path + configFileTempSuffix
Expand All @@ -492,12 +489,12 @@ func (r *TrafficOpsReq) replaceCfgFile(cfg *ConfigFile) (*RestartData, error) {
// we'd end up with malformed files.

if _, err := util.WriteFileWithOwner(tmpFileName, cfg.Body, &cfg.Uid, &cfg.Gid, cfg.Perm); err != nil {
return &RestartData{Name: cfg.Name}, errors.New("Failed to write temp config file '" + tmpFileName + "': " + err.Error())
return &FileRestartData{Name: cfg.Name}, errors.New("Failed to write temp config file '" + tmpFileName + "': " + err.Error())
}

log.Infof("Copying temp file '%s' to real '%s'\n", tmpFileName, cfg.Path)
if err := os.Rename(tmpFileName, cfg.Path); err != nil {
return &RestartData{Name: cfg.Name}, errors.New("Failed to move temp '" + tmpFileName + "' to real '" + cfg.Path + "': " + err.Error())
return &FileRestartData{Name: cfg.Name}, errors.New("Failed to move temp '" + tmpFileName + "' to real '" + cfg.Path + "': " + err.Error())
}
cfg.ChangeApplied = true
r.changedFiles = append(r.changedFiles, cfg.Path)
Expand Down Expand Up @@ -526,13 +523,15 @@ func (r *TrafficOpsReq) replaceCfgFile(cfg *ConfigFile) (*RestartData, error) {
log.Debugf("Reload state after %s: remap.config: %t reload: %t restart: %t ntpd: %t sysctl: %t", cfg.Name, remapConfigReload, trafficCtlReload, trafficServerRestart, ntpdRestart, sysCtlReload)

log.Debugf("Setting change applied for '%s'\n", cfg.Name)
return &RestartData{
Name: cfg.Name,
TrafficCtlReload: trafficCtlReload,
SysCtlReload: sysCtlReload,
NtpdRestart: ntpdRestart,
TrafficServerRestart: trafficServerRestart,
RemapConfigReload: remapConfigReload,
return &FileRestartData{
Name: cfg.Name,
RestartData: RestartData{
TrafficCtlReload: trafficCtlReload,
SysCtlReload: sysCtlReload,
NtpdRestart: ntpdRestart,
TrafficServerRestart: trafficServerRestart,
RemapConfigReload: remapConfigReload,
},
}, nil
}

Expand Down Expand Up @@ -793,24 +792,17 @@ func (r *TrafficOpsReq) CheckSyncDSState() (UpdateStatus, error) {
}

// CheckReloadRestart determines the final reload/restart state after all config files are processed.
func (r *TrafficOpsReq) CheckReloadRestart(data []RestartData) (bool, bool, bool, bool, bool, bool) {
trafficCtlReload := false
sysCtlReload := false
ntpdRestart := false
teakdRestart := false
trafficServerRestart := false
remapConfigReload := false

func (r *TrafficOpsReq) CheckReloadRestart(data []FileRestartData) RestartData {
rd := RestartData{}
for _, changedFile := range data {
trafficCtlReload = trafficCtlReload || changedFile.TrafficCtlReload
sysCtlReload = sysCtlReload || changedFile.SysCtlReload
ntpdRestart = ntpdRestart || changedFile.NtpdRestart
teakdRestart = teakdRestart || changedFile.TeakdRestart
trafficServerRestart = trafficServerRestart || changedFile.TrafficServerRestart
remapConfigReload = remapConfigReload || changedFile.RemapConfigReload
}

return trafficCtlReload, sysCtlReload, ntpdRestart, teakdRestart, trafficServerRestart, remapConfigReload
rd.TrafficCtlReload = rd.TrafficCtlReload || changedFile.TrafficCtlReload
rd.SysCtlReload = rd.SysCtlReload || changedFile.SysCtlReload
rd.NtpdRestart = rd.NtpdRestart || changedFile.NtpdRestart
rd.TeakdRestart = rd.TeakdRestart || changedFile.TeakdRestart
rd.TrafficServerRestart = rd.TrafficServerRestart || changedFile.TrafficServerRestart
rd.RemapConfigReload = rd.RemapConfigReload || changedFile.RemapConfigReload
}
return rd
}

// ProcessConfigFiles processes all config files retrieved from Traffic Ops.
Expand Down Expand Up @@ -850,7 +842,7 @@ func (r *TrafficOpsReq) ProcessConfigFiles() (UpdateStatus, error) {
}

changesRequired := 0
shouldRestartReload := ShouldReloadRestart{[]RestartData{}}
shouldRestartReload := ShouldReloadRestart{[]FileRestartData{}}

for _, cfg := range r.configFiles {
if cfg.ChangeNeeded &&
Expand Down Expand Up @@ -882,7 +874,7 @@ func (r *TrafficOpsReq) ProcessConfigFiles() (UpdateStatus, error) {
}
}

r.TrafficCtlReload, r.SysCtlReload, r.NtpdRestart, r.TeakdRestart, r.TrafficServerRestart, r.RemapConfigReload = r.CheckReloadRestart(shouldRestartReload.ReloadRestart)
r.RestartData = r.CheckReloadRestart(shouldRestartReload.ReloadRestart)

if 0 < len(r.changedFiles) {
log.Infof("Final state: remap.config: %t reload: %t restart: %t ntpd: %t sysctl: %t", r.RemapConfigReload, r.TrafficCtlReload, r.TrafficServerRestart, r.NtpdRestart, r.SysCtlReload)
Expand Down

0 comments on commit e67c967

Please sign in to comment.