fix a bug when replacing existing containers

Signed-off-by: jason yang <jasonyangshadow@gmail.com>
This commit is contained in:
jason yang
2023-12-12 17:06:28 -07:00
parent 9c698d7a13
commit 231987ecfc
3 changed files with 23 additions and 11 deletions

View File

@@ -51,6 +51,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- Fixed a bug where profile tags were erroneously overridden by empty node
values. #884
- Fixed bug where tags from profiles weren't rendered #967
- Fixed a bug when using `wwctl container import --force` to replace an existing container
will generate an error #474
### Changed

View File

@@ -1,6 +1,9 @@
package imprt
import "github.com/spf13/cobra"
import (
"github.com/hpcng/warewulf/internal/pkg/wwlog"
"github.com/spf13/cobra"
)
var (
baseCmd = &cobra.Command{
@@ -19,6 +22,11 @@ Imported containers are used to create bootable VNFS images.`,
Example: "wwctl container import docker://ghcr.io/hpcng/warewulf-rockylinux:8 rockylinux-8",
RunE: CobraRunE,
Args: cobra.MinimumNArgs(1),
PreRun: func(cmd *cobra.Command, args []string) {
if SetForce && SetUpdate {
wwlog.Warn("Both --force and --update flags are set, will ignore --update flag")
}
},
}
SetForce bool
SetUpdate bool

View File

@@ -175,21 +175,23 @@ func ContainerImport(cip *wwapiv1.ContainerImportParameter) (containerName strin
containerName = cip.Name
fullPath := container.SourceDir(cip.Name)
// container already exists and should be removed first
if util.IsDir(fullPath) && cip.Force {
wwlog.Info("Overwriting existing VNFS")
err = os.RemoveAll(fullPath)
if err != nil {
wwlog.ErrorExc(err, "")
return
}
}
if util.IsDir(fullPath) {
if cip.Force {
wwlog.Info("Overwriting existing VNFS")
err = os.RemoveAll(fullPath)
if err != nil {
wwlog.ErrorExc(err, "")
return
}
} else if cip.Update {
wwlog.Info("Updating existing VNFS")
} else {
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")
} else if strings.HasPrefix(cip.Source, "docker://") || strings.HasPrefix(cip.Source, "docker-daemon://") ||
strings.HasPrefix(cip.Source, "file://") || util.IsFile(cip.Source) {
var sCtx *types.SystemContext