import nodes from file

This commit is contained in:
Christian Goll
2022-09-23 15:31:27 +02:00
parent a4fbe428e2
commit ddf72b6b09
3 changed files with 73 additions and 0 deletions

View File

@@ -0,0 +1,46 @@
package imprt
import (
"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)
}
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")
}
return nil
}