From ee90bfaa725fd8569b44bf61a2c8e908dff04268 Mon Sep 17 00:00:00 2001 From: Mikhail Klementev Date: Wed, 24 May 2023 16:29:52 +0000 Subject: [PATCH] fix: support force kernel reinstallation --- distro/distro.go | 4 ++++ kernel.go | 43 ++++++++++++++++++++++++++++++++++++------- 2 files changed, 40 insertions(+), 7 deletions(-) diff --git a/distro/distro.go b/distro/distro.go index cd73ef2..0463f37 100644 --- a/distro/distro.go +++ b/distro/distro.go @@ -35,6 +35,10 @@ type Distro struct { Release string } +func (d Distro) String() string { + return d.ID.String() + " " + d.Release +} + func (d Distro) Packages() (packages []string, err error) { for _, dd := range distros { if d.ID != None && d.ID != dd.Distro().ID { diff --git a/kernel.go b/kernel.go index 2bffd50..fd28e0f 100644 --- a/kernel.go +++ b/kernel.go @@ -9,6 +9,7 @@ import ( "fmt" "math" "os" + "strings" "time" "github.com/naoina/toml" @@ -39,6 +40,7 @@ type KernelCmd struct { ConfigRegen KernelConfigRegenCmd `cmd:"" help:"regenerate config"` shutdown bool + kcfg config.KernelConfig } func (cmd KernelCmd) UpdateConfig() (err error) { @@ -77,6 +79,29 @@ func (cmd KernelCmd) UpdateConfig() (err error) { } func (cmd *KernelCmd) GenKernel(km config.Target, pkg string, max *int) { + flog := log.With(). + Str("kernel", pkg). + Str("distro", km.Distro.String()). + Logger() + + reinstall := false + for _, kinfo := range cmd.kcfg.Kernels { + if strings.Contains(pkg, kinfo.KernelVersion) { + if !cmd.Force { + flog.Info().Msg("already installed") + return + } + reinstall = true + break + } + } + + if reinstall { + flog.Info().Msg("reinstall") + } else { + flog.Info().Msg("install") + } + var attempt int for { attempt++ @@ -85,31 +110,35 @@ func (cmd *KernelCmd) GenKernel(km config.Target, pkg string, max *int) { return } - // TODO cmd.Force err := km.Distro.Install(pkg, !cmd.NoHeaders) if err == nil { *max-- + flog.Info().Msg("success") break } else if attempt >= cmd.Retries { - log.Error().Err(err).Msg("install kernel") - log.Debug().Msg("skip") + flog.Error().Err(err).Msg("install kernel") + flog.Debug().Msg("skip") break } else { - log.Warn().Err(err).Msg("install kernel") + flog.Warn().Err(err).Msg("install kernel") time.Sleep(time.Second) - log.Info().Msg("retry") + flog.Info().Msg("retry") } } } func (cmd *KernelCmd) Generate(g *Globals, km config.Target, max int) (err error) { - // TODO cmd.Update + cmd.kcfg, err = config.ReadKernelConfig(g.Config.Kernels) + if err != nil { + log.Debug().Err(err).Msg("read kernels config") + } + container.Commands = g.Config.Docker.Commands container.Registry = g.Config.Docker.Registry - log.Info().Msgf("Generating for kernel mask %v", km) + log.Info().Msgf("Generating for target %v", km) _, err = kernel.GenRootfsImage(container.Image{Name: km.DockerName()}, !cmd.NoDownload)