Refactor gRPC-based profile functions
Signed-off-by: Jonathon Anderson <janderson@ciq.com>
This commit is contained in:
@@ -4,12 +4,11 @@ import (
|
||||
"fmt"
|
||||
"strings"
|
||||
|
||||
apiprofile "github.com/warewulf/warewulf/internal/pkg/api/profile"
|
||||
"github.com/warewulf/warewulf/internal/pkg/node"
|
||||
"gopkg.in/yaml.v3"
|
||||
|
||||
"github.com/spf13/cobra"
|
||||
"github.com/warewulf/warewulf/internal/pkg/api/routes/wwapiv1"
|
||||
"github.com/warewulf/warewulf/internal/pkg/node"
|
||||
"github.com/warewulf/warewulf/internal/pkg/util"
|
||||
)
|
||||
|
||||
func CobraRunE(vars *variables) func(cmd *cobra.Command, args []string) (err error) {
|
||||
@@ -49,11 +48,23 @@ func CobraRunE(vars *variables) func(cmd *cobra.Command, args []string) (err err
|
||||
if err != nil {
|
||||
return fmt.Errorf("can not marshall nodeInfo: %w", err)
|
||||
}
|
||||
set := wwapiv1.NodeAddParameter{
|
||||
NodeConfYaml: string(buffer[:]),
|
||||
NodeNames: args,
|
||||
Force: true,
|
||||
nodeDB, err := node.New()
|
||||
if err != nil {
|
||||
return fmt.Errorf("could not open database: %w", err)
|
||||
}
|
||||
return apiprofile.ProfileAdd(&set)
|
||||
for _, p := range args {
|
||||
if util.InSlice(nodeDB.ListAllProfiles(), p) {
|
||||
return fmt.Errorf("profile with name %s already exists", p)
|
||||
}
|
||||
pNew, err := nodeDB.AddProfile(p)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
err = yaml.Unmarshal(buffer, pNew)
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to add profile: %w", err)
|
||||
}
|
||||
}
|
||||
return nodeDB.Persist()
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,13 +1,15 @@
|
||||
package list
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"sort"
|
||||
"strings"
|
||||
|
||||
"github.com/spf13/cobra"
|
||||
"gopkg.in/yaml.v3"
|
||||
|
||||
"github.com/warewulf/warewulf/internal/app/wwctl/table"
|
||||
apiprofile "github.com/warewulf/warewulf/internal/pkg/api/profile"
|
||||
"github.com/warewulf/warewulf/internal/pkg/api/routes/wwapiv1"
|
||||
"github.com/warewulf/warewulf/internal/pkg/node"
|
||||
"github.com/warewulf/warewulf/internal/pkg/wwlog"
|
||||
)
|
||||
|
||||
@@ -16,27 +18,49 @@ func CobraRunE(vars *variables) func(cmd *cobra.Command, args []string) (err err
|
||||
if len(args) > 0 && strings.Contains(args[0], ",") {
|
||||
args = strings.FieldsFunc(args[0], func(r rune) bool { return r == ',' })
|
||||
}
|
||||
req := wwapiv1.GetProfileList{
|
||||
ShowAll: vars.showAll,
|
||||
ShowYaml: vars.showYaml,
|
||||
ShowJson: vars.showJson,
|
||||
Profiles: args,
|
||||
}
|
||||
profileInfo, err := apiprofile.ProfileList(&req)
|
||||
|
||||
nodeDB, err := node.New()
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
if len(profileInfo.Output) > 0 {
|
||||
if vars.showYaml || vars.showJson {
|
||||
wwlog.Info(profileInfo.Output[0])
|
||||
} else {
|
||||
t := table.New(cmd.OutOrStdout())
|
||||
t.AddHeader(table.Prep(strings.Split(profileInfo.Output[0], ":=:"))...)
|
||||
for _, val := range profileInfo.Output[1:] {
|
||||
t.AddLine(table.Prep(strings.Split(val, ":=:"))...)
|
||||
}
|
||||
t.Print()
|
||||
profiles, err := nodeDB.FindAllProfiles()
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
profiles = node.FilterProfileListByName(profiles, args)
|
||||
sort.Slice(profiles, func(i, j int) bool {
|
||||
return profiles[i].Id() < profiles[j].Id()
|
||||
})
|
||||
|
||||
if vars.showYaml || vars.showJson {
|
||||
profileMap := make(map[string]node.Profile)
|
||||
for _, profile := range profiles {
|
||||
profileMap[profile.Id()] = profile
|
||||
}
|
||||
var buf []byte
|
||||
if vars.showJson {
|
||||
buf, _ = json.MarshalIndent(profileMap, "", " ")
|
||||
} else {
|
||||
buf, _ = yaml.Marshal(profileMap)
|
||||
}
|
||||
wwlog.Info(string(buf))
|
||||
} else if vars.showAll {
|
||||
t := table.New(cmd.OutOrStdout())
|
||||
t.AddHeader("PROFILE", "FIELD", "VALUE")
|
||||
for _, p := range profiles {
|
||||
fields := node.GetFieldList(p)
|
||||
for _, f := range fields {
|
||||
t.AddLine(table.Prep([]string{p.Id(), f.Field, f.Value})...)
|
||||
}
|
||||
}
|
||||
t.Print()
|
||||
} else {
|
||||
t := table.New(cmd.OutOrStdout())
|
||||
t.AddHeader("PROFILE NAME", "COMMENT/DESCRIPTION")
|
||||
for _, profile := range profiles {
|
||||
t.AddLine(table.Prep([]string{profile.Id(), profile.Comment})...)
|
||||
}
|
||||
t.Print()
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
@@ -5,12 +5,10 @@ import (
|
||||
"strings"
|
||||
|
||||
"github.com/spf13/cobra"
|
||||
apiprofile "github.com/warewulf/warewulf/internal/pkg/api/profile"
|
||||
"github.com/warewulf/warewulf/internal/pkg/api/routes/wwapiv1"
|
||||
"github.com/warewulf/warewulf/internal/pkg/node"
|
||||
"github.com/warewulf/warewulf/internal/pkg/util"
|
||||
"github.com/warewulf/warewulf/internal/pkg/warewulfd"
|
||||
"github.com/warewulf/warewulf/internal/pkg/wwlog"
|
||||
"gopkg.in/yaml.v3"
|
||||
)
|
||||
|
||||
func CobraRunE(vars *variables) func(cmd *cobra.Command, args []string) (err error) {
|
||||
@@ -48,42 +46,106 @@ func CobraRunE(vars *variables) func(cmd *cobra.Command, args []string) (err err
|
||||
}
|
||||
delete(vars.profileConf.Disks, "UNDEF")
|
||||
vars.profileConf.Ipmi.Tags = vars.profileAdd.IpmiTagsAdd
|
||||
buffer, err := yaml.Marshal(vars.profileConf)
|
||||
if err != nil {
|
||||
return fmt.Errorf("can not marshall nodeInfo: %s", err)
|
||||
}
|
||||
wwlog.Debug("sending following values: %s", string(buffer))
|
||||
set := wwapiv1.ConfSetParameter{
|
||||
NodeConfYaml: string(buffer[:]),
|
||||
NetdevDelete: vars.profileDel.NetDel,
|
||||
PartitionDelete: vars.profileDel.PartDel,
|
||||
DiskDelete: vars.profileDel.DiskDel,
|
||||
FilesystemDelete: vars.profileDel.FsDel,
|
||||
TagAdd: vars.profileAdd.TagsAdd,
|
||||
TagDel: vars.profileDel.TagsDel,
|
||||
NetTagAdd: vars.profileAdd.NetTagsAdd,
|
||||
NetTagDel: vars.profileDel.NetTagsDel,
|
||||
IpmiTagAdd: vars.profileAdd.IpmiTagsAdd,
|
||||
IpmiTagDel: vars.profileDel.IpmiTagsDel,
|
||||
|
||||
AllConfs: vars.setNodeAll,
|
||||
Force: vars.setForce,
|
||||
ConfList: args,
|
||||
nodeDB, err := node.New()
|
||||
if err != nil {
|
||||
return fmt.Errorf("could not open configuration: %w", err)
|
||||
}
|
||||
|
||||
if len(args) == 0 {
|
||||
return fmt.Errorf("no profiles specified")
|
||||
} else if len(nodeDB.ListAllProfiles()) == 0 {
|
||||
wwlog.Warn("no nodes/profiles found")
|
||||
return nil
|
||||
}
|
||||
|
||||
changed := cmd.Flags().Changed
|
||||
var count uint
|
||||
for _, profileId := range args {
|
||||
wwlog.Verbose("evaluating profile: %s", profileId)
|
||||
profilePtr, err := nodeDB.GetProfilePtr(profileId)
|
||||
if err != nil {
|
||||
wwlog.Warn("invalid profile: %s", profileId)
|
||||
continue
|
||||
}
|
||||
profilePtr.UpdateFrom(&vars.profileConf, changed)
|
||||
if vars.profileDel.NetDel != "" {
|
||||
if _, ok := profilePtr.NetDevs[vars.profileDel.NetDel]; !ok {
|
||||
return fmt.Errorf("network device name doesn't exist: %s", vars.profileDel.NetDel)
|
||||
}
|
||||
wwlog.Verbose("Profile: %s, Deleting network device: %s", profileId, vars.profileDel.NetDel)
|
||||
delete(profilePtr.NetDevs, vars.profileDel.NetDel)
|
||||
}
|
||||
if vars.profileDel.PartDel != "" {
|
||||
for diskname, disk := range profilePtr.Disks {
|
||||
if _, ok := disk.Partitions[vars.profileDel.PartDel]; ok {
|
||||
wwlog.Verbose("Profile: %s, on disk %s, deleting partition: %s", profileId, diskname, vars.profileDel.PartDel)
|
||||
delete(disk.Partitions, vars.profileDel.PartDel)
|
||||
} else {
|
||||
return fmt.Errorf("partition doesn't exist: %s", vars.profileDel.PartDel)
|
||||
}
|
||||
}
|
||||
}
|
||||
if vars.profileDel.DiskDel != "" {
|
||||
if _, ok := profilePtr.Disks[vars.profileDel.DiskDel]; ok {
|
||||
wwlog.Verbose("Profile: %s, deleting disk: %s", profileId, vars.profileDel.DiskDel)
|
||||
delete(profilePtr.Disks, vars.profileDel.DiskDel)
|
||||
} else {
|
||||
return fmt.Errorf("disk doesn't exist: %s", vars.profileDel.DiskDel)
|
||||
}
|
||||
}
|
||||
if vars.profileDel.FsDel != "" {
|
||||
if _, ok := profilePtr.FileSystems[vars.profileDel.FsDel]; ok {
|
||||
wwlog.Verbose("Profile: %s, deleting filesystem: %s", profileId, vars.profileDel.FsDel)
|
||||
delete(profilePtr.FileSystems, vars.profileDel.FsDel)
|
||||
} else {
|
||||
return fmt.Errorf("filesystem doesn't exist: %s", vars.profileDel.FsDel)
|
||||
}
|
||||
}
|
||||
for _, key := range vars.profileDel.TagsDel {
|
||||
delete(profilePtr.Tags, key)
|
||||
}
|
||||
for key, val := range vars.profileAdd.TagsAdd {
|
||||
if profilePtr.Tags == nil {
|
||||
profilePtr.Tags = make(map[string]string)
|
||||
}
|
||||
profilePtr.Tags[key] = val
|
||||
}
|
||||
for key, val := range vars.profileAdd.IpmiTagsAdd {
|
||||
if profilePtr.Ipmi.Tags == nil {
|
||||
profilePtr.Ipmi.Tags = make(map[string]string)
|
||||
}
|
||||
profilePtr.Ipmi.Tags[key] = val
|
||||
}
|
||||
for _, key := range vars.profileDel.IpmiTagsDel {
|
||||
delete(profilePtr.Ipmi.Tags, key)
|
||||
}
|
||||
if netDev, ok := profilePtr.NetDevs[vars.profileAdd.Net]; ok {
|
||||
for _, key := range vars.profileDel.NetTagsDel {
|
||||
delete(netDev.Tags, key)
|
||||
}
|
||||
// Note: original API code used set.TagAdd here (likely a bug),
|
||||
// preserving existing behavior by using TagsAdd
|
||||
if len(vars.profileAdd.TagsAdd) > 0 && netDev.Tags == nil {
|
||||
netDev.Tags = make(map[string]string)
|
||||
}
|
||||
for key, val := range vars.profileAdd.TagsAdd {
|
||||
netDev.Tags[key] = val
|
||||
}
|
||||
}
|
||||
count++
|
||||
}
|
||||
|
||||
if !vars.setYes {
|
||||
var profileCount uint
|
||||
// The checks run twice in the prompt case.
|
||||
// Avoiding putting in a blocking prompt in an API.
|
||||
_, profileCount, err = apiprofile.ProfileSetParameterCheck(&set)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
yes := util.Confirm(fmt.Sprintf("Are you sure you want to modify %d profile(s)", profileCount))
|
||||
yes := util.Confirm(fmt.Sprintf("Are you sure you want to modify %d profile(s)", count))
|
||||
if !yes {
|
||||
return err
|
||||
return nil
|
||||
}
|
||||
}
|
||||
return apiprofile.ProfileSet(&set)
|
||||
|
||||
if err := nodeDB.Persist(); err != nil {
|
||||
return err
|
||||
}
|
||||
return warewulfd.DaemonReload()
|
||||
}
|
||||
}
|
||||
|
||||
@@ -8,7 +8,6 @@ import (
|
||||
)
|
||||
|
||||
type variables struct {
|
||||
setNodeAll bool
|
||||
setYes bool
|
||||
setForce bool
|
||||
profileConf node.Profile
|
||||
|
||||
Reference in New Issue
Block a user