Create dir before extracting archive 04/91304/2
authorKiran Kamineni <kiran.k.kamineni@intel.com>
Fri, 12 Jul 2019 00:21:27 +0000 (17:21 -0700)
committerKiran Kamineni <kiran.k.kamineni@intel.com>
Fri, 12 Jul 2019 01:28:51 +0000 (18:28 -0700)
Some archives don't include a directory entry.
Eg: tgz archives generated by helm package.
This bug fix checks that a directory exists before
an extracted file is created there.

Issue-ID: MULTICLOUD-705
Change-Id: If6720948d470b83786901574f5d8d3227835a047
Signed-off-by: Kiran Kamineni <kiran.k.kamineni@intel.com>
src/k8splugin/internal/helm/helm.go
src/k8splugin/internal/rb/archive.go
src/k8splugin/internal/utils.go

index 1ab701a..2150758 100644 (file)
@@ -21,11 +21,12 @@ import (
        "io/ioutil"
        "k8s.io/helm/pkg/strvals"
        "os"
-       "path"
        "path/filepath"
        "regexp"
        "strings"
 
+       utils "github.com/onap/multicloud-k8s/src/k8splugin/internal"
+
        "github.com/ghodss/yaml"
        pkgerrors "github.com/pkg/errors"
        "k8s.io/apimachinery/pkg/runtime/schema"
@@ -135,15 +136,6 @@ func (h *TemplateClient) mergeValues(dest map[string]interface{}, src map[string
        return dest
 }
 
-func (h *TemplateClient) ensureDirectory(f string) error {
-       base := path.Dir(f)
-       _, err := os.Stat(base)
-       if err != nil && !os.IsNotExist(err) {
-               return err
-       }
-       return os.MkdirAll(base, 0755)
-}
-
 // GenerateKubernetesArtifacts a mapping of type to fully evaluated helm template
 func (h *TemplateClient) GenerateKubernetesArtifacts(inputPath string, valueFiles []string,
        values []string) ([]KubernetesResourceTemplate, error) {
@@ -245,7 +237,7 @@ func (h *TemplateClient) GenerateKubernetesArtifacts(inputPath string, valueFile
                }
 
                mfilePath := filepath.Join(outputDir, m.Name)
-               h.ensureDirectory(mfilePath)
+               utils.EnsureDirectory(mfilePath)
                err = ioutil.WriteFile(mfilePath, []byte(data), 0666)
                if err != nil {
                        return retData, err
index 624adfb..c075313 100644 (file)
@@ -24,6 +24,8 @@ import (
        "io/ioutil"
        "os"
        "path/filepath"
+
+       utils "github.com/onap/multicloud-k8s/src/k8splugin/internal"
 )
 
 func isTarGz(r io.Reader) error {
@@ -113,6 +115,11 @@ func ExtractTarBall(r io.Reader) (string, error) {
                                }
                        }
                case tar.TypeReg:
+                       err = utils.EnsureDirectory(target)
+                       if err != nil {
+                               return "", pkgerrors.Wrap(err, "Creating Directory")
+                       }
+
                        f, err := os.OpenFile(target, os.O_CREATE|os.O_RDWR, os.FileMode(header.Mode))
                        if err != nil {
                                return "", pkgerrors.Wrap(err, "Creating file")
index 627fb30..47a236c 100644 (file)
@@ -18,6 +18,7 @@ import (
        "log"
        "os"
        "path/filepath"
+       "path"
        "plugin"
        "strings"
 
@@ -128,3 +129,14 @@ func CheckInitialSettings() error {
 
        return nil
 }
+
+//EnsureDirectory makes sure that the directories specified in the path exist
+//If not, it will create them, if possible.
+func EnsureDirectory(f string) error {
+       base := path.Dir(f)
+       _, err := os.Stat(base)
+       if err != nil && !os.IsNotExist(err) {
+               return err
+       }
+       return os.MkdirAll(base, 0755)
+}