1
0

Graceful shutdown on ^C

This commit is contained in:
dump_stack() 2023-04-07 20:52:45 +00:00
parent b812048408
commit 841fd7f585
Signed by: dump_stack
GPG Key ID: BE44DA8C062D87DC

View File

@ -12,6 +12,7 @@ import (
"math/rand" "math/rand"
"os" "os"
"os/exec" "os/exec"
"os/signal"
"os/user" "os/user"
"regexp" "regexp"
"runtime" "runtime"
@ -68,6 +69,9 @@ func (cmd KernelAutogenCmd) Run(kernelCmd *KernelCmd, g *Globals) (err error) {
return return
} }
shutdown := false
setSigintHandler(&shutdown)
for _, sk := range ka.SupportedKernels { for _, sk := range ka.SupportedKernels {
if sk.DistroRelease == "" { if sk.DistroRelease == "" {
err = errors.New("Please set distro_release") err = errors.New("Please set distro_release")
@ -82,10 +86,14 @@ func (cmd KernelAutogenCmd) Run(kernelCmd *KernelCmd, g *Globals) (err error) {
kernelCmd.Force, kernelCmd.Force,
!kernelCmd.NoHeaders, !kernelCmd.NoHeaders,
kernelCmd.Shuffle, kernelCmd.Shuffle,
&shutdown,
) )
if err != nil { if err != nil {
return return
} }
if shutdown {
break
}
} }
return updateKernelsCfg(kernelCmd.UseHost, !kernelCmd.NoDownload) return updateKernelsCfg(kernelCmd.UseHost, !kernelCmd.NoDownload)
@ -102,6 +110,9 @@ func (cmd *KernelGenallCmd) Run(kernelCmd *KernelCmd, g *Globals) (err error) {
return return
} }
shutdown := false
setSigintHandler(&shutdown)
km := config.KernelMask{ km := config.KernelMask{
DistroType: distroType, DistroType: distroType,
DistroRelease: cmd.Ver, DistroRelease: cmd.Ver,
@ -115,6 +126,7 @@ func (cmd *KernelGenallCmd) Run(kernelCmd *KernelCmd, g *Globals) (err error) {
kernelCmd.Force, kernelCmd.Force,
!kernelCmd.NoHeaders, !kernelCmd.NoHeaders,
kernelCmd.Shuffle, kernelCmd.Shuffle,
&shutdown,
) )
if err != nil { if err != nil {
return return
@ -135,6 +147,9 @@ func (cmd *KernelInstallCmd) Run(kernelCmd *KernelCmd, g *Globals) (err error) {
return return
} }
shutdown := false
setSigintHandler(&shutdown)
km := config.KernelMask{ km := config.KernelMask{
DistroType: distroType, DistroType: distroType,
DistroRelease: cmd.Ver, DistroRelease: cmd.Ver,
@ -148,6 +163,7 @@ func (cmd *KernelInstallCmd) Run(kernelCmd *KernelCmd, g *Globals) (err error) {
kernelCmd.Force, kernelCmd.Force,
!kernelCmd.NoHeaders, !kernelCmd.NoHeaders,
kernelCmd.Shuffle, kernelCmd.Shuffle,
&shutdown,
) )
if err != nil { if err != nil {
return return
@ -659,20 +675,40 @@ func shuffleStrings(a []string) []string {
return a return a
} }
func setSigintHandler(variable *bool) {
c := make(chan os.Signal, 1)
signal.Notify(c, os.Interrupt)
go func() {
counter := 0
for _ = range c {
if counter == 0 {
*variable = true
log.Warn().Msg("shutdown requested, finishing work")
log.Info().Msg("^C a couple of times more for an unsafe exit")
} else if counter >= 3 {
log.Fatal().Msg("unsafe exit")
}
counter += 1
}
}()
}
func generateKernels(km config.KernelMask, registry string, func generateKernels(km config.KernelMask, registry string,
commands []config.DockerCommand, max, retries int64, commands []config.DockerCommand, max, retries int64,
download, force, headers, shuffle bool) (err error) { download, force, headers, shuffle bool, shutdown *bool) (err error) {
log.Info().Msgf("Generating for kernel mask %v", km) log.Info().Msgf("Generating for kernel mask %v", km)
_, err = genRootfsImage(containerImageInfo{Name: km.DockerName()}, _, err = genRootfsImage(containerImageInfo{Name: km.DockerName()},
download) download)
if err != nil { if err != nil || *shutdown {
return return
} }
err = generateBaseDockerImage(registry, commands, km) err = generateBaseDockerImage(registry, commands, km)
if err != nil { if err != nil || *shutdown {
return return
} }
@ -686,7 +722,7 @@ func generateKernels(km config.KernelMask, registry string,
default: default:
err = fmt.Errorf("%s not yet supported", km.DistroType.String()) err = fmt.Errorf("%s not yet supported", km.DistroType.String())
} }
if err != nil { if err != nil || *shutdown {
return return
} }
@ -705,6 +741,11 @@ func generateKernels(km config.KernelMask, registry string,
for { for {
attempt++ attempt++
if *shutdown {
err = nil
return
}
err = installKernel(km, pkg, force, headers) err = installKernel(km, pkg, force, headers)
if err == nil { if err == nil {
max-- max--