Merge branch 'development' into container-info2

This commit is contained in:
Christian Goll
2022-10-13 10:43:50 +02:00
73 changed files with 4237 additions and 420 deletions

View File

@@ -24,7 +24,12 @@ func CobraRunE(cmd *cobra.Command, args []string) (err error) {
if !ShowAll {
fmt.Printf("%s\n", r.Rootfs)
} else {
kernelVersion := r.KernelVersion
if kernelVersion == "" {
kernelVersion = "not found"
}
fmt.Printf("Name: %s\n", r.Name)
fmt.Printf("KernelVersion: %s\n", kernelVersion)
fmt.Printf("Rootfs: %s\n", r.Rootfs)
fmt.Printf("Nr nodes: %d\n", len(r.Nodes))
fmt.Printf("Nodes: %s\n", r.Nodes)

View File

@@ -0,0 +1,115 @@
package edit
import (
"crypto/sha256"
"encoding/hex"
"fmt"
"io"
"io/ioutil"
"os"
"strings"
apinode "github.com/hpcng/warewulf/internal/pkg/api/node"
"github.com/hpcng/warewulf/internal/pkg/api/routes/wwapiv1"
apiutil "github.com/hpcng/warewulf/internal/pkg/api/util"
"github.com/hpcng/warewulf/internal/pkg/node"
"github.com/hpcng/warewulf/internal/pkg/util"
"github.com/hpcng/warewulf/internal/pkg/wwlog"
"github.com/spf13/cobra"
"gopkg.in/yaml.v2"
)
func CobraRunE(cmd *cobra.Command, args []string) error {
canWrite := apiutil.CanWriteConfig()
if !canWrite.CanWriteConfig {
wwlog.Error("Can't write to config exiting")
os.Exit(1)
}
editor := os.Getenv("EDITOR")
if editor == "" {
editor = "/bin/vi"
}
if len(args) == 0 {
args = append(args, ".*")
}
filterList := wwapiv1.NodeList{
Output: args,
}
nodeListMsg := apinode.FilteredNodes(&filterList)
nodeMap := make(map[string]*node.NodeConf)
// got proper yaml back
_ = yaml.Unmarshal([]byte(nodeListMsg.NodeConfMapYaml), nodeMap)
file, err := ioutil.TempFile("/tmp", "ww4NodeEdit*.yaml")
if err != nil {
wwlog.Error("Could not create temp file:%s \n", err)
}
defer os.Remove(file.Name())
nodeConf := node.NewConf()
yamlTemplate := nodeConf.UnmarshalConf([]string{"tagsdel", "default", "profiles"})
for {
_ = file.Truncate(0)
_, _ = file.Seek(0, 0)
if !NoHeader {
_, _ = file.WriteString("#nodename:\n# " + strings.Join(yamlTemplate, "\n# ") + "\n")
}
_, _ = file.WriteString(nodeListMsg.NodeConfMapYaml)
_, _ = file.Seek(0, 0)
hasher := sha256.New()
if _, err := io.Copy(hasher, file); err != nil {
wwlog.Error("Problems getting checksum of file %s\n", err)
}
sum1 := hex.EncodeToString(hasher.Sum(nil))
err = util.ExecInteractive(editor, file.Name())
if err != nil {
wwlog.Error("Editor process existed with non-zero\n")
os.Exit(1)
}
_, _ = file.Seek(0, 0)
hasher.Reset()
if _, err := io.Copy(hasher, file); err != nil {
wwlog.Error("Problems getting checksum of file %s\n", err)
}
sum2 := hex.EncodeToString(hasher.Sum(nil))
wwlog.Debug("Hashes are before %s and after %s\n", sum1, sum2)
if sum1 != sum2 {
wwlog.Debug("Nodes were modified")
modifiedNodeMap := make(map[string]*node.NodeConf)
_, _ = file.Seek(0, 0)
// ignore error as only may occurs under strange circumstances
buffer, _ := io.ReadAll(file)
err = yaml.Unmarshal(buffer, modifiedNodeMap)
if err == nil {
nodeList := make([]string, len(nodeMap))
i := 0
for key := range nodeMap {
nodeList[i] = key
i++
}
yes := apiutil.ConfirmationPrompt(fmt.Sprintf("Are you sure you want to modify %d nodes", len(modifiedNodeMap)))
if !yes {
break
}
err = apinode.NodeDelete(&wwapiv1.NodeDeleteParameter{NodeNames: nodeList, Force: true})
if err != nil {
wwlog.Verbose("Problem deleting nodes before modification %s")
}
buffer, _ = yaml.Marshal(modifiedNodeMap)
err = apinode.NodeAddFromYaml(&wwapiv1.NodeYaml{NodeConfMapYaml: string(buffer)})
if err != nil {
wwlog.Error("Got following problem when writing back yaml: %s", err)
os.Exit(1)
}
break
} else {
yes := apiutil.ConfirmationPrompt(fmt.Sprintf("Got following error on parsing: %s, Retry", err))
if !yes {
break
}
}
} else {
break
}
}
return nil
}

View File

@@ -0,0 +1,39 @@
package edit
import (
"github.com/hpcng/warewulf/internal/pkg/node"
"github.com/spf13/cobra"
)
var (
baseCmd = &cobra.Command{
DisableFlagsInUseLine: true,
Use: "edit [OPTIONS] NODENAME",
Short: "Edit node(s) with editor",
Long: "This command opens an editor for the given nodes.",
RunE: CobraRunE,
ValidArgsFunction: func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
if len(args) != 0 {
return nil, cobra.ShellCompDirectiveNoFileComp
}
nodeDB, _ := node.New()
nodes, _ := nodeDB.FindAllNodes()
var node_names []string
for _, node := range nodes {
node_names = append(node_names, node.Id.Get())
}
return node_names, cobra.ShellCompDirectiveNoFileComp
},
}
NoHeader bool
)
func init() {
baseCmd.PersistentFlags().BoolVar(&NoHeader, "noheader", false, "Do not print header")
}
// GetRootCommand returns the root cobra.Command for the application.
func GetCommand() *cobra.Command {
return baseCmd
}

View File

@@ -0,0 +1,26 @@
package export
import (
"fmt"
apinode "github.com/hpcng/warewulf/internal/pkg/api/node"
"github.com/hpcng/warewulf/internal/pkg/api/routes/wwapiv1"
"github.com/spf13/cobra"
)
func CobraRunE(cmd *cobra.Command, args []string) error {
if len(args) == 0 {
args = append(args, ".*")
}
filterList := wwapiv1.NodeList{
Output: args,
}
nodeListMsg := apinode.FilteredNodes(&filterList)
/*
nodeMap := make(map[string]*node.NodeConf)
// got proper yaml back
_ = yaml.Unmarshal([]byte(nodeListMsg.NodeConfMapYaml), nodeMap)
*/
fmt.Println(nodeListMsg.NodeConfMapYaml)
return nil
}

View File

@@ -0,0 +1,38 @@
package export
import (
"github.com/hpcng/warewulf/internal/pkg/node"
"github.com/spf13/cobra"
)
var (
baseCmd = &cobra.Command{
DisableFlagsInUseLine: true,
Use: "export NODENAME",
Short: "Export nodes as yaml to stdout",
Long: "This command exports the given nodes as yaml to stdout.",
RunE: CobraRunE,
ValidArgsFunction: func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
if len(args) != 0 {
return nil, cobra.ShellCompDirectiveNoFileComp
}
nodeDB, _ := node.New()
nodes, _ := nodeDB.FindAllNodes()
var node_names []string
for _, node := range nodes {
node_names = append(node_names, node.Id.Get())
}
return node_names, cobra.ShellCompDirectiveNoFileComp
},
}
NoHeader bool
)
func init() {
}
// GetRootCommand returns the root cobra.Command for the application.
func GetCommand() *cobra.Command {
return baseCmd
}

View File

@@ -0,0 +1,98 @@
package imprt
import (
"bytes"
"encoding/csv"
"fmt"
"io"
"os"
apinode "github.com/hpcng/warewulf/internal/pkg/api/node"
"github.com/hpcng/warewulf/internal/pkg/api/routes/wwapiv1"
apiutil "github.com/hpcng/warewulf/internal/pkg/api/util"
"github.com/hpcng/warewulf/internal/pkg/node"
"github.com/hpcng/warewulf/internal/pkg/wwlog"
"github.com/spf13/cobra"
"gopkg.in/yaml.v2"
)
func CobraRunE(cmd *cobra.Command, args []string) error {
file, err := os.Open(args[0])
if err != nil {
wwlog.Error("Could not open file:%s \n", err)
os.Exit(1)
}
defer file.Close()
importMap := make(map[string]*node.NodeConf)
buffer, err := io.ReadAll(file)
if err != nil {
wwlog.Error("Could not read:%s\n", err)
os.Exit(1)
}
if !ImportCVS {
err = yaml.Unmarshal(buffer, importMap)
if err == nil {
yes := apiutil.ConfirmationPrompt(fmt.Sprintf("Are you sure you want to modify %d nodes", len(importMap)))
if yes {
err = apinode.NodeAddFromYaml(&wwapiv1.NodeYaml{NodeConfMapYaml: string(buffer)})
if err != nil {
wwlog.Error("Got following problem when writing back yaml: %s", err)
os.Exit(1)
}
}
} else {
wwlog.Error("Could not parse import file")
}
} else {
// reading from buffer is a bit overshot
csvReader := csv.NewReader(bytes.NewReader(buffer))
records, err := csvReader.ReadAll()
if err != nil {
wwlog.Error("Could not parse %s: %s\n", args[0], err)
os.Exit(1)
}
if len(records) < 1 || len(records[0]) < 1 {
wwlog.Error("Did not find any data in %s\n", args[0])
os.Exit(1)
}
if !(records[0][0] == "node" || records[0][0] == "nodename") {
Usage()
os.Exit(1)
}
argsLen := len(records[0])
for i, line := range records[1:] {
if len(line) != argsLen {
wwlog.Error("Wrong number of fields in lube %u\n", i+1)
os.Exit(1)
}
for j := range line {
if j == 0 {
continue
}
if importMap[line[0]] == nil {
importMap[line[0]] = new(node.NodeConf)
}
ok := importMap[line[0]].SetLopt(records[0][j], line[j])
if !(ok) {
wwlog.Debug("Could not import %s\n", line[j])
}
}
}
yes := apiutil.ConfirmationPrompt(fmt.Sprintf("Are you sure you want to import %d nodes", len(importMap)))
if yes {
// create second buffer an marshall nodeMap to it
buffer, err = yaml.Marshal(importMap)
if err != nil {
wwlog.Error("Got following problem when creating yaml: %s", err)
}
err = apinode.NodeAddFromYaml(&wwapiv1.NodeYaml{NodeConfMapYaml: string(buffer)})
if err != nil {
wwlog.Error("Got following problem when writing back yaml: %s", err)
os.Exit(1)
}
}
}
return nil
}

View File

@@ -0,0 +1,35 @@
package imprt
import (
"fmt"
"github.com/spf13/cobra"
)
var (
baseCmd = &cobra.Command{
DisableFlagsInUseLine: true,
Use: "import [OPTIONS] NODENAME",
Short: "Import node(s) from yaml file",
Long: "This command imports all the nodes defined in a file. It will overwrite nodes with same name.",
RunE: CobraRunE,
Args: cobra.MinimumNArgs(1),
Aliases: []string{"import"},
}
ImportCVS bool
)
func init() {
baseCmd.PersistentFlags().BoolVarP(&ImportCVS, "cvs", "c", false, "Import CVS file")
}
// GetRootCommand returns the root cobra.Command for the application.
func GetCommand() *cobra.Command {
return baseCmd
}
func Usage() {
fmt.Println(`The csv file must be structured in following way:
node,option1,option2,net.netname1.netopt
node01,value1,value2,net.netname1,netvalue`)
}

View File

@@ -4,6 +4,9 @@ import (
"github.com/hpcng/warewulf/internal/app/wwctl/node/add"
"github.com/hpcng/warewulf/internal/app/wwctl/node/console"
"github.com/hpcng/warewulf/internal/app/wwctl/node/delete"
"github.com/hpcng/warewulf/internal/app/wwctl/node/edit"
"github.com/hpcng/warewulf/internal/app/wwctl/node/export"
"github.com/hpcng/warewulf/internal/app/wwctl/node/imprt"
"github.com/hpcng/warewulf/internal/app/wwctl/node/list"
"github.com/hpcng/warewulf/internal/app/wwctl/node/sensors"
"github.com/hpcng/warewulf/internal/app/wwctl/node/set"
@@ -30,6 +33,9 @@ func init() {
baseCmd.AddCommand(delete.GetCommand())
baseCmd.AddCommand(console.GetCommand())
baseCmd.AddCommand(nodestatus.GetCommand())
baseCmd.AddCommand(edit.GetCommand())
baseCmd.AddCommand(imprt.GetCommand())
baseCmd.AddCommand(export.GetCommand())
}
// GetRootCommand returns the root cobra.Command for the application.

View File

@@ -46,7 +46,15 @@ func CobraRunE(cmd *cobra.Command, args []string) error {
fmt.Print(string(f))
} else {
var host node.NodeInfo
if !util.IsFile(overlayFile) {
wwlog.Debug("%s is not a file\n", overlayFile)
wwlog.Error("%s:%s is not a file\n", overlayName, fileName)
os.Exit(1)
}
if filepath.Ext(overlayFile) != ".ww" {
wwlog.Warn("%s lacks the '.ww' suffix, will not be rendered in an overlay\n", fileName)
}
nodeDB, err := node.New()
if err != nil {
wwlog.Error("Could not open node configuration: %s\n", err)
@@ -57,32 +65,24 @@ func CobraRunE(cmd *cobra.Command, args []string) error {
wwlog.Error("Could not get node list: %s\n", err)
os.Exit(1)
}
node := node.FilterByName(nodes, []string{NodeName})
if len(node) != 1 {
filteredNodes := node.FilterByName(nodes, []string{NodeName})
if len(filteredNodes) != 1 {
wwlog.Error("%v does not identify a single node\n", NodeName)
os.Exit(1)
}
host = node[0]
if !util.IsFile(args[0]) {
wwlog.Error("%s is not a file\n", args[0])
}
tstruct := overlay.InitStruct(host)
tstruct.BuildSource = args[0]
tstruct := overlay.InitStruct(filteredNodes[0])
tstruct.BuildSource = overlayFile
buffer, backupFile, writeFile, err := overlay.RenderTemplateFile(overlayFile, tstruct)
if err != nil {
return err
}
if filepath.Ext(args[0]) != ".ww" {
wwlog.Warn("%s has not the '.ww' so wont be rendered if in overlay\n", args[0])
}
var outBuffer bytes.Buffer
// search for magic file name comment
bufferScanner := bufio.NewScanner(bytes.NewReader(buffer.Bytes()))
bufferScanner.Split(overlay.ScanLines)
reg := regexp.MustCompile(`.*{{\s*/\*\s*file\s*["'](.*)["']\s*\*/\s*}}.*`)
foundFileComment := false
destFileName := strings.TrimSuffix(args[0], ".ww")
destFileName := strings.TrimSuffix(fileName, ".ww")
for bufferScanner.Scan() {
line := bufferScanner.Text()
filenameFromTemplate := reg.FindAllStringSubmatch(line, -1)
@@ -106,8 +106,7 @@ func CobraRunE(cmd *cobra.Command, args []string) error {
wwlog.Info("backupFile: %v\nwriteFile: %v\n", backupFile, writeFile)
wwlog.Info("Filename: %s\n\n", destFileName)
}
wwlog.Info("%s", outBuffer.String())
fmt.Print(outBuffer.String())
}
return nil
}

View File

@@ -1,15 +1,30 @@
package delete
import "github.com/spf13/cobra"
import (
"github.com/hpcng/warewulf/internal/pkg/node"
"github.com/spf13/cobra"
)
var (
baseCmd = &cobra.Command{
DisableFlagsInUseLine: true,
Use: "delete [OPTIONS] PROFILE",
Short: "Delete a node profile",
Long: "This command deletes the node PROFILE. You may use a pattern for PROFILE.",
RunE: CobraRunE,
Args: cobra.MinimumNArgs(1),
Use: "delete [OPTIONS] PROFILE",
Short: "Delete a node profile",
Long: "This command deletes the node PROFILE. You may use a pattern for PROFILE.",
RunE: CobraRunE,
Args: cobra.MinimumNArgs(1),
ValidArgsFunction: func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
if len(args) != 0 {
return nil, cobra.ShellCompDirectiveNoFileComp
}
nodeDB, _ := node.New()
profiles, _ := nodeDB.FindAllProfiles()
var p_names []string
for _, profile := range profiles {
p_names = append(p_names, profile.Id.Get())
}
return p_names, cobra.ShellCompDirectiveNoFileComp
},
}
SetYes bool
)

View File

@@ -0,0 +1,115 @@
package edit
import (
"crypto/sha256"
"encoding/hex"
"fmt"
"io"
"io/ioutil"
"os"
"strings"
apiprofile "github.com/hpcng/warewulf/internal/pkg/api/profile"
"github.com/hpcng/warewulf/internal/pkg/api/routes/wwapiv1"
apiutil "github.com/hpcng/warewulf/internal/pkg/api/util"
"github.com/hpcng/warewulf/internal/pkg/node"
"github.com/hpcng/warewulf/internal/pkg/util"
"github.com/hpcng/warewulf/internal/pkg/wwlog"
"github.com/spf13/cobra"
"gopkg.in/yaml.v2"
)
func CobraRunE(cmd *cobra.Command, args []string) error {
canWrite := apiutil.CanWriteConfig()
if !canWrite.CanWriteConfig {
wwlog.Error("Can't write to config exiting")
os.Exit(1)
}
editor := os.Getenv("EDITOR")
if editor == "" {
editor = "/bin/vi"
}
if len(args) == 0 {
args = append(args, ".*")
}
filterList := wwapiv1.NodeList{
Output: args,
}
profileListMsg := apiprofile.FilteredProfiles(&filterList)
profileMap := make(map[string]*node.NodeConf)
// got proper yaml back
_ = yaml.Unmarshal([]byte(profileListMsg.NodeConfMapYaml), profileMap)
file, err := ioutil.TempFile("/tmp", "ww4ProfileEdit*.yaml")
if err != nil {
wwlog.Error("Could not create temp file:%s \n", err)
}
defer os.Remove(file.Name())
nodeConf := node.NewConf()
yamlTemplate := nodeConf.UnmarshalConf([]string{"tagsdel"})
for {
_ = file.Truncate(0)
_, _ = file.Seek(0, 0)
if !NoHeader {
_, _ = file.WriteString("#profilename:\n# " + strings.Join(yamlTemplate, "\n# ") + "\n")
}
_, _ = file.WriteString(profileListMsg.NodeConfMapYaml)
_, _ = file.Seek(0, 0)
hasher := sha256.New()
if _, err := io.Copy(hasher, file); err != nil {
wwlog.Error("Problems getting checksum of file %s\n", err)
}
sum1 := hex.EncodeToString(hasher.Sum(nil))
err = util.ExecInteractive(editor, file.Name())
if err != nil {
wwlog.Error("Editor process existed with non-zero\n")
os.Exit(1)
}
_, _ = file.Seek(0, 0)
hasher.Reset()
if _, err := io.Copy(hasher, file); err != nil {
wwlog.Error("Problems getting checksum of file %s\n", err)
}
sum2 := hex.EncodeToString(hasher.Sum(nil))
wwlog.Debug("Hashes are before %s and after %s\n", sum1, sum2)
if sum1 != sum2 {
wwlog.Debug("Nodes were modified")
modifiedProfileMap := make(map[string]*node.NodeConf)
_, _ = file.Seek(0, 0)
// ignore error as only may occurs under strange circumstances
buffer, _ := io.ReadAll(file)
err = yaml.Unmarshal(buffer, modifiedProfileMap)
if err == nil {
nodeList := make([]string, len(profileMap))
i := 0
for key := range profileMap {
nodeList[i] = key
i++
}
yes := apiutil.ConfirmationPrompt(fmt.Sprintf("Are you sure you want to modify %d nodes", len(modifiedProfileMap)))
if !yes {
break
}
err = apiprofile.ProfileDelete(&wwapiv1.NodeDeleteParameter{NodeNames: nodeList, Force: true})
if err != nil {
wwlog.Verbose("Problem deleting nodes before modification %s")
}
buffer, _ = yaml.Marshal(modifiedProfileMap)
err = apiprofile.ProfileAddFromYaml(&wwapiv1.NodeYaml{NodeConfMapYaml: string(buffer)})
if err != nil {
wwlog.Error("Got following problem when writing back yaml: %s", err)
os.Exit(1)
}
break
} else {
yes := apiutil.ConfirmationPrompt(fmt.Sprintf("Got following error on parsing: %s, Retry", err))
if !yes {
break
}
}
} else {
break
}
}
return nil
}

View File

@@ -0,0 +1,38 @@
package edit
import (
"github.com/hpcng/warewulf/internal/pkg/node"
"github.com/spf13/cobra"
)
var (
baseCmd = &cobra.Command{
DisableFlagsInUseLine: true,
Use: "edit [OPTIONS] NODENAME",
Short: "Edit node(s) with editor",
Long: "This command opens an editor for the given profiles.",
RunE: CobraRunE,
ValidArgsFunction: func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
if len(args) != 0 {
return nil, cobra.ShellCompDirectiveNoFileComp
}
nodeDB, _ := node.New()
profiles, _ := nodeDB.FindAllProfiles()
var p_names []string
for _, profile := range profiles {
p_names = append(p_names, profile.Id.Get())
}
return p_names, cobra.ShellCompDirectiveNoFileComp
},
}
NoHeader bool
)
func init() {
baseCmd.PersistentFlags().BoolVar(&NoHeader, "noheader", false, "Do not print header")
}
// GetRootCommand returns the root cobra.Command for the application.
func GetCommand() *cobra.Command {
return baseCmd
}

View File

@@ -3,6 +3,7 @@ package profile
import (
"github.com/hpcng/warewulf/internal/app/wwctl/profile/add"
"github.com/hpcng/warewulf/internal/app/wwctl/profile/delete"
"github.com/hpcng/warewulf/internal/app/wwctl/profile/edit"
"github.com/hpcng/warewulf/internal/app/wwctl/profile/list"
"github.com/hpcng/warewulf/internal/app/wwctl/profile/set"
"github.com/spf13/cobra"
@@ -11,9 +12,10 @@ import (
var (
baseCmd = &cobra.Command{
DisableFlagsInUseLine: true,
Use: "profile COMMAND [OPTIONS]",
Short: "Node configuration profile management",
Long: "Management of node profile settings",
Use: "profile COMMAND [OPTIONS]",
Short: "Node configuration profile management",
Long: "Management of node profile settings",
Aliases: []string{"nodes"},
}
)
@@ -22,6 +24,7 @@ func init() {
baseCmd.AddCommand(set.GetCommand())
baseCmd.AddCommand(add.GetCommand())
baseCmd.AddCommand(delete.GetCommand())
baseCmd.AddCommand(edit.GetCommand())
}
// GetRootCommand returns the root cobra.Command for the application.

View File

@@ -38,23 +38,27 @@ func CobraRunE(cmd *cobra.Command, args []string) error {
cmd.Usage()
os.Exit(1)
}
for _, node := range nodes {
if _, ok := node.NetDevs["default"]; !ok {
fmt.Fprintf(os.Stderr, "%s: Default network device doesn't exist\n", node.Id.Get())
var primaryNet string
for netName := range node.NetDevs {
if node.NetDevs[netName].Primary.GetB() {
primaryNet = netName
break
}
}
if primaryNet == "" {
wwlog.Error("%s: Primary network device doesn't exist\n", node.Id.Get())
continue
}
if node.NetDevs["default"].Ipaddr.Get() == "" {
fmt.Fprintf(os.Stderr, "%s: Default network IP address not configured\n", node.Id.Get())
if node.NetDevs[primaryNet].Ipaddr.Get() == "" {
wwlog.Error("%s: Primary network IP address not configured\n", node.Id.Get())
continue
}
nodename := node.Id.Print()
var command []string
command = append(command, node.NetDevs["default"].Ipaddr.Get())
command = append(command, node.NetDevs[primaryNet].Ipaddr.Get())
command = append(command, args[1:]...)
batchpool.Submit(func() {