update setChronyOptions function

This commit is contained in:
Max Erenberg 2022-11-26 17:31:50 +00:00
parent 12613df38d
commit 025a910c5c
5 changed files with 86 additions and 22 deletions

View File

@ -92,7 +92,7 @@ func (mgr *AlmaLinuxTemplateManager) transformAlmaLinuxYumRepoBaseUrl(url string
}
func (mgr *AlmaLinuxTemplateManager) PerformDistroSpecificModifications(handle *guestfs.Guestfs) (err error) {
if err = mgr.setChronyOptions(handle, "/etc/chrony.conf"); err != nil {
if err = mgr.setChronyOptions(handle); err != nil {
return
}
if err = mgr.setNetworkManagerOptions(handle); err != nil {

View File

@ -71,13 +71,12 @@ func (mgr *DebianTemplateManager) DownloadTemplate(version, codename string) (pa
return mgr.DownloadTemplateGeneric(filename, url)
}
func (mgr *DebianTemplateManager) CommandToUpdatePackageCache() []string {
return debianCommandToUpdatePackageCache()
}
func (mgr *DebianTemplateManager) PerformDistroSpecificModifications(handle *guestfs.Guestfs) (err error) {
if err = mgr.setChronyOptions(handle, "/etc/chrony/chrony.conf"); err != nil {
if err = mgr.setChronyOptions(handle); err != nil {
return
}
if err = mgr.setDhclientOptions(handle); err != nil {

View File

@ -137,7 +137,7 @@ func (mgr *FedoraTemplateManager) transformFedoraYumRepoBaseUrl(url string) stri
}
func (mgr *FedoraTemplateManager) PerformDistroSpecificModifications(handle *guestfs.Guestfs) (err error) {
if err = mgr.setChronyOptions(handle, "/etc/chrony.conf"); err != nil {
if err = mgr.setChronyOptions(handle); err != nil {
return
}
if err = mgr.setNetworkManagerOptions(handle); err != nil {

View File

@ -97,7 +97,7 @@ func (mgr *OpensuseTumbleweedTemplateManager) CommandToUpdatePackageCache() []st
}
func (mgr *OpensuseTumbleweedTemplateManager) PerformDistroSpecificModifications(handle *guestfs.Guestfs) (err error) {
if err = mgr.setChronyOptions(handle, "/etc/chrony.conf"); err != nil {
if err = mgr.setChronyOptions(handle); err != nil {
return
}
if err = mgr.maskSystemdUnit(handle, "wickedd-dhcp6.service"); err != nil {

View File

@ -243,28 +243,93 @@ func (mgr *TemplateManager) maskSystemdUnit(handle *guestfs.Guestfs, unit string
}
// setChronyOptions sets custom NTP server URLs in a chrony config file.
// It assumes that a line beginning with "pool" will already be present.
func (mgr *TemplateManager) setChronyOptions(handle *guestfs.Guestfs, path string) (err error) {
oldLines, err := handle.Read_lines(path)
func (mgr *TemplateManager) setChronyOptions(handle *guestfs.Guestfs) (err error) {
possiblePaths := []string{"/etc/chrony.conf", "/etc/chrony/chrony.conf"}
var exists bool
var path string
var newLines []string
for _, path = range possiblePaths {
exists, err = handle.Is_file(path, nil)
if err != nil {
return
}
if !exists {
continue
}
// comment out any lines beginning with "pool"
var oldLines []string
oldLines, err = handle.Read_lines(path)
if err != nil {
return
}
changed := false
newLines = make([]string, 0, len(oldLines))
for _, line := range oldLines {
if strings.HasPrefix(line, "pool ") {
newLines = append(newLines, "#"+line)
changed = true
} else {
newLines = append(newLines, line)
}
}
if changed {
newContent := strings.Join(newLines, "\n")
mgr.logger.Debug().Msg("Writing new content to " + path)
err = handle.Write(path, []byte(newContent))
if err != nil {
return
}
}
break
}
snippet := getResource("chrony-snippet")
// e.g. Debian
exists, err = handle.Is_dir("/etc/chrony/sources.d", nil)
if err != nil {
return
}
snippet := string(getResource("chrony-snippet"))
snippetLines := strings.Split(snippet, "\n")
wroteSnippet := false
newLines := make([]string, 0, len(oldLines)+len(snippetLines))
for _, line := range oldLines {
if strings.HasPrefix(line, "pool ") {
newLines = append(newLines, "#"+line)
if !wroteSnippet {
newLines = append(newLines, snippetLines...)
wroteSnippet = true
}
} else {
newLines = append(newLines, line)
if exists {
path := "/etc/chrony/sources.d/csclub.sources"
mgr.logger.Debug().Msg("Writing to " + path)
return handle.Write(path, snippet)
}
// e.g. OpenSUSE Tumbleweed
exists, err = handle.Is_dir("/etc/chrony.d", nil)
if err != nil {
return
}
if exists {
mgr.logger.Debug().Msg("Removing /etc/chrony.d/pool.conf")
err = handle.Rm_f("/etc/chrony.d/pool.conf")
if err != nil {
return
}
path := "/etc/chrony.d/csclub.conf"
mgr.logger.Debug().Msg("Writing to " + path)
return handle.Write(path, snippet)
}
// Otherwise, assume we need to modify chrony.conf directly
// e.g. Fedora
if newLines == nil {
mgr.logger.Warn().Msg("could not find chrony.conf, skipping")
return
}
serverDirectiveExists := false
for _, line := range newLines {
if strings.HasPrefix(line, "server ") {
serverDirectiveExists = true
break
}
}
newContent := strings.Join(newLines, "\n")
if serverDirectiveExists {
// Assume that this was inserted by us during a previous run
return
}
newContent := string(snippet) + "\n" + strings.Join(newLines, "\n")
mgr.logger.Debug().Msg("Writing new content to " + path)
return handle.Write(path, []byte(newContent))
}