From 025a910c5c6d6f4f749022397a8cb0c6f33379ac Mon Sep 17 00:00:00 2001 From: Max Erenberg Date: Sat, 26 Nov 2022 17:31:50 +0000 Subject: [PATCH] update setChronyOptions function --- pkg/distros/almalinux.go | 2 +- pkg/distros/debian.go | 3 +- pkg/distros/fedora.go | 2 +- pkg/distros/opensuse_tumbleweed.go | 2 +- pkg/distros/template_manager.go | 99 +++++++++++++++++++++++++----- 5 files changed, 86 insertions(+), 22 deletions(-) diff --git a/pkg/distros/almalinux.go b/pkg/distros/almalinux.go index dafff65..594d16e 100644 --- a/pkg/distros/almalinux.go +++ b/pkg/distros/almalinux.go @@ -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 { diff --git a/pkg/distros/debian.go b/pkg/distros/debian.go index 0135bbb..9a67885 100644 --- a/pkg/distros/debian.go +++ b/pkg/distros/debian.go @@ -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 { diff --git a/pkg/distros/fedora.go b/pkg/distros/fedora.go index 31417a2..8498601 100644 --- a/pkg/distros/fedora.go +++ b/pkg/distros/fedora.go @@ -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 { diff --git a/pkg/distros/opensuse_tumbleweed.go b/pkg/distros/opensuse_tumbleweed.go index 870da39..04ec502 100644 --- a/pkg/distros/opensuse_tumbleweed.go +++ b/pkg/distros/opensuse_tumbleweed.go @@ -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 { diff --git a/pkg/distros/template_manager.go b/pkg/distros/template_manager.go index f338e0c..2bc65bf 100644 --- a/pkg/distros/template_manager.go +++ b/pkg/distros/template_manager.go @@ -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)) }