update setChronyOptions function
This commit is contained in:
parent
12613df38d
commit
025a910c5c
|
@ -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 {
|
||||
|
|
|
@ -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 {
|
||||
|
|
|
@ -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 {
|
||||
|
|
|
@ -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 {
|
||||
|
|
|
@ -243,29 +243,94 @@ 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
|
||||
}
|
||||
snippet := string(getResource("chrony-snippet"))
|
||||
snippetLines := strings.Split(snippet, "\n")
|
||||
wroteSnippet := false
|
||||
newLines := make([]string, 0, len(oldLines)+len(snippetLines))
|
||||
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)
|
||||
if !wroteSnippet {
|
||||
newLines = append(newLines, snippetLines...)
|
||||
wroteSnippet = true
|
||||
}
|
||||
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
|
||||
}
|
||||
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
|
||||
}
|
||||
}
|
||||
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))
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in New Issue