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
}

View File

@@ -0,0 +1,25 @@
package imprt
import (
"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{"imprt"},
}
)
func init() {
}
// GetRootCommand returns the root cobra.Command for the application.
func GetCommand() *cobra.Command {
return baseCmd
}

View File

@@ -5,6 +5,7 @@ import (
"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/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"
@@ -32,6 +33,7 @@ func init() {
baseCmd.AddCommand(console.GetCommand())
baseCmd.AddCommand(nodestatus.GetCommand())
baseCmd.AddCommand(edit.GetCommand())
baseCmd.AddCommand(imprt.GetCommand())
}
// GetRootCommand returns the root cobra.Command for the application.