1
0

refactor: move debian-related functions to debian module

This commit is contained in:
dump_stack() 2023-05-16 09:24:34 +00:00
parent e1ae427757
commit 4d950d7302
Signed by: dump_stack
GPG Key ID: BE44DA8C062D87DC
3 changed files with 48 additions and 25 deletions

View File

@ -11,6 +11,7 @@ import (
"github.com/rs/zerolog/log"
"code.dumpstack.io/tools/out-of-tree/cache"
"code.dumpstack.io/tools/out-of-tree/config"
"code.dumpstack.io/tools/out-of-tree/container"
"code.dumpstack.io/tools/out-of-tree/fs"
@ -307,3 +308,41 @@ func ContainerVolumes(km config.KernelMask, pkgname string) (volumes container.V
return
}
func InstallCommands(km config.KernelMask, pkgname string) (cmds []string, err error) {
dk, err := getCachedKernel(pkgname + ".deb")
if err != nil {
return
}
for _, pkg := range dk.Packages() {
found, newurl := cache.PackageURL(
km.DistroType,
pkg.Deb.URL,
)
if found {
log.Debug().Msgf("cached deb found %s", newurl)
pkg.Deb.URL = newurl
}
// TODO use faketime on old releases?
pkg.Deb.URL = strings.Replace(pkg.Deb.URL, "https", "http", -1)
cmds = append(cmds, "wget --no-check-certificate "+pkg.Deb.URL)
}
cmds = append(cmds, "dpkg -i ./*.deb")
return
}
func CleanupFailed(km config.KernelMask, pkgname string) {
pkgdir := config.Dir(filepath.Join("volumes", km.DockerName(), pkgname))
log.Debug().Msgf("cleanup %s", pkgdir)
err := os.RemoveAll(pkgdir)
if err != nil {
log.Warn().Err(err).Msg("cleanup")
}
}

View File

@ -122,7 +122,7 @@ func getDebianKernel(version string) (dk DebianKernel, err error) {
}
// GetCachedKernel by deb package name
func GetCachedKernel(deb string) (dk DebianKernel, err error) {
func getCachedKernel(deb string) (dk DebianKernel, err error) {
c, err := NewCache(CachePath)
if err != nil {
log.Error().Err(err).Msg("cache")

View File

@ -25,7 +25,6 @@ import (
"code.dumpstack.io/tools/out-of-tree/config"
"code.dumpstack.io/tools/out-of-tree/container"
"code.dumpstack.io/tools/out-of-tree/distro/debian"
"code.dumpstack.io/tools/out-of-tree/distro/debian/snapshot"
"code.dumpstack.io/tools/out-of-tree/fs"
)
@ -406,35 +405,20 @@ func installKernel(sk config.KernelMask, pkgname string, force, headers bool) (e
"/boot/initramfs-%s.img %s", version, version)
}
case config.Debian:
// TODO move to distro/debian/
var dk debian.DebianKernel
dk, err = debian.GetCachedKernel(pkgname + ".deb")
var commands []string
commands, err = debian.InstallCommands(sk, pkgname)
if err != nil {
return
}
pkgs := []snapshot.Package{dk.Image}
if headers {
pkgs = append(pkgs, dk.Headers...)
pkgs = append(pkgs, dk.Dependencies...)
}
for _, pkg := range pkgs {
found, newurl := cache.PackageURL(
sk.DistroType,
pkg.Deb.URL,
)
if found {
log.Debug().Msgf("cached deb found %s", newurl)
pkg.Deb.URL = newurl
defer func() {
if err != nil {
debian.CleanupFailed(sk, pkgname)
}
}()
cmd += fmt.Sprintf(" && wget --no-check-certificate %s",
strings.Replace(pkg.Deb.URL, "https", "http", -1))
for _, command := range commands {
cmd += fmt.Sprintf(" && %s", command)
}
cmd += fmt.Sprintf(" && dpkg -i ./*deb ; apt-get -fy install")
default:
err = fmt.Errorf("%s not yet supported", sk.DistroType.String())
return