diff --git a/CHANGELOG.md b/CHANGELOG.md index 5636be7a..83f0a9c4 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/internal/app/wwctl/container/imprt/root.go b/internal/app/wwctl/container/imprt/root.go index edafc127..3465fe94 100644 --- a/internal/app/wwctl/container/imprt/root.go +++ b/internal/app/wwctl/container/imprt/root.go @@ -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 diff --git a/internal/pkg/api/container/container.go b/internal/pkg/api/container/container.go index ca5bf82a..816bedb1 100644 --- a/internal/pkg/api/container/container.go +++ b/internal/pkg/api/container/container.go @@ -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