Merge pull request #55 from yqin/topic/kernel_delete

LGTM, thank you Yong!
This commit is contained in:
Gregory M. Kurtzer
2021-04-20 20:35:52 -07:00
committed by GitHub
4 changed files with 72 additions and 0 deletions

View File

@@ -0,0 +1,41 @@
package delete
import (
"fmt"
"os"
"github.com/hpcng/warewulf/internal/pkg/kernel"
"github.com/hpcng/warewulf/internal/pkg/node"
"github.com/hpcng/warewulf/internal/pkg/wwlog"
"github.com/spf13/cobra"
)
func CobraRunE(cmd *cobra.Command, args []string) error {
nodeDB, err := node.New()
if err != nil {
wwlog.Printf(wwlog.ERROR, "Could not open nodeDB: %s\n", err)
os.Exit(1)
}
nodes, _ := nodeDB.FindAllNodes()
ARG_LOOP:
for _, arg := range args {
for _, n := range nodes {
if n.KernelVersion.Get() == arg {
wwlog.Printf(wwlog.ERROR, "Kernel is configured for nodes, skipping: %s\n", arg)
continue ARG_LOOP
}
}
err := kernel.DeleteKernel(arg)
if err != nil {
wwlog.Printf(wwlog.ERROR, "Could not delete kernel: %s\n", arg)
} else {
fmt.Printf("Kernel has been deleted: %s\n", arg)
}
}
return nil
}

View File

@@ -0,0 +1,22 @@
package delete
import "github.com/spf13/cobra"
var (
baseCmd = &cobra.Command{
Use: "delete [flags] [kernel version]...",
Short: "Delete an imported kernel",
Long: "This command will delete a kernel that has been imported into Warewulf.",
RunE: CobraRunE,
Args: cobra.MinimumNArgs(1),
}
)
func init() {
}
// GetRootCommand returns the root cobra.Command for the application.
func GetCommand() *cobra.Command {
return baseCmd
}

View File

@@ -1,6 +1,7 @@
package kernel
import (
"github.com/hpcng/warewulf/internal/app/wwctl/kernel/delete"
"github.com/hpcng/warewulf/internal/app/wwctl/kernel/imprt"
"github.com/hpcng/warewulf/internal/app/wwctl/kernel/list"
"github.com/spf13/cobra"
@@ -18,6 +19,7 @@ var (
func init() {
baseCmd.AddCommand(imprt.GetCommand())
baseCmd.AddCommand(list.GetCommand())
baseCmd.AddCommand(delete.GetCommand())
}
// GetRootCommand returns the root cobra.Command for the application.

View File

@@ -108,3 +108,10 @@ func Build(kernelVersion string, root string) (string, error) {
return "Done", nil
}
func DeleteKernel(name string) error {
fullPath := path.Join(ParentDir(), name)
wwlog.Printf(wwlog.VERBOSE, "Removing path: %s\n", fullPath)
return os.RemoveAll(fullPath)
}