cloudbuild/pkg/distros/debian.go

90 lines
2.4 KiB
Go

package distros
import (
"bufio"
"errors"
"fmt"
"net/http"
"regexp"
"github.com/rs/zerolog/log"
"libguestfs.org/guestfs"
"git.csclub.uwaterloo.ca/cloud/cloudbuild/pkg/config"
)
type DebianTemplateManager struct {
TemplateManager
}
// NewDebianTemplateManager returns a DebianTemplateManager with the given
// config values.
func NewDebianTemplateManager(cfg *config.Config) *DebianTemplateManager {
logger := log.With().Str("distro", "debian").Logger()
debianTemplateManager := DebianTemplateManager{
TemplateManager{
cfg: cfg,
logger: &logger,
impl: nil,
},
}
debianTemplateManager.TemplateManager.impl = &debianTemplateManager
return &debianTemplateManager
}
func (mgr *DebianTemplateManager) GetLatestVersion() (version string, codename string, err error) {
// We're only interested in the major version (integer part)
versionPattern := regexp.MustCompile("^Version: (\\d+)(?:\\.\\d)?$")
codenamePattern := regexp.MustCompile("^Codename: ([a-z-]+)$")
resp, err := http.Get("https://mirror.csclub.uwaterloo.ca/debian/dists/stable/InRelease")
if err != nil {
return
}
defer resp.Body.Close()
scanner := bufio.NewScanner(resp.Body)
for scanner.Scan() {
if version == "" {
submatches := versionPattern.FindStringSubmatch(scanner.Text())
if submatches != nil {
version = submatches[1]
}
}
if codename == "" {
submatches := codenamePattern.FindStringSubmatch(scanner.Text())
if submatches != nil {
codename = submatches[1]
}
}
if version != "" && codename != "" {
break
}
}
if version == "" {
err = errors.New("Could not determine latest version of Debian")
}
return
}
func (mgr *DebianTemplateManager) DownloadTemplate(version, codename string) (path string, err error) {
filename := fmt.Sprintf("debian-%s-genericcloud-amd64.qcow2", version)
url := fmt.Sprintf("https://cloud.debian.org/images/cloud/%s/latest/%s", codename, filename)
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); err != nil {
return
}
if err = mgr.setDhclientOptions(handle); err != nil {
return
}
if err = mgr.replaceDebianMirrorUrls(handle); err != nil {
return
}
return
}