Clean cmd + VM image pruning
This commit is contained in:
parent
6529ba690a
commit
8666a21d0b
4 changed files with 97 additions and 8 deletions
41
cmd/clean.go
Normal file
41
cmd/clean.go
Normal file
|
|
@ -0,0 +1,41 @@
|
||||||
|
package cmd
|
||||||
|
|
||||||
|
import (
|
||||||
|
"bufio"
|
||||||
|
"fmt"
|
||||||
|
"log/slog"
|
||||||
|
"os"
|
||||||
|
"strings"
|
||||||
|
|
||||||
|
"github.com/spf13/cobra"
|
||||||
|
)
|
||||||
|
|
||||||
|
var cleanCmd = &cobra.Command{
|
||||||
|
Use: "clean",
|
||||||
|
Short: "Remove all downloaded VM images.",
|
||||||
|
Run: func(cmd *cobra.Command, args []string) {
|
||||||
|
store := createStore()
|
||||||
|
|
||||||
|
fmt.Fprintf(os.Stderr, "Will delete all VM images in the data directory. Proceed? (y/n) > ")
|
||||||
|
|
||||||
|
reader := bufio.NewReader(os.Stdin)
|
||||||
|
answer, err := reader.ReadBytes('\n')
|
||||||
|
if err != nil {
|
||||||
|
slog.Error("Failed to read answer", "error", err.Error())
|
||||||
|
os.Exit(1)
|
||||||
|
}
|
||||||
|
|
||||||
|
if strings.ToLower(string(answer)) != "y\n" {
|
||||||
|
fmt.Fprintf(os.Stderr, "Aborted.\n")
|
||||||
|
os.Exit(2)
|
||||||
|
}
|
||||||
|
|
||||||
|
deleted, err := store.CleanImages(false)
|
||||||
|
if err != nil {
|
||||||
|
slog.Error("Failed to clean images", "error", err.Error())
|
||||||
|
os.Exit(1)
|
||||||
|
}
|
||||||
|
|
||||||
|
slog.Info("Successful VM image cleanup", "deleted", deleted)
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
@ -41,6 +41,7 @@ func init() {
|
||||||
rootCmd.AddCommand(lsCmd)
|
rootCmd.AddCommand(lsCmd)
|
||||||
rootCmd.AddCommand(runCmd)
|
rootCmd.AddCommand(runCmd)
|
||||||
rootCmd.AddCommand(shellCmd)
|
rootCmd.AddCommand(shellCmd)
|
||||||
|
rootCmd.AddCommand(cleanCmd)
|
||||||
|
|
||||||
rootCmd.PersistentFlags().BoolVar(&vmDebugFlag, "vmdebug", false, "Enables the VM debug mode. This will open an accessible VM monitor. You can log in with root user and no password.")
|
rootCmd.PersistentFlags().BoolVar(&vmDebugFlag, "vmdebug", false, "Enables the VM debug mode. This will open an accessible VM monitor. You can log in with root user and no password.")
|
||||||
rootCmd.PersistentFlags().BoolVar(&unrestrictedNetworkingFlag, "unrestricted-networking", false, "Enables unrestricted networking. This will allow the VM to connect to the internet.")
|
rootCmd.PersistentFlags().BoolVar(&unrestrictedNetworkingFlag, "unrestricted-networking", false, "Enables unrestricted networking. This will allow the VM to connect to the internet.")
|
||||||
|
|
|
||||||
10
cmd/utils.go
10
cmd/utils.go
|
|
@ -54,14 +54,19 @@ func doUSBRootCheck() {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func runVM(passthroughArg string, fn func(context.Context, *vm.VM, *vm.FileManager) int, forwardPortsRules []vm.PortForwardingRule, unrestrictedNetworking bool) int {
|
func createStore() *storage.Storage {
|
||||||
store, err := storage.NewStorage(slog.With("caller", "storage"), dataDirFlag)
|
store, err := storage.NewStorage(slog.With("caller", "storage"), dataDirFlag)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
slog.Error("Failed to create Linsk data storage", "error", err.Error(), "data-dir", dataDirFlag)
|
slog.Error("Failed to create Linsk data storage", "error", err.Error(), "data-dir", dataDirFlag)
|
||||||
os.Exit(1)
|
os.Exit(1)
|
||||||
}
|
}
|
||||||
|
|
||||||
_, err = store.ValidateImageHashOrDownload()
|
return store
|
||||||
|
}
|
||||||
|
|
||||||
|
func runVM(passthroughArg string, fn func(context.Context, *vm.VM, *vm.FileManager) int, forwardPortsRules []vm.PortForwardingRule, unrestrictedNetworking bool) int {
|
||||||
|
store := createStore()
|
||||||
|
_, err := store.ValidateImageHashOrDownload()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
slog.Error("Failed to validate image hash or download image", "error", err.Error())
|
slog.Error("Failed to validate image hash or download image", "error", err.Error())
|
||||||
os.Exit(1)
|
os.Exit(1)
|
||||||
|
|
@ -92,7 +97,6 @@ func runVM(passthroughArg string, fn func(context.Context, *vm.VM, *vm.FileManag
|
||||||
ShowDisplay: vmDebugFlag,
|
ShowDisplay: vmDebugFlag,
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO: Alpine image should be downloaded from somewhere.
|
|
||||||
vi, err := vm.NewVM(slog.Default().With("caller", "vm"), vmCfg)
|
vi, err := vm.NewVM(slog.Default().With("caller", "vm"), vmCfg)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
slog.Error("Failed to create vm instance", "error", err.Error())
|
slog.Error("Failed to create vm instance", "error", err.Error())
|
||||||
|
|
|
||||||
|
|
@ -11,6 +11,7 @@ import (
|
||||||
"net/http"
|
"net/http"
|
||||||
"os"
|
"os"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
|
"strings"
|
||||||
|
|
||||||
"github.com/dustin/go-humanize"
|
"github.com/dustin/go-humanize"
|
||||||
"github.com/pkg/errors"
|
"github.com/pkg/errors"
|
||||||
|
|
@ -41,6 +42,35 @@ func NewStorage(logger *slog.Logger, dataDir string) (*Storage, error) {
|
||||||
}, nil
|
}, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (s *Storage) CleanImages(retainLatest bool) (int, error) {
|
||||||
|
dirEntries, err := os.ReadDir(s.path)
|
||||||
|
if err != nil {
|
||||||
|
return 0, errors.Wrap(err, "read dir")
|
||||||
|
}
|
||||||
|
|
||||||
|
localImagePath := s.GetLocalImagePath()
|
||||||
|
|
||||||
|
var deleted int
|
||||||
|
|
||||||
|
for _, entry := range dirEntries {
|
||||||
|
fullPath := filepath.Join(s.path, entry.Name())
|
||||||
|
if !strings.HasSuffix(fullPath, ".qcow2") {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
if !(retainLatest && fullPath == localImagePath) {
|
||||||
|
s.logger.Warn("Removing VM image", "path", fullPath)
|
||||||
|
err = os.Remove(fullPath)
|
||||||
|
if err != nil {
|
||||||
|
return deleted, errors.Wrapf(err, "remove vm image (path '%v')", fullPath)
|
||||||
|
}
|
||||||
|
deleted++
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return deleted, nil
|
||||||
|
}
|
||||||
|
|
||||||
func (s *Storage) GetLocalImagePath() string {
|
func (s *Storage) GetLocalImagePath() string {
|
||||||
return filepath.Join(s.path, hex.EncodeToString(imageHash[:])+".qcow2")
|
return filepath.Join(s.path, hex.EncodeToString(imageHash[:])+".qcow2")
|
||||||
}
|
}
|
||||||
|
|
@ -143,16 +173,29 @@ func (s *Storage) ValidateImageHash() error {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *Storage) ValidateImageHashOrDownload() (bool, error) {
|
func (s *Storage) ValidateImageHashOrDownload() (bool, error) {
|
||||||
|
var downloaded bool
|
||||||
err := s.ValidateImageHash()
|
err := s.ValidateImageHash()
|
||||||
if err == nil {
|
if err != nil {
|
||||||
return false, nil
|
if errors.Is(err, os.ErrNotExist) {
|
||||||
|
err = s.DownloadImage()
|
||||||
|
if err != nil {
|
||||||
|
return false, errors.Wrap(err, "download iamge")
|
||||||
|
}
|
||||||
|
|
||||||
|
downloaded = true
|
||||||
|
} else {
|
||||||
|
return false, errors.Wrap(err, "validate image hash")
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if errors.Is(err, os.ErrNotExist) {
|
deletedImagesCount, err := s.CleanImages(true)
|
||||||
return true, errors.Wrap(s.DownloadImage(), "download image")
|
if err != nil {
|
||||||
|
s.logger.Warn("Failed to prune old VM images", "error", err, "deleted", deletedImagesCount)
|
||||||
|
} else {
|
||||||
|
s.logger.Info("Pruned old VM images", "deleted", deletedImagesCount)
|
||||||
}
|
}
|
||||||
|
|
||||||
return false, err
|
return downloaded, err
|
||||||
}
|
}
|
||||||
|
|
||||||
func copyWithProgress(dst io.Writer, src io.Reader, blockSize int, length int64, report func(int, float64)) (int, error) {
|
func copyWithProgress(dst io.Writer, src io.Reader, blockSize int, length int64, report func(int, float64)) (int, error) {
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue