Added basic profile handling CLI

This commit is contained in:
Gregory Kurtzer
2020-11-27 16:37:56 -08:00
parent 38ce925639
commit 41dd2e3a59
10 changed files with 445 additions and 0 deletions

View File

@@ -0,0 +1,85 @@
package delete
import (
"fmt"
"github.com/hpcng/warewulf/internal/pkg/node"
"github.com/hpcng/warewulf/internal/pkg/util"
"github.com/hpcng/warewulf/internal/pkg/wwlog"
"github.com/manifoldco/promptui"
"github.com/spf13/cobra"
"os"
)
func CobraRunE(cmd *cobra.Command, args []string) error {
var count int
var numNodes int
var numGroups int
nodeDB, err := node.New()
if err != nil {
wwlog.Printf(wwlog.ERROR, "Failed to open node database: %s\n", err)
os.Exit(1)
}
nodes, err := nodeDB.FindAllNodes()
if err != nil {
wwlog.Printf(wwlog.ERROR, "Could not load all nodes: %s\n", err)
os.Exit(1)
}
groups, err := nodeDB.FindAllGroups()
if err != nil {
wwlog.Printf(wwlog.ERROR, "Could not load all groups: %s\n", err)
os.Exit(1)
}
for _, p := range args {
err := nodeDB.DelProfile(p)
if err != nil {
wwlog.Printf(wwlog.ERROR, "%s\n", err)
continue
}
for _, n := range nodes {
for _, np := range n.Profiles {
if np == p {
numNodes++
wwlog.Printf(wwlog.VERBOSE, "Removing profile from node %s: %s\n", n.Fqdn.Get(), p)
n.Profiles = util.SliceRemoveElement(n.Profiles, p)
nodeDB.NodeUpdate(n)
}
}
}
for _, g := range groups {
for _, np := range g.Profiles {
if np == p {
numGroups++
wwlog.Printf(wwlog.VERBOSE, "Removing profile from group %s: %s\n", g.Id, p)
g.Profiles = util.SliceRemoveElement(g.Profiles, p)
nodeDB.GroupUpdate(g)
}
}
}
count++
}
if count > 0 {
q := fmt.Sprintf("Are you sure you want to delete %d profile(s) (%d groups, %d nodes)", count, numGroups, numNodes)
prompt := promptui.Prompt{
Label: q,
IsConfirm: true,
}
result, _ := prompt.Run()
if result == "y" || result == "yes" {
nodeDB.Persist()
}
} else {
wwlog.Printf(wwlog.INFO, "No groups found\n")
}
return nil
}

View File

@@ -0,0 +1,20 @@
package delete
import "github.com/spf13/cobra"
var (
baseCmd = &cobra.Command{
Use: "delete",
Short: "Delete profiles",
Long: "Profile configurations ",
RunE: CobraRunE,
}
)
func init() {
}
// GetRootCommand returns the root cobra.Command for the application.
func GetCommand() *cobra.Command {
return baseCmd
}