From ff9dcdffa283e1a5df842a25aae7263a2eeaf28d Mon Sep 17 00:00:00 2001 From: AlexSSD7 Date: Thu, 31 Aug 2023 19:46:13 +0100 Subject: [PATCH] linsk clean: Prune net taps --- cmd/clean.go | 29 +++++++++++++++++++++++++++-- cmd/run.go | 2 +- nettap/nettap.go | 4 ++++ 3 files changed, 32 insertions(+), 3 deletions(-) diff --git a/cmd/clean.go b/cmd/clean.go index f61be4c..9132b06 100644 --- a/cmd/clean.go +++ b/cmd/clean.go @@ -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) }, } diff --git a/cmd/run.go b/cmd/run.go index 55ae31c..d406dc8 100644 --- a/cmd/run.go +++ b/cmd/run.go @@ -75,7 +75,7 @@ var runCmd = &cobra.Command{ 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() return 0 diff --git a/nettap/nettap.go b/nettap/nettap.go index 76812e8..7d7b698 100644 --- a/nettap/nettap.go +++ b/nettap/nettap.go @@ -19,6 +19,10 @@ import ( "github.com/pkg/errors" ) +func Available() bool { + return true +} + type TapManager struct { logger *slog.Logger