modify /etc/cloud/cloud.cfg for Debian

This commit is contained in:
Max Erenberg 2023-06-18 23:38:27 -04:00
parent c7f71ce2a9
commit db59c99f44
1 changed files with 47 additions and 1 deletions

View File

@ -2,6 +2,7 @@ package distros
import (
"bufio"
"bytes"
"errors"
"fmt"
"net/http"
@ -71,10 +72,55 @@ func (mgr *DebianTemplateManager) DownloadTemplate(version, codename string) (pa
return mgr.DownloadTemplateGeneric(filename, url)
}
func (mgr *DebianTemplateManager) replaceFailsafeMirrorsInCloudCfg(handle *guestfs.Guestfs) error {
// In Debian 12 (bookworm), /etc/cloud/cloud.cfg has the following snippet:
//
// system_info:
// package_mirrors:
// - arches: [default]
// failsafe:
// primary: https://deb.debian.org/debian
// security: https://deb.debian.org/debian-security
//
// Since we can't override the system_info settings, we need to overwrite this file.
filePaths := []string{"/etc/cloud/cloud.cfg"}
// To be safe, we check other config files under /etc/cloud/cloud.cfg.d as well.
extraFilePaths, err := handle.Glob_expand("/etc/cloud/cloud.cfg.d/*.cfg", nil)
if err != nil {
return fmt.Errorf("Could not expand glob /etc/cloud/cloud.cfg.d/*.cfg: %w", err)
}
filePaths = append(filePaths, extraFilePaths...)
debMirrorUrlRegex := regexp.MustCompile(`https?://deb\.debian\.org`)
replacement := []byte("http://" + mgr.cfg.MirrorHost)
for _, filePath := range filePaths {
mgr.logger.Debug().Str("file", filePath).Msg("Checking for deb.debian.org URLs")
content, err := handle.Read_file(filePath)
if err != nil {
return fmt.Errorf("Could not read %s: %w", filePath, err)
}
newContent := debMirrorUrlRegex.ReplaceAll(content, replacement)
if bytes.Equal(content, newContent) {
continue
}
mgr.logger.Debug().Str("file", filePath).Msg("Replacing deb.debian.org URLs")
err = handle.Write(filePath, newContent)
if err != nil {
return fmt.Errorf("Could not write to %s: %w", filePath, err)
}
}
return nil
}
func (mgr *DebianTemplateManager) CommandToUpdatePackageCache() []string {
return debianCommandToUpdatePackageCache()
}
func (mgr *DebianTemplateManager) PerformDistroSpecificModifications(handle *guestfs.Guestfs) error {
return mgr.replaceDebianMirrorUrls(handle)
if err := mgr.replaceFailsafeMirrorsInCloudCfg(handle); err != nil {
return err
}
if err := mgr.replaceDebianMirrorUrls(handle); err != nil {
return err
}
return nil
}