linsk clean: Prune net taps

This commit is contained in:
AlexSSD7 2023-08-31 19:46:13 +01:00
commit ff9dcdffa2
3 changed files with 32 additions and 3 deletions

View file

@ -7,6 +7,7 @@ import (
"os"
"strings"
"github.com/AlexSSD7/linsk/nettap"
"github.com/AlexSSD7/linsk/utils"
"github.com/spf13/cobra"
)
@ -17,6 +18,32 @@ var cleanCmd = &cobra.Command{
Run: func(cmd *cobra.Command, args []string) {
store := createStore()
if nettap.Available() {
tm, err := nettap.NewTapManager(slog.With("caller", "nettap-manager"))
if err != nil {
slog.Error("Failed to create network tap manager, will not attempt to remove dangling tap interfaces", "error", err.Error())
} else {
tapAllocs, err := store.ListNetTapAllocations()
if err != nil {
slog.Error("Failed to list net tap allocations, will not attempt to remove dangling tap interfaces", "error", err.Error())
} else {
removed, err := tm.PruneTaps(tapAllocs)
if err != nil {
slog.Error("Failed to prune dangling network taps", "error", err.Error())
} else if len(removed) > 0 {
slog.Info("Removed dangling network taps", "count", len(removed))
}
for _, removedTapName := range removed {
err = store.ReleaseNetTapAllocation(removedTapName)
if err != nil {
slog.Error("Failed to release removed network tap allocation", "error", err.Error(), "name", removedTapName)
}
}
}
}
}
rmPath := store.DataDirPath()
fmt.Fprintf(os.Stderr, "Will permanently remove '"+rmPath+"'. Proceed? (y/n) > ")
@ -38,8 +65,6 @@ var cleanCmd = &cobra.Command{
os.Exit(1)
}
// TODO: Clean network tap allocations, if any.
slog.Info("Deleted data directory", "path", rmPath)
},
}