Return non-zero exit code on overlay sub-commands

Signed-off-by: xu yang <xyang@ciq.com>
This commit is contained in:
xu yang
2024-10-30 05:50:39 +00:00
committed by Jonathon Anderson
parent 795c44e650
commit 97c6772e51
10 changed files with 50 additions and 93 deletions

View File

@@ -72,6 +72,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/).
- Return non-zero exit code on profile sub-commands #1435
- Fix issue that NetworkManager marks managed interfaces "unmanaged" if they do
not have a device specified. #1154
- Return non-zero exit code on overlay sub-commands #1423
## v4.5.8, 2024-10-01

View File

@@ -19,7 +19,7 @@ func CobraRunE(cmd *cobra.Command, args []string) error {
controller := warewulfconf.Get()
nodeDB, err := node.New()
if err != nil {
return fmt.Errorf("couldn't open node configuration: %s", err)
return fmt.Errorf("could not open node configuration: %s", err)
}
db, err := nodeDB.FindAllNodes()
@@ -89,7 +89,7 @@ func CobraRunE(cmd *cobra.Command, args []string) error {
}
if err != nil {
return fmt.Errorf("Some overlays failed to be generated: %s", err)
return fmt.Errorf("some overlays failed to be generated: %s", err)
}
}
return nil

View File

@@ -1,6 +1,7 @@
package chmod
import (
"fmt"
"os"
"path"
"strconv"
@@ -8,7 +9,6 @@ import (
"github.com/spf13/cobra"
"github.com/warewulf/warewulf/internal/pkg/overlay"
"github.com/warewulf/warewulf/internal/pkg/util"
"github.com/warewulf/warewulf/internal/pkg/wwlog"
)
func CobraRunE(cmd *cobra.Command, args []string) error {
@@ -19,28 +19,24 @@ func CobraRunE(cmd *cobra.Command, args []string) error {
permissionMode, err := strconv.ParseUint(args[2], 8, 32)
if err != nil {
wwlog.Error("Could not convert requested mode: %s", err)
os.Exit(1)
return fmt.Errorf("could not convert requested mode: %s", err)
}
overlaySourceDir = overlay.OverlaySourceDir(overlayName)
if !util.IsDir(overlaySourceDir) {
wwlog.Error("Overlay does not exist: %s", overlayName)
os.Exit(1)
return fmt.Errorf("overlay does not exist: %s", overlayName)
}
overlayFile := path.Join(overlaySourceDir, fileName)
if !util.IsFile(overlayFile) && !util.IsDir(overlayFile) {
wwlog.Error("File does not exist within overlay: %s:%s", overlayName, fileName)
os.Exit(1)
return fmt.Errorf("file does not exist within overlay: %s:%s", overlayName, fileName)
}
err = os.Chmod(overlayFile, os.FileMode(permissionMode))
if err != nil {
wwlog.Error("Could not set permission: %s", err)
os.Exit(1)
return fmt.Errorf("could not set permission: %s", err)
}
return nil

View File

@@ -1,13 +1,13 @@
package chown
import (
"fmt"
"os"
"path"
"strconv"
"github.com/warewulf/warewulf/internal/pkg/overlay"
"github.com/warewulf/warewulf/internal/pkg/util"
"github.com/warewulf/warewulf/internal/pkg/wwlog"
"github.com/spf13/cobra"
)
@@ -23,15 +23,13 @@ func CobraRunE(cmd *cobra.Command, args []string) error {
uid, err = strconv.Atoi(args[2])
if err != nil {
wwlog.Error("UID is not an integer: %s", args[2])
os.Exit(1)
return fmt.Errorf("UID is not an integer: %s", args[2])
}
if len(args) > 3 {
gid, err = strconv.Atoi(args[3])
if err != nil {
wwlog.Error("GID is not an integer: %s", args[3])
os.Exit(1)
return fmt.Errorf("GID is not an integer: %s", args[3])
}
} else {
gid = -1
@@ -40,21 +38,18 @@ func CobraRunE(cmd *cobra.Command, args []string) error {
overlaySourceDir = overlay.OverlaySourceDir(overlayName)
if !util.IsDir(overlaySourceDir) {
wwlog.Error("Overlay does not exist: %s", overlayName)
os.Exit(1)
return fmt.Errorf("overlay does not exist: %s", overlayName)
}
overlayFile := path.Join(overlaySourceDir, fileName)
if !util.IsFile(overlayFile) && !util.IsDir(overlayFile) {
wwlog.Error("File does not exist within overlay: %s:%s", overlayName, fileName)
os.Exit(1)
return fmt.Errorf("file does not exist within overlay: %s:%s", overlayName, fileName)
}
err = os.Chown(overlayFile, uid, gid)
if err != nil {
wwlog.Error("Could not set ownership: %s", err)
os.Exit(1)
return fmt.Errorf("could not set ownership: %s", err)
}
return nil

View File

@@ -1,20 +1,11 @@
package create
import (
"os"
"github.com/spf13/cobra"
"github.com/warewulf/warewulf/internal/pkg/overlay"
"github.com/warewulf/warewulf/internal/pkg/wwlog"
)
func CobraRunE(cmd *cobra.Command, args []string) error {
err := overlay.OverlayInit(args[0])
if err != nil {
wwlog.Error("%s", err)
os.Exit(1)
}
return nil
func CobraRunE(cmd *cobra.Command, args []string) (err error) {
err = overlay.OverlayInit(args[0])
return
}

View File

@@ -25,13 +25,11 @@ func CobraRunE(cmd *cobra.Command, args []string) error {
overlayPath = overlay.OverlaySourceDir(overlayName)
if overlayPath == "" {
wwlog.Error("Overlay name did not resolve: '%s'", overlayName)
os.Exit(1)
return fmt.Errorf("overlay name did not resolve: '%s'", overlayName)
}
if !util.IsDir(overlayPath) {
wwlog.Error("Overlay does not exist: %s", overlayName)
os.Exit(1)
return fmt.Errorf("overlay does not exist: %s", overlayName)
}
if fileName == "" {
@@ -49,29 +47,24 @@ func CobraRunE(cmd *cobra.Command, args []string) error {
return errors.Wrap(err, "failed deleting overlay")
}
}
fmt.Printf("Deleted overlay: %s\n", args[0])
wwlog.Info("Deleted overlay: %s\n", args[0])
} else {
removePath := path.Join(overlayPath, fileName)
if !util.IsDir(removePath) && !util.IsFile(removePath) {
wwlog.Error("Path to remove doesn't exist in overlay: %s", removePath)
os.Exit(1)
return fmt.Errorf("path to remove doesn't exist in overlay: %s", removePath)
}
if Force {
err := os.RemoveAll(removePath)
if err != nil {
wwlog.Error("Failed deleting file from overlay: %s:%s", overlayName, overlayPath)
wwlog.Error("%s", err)
os.Exit(1)
return fmt.Errorf("failed deleting file from overlay: %s:%s", overlayName, overlayPath)
}
} else {
err := os.Remove(removePath)
if err != nil {
wwlog.Error("Failed deleting overlay: %s:%s", overlayName, overlayPath)
wwlog.Error("%s", err)
os.Exit(1)
return fmt.Errorf("failed deleting overlay: %s:%s", overlayName, overlayPath)
}
}

View File

@@ -1,6 +1,7 @@
package edit
import (
"fmt"
"io"
"os"
"path"
@@ -40,8 +41,7 @@ func CobraRunE(cmd *cobra.Command, args []string) error {
overlaySourceDir := overlay.OverlaySourceDir(overlayName)
if !util.IsDir(overlaySourceDir) {
wwlog.Error("Overlay does not exist: %s", overlayName)
os.Exit(1)
return fmt.Errorf("overlay does not exist: %s", overlayName)
}
overlayFile := path.Join(overlaySourceDir, fileName)
@@ -51,20 +51,17 @@ func CobraRunE(cmd *cobra.Command, args []string) error {
if CreateDirs {
err := os.MkdirAll(overlayFileDir, 0755)
if err != nil {
wwlog.Error("Could not create directory: %s", overlayFileDir)
os.Exit(1)
return fmt.Errorf("could not create directory: %s", overlayFileDir)
}
} else {
if !util.IsDir(overlayFileDir) {
wwlog.Error("%s does not exist. Use '--parents' option to create automatically.", overlayFileDir)
os.Exit(1)
return fmt.Errorf("%s does not exist. Use '--parents' option to create automatically", overlayFileDir)
}
}
tempFile, tempFileErr := os.CreateTemp("", "ww-overlay-edit-")
if tempFileErr != nil {
wwlog.Error("Unable to create temporary file for editing: %s", tempFileErr)
os.Exit(1)
return fmt.Errorf("unable to create temporary file for editing: %s", tempFileErr)
}
defer os.Remove(tempFile.Name())
wwlog.Debug("Using temporary file %s", tempFile.Name())
@@ -72,26 +69,22 @@ func CobraRunE(cmd *cobra.Command, args []string) error {
if util.IsFile(overlayFile) {
originalFile, openErr := os.Open(overlayFile)
if openErr != nil {
wwlog.Error("Unable to open %s: %s", overlayFile, openErr)
os.Exit(1)
return fmt.Errorf("unable to open %s: %s", overlayFile, openErr)
}
if _, err := io.Copy(tempFile, originalFile); err != nil {
wwlog.Error("Unable to copy %s to %s for editing: %s", originalFile.Name(), tempFile.Name(), err)
os.Exit(1)
return fmt.Errorf("unable to copy %s to %s for editing: %s", originalFile.Name(), tempFile.Name(), err)
}
originalFile.Close()
} else if filepath.Ext(overlayFile) == ".ww" {
if _, err := tempFile.Write([]byte(initialTemplate)); err != nil {
wwlog.Error("Unable to write to %s: %s", tempFile.Name(), err)
os.Exit(1)
return fmt.Errorf("unable to write to %s: %s", tempFile.Name(), err)
}
}
tempFile.Close()
var startTime time.Time
if fileInfo, err := os.Stat(tempFile.Name()); err != nil {
wwlog.Error("Unable to stat %s: %s", tempFile.Name(), err)
os.Exit(1)
return fmt.Errorf("unable to stat %s: %s", tempFile.Name(), err)
} else {
startTime = fileInfo.ModTime()
}
@@ -101,13 +94,11 @@ func CobraRunE(cmd *cobra.Command, args []string) error {
editor = "/bin/vi"
}
if editorErr := util.ExecInteractive(editor, tempFile.Name()); editorErr != nil {
wwlog.Error("Editor process exited with an error: %s", editorErr)
os.Exit(1)
return fmt.Errorf("editor process exited with an error: %s", editorErr)
}
if fileInfo, err := os.Stat(tempFile.Name()); err != nil {
wwlog.Error("Unable to stat %s: %s", tempFile.Name(), err)
os.Exit(1)
return fmt.Errorf("unable to stat %s: %s", tempFile.Name(), err)
} else {
if startTime == fileInfo.ModTime() {
wwlog.Debug("No change detected. Not updating overlay.")
@@ -123,8 +114,7 @@ func CobraRunE(cmd *cobra.Command, args []string) error {
wwlog.Debug("Unable to rename temp file: %s to overlay file: %s, try copying the data", tempFile.Name(), overlayFile)
cerr := util.CopyFile(tempFile.Name(), overlayFile)
if cerr != nil {
wwlog.Error("Unable to copy data from temp file: %s to target file: %s, err: %s", tempFile.Name(), overlayFile, err)
return cerr
return fmt.Errorf("unable to copy data from temp file: %s to target file: %s, err: %s", tempFile.Name(), overlayFile, err)
}
}

View File

@@ -1,6 +1,7 @@
package imprt
import (
"fmt"
"os"
"path"
"path/filepath"
@@ -30,8 +31,7 @@ func CobraRunE(cmd *cobra.Command, args []string) error {
overlaySource = overlay.OverlaySourceDir(overlayName)
if !util.IsDir(overlaySource) {
wwlog.Error("Overlay does not exist: %s", overlayName)
os.Exit(1)
return fmt.Errorf("overlay does not exist: %s", overlayName)
}
if util.IsDir(path.Join(overlaySource, dest)) {
@@ -39,8 +39,7 @@ func CobraRunE(cmd *cobra.Command, args []string) error {
}
if util.IsFile(path.Join(overlaySource, dest)) {
wwlog.Error("A file with that name already exists in the overlay %s\n:", overlayName)
os.Exit(1)
return fmt.Errorf("a file with that name already exists in the overlay: %s", overlayName)
}
if CreateDirs {
@@ -49,13 +48,11 @@ func CobraRunE(cmd *cobra.Command, args []string) error {
wwlog.Debug("Create dir: %s", parent)
srcInfo, err := os.Stat(source)
if err != nil {
wwlog.Error("Could not retrieve the stat for file: %s", err)
return err
return fmt.Errorf("could not retrieve the stat for file: %s", err)
}
err = os.MkdirAll(parent, srcInfo.Mode())
if err != nil {
wwlog.Error("Could not create parent dif: %s: %v", parent, err)
return err
return fmt.Errorf("could not create parent dif: %s: %v", parent, err)
}
}
}
@@ -68,14 +65,12 @@ func CobraRunE(cmd *cobra.Command, args []string) error {
if !NoOverlayUpdate {
n, err := node.New()
if err != nil {
wwlog.Error("Could not open node configuration: %s", err)
os.Exit(1)
return fmt.Errorf("could not open node configuration: %s", err)
}
nodes, err := n.FindAllNodes()
if err != nil {
wwlog.Error("Could not get node list: %s", err)
os.Exit(1)
return fmt.Errorf("could not get node list: %s", err)
}
var updateNodes []node.NodeConf

View File

@@ -1,6 +1,7 @@
package mkdir
import (
"fmt"
"os"
"path"
@@ -19,8 +20,7 @@ func CobraRunE(cmd *cobra.Command, args []string) error {
overlaySourceDir = overlay.OverlaySourceDir(overlayName)
if !util.IsDir(overlaySourceDir) {
wwlog.Error("Overlay does not exist: %s", overlayName)
os.Exit(1)
return fmt.Errorf("overlay does not exist: %s", overlayName)
}
overlayDir := path.Join(overlaySourceDir, dirName)
@@ -29,8 +29,7 @@ func CobraRunE(cmd *cobra.Command, args []string) error {
err := os.MkdirAll(overlayDir, os.FileMode(PermMode))
if err != nil {
wwlog.Error("Could not create directory: %s", path.Dir(overlayDir))
os.Exit(1)
return fmt.Errorf("could not create directory: %s", path.Dir(overlayDir))
}
return nil

View File

@@ -3,7 +3,7 @@ package show
import (
"bufio"
"bytes"
"errors"
"fmt"
"os"
"path"
"path/filepath"
@@ -25,15 +25,13 @@ func CobraRunE(cmd *cobra.Command, args []string) error {
overlaySourceDir = overlay.OverlaySourceDir(overlayName)
if !util.IsDir(overlaySourceDir) {
err := errors.New("overlay does not exist")
return err
return fmt.Errorf("overlay dir: %s does not exist", overlaySourceDir)
}
overlayFile := path.Join(overlaySourceDir, fileName)
if !util.IsFile(overlayFile) {
err := errors.New("file does not exist within overlay")
return err
return fmt.Errorf("file: %s does not exist within overlay", overlayFile)
}
if NodeName == "" {
@@ -45,8 +43,7 @@ func CobraRunE(cmd *cobra.Command, args []string) error {
wwlog.Output("%s", string(f))
} else {
if !util.IsFile(overlayFile) {
err := errors.New("not a file")
return err
return fmt.Errorf("%s is not a file", overlayFile)
}
if filepath.Ext(overlayFile) != ".ww" {
wwlog.Warn("%s lacks the '.ww' suffix, will not be rendered in an overlay", fileName)
@@ -59,7 +56,7 @@ func CobraRunE(cmd *cobra.Command, args []string) error {
if err == node.ErrNotFound {
hostName, err := os.Hostname()
if err != nil {
wwlog.Error("Could not get host name: %s", err)
return fmt.Errorf("could not get host name: %s", err)
}
nodeConf = node.NewNode(hostName)
nodeConf.ClusterName = hostName