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) {
|
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
|
return
|
||||||
}
|
}
|
||||||
if err = mgr.setNetworkManagerOptions(handle); err != nil {
|
if err = mgr.setNetworkManagerOptions(handle); err != nil {
|
||||||
|
|
|
@ -71,13 +71,12 @@ func (mgr *DebianTemplateManager) DownloadTemplate(version, codename string) (pa
|
||||||
return mgr.DownloadTemplateGeneric(filename, url)
|
return mgr.DownloadTemplateGeneric(filename, url)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
func (mgr *DebianTemplateManager) CommandToUpdatePackageCache() []string {
|
func (mgr *DebianTemplateManager) CommandToUpdatePackageCache() []string {
|
||||||
return debianCommandToUpdatePackageCache()
|
return debianCommandToUpdatePackageCache()
|
||||||
}
|
}
|
||||||
|
|
||||||
func (mgr *DebianTemplateManager) PerformDistroSpecificModifications(handle *guestfs.Guestfs) (err error) {
|
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
|
return
|
||||||
}
|
}
|
||||||
if err = mgr.setDhclientOptions(handle); err != nil {
|
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) {
|
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
|
return
|
||||||
}
|
}
|
||||||
if err = mgr.setNetworkManagerOptions(handle); err != nil {
|
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) {
|
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
|
return
|
||||||
}
|
}
|
||||||
if err = mgr.maskSystemdUnit(handle, "wickedd-dhcp6.service"); err != nil {
|
if err = mgr.maskSystemdUnit(handle, "wickedd-dhcp6.service"); err != nil {
|
||||||
|
|
|
@ -243,28 +243,93 @@ func (mgr *TemplateManager) maskSystemdUnit(handle *guestfs.Guestfs, unit string
|
||||||
}
|
}
|
||||||
|
|
||||||
// setChronyOptions sets custom NTP server URLs in a chrony config file.
|
// 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) (err error) {
|
||||||
func (mgr *TemplateManager) setChronyOptions(handle *guestfs.Guestfs, path string) (err error) {
|
possiblePaths := []string{"/etc/chrony.conf", "/etc/chrony/chrony.conf"}
|
||||||
oldLines, err := handle.Read_lines(path)
|
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 {
|
if err != nil {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
snippet := string(getResource("chrony-snippet"))
|
if exists {
|
||||||
snippetLines := strings.Split(snippet, "\n")
|
path := "/etc/chrony/sources.d/csclub.sources"
|
||||||
wroteSnippet := false
|
mgr.logger.Debug().Msg("Writing to " + path)
|
||||||
newLines := make([]string, 0, len(oldLines)+len(snippetLines))
|
return handle.Write(path, snippet)
|
||||||
for _, line := range oldLines {
|
}
|
||||||
if strings.HasPrefix(line, "pool ") {
|
|
||||||
newLines = append(newLines, "#"+line)
|
// e.g. OpenSUSE Tumbleweed
|
||||||
if !wroteSnippet {
|
exists, err = handle.Is_dir("/etc/chrony.d", nil)
|
||||||
newLines = append(newLines, snippetLines...)
|
if err != nil {
|
||||||
wroteSnippet = true
|
return
|
||||||
}
|
}
|
||||||
} else {
|
if exists {
|
||||||
newLines = append(newLines, line)
|
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)
|
mgr.logger.Debug().Msg("Writing new content to " + path)
|
||||||
return handle.Write(path, []byte(newContent))
|
return handle.Write(path, []byte(newContent))
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue