return non-zero exit code on container sub-commands

Signed-off-by: xu yang <xyang@ciq.com>
This commit is contained in:
xu yang
2024-09-25 08:50:50 +00:00
parent f73832c337
commit 94e203dd2e
8 changed files with 23 additions and 56 deletions

View File

@@ -43,6 +43,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/).
- Fix an invalid format issue for the GitHub nightly build action. #1258
- Return non-zero exit code on overlay build failure #1393
- Return non-zero exit code on container copy failure #1377
- Return non-zero exit code on container sub-commands #1414
## v4.5.8, unreleased

View File

@@ -28,15 +28,13 @@ Run "true" or "false" to enforce or abort image rebuild.`
func CobraRunE(cmd *cobra.Command, args []string) (err error) {
if os.Getpid() != 1 {
wwlog.Error("PID is not 1: %d", os.Getpid())
os.Exit(1)
return fmt.Errorf("PID is not 1: %d", os.Getpid())
}
containerName := args[0]
if !container.ValidSource(containerName) {
wwlog.Error("Unknown Warewulf container: %s", containerName)
os.Exit(1)
return fmt.Errorf("unknown Warewulf container: %s", containerName)
}
conf := warewulfconf.Get()
runDir := container.RunDir(containerName)
@@ -97,34 +95,29 @@ func CobraRunE(cmd *cobra.Command, args []string) (err error) {
} else if nodename != "" {
nodeDB, 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 := nodeDB.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)
}
nodes = node.FilterByName(nodes, []string{nodename})
if len(nodes) != 1 {
wwlog.Error("No single node idendified with %s", nodename)
os.Exit(1)
return fmt.Errorf("no single node idendified with %s", nodename)
}
overlays := nodes[0].SystemOverlay.GetSlice()
overlays = append(overlays, nodes[0].RuntimeOverlay.GetSlice()...)
err = overlay.BuildOverlayIndir(nodes[0], overlays, path.Join(runDir, "nodeoverlay"))
if err != nil {
wwlog.Error("Could not build overlay: %s", err)
os.Exit(1)
return fmt.Errorf("could not build overlay: %s", err)
}
options := fmt.Sprintf("lowerdir=%s:%s:%s",
path.Join(runDir, "lower"), containerPath, path.Join(runDir, "nodeoverlay"))
wwlog.Debug("overlay options: %s", options)
err = syscall.Mount("overlay", containerPath, "overlay", 0, options)
if err != nil {
wwlog.Warn(fmt.Sprintf("Couldn't create overlay for node render overlay: %s", err))
os.Exit(1)
return fmt.Errorf("Couldn't create overlay for node render overlay: %s", err)
}
ps1Str = fmt.Sprintf("[%s|ro|%s] Warewulf> ", containerName, nodename)
}

View File

@@ -97,8 +97,7 @@ func CobraRunE(cmd *cobra.Command, args []string) error {
containerName := args[0]
wwlog.Debug("CobraRunE:containerName: %v", containerName)
if !container.ValidSource(containerName) {
wwlog.Error("Unknown Warewulf container: %s", containerName)
os.Exit(1)
return fmt.Errorf("unknown Warewulf container: %s", containerName)
}
os.Setenv("WW_CONTAINER_SHELL", containerName)
@@ -111,16 +110,14 @@ func CobraRunE(cmd *cobra.Command, args []string) error {
err := runContainedCmd(cmd, containerName, args[1:])
if err != nil {
wwlog.Error("Failed executing container command: %s", err)
os.Exit(1)
return fmt.Errorf("failed executing container command: %s", err)
}
if util.IsFile(path.Join(containerPath, "/etc/warewulf/container_exit.sh")) {
wwlog.Verbose("Found clean script: /etc/warewulf/container_exit.sh")
err = runContainedCmd(cmd, containerName, []string{"/bin/sh", "/etc/warewulf/container_exit.sh"})
if err != nil {
wwlog.Error("Failed executing exit script: %s", err)
os.Exit(1)
return fmt.Errorf("failed executing exit script: %s", err)
}
}
@@ -155,8 +152,7 @@ func CobraRunE(cmd *cobra.Command, args []string) error {
fmt.Printf("Rebuilding container...\n")
err = container.Build(containerName, false)
if err != nil {
wwlog.Error("Could not build container %s: %s", containerName, err)
os.Exit(1)
return fmt.Errorf("could not build container %s: %s", containerName, err)
}
return nil
}

View File

@@ -9,7 +9,6 @@ import (
apicontainer "github.com/warewulf/warewulf/internal/pkg/api/container"
"github.com/warewulf/warewulf/internal/pkg/container"
"github.com/warewulf/warewulf/internal/pkg/util"
"github.com/warewulf/warewulf/internal/pkg/wwlog"
)
var containerList = apicontainer.ContainerList
@@ -20,7 +19,6 @@ func CobraRunE(vars *variables) func(cmd *cobra.Command, args []string) (err err
if showSize || vars.full || vars.kernel {
containerInfo, err := containerList()
if err != nil {
wwlog.Error("%s", err)
return err
}
if vars.full {

View File

@@ -30,8 +30,7 @@ func CobraRunE(cmd *cobra.Command, args []string) (err error) {
}
if !container.ValidName(crp.TargetName) {
wwlog.Error("Container name contains illegal characters : %s", crp.TargetName)
return
return fmt.Errorf("container name contains illegal characters : %s", crp.TargetName)
}
err = api.ContainerRename(crp)

View File

@@ -4,6 +4,7 @@
package shell
import (
"fmt"
"os"
"path"
@@ -19,13 +20,11 @@ func CobraRunE(cmd *cobra.Command, args []string) error {
var allargs []string
if !container.ValidSource(containerName) {
wwlog.Error("Unknown Warewulf container: %s", containerName)
os.Exit(1)
return fmt.Errorf("unknown Warewulf container: %s", containerName)
}
shellName := os.Getenv("SHELL")
if !container.ValidSource(containerName) {
wwlog.Error("Unknown Warewulf container: %s", containerName)
os.Exit(1)
return fmt.Errorf("unknown Warewulf container: %s", containerName)
}
var shells []string
if shellName == "" {

View File

@@ -2,7 +2,6 @@ package syncuser
import (
"fmt"
"os"
"github.com/spf13/cobra"
container_build "github.com/warewulf/warewulf/internal/pkg/api/container"
@@ -18,8 +17,7 @@ func CobraRunE(cmd *cobra.Command, args []string) error {
}
err := container.SyncUids(containerName, write)
if err != nil {
wwlog.Error("Error in synchronize: %s", err)
os.Exit(1)
return fmt.Errorf("error in synchronize: %s", err)
}
if write && !build {
@@ -35,8 +33,7 @@ func CobraRunE(cmd *cobra.Command, args []string) error {
}
err := container_build.ContainerBuild(cbp)
if err != nil {
wwlog.Error("Error during container build: %s", err)
os.Exit(1)
return fmt.Errorf("error during container build: %s", err)
}
}

View File

@@ -70,27 +70,23 @@ func ContainerBuild(cbp *wwapiv1.ContainerBuildParameter) (err error) {
for _, c := range containers {
if !container.ValidSource(c) {
err = fmt.Errorf("VNFS name does not exist: %s", c)
wwlog.Error("%s", err)
return
return fmt.Errorf("VNFS name does not exist: %s", c)
}
err = container.Build(c, cbp.Force)
if err != nil {
wwlog.Error("Could not build container %s: %s", c, err)
return
return fmt.Errorf("could not build container %s: %s", c, err)
}
}
if cbp.Default {
if len(containers) != 1 {
wwlog.Error("Can only set default for one container")
return fmt.Errorf("can only set default for one container")
} else {
var nodeDB node.NodeYaml
nodeDB, err = node.New()
if err != nil {
wwlog.Error("Could not open node configuration: %s", err)
return
return fmt.Errorf("could not open node configuration: %s", err)
}
// TODO: Don't loop through profiles, instead have a nodeDB function that goes directly to the map
@@ -124,8 +120,7 @@ func ContainerDelete(cdp *wwapiv1.ContainerDeleteParameter) (err error) {
nodeDB, err := node.New()
if err != nil {
wwlog.Error("Could not open nodeDB: %s", err)
return
return fmt.Errorf("could not open nodeDB: %s", err)
}
nodes, err := nodeDB.FindAllNodes()
@@ -176,7 +171,6 @@ func ContainerImport(cip *wwapiv1.ContainerImportParameter) (containerName strin
}
if !container.ValidName(cip.Name) {
err = fmt.Errorf("VNFS name contains illegal characters: %s", cip.Name)
wwlog.Error(err.Error())
return
}
@@ -188,7 +182,6 @@ func ContainerImport(cip *wwapiv1.ContainerImportParameter) (containerName strin
wwlog.Info("Overwriting existing VNFS")
err = os.RemoveAll(fullPath)
if err != nil {
wwlog.ErrorExc(err, "")
return
}
}
@@ -196,7 +189,6 @@ func ContainerImport(cip *wwapiv1.ContainerImportParameter) (containerName strin
if util.IsDir(fullPath) {
if !cip.Update {
err = fmt.Errorf("VNFS Name exists, specify --force, --update, or choose a different name: %s", cip.Name)
wwlog.Error(err.Error())
return
}
wwlog.Info("Updating existing VNFS")
@@ -205,7 +197,6 @@ func ContainerImport(cip *wwapiv1.ContainerImportParameter) (containerName strin
var sCtx *types.SystemContext
sCtx, err = getSystemContext(cip.OciNoHttps, cip.OciUsername, cip.OciPassword)
if err != nil {
wwlog.ErrorExc(err, "")
return
}
@@ -213,14 +204,12 @@ func ContainerImport(cip *wwapiv1.ContainerImportParameter) (containerName strin
cip.Source, err = filepath.Abs(cip.Source)
if err != nil {
err = fmt.Errorf("when resolving absolute path of %s, err: %v", cip.Source, err)
wwlog.Error(err.Error())
return
}
}
err = container.ImportDocker(cip.Source, cip.Name, sCtx)
if err != nil {
err = fmt.Errorf("could not import image: %s", err.Error())
wwlog.Error(err.Error())
_ = container.DeleteSource(cip.Name)
return
}
@@ -228,13 +217,11 @@ func ContainerImport(cip *wwapiv1.ContainerImportParameter) (containerName strin
err = container.ImportDirectory(cip.Source, cip.Name)
if err != nil {
err = fmt.Errorf("could not import image: %s", err.Error())
wwlog.Error(err.Error())
_ = container.DeleteSource(cip.Name)
return
}
} else {
err = fmt.Errorf("invalid dir or uri: %s", cip.Source)
wwlog.Error(err.Error())
return
}
@@ -242,7 +229,6 @@ func ContainerImport(cip *wwapiv1.ContainerImportParameter) (containerName strin
err = container.SyncUids(cip.Name, true)
if err != nil {
err = fmt.Errorf("error in user sync, fix error and run 'syncuser' manually: %s", err)
wwlog.Error(err.Error())
return
}
}
@@ -252,7 +238,6 @@ func ContainerImport(cip *wwapiv1.ContainerImportParameter) (containerName strin
err = container.Build(cip.Name, true)
if err != nil {
err = fmt.Errorf("could not build container %s: %s", cip.Name, err.Error())
wwlog.Error(err.Error())
return
}
}
@@ -262,7 +247,6 @@ func ContainerImport(cip *wwapiv1.ContainerImportParameter) (containerName strin
nodeDB, err = node.New()
if err != nil {
err = fmt.Errorf("could not open node configuration: %s", err.Error())
wwlog.Error(err.Error())
return
}