2023-05-13 10:47:47 +00:00
|
|
|
|
// Copyright 2023 Mikhail Klementev. All rights reserved.
|
|
|
|
|
// Use of this source code is governed by a AGPLv3 license
|
|
|
|
|
// (or later) that can be found in the LICENSE file.
|
|
|
|
|
|
|
|
|
|
package kernel
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"math/rand"
|
|
|
|
|
"os"
|
|
|
|
|
"os/signal"
|
2023-05-21 20:31:47 +00:00
|
|
|
|
"path/filepath"
|
2023-05-18 21:37:07 +00:00
|
|
|
|
"regexp"
|
2023-05-13 10:47:47 +00:00
|
|
|
|
|
|
|
|
|
"github.com/rs/zerolog/log"
|
|
|
|
|
|
2024-02-20 13:25:31 +00:00
|
|
|
|
"code.dumpstack.io/tools/out-of-tree/artifact"
|
2023-05-13 10:47:47 +00:00
|
|
|
|
"code.dumpstack.io/tools/out-of-tree/cache"
|
2024-02-20 13:25:31 +00:00
|
|
|
|
"code.dumpstack.io/tools/out-of-tree/config/dotfiles"
|
2023-05-13 10:47:47 +00:00
|
|
|
|
"code.dumpstack.io/tools/out-of-tree/fs"
|
|
|
|
|
)
|
|
|
|
|
|
2024-02-20 13:25:31 +00:00
|
|
|
|
func MatchPackages(km artifact.Target) (packages []string, err error) {
|
2023-05-18 21:37:07 +00:00
|
|
|
|
pkgs, err := km.Distro.Packages()
|
|
|
|
|
if err != nil {
|
|
|
|
|
return
|
2023-05-13 10:47:47 +00:00
|
|
|
|
}
|
2023-05-18 21:37:07 +00:00
|
|
|
|
|
|
|
|
|
r, err := regexp.Compile(km.Kernel.Regex)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
2023-05-25 18:35:27 +00:00
|
|
|
|
exr, err := regexp.Compile(km.Kernel.ExcludeRegex)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
2023-05-18 21:37:07 +00:00
|
|
|
|
for _, pkg := range pkgs {
|
2023-05-25 18:35:27 +00:00
|
|
|
|
if !r.MatchString(pkg) {
|
|
|
|
|
continue
|
2023-05-18 21:37:07 +00:00
|
|
|
|
}
|
2023-05-25 18:35:27 +00:00
|
|
|
|
|
|
|
|
|
if km.Kernel.ExcludeRegex != "" && exr.MatchString(pkg) {
|
|
|
|
|
continue
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
packages = append(packages, pkg)
|
2023-05-18 21:37:07 +00:00
|
|
|
|
}
|
|
|
|
|
|
2023-05-13 10:47:47 +00:00
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
2023-06-15 15:24:29 +00:00
|
|
|
|
func GenRootfsImage(imageFile string, download bool) (rootfs string, err error) {
|
2024-02-20 13:25:31 +00:00
|
|
|
|
imagesPath := dotfiles.Dir("images")
|
2023-05-13 10:47:47 +00:00
|
|
|
|
|
2023-05-21 20:31:47 +00:00
|
|
|
|
rootfs = filepath.Join(imagesPath, imageFile)
|
2023-05-13 10:47:47 +00:00
|
|
|
|
if !fs.PathExists(rootfs) {
|
|
|
|
|
if download {
|
|
|
|
|
log.Info().Msgf("%v not available, start download", imageFile)
|
2023-06-15 15:24:29 +00:00
|
|
|
|
err = cache.DownloadRootFS(imagesPath, imageFile)
|
2023-05-13 10:47:47 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
2023-05-23 21:21:06 +00:00
|
|
|
|
func ShuffleStrings(a []string) []string {
|
2023-05-13 10:47:47 +00:00
|
|
|
|
// Fisher–Yates shuffle
|
|
|
|
|
for i := len(a) - 1; i > 0; i-- {
|
|
|
|
|
j := rand.Intn(i + 1)
|
|
|
|
|
a[i], a[j] = a[j], a[i]
|
|
|
|
|
}
|
|
|
|
|
return a
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func SetSigintHandler(variable *bool) {
|
|
|
|
|
c := make(chan os.Signal, 1)
|
|
|
|
|
signal.Notify(c, os.Interrupt)
|
|
|
|
|
go func() {
|
|
|
|
|
counter := 0
|
2024-02-20 13:25:31 +00:00
|
|
|
|
for range c {
|
2023-05-13 10:47:47 +00:00
|
|
|
|
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
|
|
|
|
|
}
|
|
|
|
|
}()
|
|
|
|
|
|
|
|
|
|
}
|