Initial wwctl upgrade command

Signed-off-by: Jonathon Anderson <janderson@ciq.com>
This commit is contained in:
Jonathon Anderson
2024-11-05 12:03:03 -07:00
parent 336577c221
commit 981d11a012
5 changed files with 68 additions and 7 deletions

View File

@@ -0,0 +1,48 @@
package upgrade
import (
"os"
"github.com/spf13/cobra"
"github.com/warewulf/warewulf/internal/pkg/node"
libupgrade "github.com/warewulf/warewulf/internal/pkg/upgrade"
"github.com/warewulf/warewulf/internal/pkg/util"
)
var (
Command = &cobra.Command{
DisableFlagsInUseLine: true,
Use: "upgrade [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,
}
addDefaults bool
inputPath string
outputPath string
)
func init() {
Command.Flags().BoolVar(&addDefaults, "add-defaults", false, "Configure a default profile and set default node values")
Command.Flags().StringVarP(&inputPath, "input-path", "i", node.ConfigFile, "Path to a legacy nodes.conf")
Command.Flags().StringVarP(&outputPath, "output-path", "o", node.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.Parse(data)
if err != nil {
return err
}
upgraded := legacy.Upgrade(addDefaults)
if err := util.CopyFile(outputPath, outputPath+"-old"); err != nil {
return err
}
return upgraded.PersistToFile(outputPath)
}