Skip to content
This repository has been archived by the owner on May 3, 2022. It is now read-only.

Commit

Permalink
fix(packager): import to dir in dest not temp dir
Browse files Browse the repository at this point in the history
Before, we were unpacking a bundle to a temp directory,
then loading it to validate the bundle. This caused
some challenges on windows around file permissions.

This PR changes the import logic so that a bundle is
unpacked in a directory at the destination specified
called <bundle-name>-<version>/ instead and then
validated. that directory is removed if the bundle
is not valid. If a directory already exists with that
name, it is overwritten.

resolves #458
  • Loading branch information
Michelle Noorali committed Nov 20, 2018
1 parent d94e3df commit 37fae94
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 23 deletions.
29 changes: 12 additions & 17 deletions pkg/packager/import.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ import (
"context"
"errors"
"fmt"
"io/ioutil"
"os"
"path/filepath"
"strings"

"github.com/docker/docker/client"
"github.com/docker/docker/pkg/archive"
Expand Down Expand Up @@ -52,11 +52,11 @@ func NewImporter(source, destination string, load loader.Loader) (*Importer, err

// Import decompresses a bundle from Source (location of the compressed bundle) and properly places artifacts in the correct location(s)
func (im *Importer) Import() error {
tempDir, err := ioutil.TempDir("", "duffle-import")
if err != nil {
baseDir := strings.TrimSuffix(filepath.Base(im.Source), ".tgz")
dest := filepath.Join(im.Destination, baseDir)
if err := os.MkdirAll(dest, 0755); err != nil {
return err
}
defer os.RemoveAll(tempDir)

reader, err := os.Open(im.Source)
if err != nil {
Expand All @@ -71,31 +71,26 @@ func (im *Importer) Import() error {
// Issue #416
NoLchown: true,
}
if err := archive.Untar(reader, tempDir, tarOptions); err != nil {
if err := archive.Untar(reader, dest, tarOptions); err != nil {
return fmt.Errorf("untar failed: %s", err)
}

// We try to load a bundle.cnab file first, and fall back to a bundle.json
ext := "cnab"
if _, err := os.Stat(filepath.Join(im.Destination, "bundle.cnab")); os.IsNotExist(err) {
if _, err := os.Stat(filepath.Join(dest, "bundle.cnab")); os.IsNotExist(err) {
ext = "json"
}

bun, err := im.Loader.Load(filepath.Join(tempDir, "bundle."+ext))
_, err = im.Loader.Load(filepath.Join(dest, "bundle."+ext))
if err != nil {
return fmt.Errorf("failed to load bundle.%s: %s", ext, err)
}

bunDir := filepath.Join(im.Destination, bun.Name)
if _, err := os.Stat(bunDir); os.IsNotExist(err) {
if err := os.Rename(tempDir, bunDir); err != nil {
return fmt.Errorf("move failed: %s", err)
removeErr := os.RemoveAll(dest)
if removeErr != nil {
return fmt.Errorf("failed to load and validate bundle.%s on import %s and failed to remove invalid bundle from filesystem %s", ext, err, removeErr)
}
} else {
return fmt.Errorf("Attempted to unpack bundle to %s but path already exists", bunDir)
return fmt.Errorf("failed to load and validate bundle.%s: %s", ext, err)
}

artifactsDir := filepath.Join(bunDir, "artifacts")
artifactsDir := filepath.Join(dest, "artifacts")
_, err = os.Stat(artifactsDir)
if err == nil {
filepath.Walk(artifactsDir, func(path string, info os.FileInfo, err error) error {
Expand Down
23 changes: 17 additions & 6 deletions pkg/packager/import_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@ import (
"path/filepath"
"testing"

"github.com/deis/duffle/pkg/loader"

"github.com/stretchr/testify/assert"

"github.com/deis/duffle/pkg/loader"
)

func TestImport(t *testing.T) {
Expand All @@ -30,13 +30,24 @@ func TestImport(t *testing.T) {
t.Fatalf("import failed: %v", err)
}

expectedBundlePath := filepath.Join(tempDir, "examplebun")
is.DirExists(expectedBundlePath, "expected examplebun to exist")
expectedBundlePath := filepath.Join(tempDir, "examplebun-0.1.0")
is.DirExistsf(expectedBundlePath, "expected examplebun to exist")
}

func TestMalformedImport(t *testing.T) {
tempDir, err := ioutil.TempDir("", "duffle-import-test")
if err != nil {
t.Fatal(err)
}
defer os.RemoveAll(tempDir)

im = Importer{
im := Importer{
Source: "testdata/malformed-0.1.0.tgz",
Destination: tempDir,
Loader: loader.NewDetectingLoader(),
}
is.Error(im.Import(), "expected malformed bundle error")

if err = im.Import(); err == nil {
t.Error("expected malformed bundle error")
}
}

0 comments on commit 37fae94

Please sign in to comment.