Support upgrading warewulf.conf
Signed-off-by: Jonathon Anderson <janderson@ciq.com>
This commit is contained in:
56
internal/app/wwctl/upgrade/config/cobra.go
Normal file
56
internal/app/wwctl/upgrade/config/cobra.go
Normal file
@@ -0,0 +1,56 @@
|
||||
package config
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
|
||||
"github.com/spf13/cobra"
|
||||
|
||||
"github.com/warewulf/warewulf/internal/pkg/config"
|
||||
libupgrade "github.com/warewulf/warewulf/internal/pkg/upgrade"
|
||||
"github.com/warewulf/warewulf/internal/pkg/util"
|
||||
)
|
||||
|
||||
var (
|
||||
Command = &cobra.Command{
|
||||
DisableFlagsInUseLine: true,
|
||||
Use: "config [OPTIONS]",
|
||||
Short: "Upgrade an existing nodes.conf",
|
||||
Long: `Upgrades nodes.conf from a previous version of Warewulf 4 to a format
|
||||
supported by the current version.`,
|
||||
RunE: UpgradeNodesConf,
|
||||
}
|
||||
|
||||
inputPath string
|
||||
outputPath string
|
||||
)
|
||||
|
||||
func init() {
|
||||
Command.Flags().StringVarP(&inputPath, "input-path", "i", config.ConfigFile, "Path to a legacy nodes.conf")
|
||||
Command.Flags().StringVarP(&outputPath, "output-path", "o", config.ConfigFile, "Path to write the upgraded nodes.conf to")
|
||||
}
|
||||
|
||||
func UpgradeNodesConf(cmd *cobra.Command, args []string) error {
|
||||
data, err := os.ReadFile(inputPath)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
legacy, err := libupgrade.ParseConfig(data)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
upgraded := legacy.Upgrade()
|
||||
if outputPath == "-" {
|
||||
upgradedYaml, err := upgraded.Dump()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
fmt.Print(string(upgradedYaml))
|
||||
return nil
|
||||
} else {
|
||||
if err := util.CopyFile(outputPath, outputPath+"-old"); err != nil {
|
||||
return err
|
||||
}
|
||||
return upgraded.PersistToFile(outputPath)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user