Skip to content

Commit

Permalink
feat(import): copy folder contents (#453)
Browse files Browse the repository at this point in the history
* feat(import): copy dir contents

  import:
    - path: folder1/
      dest: /

vs

  import:
    - path: folder1
      dest: /

The former will copy the contents of folder1/ at / and the latter will
make and copy a /folder1 at /

Signed-off-by: Ramkumar Chinchani <[email protected]>

* feat(import): allow dest dir renaming during import

import:
    - path: folder1
    dest: /folder2

will cause contents of folder1 to show up as and under /folder2

Signed-off-by: Ramkumar Chinchani <[email protected]>

---------

Signed-off-by: Ramkumar Chinchani <[email protected]>
  • Loading branch information
rchincha authored Nov 6, 2023
1 parent 67d1ffb commit 533c4a6
Show file tree
Hide file tree
Showing 6 changed files with 177 additions and 7 deletions.
2 changes: 1 addition & 1 deletion cmd/stacker/grab.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,5 +43,5 @@ func doGrab(ctx *cli.Context) error {
return err
}

return stacker.Grab(config, s, name, parts[1], cwd, nil, -1, -1)
return stacker.Grab(config, s, name, parts[1], cwd, "", nil, -1, -1)
}
15 changes: 15 additions & 0 deletions pkg/overlay/overlay-dirs.go
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,8 @@ func validateOverlayDirs(name string, overlayDirs []types.OverlayDir, rootfs str
break
}

log.Debugf("overlayDirs: %+v", overlayDirs)

for ovlindex, ovldir := range overlayDirs {
if ovldir.Dest == "" {
continue
Expand All @@ -124,6 +126,15 @@ func validateOverlayDirs(name string, overlayDirs []types.OverlayDir, rootfs str
return errors.Wrapf(err, "unable to stat %s", contents)
}

contents, err = filepath.EvalSymlinks(contents)
if err != nil {
if errors.Is(err, os.ErrNotExist) {
continue
}

return errors.Wrapf(err, "unable to eval symlink %s", contents)
}

dest := path.Join(contents, ovldir.Dest)
realdest, err := filepath.EvalSymlinks(dest)
if err != nil {
Expand All @@ -135,6 +146,10 @@ func validateOverlayDirs(name string, overlayDirs []types.OverlayDir, rootfs str
}

overlayDirs[ovlindex].Dest = strings.TrimPrefix(realdest, contents)
if overlayDirs[ovlindex].Dest == "" {
overlayDirs[ovlindex].Dest = ovldir.Dest
}

if ovldir.Dest != overlayDirs[ovlindex].Dest {
log.Infof("overlay dest %s is a symlink, patching to %s", ovldir.Dest, overlayDirs[ovlindex].Dest)
break
Expand Down
9 changes: 7 additions & 2 deletions pkg/stacker/grab.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import (
)

func Grab(sc types.StackerConfig, storage types.Storage, name string, source string, targetDir string,
mode *fs.FileMode, uid, gid int,
idest string, mode *fs.FileMode, uid, gid int,
) error {
c, err := container.New(sc, name)
if err != nil {
Expand All @@ -31,7 +31,12 @@ func Grab(sc types.StackerConfig, storage types.Storage, name string, source str
}

bcmd := []string{insideStaticStacker, "internal-go"}
err = c.Execute(append(bcmd, "cp", source, "/stacker/"+path.Base(source)), nil)

if idest == "" || source[len(source)-1:] != "/" {
err = c.Execute(append(bcmd, "cp", source, "/stacker/"+path.Base(source)), nil)
} else {
err = c.Execute(append(bcmd, "cp", source, "/stacker/"), nil)
}
if err != nil {
return err
}
Expand Down
31 changes: 27 additions & 4 deletions pkg/stacker/import.go
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,17 @@ func importFile(imp string, cacheDir string, hash string, idest string, mode *fs
return dest, nil
}

dest := path.Join(cacheDir, path.Base(imp))
var dest string
if imp[len(imp)-1:] != "/" {
if idest != "" && path.Base(imp) != path.Base(idest) {
dest = path.Join(cacheDir, path.Base(idest))
} else {
dest = path.Join(cacheDir, path.Base(imp))
}
} else {
dest = cacheDir
}

if err := os.MkdirAll(dest, 0755); err != nil {
return "", errors.Wrapf(err, "failed making cache dir")
}
Expand Down Expand Up @@ -156,7 +166,20 @@ func importFile(imp string, cacheDir string, hash string, idest string, mode *fs
fallthrough
case mtree.Extra:
srcpath := path.Join(imp, d.Path())
destpath := path.Join(cacheDir, path.Base(imp), d.Path())
var destpath string
if imp[len(imp)-1:] != "/" {
if idest != "" && path.Base(imp) != path.Base(idest) {
if idest[len(idest)-1:] != "/" {
destpath = path.Join(cacheDir, path.Base(idest), d.Path())
} else {
destpath = path.Join(cacheDir, path.Base(imp), d.Path())
}
} else {
destpath = path.Join(cacheDir, path.Base(imp), d.Path())
}
} else {
destpath = path.Join(cacheDir, d.Path())
}

if d.New().IsDir() {
fi, err := os.Lstat(destpath)
Expand Down Expand Up @@ -260,7 +283,7 @@ func acquireUrl(c types.StackerConfig, storage types.Storage, i string, cache st
return "", err
}
defer cleanup()
err = Grab(c, storage, snap, url.Path, cache, mode, uid, gid)
err = Grab(c, storage, snap, url.Path, cache, idest, mode, uid, gid)
if err != nil {
return "", err
}
Expand Down Expand Up @@ -349,7 +372,7 @@ func Import(c types.StackerConfig, storage types.Storage, name string, imports t

dest := i.Dest

if i.Dest[len(i.Dest)-1:] != "/" {
if i.Dest[len(i.Dest)-1:] != "/" && i.Path[len(i.Path)-1:] != "/" {
dest = path.Dir(i.Dest)
}

Expand Down
3 changes: 3 additions & 0 deletions pkg/types/layer.go
Original file line number Diff line number Diff line change
Expand Up @@ -389,6 +389,9 @@ func (l Layer) absolutify(referenceDirectory string) (Layer, error) {
if err != nil {
return ret, err
}
if rawImport.Path[len(rawImport.Path)-1:] == "/" {
absImportPath += "/"
}
absImport := Import{Hash: rawImport.Hash, Path: absImportPath, Dest: rawImport.Dest, Mode: rawImport.Mode, Uid: rawImport.Uid, Gid: rawImport.Gid}
ret.Imports = append(ret.Imports, absImport)
}
Expand Down
124 changes: 124 additions & 0 deletions test/import.bats
Original file line number Diff line number Diff line change
Expand Up @@ -390,6 +390,130 @@ eigth:
[ -f /dir/file2 ]
[ -f /dir/files/file3 ]
EOF
stacker build
}

@test "import with dir contents" {
mkdir folder1
touch folder1/file1
mkdir folder1/subfolder2
touch folder1/subfolder2/subfile1
cat > stacker.yaml <<EOF
first:
from:
type: oci
url: $CENTOS_OCI
import:
- path: folder1/
dest: /folder1/
run: |
[ -f /folder1/file1 ]
second:
from:
type: built
tag: first
run: |
mkdir -p /folder1
touch /folder1/file1
touch /folder1/file2
third:
from:
type: oci
url: $CENTOS_OCI
import:
- path: stacker://second/folder1/
dest: /folder1/
run: |
[ -f /folder1/file1 ]
[ -f /folder1/file2 ]
fourth:
from:
type: oci
url: $CENTOS_OCI
import:
- path: folder1/
dest: /
run: |
ls -l /
[ -f /file1 ]
mkdir -p /folder4/subfolder5
touch /folder4/subfolder5/subfile6
fifth:
from:
type: oci
url: $CENTOS_OCI
import:
- path: folder1/subfolder2/
dest: /folder3/
- path: folder1/subfolder2
dest: /folder4
- path: stacker://fourth/folder4/subfolder5/
dest: /folder6/
- path: stacker://fourth/folder4/
dest: /folder7/
run: |
ls -l /folder*
[ -f /folder3/subfile1 ]
[ ! -e /folder3/subfolder2 ]
[ -f /folder4/subfile1 ]
[ -f /folder6/subfile6 ]
[ ! -e /folder6/subfolder5 ]
[ -f /folder7/subfolder5/subfile6 ]
EOF
stacker build
}

@test "dir path behavior" {
mkdir -p folder1
touch folder1/file1
mkdir -p folder1/subfolder2
touch folder1/subfolder2/subfile1
cat > stacker.yaml <<EOF
src_folder_dest_non_existent_folder_case1:
from:
type: docker
url: docker://ubuntu:latest
import:
- path: folder1
dest: /folder2
run: |
[ -f /folder2/file1 ]
src_folder_dest_non_existent_folder_case2:
from:
type: docker
url: docker://ubuntu:latest
import:
- path: folder1/
dest: /folder2
run: |
[ -f /folder2/file1 ]
src_folder_dest_non_existent_folder_case3:
from:
type: docker
url: docker://ubuntu:latest
import:
- path: folder1
dest: /folder2/
run: |
ls -al /
ls -al /folder2
[ -f /folder2/folder1/file1 ]
src_folder_dest_non_existent_folder_case4:
from:
type: docker
url: docker://ubuntu:latest
import:
- path: folder1/
dest: /folder2/
run: |
[ -f /folder2/file1 ]
EOF
stacker build
}

0 comments on commit 533c4a6

Please sign in to comment.