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" "os"
"strings" "strings"
"github.com/AlexSSD7/linsk/nettap"
"github.com/AlexSSD7/linsk/utils" "github.com/AlexSSD7/linsk/utils"
"github.com/spf13/cobra" "github.com/spf13/cobra"
) )
@ -17,6 +18,32 @@ var cleanCmd = &cobra.Command{
Run: func(cmd *cobra.Command, args []string) { Run: func(cmd *cobra.Command, args []string) {
store := createStore() 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() rmPath := store.DataDirPath()
fmt.Fprintf(os.Stderr, "Will permanently remove '"+rmPath+"'. Proceed? (y/n) > ") fmt.Fprintf(os.Stderr, "Will permanently remove '"+rmPath+"'. Proceed? (y/n) > ")
@ -38,8 +65,6 @@ var cleanCmd = &cobra.Command{
os.Exit(1) os.Exit(1)
} }
// TODO: Clean network tap allocations, if any.
slog.Info("Deleted data directory", "path", rmPath) slog.Info("Deleted data directory", "path", rmPath)
}, },
} }

View file

@ -75,7 +75,7 @@ var runCmd = &cobra.Command{
slog.Info("Started the network share successfully", "type", "ftp") slog.Info("Started the network share successfully", "type", "ftp")
fmt.Fprintf(os.Stderr, "============================\n[Network File Share Config]\nThe network file share was started. Please use the credentials below to connect to the file server.\n\nType: "+strings.ToUpper(shareBackendFlag)+"\nURL: %v\nUsername: linsk\nPassword: %v\n===========================\n", shareURI, sharePWD) fmt.Fprintf(os.Stderr, "===========================\n[Network File Share Config]\nThe network file share was started. Please use the credentials below to connect to the file server.\n\nType: "+strings.ToUpper(shareBackendFlag)+"\nURL: %v\nUsername: linsk\nPassword: %v\n===========================\n", shareURI, sharePWD)
<-ctx.Done() <-ctx.Done()
return 0 return 0

View file

@ -19,6 +19,10 @@ import (
"github.com/pkg/errors" "github.com/pkg/errors"
) )
func Available() bool {
return true
}
type TapManager struct { type TapManager struct {
logger *slog.Logger logger *slog.Logger