From 84bfccba439dd2683787bf5abf1050758b592bec Mon Sep 17 00:00:00 2001 From: jason yang Date: Wed, 22 Mar 2023 04:18:50 +0000 Subject: [PATCH] add support for resolving absolute path automatically Signed-off-by: jason yang --- CHANGELOG.md | 1 + internal/pkg/api/container/container.go | 17 +++++++++++------ 2 files changed, 12 insertions(+), 6 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index d1017537..9e3db101 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -14,6 +14,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 - Added a missing `.ww` extension to the `70-ww4-netname.rules` template in the wwinit overlay. - Restrict access to `/warewulf/config` to root only. (#728, #742) +- Add support for resolving absolute path automatically. #493 ### Changed diff --git a/internal/pkg/api/container/container.go b/internal/pkg/api/container/container.go index fdf720fd..5ae4347a 100644 --- a/internal/pkg/api/container/container.go +++ b/internal/pkg/api/container/container.go @@ -4,6 +4,7 @@ import ( "fmt" "os" "path" + "path/filepath" "strconv" "strings" @@ -18,7 +19,6 @@ import ( ) func ContainerBuild(cbp *wwapiv1.ContainerBuildParameter) (err error) { - if cbp == nil { return fmt.Errorf("ContainerBuildParameter is nil") } @@ -60,7 +60,7 @@ func ContainerBuild(cbp *wwapiv1.ContainerBuildParameter) (err error) { return } - //TODO: Don't loop through profiles, instead have a nodeDB function that goes directly to the map + // TODO: Don't loop through profiles, instead have a nodeDB function that goes directly to the map profiles, _ := nodeDB.FindAllProfiles() for _, profile := range profiles { wwlog.Debug("Looking for profile default: %s", profile.Id.Get()) @@ -85,7 +85,6 @@ func ContainerBuild(cbp *wwapiv1.ContainerBuildParameter) (err error) { } func ContainerDelete(cdp *wwapiv1.ContainerDeleteParameter) (err error) { - if cdp == nil { return fmt.Errorf("ContainerDeleteParameter is nil") } @@ -132,7 +131,6 @@ ARG_LOOP: } func ContainerImport(cip *wwapiv1.ContainerImportParameter) (containerName string, err error) { - if cip == nil { err = fmt.Errorf("NodeAddParameter is nil") return @@ -176,6 +174,14 @@ func ContainerImport(cip *wwapiv1.ContainerImportParameter) (containerName strin // TODO: mhink - return was missing here. Was that deliberate? } + if util.IsFile(cip.Source) && !filepath.IsAbs(cip.Source) { + 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()) @@ -221,7 +227,7 @@ func ContainerImport(cip *wwapiv1.ContainerImportParameter) (containerName strin return } - //TODO: Don't loop through profiles, instead have a nodeDB function that goes directly to the map + // TODO: Don't loop through profiles, instead have a nodeDB function that goes directly to the map profiles, _ := nodeDB.FindAllProfiles() for _, profile := range profiles { wwlog.Debug("Looking for profile default: %s", profile.Id.Get()) @@ -332,7 +338,6 @@ func ContainerList() (containerInfo []*wwapiv1.ContainerInfo, err error) { } func ContainerShow(csp *wwapiv1.ContainerShowParameter) (response *wwapiv1.ContainerShowResponse, err error) { - containerName := csp.ContainerName if !container.ValidName(containerName) {