Skip to content

Commit

Permalink
feat(import): copy dir contents
Browse files Browse the repository at this point in the history
  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]>
  • Loading branch information
rchincha committed Apr 17, 2023
1 parent 479fca8 commit 6e339fe
Show file tree
Hide file tree
Showing 6 changed files with 86 additions and 6 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
8 changes: 6 additions & 2 deletions pkg/stacker/grab.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,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 Down Expand Up @@ -41,7 +41,11 @@ func Grab(sc types.StackerConfig, storage types.Storage, name string, source str
return err
}

err = c.Execute(fmt.Sprintf("/static-stacker internal-go cp %s /stacker/%s", source, path.Base(source)), nil)
if idest == "" || source[len(source)-1:] != "/" {
err = c.Execute(fmt.Sprintf("/static-stacker internal-go cp %s /stacker/%s", source, path.Base(source)), nil)
} else {
err = c.Execute(fmt.Sprintf("/static-stacker internal-go cp %s /stacker/", source), nil)
}
if err != nil {
return err
}
Expand Down
17 changes: 14 additions & 3 deletions pkg/stacker/import.go
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,13 @@ 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:] != "/" {
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 +162,12 @@ 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:] == "/" {
destpath = path.Join(cacheDir, d.Path())
} else {
destpath = path.Join(cacheDir, path.Base(imp), d.Path())
}

if d.New().IsDir() {
fi, err := os.Lstat(destpath)
Expand Down Expand Up @@ -260,7 +271,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
3 changes: 3 additions & 0 deletions pkg/types/layer.go
Original file line number Diff line number Diff line change
Expand Up @@ -406,6 +406,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
47 changes: 47 additions & 0 deletions test/import.bats
Original file line number Diff line number Diff line change
Expand Up @@ -390,6 +390,53 @@ eigth:
[ -f /dir/file2 ]
[ -f /dir/files/file3 ]
EOF
stacker build
}

@test "import with dir contents" {
mkdir folder1
touch folder1/file1
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 ]
EOF
stacker build
}

0 comments on commit 6e339fe

Please sign in to comment.