rebased for the use without NodeInfo

make fanout configureable via commanline

Signed-off-by: Christian Goll <cgoll@suse.com>
This commit is contained in:
Jonathon Anderson
2024-11-07 17:45:02 -07:00
parent 0490f4c9a5
commit 2e2a7d7de3
22 changed files with 304 additions and 250 deletions

View File

@@ -44,9 +44,7 @@ func CobraRunE(cmd *cobra.Command, args []string) error {
wwlog.Error("%s: No IPMI IP address", node.Id())
continue
}
var conf node.NodeConf
conf.GetFrom(n)
ipmiCmd := power.IPMI{IpmiConf: *conf.Ipmi}
ipmiCmd := power.IPMI{IpmiConf: *node.Ipmi}
err := ipmiCmd.Console()
if err != nil {
wwlog.Error("%s: Console problem", node.Id())

View File

@@ -12,75 +12,77 @@ import (
"github.com/warewulf/warewulf/internal/pkg/wwlog"
)
func CobraRunE(cmd *cobra.Command, args []string) error {
var returnErr error = nil
nodeDB, err := node.New()
if err != nil {
return fmt.Errorf("could not open node configuration: %s", err)
}
nodes, err := nodeDB.FindAllNodes()
if err != nil {
return fmt.Errorf("could not get node list: %s", err)
}
args = hostlist.Expand(args)
if len(args) > 0 {
nodes = node.FilterNodeListByName(nodes, args)
} else {
//nolint:errcheck
cmd.Usage()
os.Exit(1)
}
if len(nodes) == 0 {
wwlog.Info("No nodes found")
os.Exit(1)
}
batchpool := batch.New(50)
jobcount := len(nodes)
results := make(chan power.IPMI, jobcount)
for _, node := range nodes {
if node.Ipmi.Ipaddr.IsUnspecified() {
wwlog.Error("%s: No IPMI IP address", node.Id())
continue
}
var conf node.NodeConf
conf.GetFrom(n)
ipmiCmd := power.IPMI{IpmiConf: *conf.Ipmi}
fullFlag := full
batchpool.Submit(func() {
if fullFlag {
//nolint:errcheck
ipmiCmd.SensorList()
} else {
//nolint:errcheck
ipmiCmd.SDRList()
}
results <- ipmiCmd
})
}
batchpool.Run()
close(results)
for result := range results {
out, err := result.Result()
func CobraRunE(vars *variables) func(cmd *cobra.Command, args []string) (err error) {
return func(cmd *cobra.Command, args []string) error {
var returnErr error = nil
nodeDB, err := node.New()
if err != nil {
wwlog.Error("%s: %s", result.Ipaddr, out)
returnErr = err
continue
return fmt.Errorf("could not open node configuration: %s", err)
}
fmt.Printf("%s:\n%s\n", result.Ipaddr, out)
}
nodes, err := nodeDB.FindAllNodes()
if err != nil {
return fmt.Errorf("could not get node list: %s", err)
}
return returnErr
args = hostlist.Expand(args)
if len(args) > 0 {
nodes = node.FilterNodeListByName(nodes, args)
} else {
//nolint:errcheck
cmd.Usage()
os.Exit(1)
}
if len(nodes) == 0 {
wwlog.Info("No nodes found")
os.Exit(1)
}
batchpool := batch.New(50)
jobcount := len(nodes)
results := make(chan power.IPMI, jobcount)
for _, node := range nodes {
if node.Ipmi.Ipaddr.IsUnspecified() {
wwlog.Error("%s: No IPMI IP address", node.Id())
continue
}
ipmiCmd := power.IPMI{
IpmiConf: *node.Ipmi,
ShowOnly: vars.Showcmd,
}
batchpool.Submit(func() {
if vars.Full {
//nolint:errcheck
ipmiCmd.SensorList()
} else {
//nolint:errcheck
ipmiCmd.SDRList()
}
results <- ipmiCmd
})
}
batchpool.Run()
close(results)
for result := range results {
out, err := result.Result()
if err != nil {
wwlog.Error("%s: %s", result.Ipaddr, out)
returnErr = err
continue
}
wwlog.Info("%s:\n%s\n", result.Ipaddr, out)
}
return returnErr
}
}

View File

@@ -5,14 +5,22 @@ import (
"github.com/warewulf/warewulf/internal/pkg/node"
)
var (
powerCmd = &cobra.Command{
type variables struct {
Showcmd bool
Full bool
Fanout int
}
func GetCommand() *cobra.Command {
vars := variables{}
powerCmd := &cobra.Command{
DisableFlagsInUseLine: true,
Use: "sensors [OPTIONS] PATTERN",
Short: "Show node IPMI sensor information",
Long: "Show IPMI sensor information for nodes matching PATTERN.",
Args: cobra.MinimumNArgs(1),
RunE: CobraRunE,
RunE: CobraRunE(&vars),
ValidArgsFunction: func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
if len(args) != 0 {
return nil, cobra.ShellCompDirectiveNoFileComp
@@ -27,14 +35,8 @@ var (
return node_names, cobra.ShellCompDirectiveNoFileComp
},
}
full bool
)
func init() {
powerCmd.PersistentFlags().BoolVarP(&full, "full", "F", false, "show detailed output.")
}
// GetRootCommand returns the root cobra.Command for the application.
func GetCommand() *cobra.Command {
powerCmd.PersistentFlags().BoolVarP(&vars.Full, "full", "F", false, "show detailed output.")
powerCmd.PersistentFlags().BoolVarP(&vars.Showcmd, "show", "s", false, "only show command which will be executed")
powerCmd.PersistentFlags().IntVar(&vars.Fanout, "fanout", 50, "how many command should be executed in parallel")
return powerCmd
}

View File

@@ -0,0 +1,42 @@
package sensors
import (
"bytes"
"strings"
"testing"
"github.com/stretchr/testify/assert"
"github.com/warewulf/warewulf/internal/pkg/testenv"
"github.com/warewulf/warewulf/internal/pkg/warewulfd"
"github.com/warewulf/warewulf/internal/pkg/wwlog"
)
func Test_Power_Status(t *testing.T) {
env := testenv.New(t)
defer env.RemoveAll(t)
env.WriteFile(t, "etc/warewulf/nodes.conf", `WW_INTERNAL: 43
nodeprofiles:
default:
ipmi:
username: admin
password: admin
nodes:
n01:
profiles:
- default
ipmi:
ipaddr: 10.10.10.10`)
warewulfd.SetNoDaemon()
t.Run("ipmitool status test", func(t *testing.T) {
baseCmd := GetCommand()
buf := new(bytes.Buffer)
baseCmd.SetOut(buf)
baseCmd.SetErr(buf)
wwlog.SetLogWriter(buf)
baseCmd.SetArgs([]string{"--show", "n01"})
err := baseCmd.Execute()
assert.NoError(t, err)
assert.Equal(t, "10.10.10.10:\nipmitool -I lan -H 10.10.10.10 -p 623 -U admin -P admin -e ~ sdr list", strings.TrimSpace(buf.String()))
})
}

View File

@@ -16,46 +16,47 @@ func CobraRunE(vars *variables) func(cmd *cobra.Command, args []string) (err err
return func(cmd *cobra.Command, args []string) error {
var returnErr error = nil
nodeDB, err := node.New()
if err != nil {
return fmt.Errorf("could not open node configuration: %s", err)
}
nodeDB, err := node.New()
if err != nil {
return fmt.Errorf("could not open node configuration: %s", err)
}
nodes, err := nodeDB.FindAllNodes()
if err != nil {
return fmt.Errorf("could not get node list: %s", err)
}
nodes, err := nodeDB.FindAllNodes()
if err != nil {
return fmt.Errorf("could not get node list: %s", err)
}
if len(args) > 0 {
nodes = node.FilterByName(nodes, hostlist.Expand(args))
nodes = node.FilterNodeListByName(nodes, hostlist.Expand(args))
} else {
//nolint:errcheck
cmd.Usage()
os.Exit(1)
}
if len(nodes) == 0 {
return fmt.Errorf("no nodes found")
}
if len(nodes) == 0 {
return fmt.Errorf("no nodes found")
}
batchpool := batch.New(50)
batchpool := batch.New(vars.Fanout)
jobcount := len(nodes)
results := make(chan power.IPMI, jobcount)
for _, n := range nodes {
for _, node := range nodes {
if node.Ipmi.Ipaddr.IsUnspecified() {
wwlog.Error("%s: No IPMI IP address", node.Id())
continue
}
var conf node.NodeConf
conf.GetFrom(n)
ipmiCmd := power.IPMI{IpmiConf: *conf.Ipmi}
batchpool.Submit(func() {
//nolint:errcheck
ipmiCmd.PowerCycle()
results <- ipmiCmd
})
if node.Ipmi.Ipaddr.IsUnspecified() {
wwlog.Error("%s: No IPMI IP address", node.Id())
continue
}
ipmiCmd := power.IPMI{
IpmiConf: *node.Ipmi,
ShowOnly: vars.Showcmd,
}
batchpool.Submit(func() {
//nolint:errcheck
ipmiCmd.PowerCycle()
results <- ipmiCmd
})
}

View File

@@ -7,6 +7,7 @@ import (
type variables struct {
Showcmd bool
Fanout int
}
// GetRootCommand returns the root cobra.Command for the application.
@@ -27,11 +28,12 @@ func GetCommand() *cobra.Command {
nodes, _ := nodeDB.FindAllNodes()
var node_names []string
for _, node := range nodes {
node_names = append(node_names, node.Id.Get())
node_names = append(node_names, node.Id())
}
return node_names, cobra.ShellCompDirectiveNoFileComp
},
}
powerCmd.PersistentFlags().BoolVarP(&vars.Showcmd, "show", "s", false, "only show command which will be executed")
powerCmd.PersistentFlags().IntVar(&vars.Fanout, "fanout", 50, "how many command should be executed in parallel")
return powerCmd
}

View File

@@ -1,4 +1,4 @@
package powercycle
package cycle
import (
"bytes"

View File

@@ -16,46 +16,47 @@ func CobraRunE(vars *variables) func(cmd *cobra.Command, args []string) (err err
return func(cmd *cobra.Command, args []string) error {
var returnErr error = nil
nodeDB, err := node.New()
if err != nil {
return fmt.Errorf("could not open node configuration: %s", err)
}
nodeDB, err := node.New()
if err != nil {
return fmt.Errorf("could not open node configuration: %s", err)
}
nodes, err := nodeDB.FindAllNodes()
if err != nil {
return fmt.Errorf("could not get node list: %s", err)
}
nodes, err := nodeDB.FindAllNodes()
if err != nil {
return fmt.Errorf("could not get node list: %s", err)
}
if len(args) > 0 {
nodes = node.FilterByName(nodes, hostlist.Expand(args))
nodes = node.FilterNodeListByName(nodes, hostlist.Expand(args))
} else {
//nolint:errcheck
cmd.Usage()
os.Exit(1)
}
if len(nodes) == 0 {
return fmt.Errorf("no nodes found")
}
if len(nodes) == 0 {
return fmt.Errorf("no nodes found")
}
batchpool := batch.New(50)
batchpool := batch.New(vars.Fanout)
jobcount := len(nodes)
results := make(chan power.IPMI, jobcount)
for _, node := range nodes {
for _, node := range nodes {
if node.Ipmi.Ipaddr.IsUnspecified() {
wwlog.Error("%s: No IPMI IP address", node.Id())
continue
}
var conf node.NodeConf
conf.GetFrom(n)
ipmiCmd := power.IPMI{IpmiConf: *conf.Ipmi}
batchpool.Submit(func() {
//nolint:errcheck
ipmiCmd.PowerOff()
results <- ipmiCmd
})
if node.Ipmi.Ipaddr.IsUnspecified() {
wwlog.Error("%s: No IPMI IP address", node.Id())
continue
}
ipmiCmd := power.IPMI{
IpmiConf: *node.Ipmi,
ShowOnly: vars.Showcmd,
}
batchpool.Submit(func() {
//nolint:errcheck
ipmiCmd.PowerOff()
results <- ipmiCmd
})
}

View File

@@ -7,6 +7,7 @@ import (
type variables struct {
Showcmd bool
Fanout int
}
// GetRootCommand returns the root cobra.Command for the application.
@@ -33,6 +34,7 @@ func GetCommand() *cobra.Command {
},
}
powerCmd.PersistentFlags().BoolVarP(&vars.Showcmd, "show", "s", false, "only show command which will be executed")
powerCmd.PersistentFlags().IntVar(&vars.Fanout, "fanout", 50, "how many command should be executed in parallel")
return powerCmd
}

View File

@@ -1,4 +1,4 @@
package poweroff
package off
import (
"bytes"

View File

@@ -17,46 +17,47 @@ func CobraRunE(vars *variables) func(cmd *cobra.Command, args []string) (err err
var returnErr error = nil
nodeDB, err := node.New()
if err != nil {
return fmt.Errorf("could not open node configuration: %s", err)
}
nodeDB, err := node.New()
if err != nil {
return fmt.Errorf("could not open node configuration: %s", err)
}
nodes, err := nodeDB.FindAllNodes()
if err != nil {
return fmt.Errorf("could not get node list: %s", err)
}
nodes, err := nodeDB.FindAllNodes()
if err != nil {
return fmt.Errorf("could not get node list: %s", err)
}
if len(args) > 0 {
nodes = node.FilterByName(nodes, hostlist.Expand(args))
nodes = node.FilterNodeListByName(nodes, hostlist.Expand(args))
} else {
//nolint:errcheck
cmd.Usage()
os.Exit(1)
}
if len(nodes) == 0 {
return fmt.Errorf("no nodes found")
}
if len(nodes) == 0 {
return fmt.Errorf("no nodes found")
}
batchpool := batch.New(50)
batchpool := batch.New(vars.Fanout)
jobcount := len(nodes)
results := make(chan power.IPMI, jobcount)
for _, node := range nodes {
for _, node := range nodes {
if node.Ipmi.Ipaddr.IsUnspecified() {
wwlog.Error("%s: No IPMI IP address", node.Id())
continue
}
var conf node.NodeConf
conf.GetFrom(n)
ipmiCmd := power.IPMI{IpmiConf: *conf.Ipmi}
batchpool.Submit(func() {
//nolint:errcheck
ipmiCmd.PowerOn()
results <- ipmiCmd
})
if node.Ipmi.Ipaddr.IsUnspecified() {
wwlog.Error("%s: No IPMI IP address", node.Id())
continue
}
ipmiCmd := power.IPMI{
IpmiConf: *node.Ipmi,
ShowOnly: vars.Showcmd,
}
batchpool.Submit(func() {
//nolint:errcheck
ipmiCmd.PowerOn()
results <- ipmiCmd
})
}

View File

@@ -7,6 +7,7 @@ import (
type variables struct {
Showcmd bool
Fanout int
}
// GetRootCommand returns the root cobra.Command for the application.
@@ -32,6 +33,7 @@ func GetCommand() *cobra.Command {
},
}
powerCmd.PersistentFlags().BoolVarP(&vars.Showcmd, "show", "s", false, "only show command which will be executed")
powerCmd.PersistentFlags().IntVar(&vars.Fanout, "fanout", 50, "how many command should be executed in parallel")
return powerCmd
}

View File

@@ -1,4 +1,4 @@
package poweron
package on
import (
"bytes"

View File

@@ -17,53 +17,47 @@ func CobraRunE(vars *variables) func(cmd *cobra.Command, args []string) (err err
var returnErr error = nil
nodeDB, err := node.New()
if err != nil {
return fmt.Errorf("could not open node configuration: %s", err)
}
nodeDB, err := node.New()
if err != nil {
return fmt.Errorf("could not open node configuration: %s", err)
}
nodes, err := nodeDB.FindAllNodes()
if err != nil {
return fmt.Errorf("cloud not get nodeList: %s", err)
}
nodes, err := nodeDB.FindAllNodes()
if err != nil {
return fmt.Errorf("cloud not get nodeList: %s", err)
}
if len(args) > 0 {
nodes = node.FilterByName(nodes, hostlist.Expand(args))
nodes = node.FilterNodeListByName(nodes, hostlist.Expand(args))
} else {
//nolint:errcheck
cmd.Usage()
os.Exit(1)
}
if len(args) > 0 {
nodes = node.FilterNodeListByName(nodes, hostlist.Expand(args))
} else {
//nolint:errcheck
cmd.Usage()
os.Exit(1)
}
if len(nodes) == 0 {
return fmt.Errorf("no nodes found")
}
if len(nodes) == 0 {
return fmt.Errorf("no nodes found")
}
batchpool := batch.New(50)
batchpool := batch.New(vars.Fanout)
jobcount := len(nodes)
results := make(chan power.IPMI, jobcount)
for _, n := range nodes {
for _, node := range nodes {
if node.Ipmi.Ipaddr.IsUnspecified() {
wwlog.Error("%s: No IPMI IP address", node.Id())
continue
}
var conf node.NodeConf
conf.GetFrom(n)
ipmiCmd := power.IPMI{IpmiConf: *conf.Ipmi}
batchpool.Submit(func() {
//nolint:errcheck
ipmiCmd.PowerReset()
results <- ipmiCmd
})
if node.Ipmi.Ipaddr.IsUnspecified() {
wwlog.Error("%s: No IPMI IP address", node.Id())
continue
}
ipmiCmd := power.IPMI{
IpmiConf: *node.Ipmi,
ShowOnly: vars.Showcmd,
}
batchpool.Submit(func() {
//nolint:errcheck
ipmiCmd.PowerReset()
results <- ipmiCmd
})
}

View File

@@ -7,6 +7,7 @@ import (
type variables struct {
Showcmd bool
Fanout int
}
// GetRootCommand returns the root cobra.Command for the application.
@@ -33,6 +34,6 @@ func GetCommand() *cobra.Command {
},
}
powerCmd.PersistentFlags().BoolVarP(&vars.Showcmd, "show", "s", false, "only show command which will be executed")
powerCmd.PersistentFlags().IntVar(&vars.Fanout, "fanout", 50, "how many command should be executed in parallel")
return powerCmd
}

View File

@@ -1,4 +1,4 @@
package powerreset
package reset
import (
"bytes"

View File

@@ -17,46 +17,47 @@ func CobraRunE(vars *variables) func(cmd *cobra.Command, args []string) (err err
var returnErr error = nil
nodeDB, err := node.New()
if err != nil {
return fmt.Errorf("could not open node configuration: %s", err)
}
nodeDB, err := node.New()
if err != nil {
return fmt.Errorf("could not open node configuration: %s", err)
}
nodes, err := nodeDB.FindAllNodes()
if err != nil {
return fmt.Errorf("could not get nodeList: %s", err)
}
nodes, err := nodeDB.FindAllNodes()
if err != nil {
return fmt.Errorf("could not get nodeList: %s", err)
}
if len(args) > 0 {
nodes = node.FilterNodeListByName(nodes, hostlist.Expand(args))
} else {
//nolint:errcheck
cmd.Usage()
os.Exit(1)
}
if len(args) > 0 {
nodes = node.FilterNodeListByName(nodes, hostlist.Expand(args))
} else {
//nolint:errcheck
cmd.Usage()
os.Exit(1)
}
if len(nodes) == 0 {
return fmt.Errorf("no nodes found")
}
if len(nodes) == 0 {
return fmt.Errorf("no nodes found")
}
batchpool := batch.New(50)
batchpool := batch.New(vars.Fanout)
jobcount := len(nodes)
results := make(chan power.IPMI, jobcount)
for _, n := range nodes {
for _, node := range nodes {
if node.Ipmi.Ipaddr.IsUnspecified() {
wwlog.Error("%s: No IPMI IP address", node.Id())
continue
}
var conf node.NodeConf
conf.GetFrom(n)
ipmiCmd := power.IPMI{IpmiConf: *conf.Ipmi}
batchpool.Submit(func() {
//nolint:errcheck
ipmiCmd.PowerSoft()
results <- ipmiCmd
})
if node.Ipmi.Ipaddr.IsUnspecified() {
wwlog.Error("%s: No IPMI IP address", node.Id())
continue
}
ipmiCmd := power.IPMI{
IpmiConf: *node.Ipmi,
ShowOnly: vars.Showcmd,
}
batchpool.Submit(func() {
//nolint:errcheck
ipmiCmd.PowerSoft()
results <- ipmiCmd
})
}

View File

@@ -7,6 +7,7 @@ import (
type variables struct {
Showcmd bool
Fanout int
}
// GetRootCommand returns the root cobra.Command for the application.
@@ -16,7 +17,7 @@ func GetCommand() *cobra.Command {
DisableFlagsInUseLine: true,
Use: "soft",
Short: "Gracefully shuts down the given node(s)",
Long: "This command uses the operationg system to shut down the set of nodes specified by PATTERN.",
Long: "This command uses the operating system to shut down the set of nodes specified by PATTERN.",
RunE: CobraRunE(&vars),
ValidArgsFunction: func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
if len(args) != 0 {
@@ -33,5 +34,6 @@ func GetCommand() *cobra.Command {
},
}
powerCmd.PersistentFlags().BoolVarP(&vars.Showcmd, "show", "s", false, "only show command which will be executed")
powerCmd.PersistentFlags().IntVar(&vars.Fanout, "fanout", 50, "how many command should be executed in parallel")
return powerCmd
}

View File

@@ -1,4 +1,4 @@
package powersoft
package soft
import (
"bytes"

View File

@@ -27,35 +27,36 @@ func CobraRunE(vars *variables) func(cmd *cobra.Command, args []string) (err err
}
if len(args) > 0 {
nodes = node.FilterByName(nodes, hostlist.Expand(args))
nodes = node.FilterNodeListByName(nodes, hostlist.Expand(args))
} else {
//nolint:errcheck
cmd.Usage()
os.Exit(1)
}
if len(nodes) == 0 {
return fmt.Errorf("no nodes found")
}
if len(nodes) == 0 {
return fmt.Errorf("no nodes found")
}
batchpool := batch.New(50)
jobcount := len(nodes)
results := make(chan power.IPMI, jobcount)
for _, n := range nodes {
for _, node := range nodes {
if node.Ipmi.Ipaddr.IsUnspecified() {
wwlog.Error("%s: No IPMI IP address", node.Id())
continue
}
var conf node.NodeConf
conf.GetFrom(n)
ipmiCmd := power.IPMI{IpmiConf: *conf.Ipmi}
batchpool.Submit(func() {
//nolint:errcheck
ipmiCmd.PowerStatus()
results <- ipmiCmd
})
if node.Ipmi.Ipaddr.IsUnspecified() {
wwlog.Error("%s: No IPMI IP address", node.Id())
continue
}
ipmiCmd := power.IPMI{
IpmiConf: *node.Ipmi,
ShowOnly: vars.Showcmd,
}
batchpool.Submit(func() {
//nolint:errcheck
ipmiCmd.PowerStatus()
results <- ipmiCmd
})
}

View File

@@ -7,6 +7,7 @@ import (
type variables struct {
Showcmd bool
Fanout int
}
// GetRootCommand returns the root cobra.Command for the application.
@@ -33,5 +34,6 @@ func GetCommand() *cobra.Command {
},
}
powerCmd.PersistentFlags().BoolVarP(&vars.Showcmd, "show", "s", false, "only show command which will be executed")
powerCmd.PersistentFlags().IntVar(&vars.Fanout, "fanout", 50, "how many command should be executed in parallel")
return powerCmd
}

View File

@@ -1,4 +1,4 @@
package powerstatus
package status
import (
"bytes"