replaced errors.Wrap with fmt.Errorf

Signed-off-by: Christian Goll <cgoll@suse.com>
This commit is contained in:
Christian Goll
2024-11-07 17:08:34 +01:00
committed by Jonathon Anderson
parent c0703c32d4
commit 0dd0317740
30 changed files with 124 additions and 137 deletions

View File

@@ -11,7 +11,6 @@ import (
"strings"
"syscall"
"github.com/pkg/errors"
"github.com/spf13/cobra"
warewulfconf "github.com/warewulf/warewulf/internal/pkg/config"
"github.com/warewulf/warewulf/internal/pkg/container"
@@ -39,7 +38,7 @@ func CobraRunE(cmd *cobra.Command, args []string) (err error) {
conf := warewulfconf.Get()
runDir := container.RunDir(containerName)
if _, err := os.Stat(runDir); os.IsNotExist(err) {
return errors.Wrap(err, "container run directory does not exist")
return fmt.Errorf("container run directory does not exist: %w", err)
}
mountPts := conf.MountsContainer
mountPts = append(container.InitMountPnts(binds), mountPts...)
@@ -80,7 +79,7 @@ func CobraRunE(cmd *cobra.Command, args []string) (err error) {
// running in a private PID space, so also make / private, so that nothing gets out from here
err = syscall.Mount("", "/", "", syscall.MS_PRIVATE|syscall.MS_REC, "")
if err != nil {
return errors.Wrap(err, "failed to mount")
return fmt.Errorf("failed to mount: %w", err)
}
ps1Str := fmt.Sprintf("[%s|%s] Warewulf> ", containerName, exitEval)
wwlog.Info(msgStr)
@@ -126,11 +125,11 @@ func CobraRunE(cmd *cobra.Command, args []string) (err error) {
ps1Str = fmt.Sprintf("[%s|ro] Warewulf> ", containerName)
err = syscall.Mount(containerPath, containerPath, "", syscall.MS_BIND, "")
if err != nil {
return errors.Wrap(err, "failed to prepare bind mount")
return fmt.Errorf("failed to prepare bind mount: %w", err)
}
err = syscall.Mount(containerPath, containerPath, "", syscall.MS_REMOUNT|syscall.MS_RDONLY|syscall.MS_BIND, "")
if err != nil {
return errors.Wrap(err, "failed to remount ro")
return fmt.Errorf("failed to remount ro: %w", err)
}
}
@@ -156,25 +155,25 @@ func CobraRunE(cmd *cobra.Command, args []string) (err error) {
err = syscall.Chroot(containerPath)
if err != nil {
return errors.Wrap(err, "failed to chroot")
return fmt.Errorf("failed to chroot: %w", err)
}
err = os.Chdir("/")
if err != nil {
return errors.Wrap(err, "failed to chdir")
return fmt.Errorf("failed to chdir: %w", err)
}
if err := syscall.Mount("devtmpfs", "/dev", "devtmpfs", 0, ""); err != nil {
return errors.Wrap(err, "failed to mount /dev")
return fmt.Errorf("failed to mount /dev: %w", err)
}
if err := syscall.Mount("sysfs", "/sys", "sysfs", 0, ""); err != nil {
return errors.Wrap(err, "failed to mount /sys")
return fmt.Errorf("failed to mount /sys: %w", err)
}
if err := syscall.Mount("proc", "/proc", "proc", 0, ""); err != nil {
return errors.Wrap(err, "failed to mount /proc")
return fmt.Errorf("failed to mount /proc: %w", err)
}
if err := syscall.Mount("tmpfs", "/run", "tmpfs", 0, ""); err != nil {
return errors.Wrap(err, "failed to mount /run")
return fmt.Errorf("failed to mount /run: %w", err)
}
os.Setenv("PS1", ps1Str)

View File

@@ -3,7 +3,6 @@ package imprt
import (
"fmt"
"github.com/pkg/errors"
"github.com/spf13/cobra"
"github.com/warewulf/warewulf/internal/pkg/container"
"github.com/warewulf/warewulf/internal/pkg/kernel"
@@ -68,12 +67,12 @@ func CobraRunE(cmd *cobra.Command, args []string) error {
}
err = nodeDB.Persist()
if err != nil {
return errors.Wrap(err, "failed to persist nodedb")
return fmt.Errorf("failed to persist nodedb: %w", err)
}
fmt.Printf("Set default kernel version to: %s\n", args[0])
err = warewulfd.DaemonReload()
if err != nil {
return errors.Wrap(err, "failed to reload warewulf daemon")
return fmt.Errorf("failed to reload warewulf daemon: %w", err)
}
}

View File

@@ -8,7 +8,6 @@ import (
"os"
"strings"
"github.com/pkg/errors"
"github.com/spf13/cobra"
apinode "github.com/warewulf/warewulf/internal/pkg/api/node"
"github.com/warewulf/warewulf/internal/pkg/api/routes/wwapiv1"
@@ -133,7 +132,7 @@ func CobraRunE(cmd *cobra.Command, args []string) error {
err = warewulfd.DaemonReload()
if err != nil {
return errors.Wrap(err, "failed to reload warewulf daemon")
return fmt.Errorf("failed to reload warewulf daemon: %w", err)
}
return nil

View File

@@ -39,12 +39,12 @@ func CobraRunE(cmd *cobra.Command, args []string) error {
if Force {
err := os.RemoveAll(overlayPath)
if err != nil {
return errors.Wrap(err, "failed deleting overlay")
return fmt.Errorf("failed deleting overlay: %w", err)
}
} else {
err := os.Remove(overlayPath)
if err != nil {
return errors.Wrap(err, "failed deleting overlay")
return fmt.Errorf("failed deleting overlay: %w", err)
}
}
wwlog.Info("Deleted overlay: %s\n", args[0])

View File

@@ -6,7 +6,6 @@ import (
"path"
"path/filepath"
"github.com/pkg/errors"
"github.com/spf13/cobra"
"github.com/warewulf/warewulf/internal/pkg/node"
"github.com/warewulf/warewulf/internal/pkg/overlay"
@@ -59,7 +58,7 @@ func CobraRunE(cmd *cobra.Command, args []string) error {
err := util.CopyFile(source, path.Join(overlaySource, dest))
if err != nil {
return errors.Wrap(err, "could not copy file into overlay")
return fmt.Errorf("could not copy file into overlay: %w", err)
}
if !NoOverlayUpdate {

View File

@@ -1,10 +1,10 @@
package list
import (
"fmt"
"os"
"syscall"
"github.com/pkg/errors"
"github.com/spf13/cobra"
"github.com/warewulf/warewulf/internal/pkg/overlay"
"github.com/warewulf/warewulf/internal/pkg/util"
@@ -20,7 +20,7 @@ func CobraRunE(cmd *cobra.Command, args []string) error {
var err error
overlays, err = overlay.FindOverlays()
if err != nil {
return errors.Wrap(err, "could not obtain list of overlays from system")
return fmt.Errorf("could not obtain list of overlays from system: %w", err)
}
}

View File

@@ -4,7 +4,6 @@ import (
"fmt"
"github.com/manifoldco/promptui"
"github.com/pkg/errors"
"github.com/spf13/cobra"
"github.com/warewulf/warewulf/internal/pkg/node"
"github.com/warewulf/warewulf/internal/pkg/util"
@@ -39,7 +38,7 @@ func CobraRunE(cmd *cobra.Command, args []string) error {
wwlog.Verbose("Removing profile from node %s: %s", n.Id(), r)
n.Profiles = append(n.Profiles[:i], n.Profiles[i+1:]...)
if err != nil {
return errors.Wrap(err, "failed to update node")
return fmt.Errorf("failed to update node: %w", err)
}
}
}
@@ -73,7 +72,7 @@ func CobraRunE(cmd *cobra.Command, args []string) error {
if SetYes {
err := nodeDB.Persist()
if err != nil {
return errors.Wrap(err, "failed to persist nodedb")
return fmt.Errorf("failed to persist nodedb: %w", err)
}
} else {
prompt := promptui.Prompt{
@@ -86,7 +85,7 @@ func CobraRunE(cmd *cobra.Command, args []string) error {
if result == "y" || result == "yes" {
err := nodeDB.Persist()
if err != nil {
return errors.Wrap(err, "failed to persist nodedb")
return fmt.Errorf("failed to persist nodedb: %w", err)
}
}
}

View File

@@ -1,7 +1,8 @@
package server
import (
"github.com/pkg/errors"
"fmt"
"github.com/spf13/cobra"
"github.com/warewulf/warewulf/internal/pkg/warewulfd"
)
@@ -21,7 +22,7 @@ func GetCommand() *cobra.Command {
func CobraRunE(cmd *cobra.Command, args []string) error {
if err := warewulfd.DaemonInitLogging(); err != nil {
return errors.Wrap(err, "failed to configure logging")
return fmt.Errorf("failed to configure logging: %w", err)
}
return errors.Wrap(warewulfd.RunServer(), "failed to start Warewulf server")
return fmt.Errorf("failed to start Warewulf server: %w", warewulfd.RunServer())
}

View File

@@ -9,7 +9,6 @@ import (
"strings"
"github.com/containers/image/v5/types"
"github.com/pkg/errors"
"github.com/warewulf/warewulf/internal/pkg/api/routes/wwapiv1"
"github.com/warewulf/warewulf/internal/pkg/container"
"github.com/warewulf/warewulf/internal/pkg/kernel"
@@ -101,7 +100,7 @@ func ContainerBuild(cbp *wwapiv1.ContainerBuildParameter) (err error) {
// TODO: Need a wrapper and flock around this. Sometimes we restart warewulfd and sometimes we don't.
err = nodeDB.Persist()
if err != nil {
return errors.Wrap(err, "failed to persist nodedb")
return fmt.Errorf("failed to persist nodedb: %w", err)
}
fmt.Printf("Set default profile to container: %s\n", containers[0])
}
@@ -260,14 +259,14 @@ func ContainerImport(cip *wwapiv1.ContainerImportParameter) (containerName strin
// reload the config or if there is something more.
err = nodeDB.Persist()
if err != nil {
err = errors.Wrap(err, "failed to persist nodedb")
err = fmt.Errorf("failed to persist nodedb: %w", err)
return
}
wwlog.Info("Set default profile to container: %s", cip.Name)
err = warewulfd.DaemonReload()
if err != nil {
err = errors.Wrap(err, "failed to reload warewulf daemon")
err = fmt.Errorf("failed to reload warewulf daemon: %w", err)
return
}
}

View File

@@ -5,7 +5,6 @@ import (
"fmt"
"net"
"github.com/pkg/errors"
"github.com/warewulf/warewulf/internal/pkg/api/routes/wwapiv1"
"github.com/warewulf/warewulf/internal/pkg/hostlist"
"github.com/warewulf/warewulf/internal/pkg/node"
@@ -24,7 +23,7 @@ func NodeAdd(nap *wwapiv1.NodeAddParameter) (err error) {
nodeDB, err := node.New()
if err != nil {
return errors.Wrap(err, "failed to open node database")
return fmt.Errorf("failed to open node database: %w", err)
}
dbHash := nodeDB.Hash()
if hex.EncodeToString(dbHash[:]) != nap.Hash && !nap.Force {
@@ -35,11 +34,11 @@ func NodeAdd(nap *wwapiv1.NodeAddParameter) (err error) {
for _, a := range node_args {
n, err := nodeDB.AddNode(a)
if err != nil {
return errors.Wrap(err, "failed to add node")
return fmt.Errorf("failed to add node: %w", err)
}
err = yaml.Unmarshal([]byte(nap.NodeConfYaml), &n)
if err != nil {
return errors.Wrap(err, "Failed to decode nodeConf")
return fmt.Errorf("Failed to decode nodeConf: %w", err)
}
wwlog.Info("Added node: %s", a)
for _, dev := range n.NetDevs {
@@ -66,12 +65,12 @@ func NodeAdd(nap *wwapiv1.NodeAddParameter) (err error) {
err = nodeDB.Persist()
if err != nil {
return errors.Wrap(err, "failed to persist new node")
return fmt.Errorf("failed to persist new node: %w", err)
}
err = warewulfd.DaemonReload()
if err != nil {
return errors.Wrap(err, "failed to reload warewulf daemon")
return fmt.Errorf("failed to reload warewulf daemon: %w", err)
}
return
}

View File

@@ -5,7 +5,6 @@ import (
"fmt"
"os"
"github.com/pkg/errors"
"github.com/warewulf/warewulf/internal/pkg/api/routes/wwapiv1"
"github.com/warewulf/warewulf/internal/pkg/hostlist"
"github.com/warewulf/warewulf/internal/pkg/node"
@@ -43,12 +42,12 @@ func NodeDelete(ndp *wwapiv1.NodeDeleteParameter) (err error) {
err = nodeDB.Persist()
if err != nil {
return errors.Wrap(err, "failed to persist nodedb")
return fmt.Errorf("failed to persist nodedb: %w", err)
}
err = warewulfd.DaemonReload()
if err != nil {
return errors.Wrap(err, "failed to reload warewulf daemon")
return fmt.Errorf("failed to reload warewulf daemon: %w", err)
}
return
}

View File

@@ -1,9 +1,9 @@
package apinode
import (
"fmt"
"os"
"github.com/pkg/errors"
"github.com/warewulf/warewulf/internal/pkg/api/routes/wwapiv1"
"github.com/warewulf/warewulf/internal/pkg/node"
"github.com/warewulf/warewulf/internal/pkg/wwlog"
@@ -35,22 +35,22 @@ Add nodes from yaml
func NodeAddFromYaml(nodeList *wwapiv1.NodeYaml) (err error) {
nodeDB, err := node.New()
if err != nil {
return errors.Wrap(err, "Could not open NodeDB: %s\n")
return fmt.Errorf("could not open NodeDB: %w", err)
}
nodeMap := make(map[string]*node.NodeConf)
err = yaml.Unmarshal([]byte(nodeList.NodeConfMapYaml), nodeMap)
if err != nil {
return errors.Wrap(err, "Could not unmarshal Yaml: %s\n")
return fmt.Errorf("could not unmarshal Yaml: %w", err)
}
for nodeName, node := range nodeMap {
err = nodeDB.SetNode(nodeName, *node)
if err != nil {
return errors.Wrap(err, "couldn't set node")
return fmt.Errorf("couldn't set node: %w", err)
}
}
err = nodeDB.Persist()
if err != nil {
return errors.Wrap(err, "failed to persist nodedb")
return fmt.Errorf("failed to persist nodedb: %w", err)
}
return nil
}

View File

@@ -19,7 +19,7 @@ func ProfileAdd(nsp *wwapiv1.NodeAddParameter) error {
}
nodeDB, err := node.New()
if err != nil {
return errors.Wrap(err, "Could not open database")
return fmt.Errorf("Could not open database: %w", err)
}
for _, p := range nsp.NodeNames {
if util.InSlice(nodeDB.ListAllProfiles(), p) {
@@ -31,12 +31,12 @@ func ProfileAdd(nsp *wwapiv1.NodeAddParameter) error {
}
err = yaml.Unmarshal([]byte(nsp.NodeConfYaml), &pNew)
if err != nil {
return errors.Wrap(err, "failed to add profile")
return fmt.Errorf("failed to add profile: %w", err)
}
}
err = nodeDB.Persist()
if err != nil {
return errors.Wrap(err, "failed to persist new profile")
return fmt.Errorf("failed to persist new profile: %w", err)
}
return nil
}

View File

@@ -3,7 +3,6 @@ package apiprofile
import (
"fmt"
"github.com/pkg/errors"
"github.com/warewulf/warewulf/internal/pkg/api/routes/wwapiv1"
"github.com/warewulf/warewulf/internal/pkg/hostlist"
"github.com/warewulf/warewulf/internal/pkg/node"
@@ -29,11 +28,11 @@ func ProfileDelete(ndp *wwapiv1.NodeDeleteParameter) (err error) {
err = nodeDB.Persist()
if err != nil {
return errors.Wrap(err, "failed to persist nodedb")
return fmt.Errorf("failed to persist nodedb: %w", err)
}
err = warewulfd.DaemonReload()
if err != nil {
return errors.Wrap(err, "failed to reload warewulf daemon")
return fmt.Errorf("failed to reload warewulf daemon: %w", err)
}
return
}

View File

@@ -4,7 +4,6 @@ import (
"fmt"
"os"
"github.com/pkg/errors"
"github.com/warewulf/warewulf/internal/pkg/api/routes/wwapiv1"
"github.com/warewulf/warewulf/internal/pkg/node"
"github.com/warewulf/warewulf/internal/pkg/wwlog"
@@ -35,7 +34,7 @@ Add profiles from yaml
func ProfileAddFromYaml(nodeList *wwapiv1.NodeAddParameter) (err error) {
nodeDB, err := node.New()
if err != nil {
return errors.Wrap(err, "Could not open NodeDB: %s\n")
return fmt.Errorf("couldn't open NodeDB: %w", err)
}
if nodeDB.StringHash() != nodeList.Hash && !nodeList.Force {
return fmt.Errorf("got wrong hash, not modifying profile database")
@@ -44,17 +43,17 @@ func ProfileAddFromYaml(nodeList *wwapiv1.NodeAddParameter) (err error) {
profileMap := make(map[string]*node.ProfileConf)
err = yaml.Unmarshal([]byte(nodeList.NodeConfYaml), profileMap)
if err != nil {
return errors.Wrap(err, "Could not unmarshall Yaml: %s\n")
return fmt.Errorf("couldn't unmarshall Yaml: %w", err)
}
for profileName, profile := range profileMap {
err = nodeDB.SetProfile(profileName, *profile)
if err != nil {
return errors.Wrap(err, "couldn't set profile")
return fmt.Errorf("couldn't set profile: %w", err)
}
}
err = nodeDB.Persist()
if err != nil {
return errors.Wrap(err, "failed to persist nodedb")
return fmt.Errorf("failed to persist nodedb: %w", err)
}
return nil
}

View File

@@ -5,7 +5,6 @@ import (
"dario.cat/mergo"
"github.com/pkg/errors"
"github.com/warewulf/warewulf/internal/pkg/api/routes/wwapiv1"
"github.com/warewulf/warewulf/internal/pkg/node"
"github.com/warewulf/warewulf/internal/pkg/util"
@@ -21,7 +20,7 @@ func ProfileSet(set *wwapiv1.ConfSetParameter) (err error) {
}
nodeDB, _, err := ProfileSetParameterCheck(set)
if err != nil {
return errors.Wrap(err, "profile set parameters are wrong")
return fmt.Errorf("profile set parameters are wrong: %w", err)
}
if err = nodeDB.Persist(); err != nil {
return err

View File

@@ -151,7 +151,7 @@ func (conf *RootConf) SetDynamicDefaults() (err error) {
if err == nil {
mask = network.Mask
} else {
return errors.Wrap(err, "Couldn't parse IP address")
return fmt.Errorf("Couldn't parse IP address: %w", err)
}
if conf.Netmask == "" {
conf.Netmask = fmt.Sprintf("%d.%d.%d.%d", mask[0], mask[1], mask[2], mask[3])

View File

@@ -3,7 +3,6 @@ package configure
import (
"fmt"
"github.com/pkg/errors"
warewulfconf "github.com/warewulf/warewulf/internal/pkg/config"
"github.com/warewulf/warewulf/internal/pkg/overlay"
"github.com/warewulf/warewulf/internal/pkg/util"
@@ -41,7 +40,7 @@ func DHCP() (err error) {
fmt.Printf("Enabling and restarting the DHCP services\n")
err = util.SystemdStart(controller.DHCP.SystemdName)
if err != nil {
return errors.Wrap(err, "failed to start")
return fmt.Errorf("failed to start: %w", err)
}
return

View File

@@ -5,7 +5,6 @@ import (
"os"
"path"
"github.com/pkg/errors"
"github.com/warewulf/warewulf/internal/pkg/node"
"github.com/warewulf/warewulf/internal/pkg/overlay"
"github.com/warewulf/warewulf/internal/pkg/util"
@@ -39,7 +38,7 @@ func Hostfile() (err error) {
if writeFile {
err = overlay.CarefulWriteBuffer("/etc/hosts", buffer, backupFile, info.Mode())
if err != nil {
return errors.Wrap(err, "could not write file from template")
return fmt.Errorf("could not write file from template: %w", err)
}
}
return

View File

@@ -3,7 +3,6 @@ package configure
import (
"fmt"
"github.com/pkg/errors"
warewulfconf "github.com/warewulf/warewulf/internal/pkg/config"
"github.com/warewulf/warewulf/internal/pkg/overlay"
"github.com/warewulf/warewulf/internal/pkg/util"
@@ -31,12 +30,12 @@ func NFS() error {
if controller.NFS.SystemdName == "" {
err := util.SystemdStart("nfs-server")
if err != nil {
return errors.Wrap(err, "failed to start nfs-server")
return fmt.Errorf("failed to start nfs-server: %w", err)
}
} else {
err := util.SystemdStart(controller.NFS.SystemdName)
if err != nil {
return errors.Wrap(err, "failed to start")
return fmt.Errorf("failed to start: %w", err)
}
}
}

View File

@@ -5,7 +5,6 @@ import (
"os"
"path"
"github.com/pkg/errors"
warewulfconf "github.com/warewulf/warewulf/internal/pkg/config"
"github.com/warewulf/warewulf/internal/pkg/util"
"github.com/warewulf/warewulf/internal/pkg/wwlog"
@@ -34,7 +33,7 @@ func SSH(keyTypes ...string) error {
err = util.ExecInteractive("ssh-keygen", "-q", "-t", k, "-f", path.Join(wwkeydir, keytype), "-C", "", "-N", "")
if err != nil {
wwlog.Error("Failed to exec ssh-keygen: %s", err)
return errors.Wrap(err, "failed to exec ssh-keygen command")
return fmt.Errorf("failed to exec ssh-keygen command: %w", err)
}
} else {
fmt.Printf("Skipping, key already exists: %s\n", keytype)
@@ -59,11 +58,11 @@ func SSH(keyTypes ...string) error {
pubKey := privKey + ".pub"
err = util.ExecInteractive("ssh-keygen", "-q", "-t", keyType, "-f", privKey, "-C", "", "-N", "")
if err != nil {
return errors.Wrap(err, "Failed to exec ssh-keygen command")
return fmt.Errorf("failed to exec ssh-keygen command: %w", err)
}
err := util.CopyFile(pubKey, authorizedKeys)
if err != nil {
return errors.Wrap(err, fmt.Sprintf("Failed to copy %s to authorized_keys", pubKey))
return fmt.Errorf("failed to copy %s to authorized_keys: %w", pubKey, err)
}
} else {
fmt.Printf("Skipping authorized_keys: no key types configured\n")

View File

@@ -1,6 +1,7 @@
package container
import (
"fmt"
"path"
"github.com/pkg/errors"
@@ -33,7 +34,7 @@ func Build(name string, buildForce bool) error {
var err error
ignore, err = util.ReadFile(excludes_file)
if err != nil {
return errors.Wrapf(err, "Failed creating directory: %s", imagePath)
return fmt.Errorf("Failed creating directory: %s: %w", imagePath, err)
}
}

View File

@@ -135,12 +135,12 @@ func Build(kernelVersion, kernelName, root string) error {
// Create the destination paths just in case it doesn't exist
err := os.MkdirAll(path.Dir(kernelDestination), 0755)
if err != nil {
return errors.Wrap(err, "failed to create kernel dest")
return fmt.Errorf("failed to create kernel dest: %w", err)
}
err = os.MkdirAll(path.Dir(driversDestination), 0755)
if err != nil {
return errors.Wrap(err, "failed to create driver dest")
return fmt.Errorf("failed to create driver dest: %w", err)
}
err = os.MkdirAll(path.Dir(versionDestination), 0755)
@@ -161,7 +161,7 @@ func Build(kernelVersion, kernelName, root string) error {
if _, err := os.Stat(kernelSource); err == nil {
kernel, err := os.Open(kernelSource)
if err != nil {
return errors.Wrap(err, "could not open kernel")
return fmt.Errorf("could not open kernel: %w", err)
}
defer kernel.Close()
@@ -171,20 +171,20 @@ func Build(kernelVersion, kernelName, root string) error {
writer, err := os.Create(kernelDestination)
if err != nil {
return errors.Wrap(err, "could not decompress kernel")
return fmt.Errorf("could not decompress kernel: %w", err)
}
defer writer.Close()
_, err = io.Copy(writer, gzipreader)
if err != nil {
return errors.Wrap(err, "could not write decompressed kernel")
return fmt.Errorf("could not write decompressed kernel: %w", err)
}
} else {
err := util.CopyFile(kernelSource, kernelDestination)
if err != nil {
return errors.Wrap(err, "could not copy kernel")
return fmt.Errorf("could not copy kernel: %w", err)
}
}
@@ -220,16 +220,16 @@ func Build(kernelVersion, kernelName, root string) error {
wwlog.Verbose("Creating version file")
file, err := os.Create(versionDestination)
if err != nil {
return errors.Wrap(err, "Failed to create version file")
return fmt.Errorf("Failed to create version file: %w", err)
}
defer file.Close()
_, err = io.WriteString(file, kernelVersion)
if err != nil {
return errors.Wrap(err, "Could not write kernel version")
return fmt.Errorf("Could not write kernel version: %w", err)
}
err = file.Sync()
if err != nil {
return errors.Wrap(err, "Could not sync kernel version")
return fmt.Errorf("Could not sync kernel version: %w", err)
}
return nil
}

View File

@@ -35,13 +35,13 @@ func BuildAllOverlays(nodes []node.NodeConf) error {
wwlog.Info("Building system overlays for %s: [%s]", n.Id(), strings.Join(sysOverlays, ", "))
err := BuildOverlay(n, "system", sysOverlays)
if err != nil {
return errors.Wrapf(err, "could not build system overlays %v for node %s", sysOverlays, n.Id())
return fmt.Errorf("could not build system overlays %v for node %s: %w", sysOverlays, n.Id(), err)
}
runOverlays := n.RuntimeOverlay
wwlog.Info("Building runtime overlays for %s: [%s]", n.Id(), strings.Join(runOverlays, ", "))
err = BuildOverlay(n, "runtime", runOverlays)
if err != nil {
return errors.Wrapf(err, "could not build runtime overlays %v for node %s", runOverlays, n.Id())
return fmt.Errorf("could not build runtime overlays %v for node %s: %w", runOverlays, n.Id(), err)
}
}
@@ -56,7 +56,7 @@ func BuildSpecificOverlays(nodes []node.NodeConf, overlayNames []string) error {
for _, overlayName := range overlayNames {
err := BuildOverlay(n, "", []string{overlayName})
if err != nil {
return errors.Wrapf(err, "could not build overlay %s for node %s", overlayName, n.Id())
return fmt.Errorf("could not build overlay %s for node %s: %w", overlayName, n.Id(), err)
}
}
}
@@ -73,7 +73,7 @@ func BuildHostOverlay() error {
hostdir := OverlaySourceDir("host")
stats, err := os.Stat(hostdir)
if err != nil {
return errors.Wrap(err, "could not build host overlay ")
return fmt.Errorf("could not build host overlay: %w ", err)
}
if !(stats.Mode() == os.FileMode(0750|os.ModeDir) || stats.Mode() == os.FileMode(0700|os.ModeDir)) {
wwlog.SecWarn("Permissions of host overlay dir %s are %s (750 is considered as secure)", hostdir, stats.Mode())
@@ -90,7 +90,7 @@ func FindOverlays() ([]string, error) {
files, err := os.ReadDir(OverlaySourceTopDir())
if err != nil {
return ret, errors.Wrap(err, "could not get list of overlays")
return ret, fmt.Errorf("could not get list of overlays: %w", err)
}
for _, file := range files {
@@ -135,14 +135,14 @@ func BuildOverlay(nodeConf node.NodeConf, context string, overlayNames []string)
err := os.MkdirAll(overlayImageDir, 0750)
if err != nil {
return errors.Wrapf(err, "Failed to create directory for %s: %s", name, overlayImageDir)
return fmt.Errorf("failed to create directory for %s: %s: %w", name, overlayImageDir, err)
}
wwlog.Debug("Created directory for %s: %s", name, overlayImageDir)
buildDir, err := os.MkdirTemp(os.TempDir(), ".wwctl-overlay-")
if err != nil {
return errors.Wrapf(err, "Failed to create temporary directory for %s", name)
return fmt.Errorf("failed to create temporary directory for %s: %w", name, err)
}
defer os.RemoveAll(buildDir)
@@ -150,7 +150,7 @@ func BuildOverlay(nodeConf node.NodeConf, context string, overlayNames []string)
err = BuildOverlayIndir(nodeConf, overlayNames, buildDir)
if err != nil {
return errors.Wrapf(err, "Failed to generate files for %s", name)
return fmt.Errorf("failed to generate files for %s: %w", name, err)
}
wwlog.Debug("Generated files for %s", name)
@@ -194,13 +194,13 @@ func BuildOverlayIndir(nodeData node.NodeConf, overlayNames []string, outputDir
wwlog.Debug("Changing directory to OverlayDir: %s", overlaySourceDir)
err := os.Chdir(overlaySourceDir)
if err != nil {
return errors.Wrapf(ErrDoesNotExist, "directory: %s name: %s", overlaySourceDir, overlayName)
return fmt.Errorf("directory: %s name: %s err: %w", overlaySourceDir, overlayName, ErrDoesNotExist)
}
wwlog.Verbose("Walking the overlay structure: %s", overlaySourceDir)
err = filepath.Walk(".", func(location string, info os.FileInfo, err error) error {
if err != nil {
return errors.Wrap(err, "error for "+location)
return fmt.Errorf("error for %s: %w", location, err)
}
wwlog.Debug("Found overlay file: %s", location)
@@ -210,11 +210,11 @@ func BuildOverlayIndir(nodeData node.NodeConf, overlayNames []string, outputDir
err = os.MkdirAll(path.Join(outputDir, location), info.Mode())
if err != nil {
return errors.Wrap(err, "could not create directory within overlay")
return fmt.Errorf("could not create directory within overlay: %w", err)
}
err = util.CopyUIDGID(location, path.Join(outputDir, location))
if err != nil {
return errors.Wrap(err, "failed setting permissions on overlay directory")
return fmt.Errorf("failed setting permissions on overlay directory: %w", err)
}
wwlog.Debug("Created directory in overlay: %s", location)
@@ -222,7 +222,7 @@ func BuildOverlayIndir(nodeData node.NodeConf, overlayNames []string, outputDir
} else if filepath.Ext(location) == ".ww" {
tstruct, err := InitStruct(nodeData)
if err != nil {
return errors.Wrap(err, fmt.Sprintf("failed to initial data for %s", nodeData.Id()))
return fmt.Errorf("failed to initial data for %s: %w", nodeData.Id(), err)
}
tstruct.BuildSource = path.Join(overlaySourceDir, location)
wwlog.Verbose("Evaluating overlay template file: %s", location)
@@ -230,7 +230,7 @@ func BuildOverlayIndir(nodeData node.NodeConf, overlayNames []string, outputDir
buffer, backupFile, writeFile, err := RenderTemplateFile(location, tstruct)
if err != nil {
return errors.Wrap(err, fmt.Sprintf("Failed to render template %s", location))
return fmt.Errorf("failed to render template %s: %w", location, err)
}
if writeFile {
destFileName := destFile
@@ -249,11 +249,11 @@ func BuildOverlayIndir(nodeData node.NodeConf, overlayNames []string, outputDir
err = CarefulWriteBuffer(path.Join(outputDir, destFileName),
fileBuffer, backupFile, info.Mode())
if err != nil {
return errors.Wrap(err, "could not write file from template")
return fmt.Errorf("could not write file from template: %w", err)
}
err = util.CopyUIDGID(location, path.Join(outputDir, destFileName))
if err != nil {
return errors.Wrap(err, "failed setting permissions on template output file")
return fmt.Errorf("failed setting permissions on template output file: %w", err)
}
fileBuffer.Reset()
}
@@ -265,11 +265,11 @@ func BuildOverlayIndir(nodeData node.NodeConf, overlayNames []string, outputDir
}
err = CarefulWriteBuffer(path.Join(outputDir, destFileName), fileBuffer, backupFile, info.Mode())
if err != nil {
return errors.Wrap(err, "could not write file from template")
return fmt.Errorf("could not write file from template: %w", err)
}
err = util.CopyUIDGID(location, path.Join(outputDir, destFileName))
if err != nil {
return errors.Wrap(err, "failed setting permissions on template output file")
return fmt.Errorf("failed setting permissions on template output file: %w", err)
}
wwlog.Debug("Wrote template file into overlay: %s", destFile)
@@ -304,14 +304,14 @@ func BuildOverlayIndir(nodeData node.NodeConf, overlayNames []string, outputDir
if err == nil {
wwlog.Debug("Copied file into overlay: %s", location)
} else {
return errors.Wrap(err, "could not copy file into overlay")
return fmt.Errorf("could not copy file into overlay: %w", err)
}
}
return nil
})
if err != nil {
return errors.Wrap(err, "failed to build overlay working directory")
return fmt.Errorf("failed to build overlay working directory: %w", err)
}
}
@@ -327,14 +327,14 @@ func CarefulWriteBuffer(destFile string, buffer bytes.Buffer, backupFile bool, p
if !util.IsFile(destFile+".wwbackup") && util.IsFile(destFile) {
err := util.CopyFile(destFile, destFile+".wwbackup")
if err != nil {
return errors.Wrapf(err, "Failed to create backup: %s -> %s.wwbackup", destFile, destFile)
return fmt.Errorf("failed to create backup: %s -> %s.wwbackup %w", destFile, destFile, err)
}
}
}
w, err := os.OpenFile(destFile, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, perm)
if err != nil {
return errors.Wrap(err, "could not open new file for template")
return fmt.Errorf("could not open new file for template %w", err)
}
defer w.Close()
_, err = buffer.WriteTo(w)
@@ -403,13 +403,13 @@ func RenderTemplateFile(fileName string, data TemplateStruct) (
// Create the template with the merged FuncMap
tmpl, err := template.New(path.Base(fileName)).Option("missingkey=default").Funcs(funcMap).ParseGlob(fileName)
if err != nil {
err = errors.Wrap(err, "could not parse template "+fileName)
err = fmt.Errorf("could not parse template %s: %w", fileName, err)
return
}
err = tmpl.Execute(&buffer, data)
if err != nil {
err = errors.Wrap(err, "could not execute template")
err = fmt.Errorf("could not execute template: %w", err)
return
}
return

View File

@@ -15,7 +15,6 @@ import (
"syscall"
"time"
"github.com/pkg/errors"
"github.com/warewulf/warewulf/internal/pkg/wwlog"
)
@@ -194,7 +193,7 @@ func FindFilterFiles(
}()
err = os.Chdir(path)
if err != nil {
return ofiles, errors.Wrapf(err, "Failed to change path: %s", path)
return ofiles, fmt.Errorf("failed to change path: %s: %w", path, err)
}
// expand our include list as fspath.Match with /foo/* would catch /foo/baar but
// not /foo/baar/sibling
@@ -277,11 +276,11 @@ func SystemdStart(systemdName string) error {
wwlog.Debug("Setting up Systemd service: %s", systemdName)
err := ExecInteractive("/bin/sh", "-c", startCmd)
if err != nil {
return errors.Wrap(err, "failed to run start cmd")
return fmt.Errorf("failed to run start cmd: %w", err)
}
err = ExecInteractive("/bin/sh", "-c", enableCmd)
if err != nil {
return errors.Wrap(err, "failed to run enable cmd")
return fmt.Errorf("failed to run enable cmd: %w", err)
}
return nil
@@ -324,13 +323,13 @@ func AppendLines(fileName string, lines []string) error {
wwlog.Verbose("appending %v lines to %s", len(lines), fileName)
file, err := os.OpenFile(fileName, os.O_APPEND|os.O_WRONLY, 0644)
if err != nil {
return errors.Wrapf(err, "Can't open file: %s", fileName)
return fmt.Errorf("can't open file: %s: %w", fileName, err)
}
defer file.Close()
for _, line := range lines {
wwlog.Debug("Appending '%s' to %s", line, fileName)
if _, err := file.WriteString(fmt.Sprintf("%s\n", line)); err != nil {
return errors.Wrapf(err, "Can't write to file: %s", fileName)
return fmt.Errorf("can't write to file: %s: %w", fileName, err)
}
}
@@ -392,7 +391,7 @@ func FileGz(
err := os.Remove(file_gz)
if err != nil {
return errors.Wrapf(err, "Could not remove existing file: %s", file_gz)
return fmt.Errorf("could not remove existing file: %s: %w", file_gz, err)
}
}
@@ -402,7 +401,7 @@ func FileGz(
compressor, err = exec.LookPath("gzip")
if err != nil {
wwlog.Verbose("Could not locate GZIP")
return errors.Wrapf(err, "No compressor program for image file: %s", file_gz)
return fmt.Errorf("no compressor program for image file: %s: %w", file_gz, err)
}
}
@@ -426,7 +425,7 @@ func FileGz(
/* Open the output file for writing: */
gzippedFile, err = os.Create(file_gz)
if err != nil {
return errors.Wrapf(err, "Unable to open compressed image file for writing: %s", file_gz)
return fmt.Errorf("unable to open compressed image file for writing: %s: %w", file_gz, err)
}
/* We'll execute gzip with output to stdout and attach stdout to the compressed file we just
@@ -439,7 +438,7 @@ func FileGz(
proc.Stdout = gzippedFile
gzipStderr, err = proc.StderrPipe()
if err != nil {
return errors.Wrapf(err, "Unable to open stderr pipe for compression program: %s", compressor)
return fmt.Errorf("unable to open stderr pipe for compression program: %s: %w", compressor, err)
}
/* Execute the command: */
@@ -448,13 +447,13 @@ func FileGz(
_ = proc.Wait()
gzippedFile.Close()
os.Remove(file_gz)
err = errors.Wrapf(err, "Unable to successfully execute compression program: %s", compressor)
err = fmt.Errorf("unable to successfully execute compression program: %s: %w", compressor, err)
} else {
err = proc.Wait()
gzippedFile.Close()
if err != nil {
os.Remove(file_gz)
err = errors.Wrapf(err, "Unable to successfully create compressed image file: %s", file_gz)
err = fmt.Errorf("unable to successfully create compressed image file: %s: %w", file_gz, err)
} else {
wwlog.Verbose("Successfully compressed image file: %s", file_gz)
}
@@ -484,7 +483,7 @@ func BuildFsImage(
err = os.MkdirAll(path.Dir(imagePath), 0755)
if err != nil {
return errors.Wrapf(err, "Failed to create image directory for %s: %s", name, imagePath)
return fmt.Errorf("failed to create image directory for %s: %s: %w", name, imagePath, err)
}
wwlog.Debug("Created image directory for %s: %s", name, imagePath)
cwd, err := os.Getwd()
@@ -497,7 +496,7 @@ func BuildFsImage(
err = os.Chdir(rootfsPath)
if err != nil {
return errors.Wrapf(err, "Failed chdir to fs directory for %s: %s", name, rootfsPath)
return fmt.Errorf("failed chdir to fs directory for %s: %s: %w", name, rootfsPath, err)
}
wwlog.Verbose("changed to: %s", rootfsPath)
files, err := FindFilterFiles(
@@ -506,7 +505,7 @@ func BuildFsImage(
ignore,
ignore_xdev)
if err != nil {
return errors.Wrapf(err, "Failed discovering files for %s: %s", name, rootfsPath)
return fmt.Errorf("failed discovering files for %s: %s: %w", name, rootfsPath, err)
}
err = CpioCreate(
@@ -515,14 +514,14 @@ func BuildFsImage(
format,
cpio_args...)
if err != nil {
return errors.Wrapf(err, "Failed creating image for %s: %s", name, imagePath)
return fmt.Errorf("failed creating image for %s: %s: %w", name, imagePath, err)
}
wwlog.Info("Created image for %s: %s", name, imagePath)
err = FileGz(imagePath)
if err != nil {
return errors.Wrapf(err, "Failed to compress image for %s: %s", name, imagePath+".gz")
return fmt.Errorf("failed to compress image for %s: %s: %w", name, imagePath+".gz", err)
}
wwlog.Info("Compressed image for %s: %s", name, imagePath+".gz")

View File

@@ -1,6 +1,7 @@
package warewulfd
import (
"fmt"
"log/syslog"
"os"
"os/exec"
@@ -62,7 +63,7 @@ func DaemonInitLogging() error {
logwriter, err := syslog.New(syslog.LOG_NOTICE, "warewulfd")
if err != nil {
return errors.Wrap(err, "Could not create syslog writer")
return fmt.Errorf("Could not create syslog writer: %w", err)
}
wwlog.SetLogFormatter(wwlog.DefaultFormatter)
@@ -86,17 +87,17 @@ func DaemonStatus() error {
dat, err := os.ReadFile(WAREWULFD_PIDFILE)
if err != nil {
return errors.Wrap(err, "could not read Warewulfd PID file")
return fmt.Errorf("could not read Warewulfd PID file: %w", err)
}
pid, _ := strconv.Atoi(string(dat))
process, err := os.FindProcess(pid)
if err != nil {
return errors.Wrap(err, "failed to find running PID")
return fmt.Errorf("failed to find running PID: %w", err)
} else {
err := process.Signal(syscall.Signal(0))
if err != nil {
return errors.Wrap(err, "failed to send process SIGCONT")
return fmt.Errorf("failed to send process SIGCONT: %w", err)
} else {
wwlog.Serv("Warewulf server is running at PID: %d", pid)
}
@@ -112,11 +113,11 @@ func DaemonReload() error {
cmd := exec.Command("/usr/sbin/service", "warewulfd", "reload")
err := cmd.Start()
if err != nil {
return errors.Wrap(err, "failed to reload warewulfd")
return fmt.Errorf("failed to reload warewulfd: %w", err)
}
err = cmd.Wait()
if err != nil {
return errors.Wrap(err, "failed to reload warewulfd")
return fmt.Errorf("failed to reload warewulfd: %w", err)
}
return nil
}

View File

@@ -1,11 +1,10 @@
package warewulfd
import (
"fmt"
"strings"
"sync"
"github.com/pkg/errors"
"github.com/warewulf/warewulf/internal/pkg/node"
"github.com/warewulf/warewulf/internal/pkg/wwlog"
)
@@ -88,11 +87,11 @@ func GetNodeOrSetDiscoverable(hwaddr string) (node.NodeConf, error) {
}
err = db.yml.Persist()
if err != nil {
return node, errors.Wrapf(err, "%s (failed to persist node configuration)", hwaddr)
return node, fmt.Errorf("%s (failed to persist node configuration) %w", hwaddr, err)
}
err = loadNodeDB()
if err != nil {
return node, errors.Wrapf(err, "%s (failed to reload configuration)", hwaddr)
return node, fmt.Errorf("%s (failed to reload configuration) %w", hwaddr, err)
}
// NOTE: previously all overlays were built here, but that will also
// be done automatically when attempting to serve an overlay that

View File

@@ -2,11 +2,11 @@ package warewulfd
import (
"encoding/json"
"fmt"
"net/http"
"sync"
"time"
"github.com/pkg/errors"
"github.com/warewulf/warewulf/internal/pkg/node"
"github.com/warewulf/warewulf/internal/pkg/wwlog"
)
@@ -86,7 +86,7 @@ func statusJSON() ([]byte, error) {
ret, err := json.MarshalIndent(statusDB, "", " ")
if err != nil {
return ret, errors.Wrap(err, "could not marshal JSON data from status structure")
return ret, fmt.Errorf("could not marshal JSON data from status structure: %w", err)
}
return ret, nil

View File

@@ -1,6 +1,7 @@
package warewulfd
import (
"fmt"
"net/http"
"os"
"os/signal"
@@ -8,7 +9,6 @@ import (
"strings"
"syscall"
"github.com/pkg/errors"
warewulfconf "github.com/warewulf/warewulf/internal/pkg/config"
"github.com/warewulf/warewulf/internal/pkg/wwlog"
)
@@ -82,7 +82,7 @@ func RunServer() error {
err = http.ListenAndServe(":"+strconv.Itoa(daemonPort), &slashFix{&wwHandler})
if err != nil {
return errors.Wrap(err, "Could not start listening service")
return fmt.Errorf("could not start listening service: %w", err)
}
return nil