1
0

fix: avoid rebuild of prebuilt containers

This commit is contained in:
dump_stack() 2024-06-06 14:17:13 +00:00
parent 9b379eded8
commit 2aae4c8e0b
Signed by: dump_stack
GPG Key ID: C9905BA72B5E02BB
2 changed files with 49 additions and 48 deletions

View File

@ -11,18 +11,15 @@ import (
"strings" "strings"
"time" "time"
"github.com/cavaliergopher/grab/v3"
"github.com/naoina/toml" "github.com/naoina/toml"
"github.com/remeh/sizedwaitgroup" "github.com/remeh/sizedwaitgroup"
"github.com/rs/zerolog/log" "github.com/rs/zerolog/log"
"code.dumpstack.io/tools/out-of-tree/artifact" "code.dumpstack.io/tools/out-of-tree/artifact"
"code.dumpstack.io/tools/out-of-tree/cache"
"code.dumpstack.io/tools/out-of-tree/config" "code.dumpstack.io/tools/out-of-tree/config"
"code.dumpstack.io/tools/out-of-tree/config/dotfiles" "code.dumpstack.io/tools/out-of-tree/config/dotfiles"
"code.dumpstack.io/tools/out-of-tree/container" "code.dumpstack.io/tools/out-of-tree/container"
"code.dumpstack.io/tools/out-of-tree/distro" "code.dumpstack.io/tools/out-of-tree/distro"
"code.dumpstack.io/tools/out-of-tree/fs"
"code.dumpstack.io/tools/out-of-tree/kernel" "code.dumpstack.io/tools/out-of-tree/kernel"
) )
@ -168,33 +165,6 @@ func (cmd *KernelCmd) GenKernel(km artifact.Target, pkg string) {
} }
} }
func (cmd *KernelCmd) fetchPrebuiltContainer(c container.Container) {
if !cmd.PrebuiltContainers {
return
}
if c.Exist() && container.UseCache {
return
}
tmp, err := fs.TempDir()
if err != nil {
return
}
defer os.RemoveAll(tmp)
resp, err := grab.Get(tmp, cache.ContainerURL(c.Name()))
if err != nil {
return
}
defer os.Remove(resp.Filename)
err = container.Load(resp.Filename, c.Name())
if err == nil {
log.Info().Msgf("use prebuilt container %s", c.Name())
}
}
func (cmd *KernelCmd) Generate(g *Globals, km artifact.Target) (err error) { func (cmd *KernelCmd) Generate(g *Globals, km artifact.Target) (err error) {
defer func() { defer func() {
if err != nil { if err != nil {
@ -204,6 +174,9 @@ func (cmd *KernelCmd) Generate(g *Globals, km artifact.Target) (err error) {
} }
}() }()
if cmd.PrebuiltContainers {
container.UsePrebuilt = false
}
if cmd.Update { if cmd.Update {
container.UseCache = false container.UseCache = false
} }
@ -230,13 +203,6 @@ func (cmd *KernelCmd) Generate(g *Globals, km artifact.Target) (err error) {
return return
} }
c, err := container.New(km.Distro)
if err != nil || cmd.shutdown {
return
}
cmd.fetchPrebuiltContainer(c)
pkgs, err := kernel.MatchPackages(km) pkgs, err := kernel.MatchPackages(km)
if err != nil || cmd.shutdown { if err != nil || cmd.shutdown {
return return
@ -305,6 +271,9 @@ type KernelListRemoteCmd struct {
} }
func (cmd *KernelListRemoteCmd) Run(kernelCmd *KernelCmd, g *Globals) (err error) { func (cmd *KernelListRemoteCmd) Run(kernelCmd *KernelCmd, g *Globals) (err error) {
if kernelCmd.PrebuiltContainers {
container.UsePrebuilt = false
}
if kernelCmd.Update { if kernelCmd.Update {
container.UseCache = false container.UseCache = false
} }
@ -330,13 +299,6 @@ func (cmd *KernelListRemoteCmd) Run(kernelCmd *KernelCmd, g *Globals) (err error
container.Registry = g.Config.Docker.Registry container.Registry = g.Config.Docker.Registry
container.Commands = g.Config.Docker.Commands container.Commands = g.Config.Docker.Commands
c, err := container.New(km.Distro)
if err != nil {
return
}
kernelCmd.fetchPrebuiltContainer(c)
pkgs, err := kernel.MatchPackages(km) pkgs, err := kernel.MatchPackages(km)
// error check skipped on purpose // error check skipped on purpose

View File

@ -16,11 +16,14 @@ import (
"strings" "strings"
"time" "time"
"github.com/cavaliergopher/grab/v3"
"github.com/rs/zerolog" "github.com/rs/zerolog"
"github.com/rs/zerolog/log" "github.com/rs/zerolog/log"
"code.dumpstack.io/tools/out-of-tree/cache"
"code.dumpstack.io/tools/out-of-tree/config/dotfiles" "code.dumpstack.io/tools/out-of-tree/config/dotfiles"
"code.dumpstack.io/tools/out-of-tree/distro" "code.dumpstack.io/tools/out-of-tree/distro"
"code.dumpstack.io/tools/out-of-tree/fs"
) )
var Runtime = "docker" var Runtime = "docker"
@ -33,6 +36,8 @@ var Commands []distro.Command
var UseCache = true var UseCache = true
var UsePrebuilt = true
var Prune = true var Prune = true
type Image struct { type Image struct {
@ -240,6 +245,33 @@ func (c Container) Exist() (yes bool) {
return return
} }
func (c Container) loadPrebuilt() (err error) {
if c.Exist() && UseCache {
return
}
tmp, err := fs.TempDir()
if err != nil {
return
}
defer os.RemoveAll(tmp)
log.Info().Msgf("download prebuilt container %s", c.Name())
resp, err := grab.Get(tmp, cache.ContainerURL(c.Name()))
if err != nil {
return
}
defer os.Remove(resp.Filename)
err = Load(resp.Filename, c.Name())
if err == nil {
log.Info().Msgf("use prebuilt container %s", c.Name())
}
return
}
func (c Container) Build(image string, envs, runs []string) (err error) { func (c Container) Build(image string, envs, runs []string) (err error) {
if c.Exist() && UseCache { if c.Exist() && UseCache {
return return
@ -290,11 +322,18 @@ func (c Container) Build(image string, envs, runs []string) (err error) {
c.Log.Info().Msg("build") c.Log.Info().Msg("build")
} }
output, err := c.build(cdir) if UsePrebuilt {
err = c.loadPrebuilt()
}
if err != nil || !UsePrebuilt {
var output string
output, err = c.build(cdir)
if err != nil { if err != nil {
c.Log.Error().Err(err).Msg(output) c.Log.Error().Err(err).Msg(output)
return return
} }
}
c.Log.Info().Msg("success") c.Log.Info().Msg("success")
return return