|
|
|
@ -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 |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
if serverDirectiveExists { |
|
|
|
|
// Assume that this was inserted by us during a previous run
|
|
|
|
|
return |
|
|
|
|
} |
|
|
|
|
newContent := strings.Join(newLines, "\n") |
|
|
|
|
newContent := string(snippet) + "\n" + strings.Join(newLines, "\n") |
|
|
|
|
mgr.logger.Debug().Msg("Writing new content to " + path) |
|
|
|
|
return handle.Write(path, []byte(newContent)) |
|
|
|
|
} |
|
|
|
|