Skip to content

Commit

Permalink
Apply .s2iignore during copy from local directory
Browse files Browse the repository at this point in the history
- Fixes #867
  • Loading branch information
nabilbendafi committed Mar 25, 2019
1 parent 2ba8a34 commit 3bdb78d
Show file tree
Hide file tree
Showing 9 changed files with 46 additions and 21 deletions.
2 changes: 1 addition & 1 deletion pkg/build/strategies/dockerfile/dockerfile.go
Original file line number Diff line number Diff line change
Expand Up @@ -363,7 +363,7 @@ func (builder *Dockerfile) Prepare(config *api.Config) error {
trimmedSrc := strings.TrimPrefix(injection.Source, filepath.VolumeName(injection.Source))
dst := filepath.Join(config.WorkingDir, constants.Injections, trimmedSrc)
glog.V(4).Infof("Copying injection content from %s to %s", injection.Source, dst)
if err := builder.fs.CopyContents(injection.Source, dst); err != nil {
if err := builder.fs.CopyContents(injection.Source, dst, nil); err != nil {
builder.setFailureReason(utilstatus.ReasonGenericS2IBuildFailed, utilstatus.ReasonMessageGenericS2iBuildFailed)
return err
}
Expand Down
4 changes: 2 additions & 2 deletions pkg/build/strategies/onbuild/onbuild.go
Original file line number Diff line number Diff line change
Expand Up @@ -179,11 +179,11 @@ func (builder *OnBuild) copySTIScripts(config *api.Config) {
sourcePath := filepath.Join(config.WorkingDir, "upload", "src")
if _, err := builder.fs.Stat(filepath.Join(scriptsPath, constants.Run)); err == nil {
glog.V(3).Info("Found S2I 'run' script, copying to application source dir")
builder.fs.Copy(filepath.Join(scriptsPath, constants.Run), filepath.Join(sourcePath, constants.Run))
builder.fs.Copy(filepath.Join(scriptsPath, constants.Run), filepath.Join(sourcePath, constants.Run), nil)
}
if _, err := builder.fs.Stat(filepath.Join(scriptsPath, constants.Assemble)); err == nil {
glog.V(3).Info("Found S2I 'assemble' script, copying to application source dir")
builder.fs.Copy(filepath.Join(scriptsPath, constants.Assemble), filepath.Join(sourcePath, constants.Assemble))
builder.fs.Copy(filepath.Join(scriptsPath, constants.Assemble), filepath.Join(sourcePath, constants.Assemble), nil)
}
}

Expand Down
2 changes: 1 addition & 1 deletion pkg/build/strategies/sti/postexecutorstep.go
Original file line number Diff line number Diff line change
Expand Up @@ -378,7 +378,7 @@ func (step *startRuntimeImageAndUploadFilesStep) copyScriptIfNeeded(script, dest
if err := step.fs.MkdirAll(destinationDir); err != nil {
return fmt.Errorf("could not create directory %q: %v", destinationDir, err)
}
if err := step.fs.Copy(src, dst); err != nil {
if err := step.fs.Copy(src, dst, nil); err != nil {
return fmt.Errorf("could not copy file (%q -> %q): %v", src, dst, err)
}
}
Expand Down
6 changes: 4 additions & 2 deletions pkg/ignore/ignore.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ func (b *DockerIgnorer) Ignore(config *api.Config) error {
5) del files
1 to 4 is in getListOfFilesToIgnore
*/
filesToDel, lerr := getListOfFilesToIgnore(config.WorkingSourceDir)
filesToDel, lerr := b.GetListOfFilesToIgnore(config.WorkingSourceDir)
if lerr != nil {
return lerr
}
Expand All @@ -53,7 +53,9 @@ func (b *DockerIgnorer) Ignore(config *api.Config) error {
return nil
}

func getListOfFilesToIgnore(workingDir string) (map[string]string, error) {
// GetListOfFilesToIgnore returns list of files from the workspace based on the contents of the
// .s2iignore file
func (b *DockerIgnorer) GetListOfFilesToIgnore(workingDir string) (map[string]string, error) {
path := filepath.Join(workingDir, constants.IgnoreFile)
file, err := os.Open(path)
if err != nil {
Expand Down
9 changes: 8 additions & 1 deletion pkg/scm/downloaders/file/download.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (

"github.com/openshift/source-to-image/pkg/api"
"github.com/openshift/source-to-image/pkg/api/constants"
"github.com/openshift/source-to-image/pkg/ignore"
"github.com/openshift/source-to-image/pkg/scm/git"
"github.com/openshift/source-to-image/pkg/util/fs"
utilglog "github.com/openshift/source-to-image/pkg/util/glog"
Expand Down Expand Up @@ -47,9 +48,15 @@ func (f *File) Download(config *api.Config) (*git.SourceInfo, error) {
return nil, RecursiveCopyError{error: fmt.Errorf("recursive copy requested, source directory %q contains the target directory %q", copySrc, config.WorkingSourceDir)}
}

di := ignore.DockerIgnorer{}
filesToIgnore, lerr := di.GetListOfFilesToIgnore(copySrc)
if lerr != nil {
return nil, lerr
}

if copySrc != config.WorkingSourceDir {
f.KeepSymlinks(config.KeepSymlinks)
err := f.CopyContents(copySrc, config.WorkingSourceDir)
err := f.CopyContents(copySrc, config.WorkingSourceDir, filesToIgnore)
if err != nil {
return nil, err
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/scm/downloaders/git/clone.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ func (c *Clone) Download(config *api.Config) (*git.SourceInfo, error) {
originalTargetDir := filepath.Join(config.WorkingDir, constants.Source)
c.RemoveDirectory(originalTargetDir)
path := filepath.Join(targetSourceDir, config.ContextDir)
err := c.CopyContents(path, originalTargetDir)
err := c.CopyContents(path, originalTargetDir, nil)
if err != nil {
return nil, err
}
Expand Down
8 changes: 6 additions & 2 deletions pkg/test/fs/fs.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ type FakeFileSystem struct {
CopyDest string
CopyError error

FilesToIgnore map[string]string

RemoveDirName string
RemoveDirError error

Expand Down Expand Up @@ -159,16 +161,18 @@ func (f *FakeFileSystem) Exists(file string) bool {
}

// Copy copies files on the fake filesystem
func (f *FakeFileSystem) Copy(sourcePath, targetPath string) error {
func (f *FakeFileSystem) Copy(sourcePath, targetPath string, filesToIgnore map[string]string) error {
f.CopySource = sourcePath
f.CopyDest = targetPath
f.FilesToIgnore = filesToIgnore
return f.CopyError
}

// CopyContents copies directory contents on the fake filesystem
func (f *FakeFileSystem) CopyContents(sourcePath, targetPath string) error {
func (f *FakeFileSystem) CopyContents(sourcePath, targetPath string, filesToIgnore map[string]string) error {
f.CopySource = sourcePath
f.CopyDest = targetPath
f.FilesToIgnore = filesToIgnore
return f.CopyError
}

Expand Down
28 changes: 20 additions & 8 deletions pkg/util/fs/fs.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@ type FileSystem interface {
MkdirAllWithPermissions(dirname string, perm os.FileMode) error
Mkdir(dirname string) error
Exists(file string) bool
Copy(sourcePath, targetPath string) error
CopyContents(sourcePath, targetPath string) error
Copy(sourcePath, targetPath string, filesToIgnore map[string]string) error
CopyContents(sourcePath, targetPath string, filesToIgnore map[string]string) error
RemoveDirectory(dir string) error
CreateWorkingDirectory() (string, error)
Open(file string) (io.ReadCloser, error)
Expand Down Expand Up @@ -186,8 +186,8 @@ func (h *fs) Exists(file string) bool {
// If the source is a directory, then the destination has to be a directory and
// we copy the content of the source directory to destination directory
// recursively.
func (h *fs) Copy(source string, dest string) (err error) {
return doCopy(h, source, dest)
func (h *fs) Copy(source string, dest string, filesToIgnore map[string]string) (err error) {
return doCopy(h, source, dest, filesToIgnore)
}

// KeepSymlinks configures fs to copy symlinks from src as symlinks to dst.
Expand Down Expand Up @@ -229,7 +229,7 @@ func handleSymlink(h FileSystem, source, dest string) (bool, error) {
return false, nil
}

func doCopy(h FileSystem, source, dest string) error {
func doCopy(h FileSystem, source, dest string, filesToIgnore map[string]string) error {
if handled, err := handleSymlink(h, source, dest); handled || err != nil {
return err
}
Expand All @@ -244,8 +244,13 @@ func doCopy(h FileSystem, source, dest string) error {
}

if sourceinfo.IsDir() {
_, ok := filesToIgnore[source]
if ok {
glog.V(5).Infof("Directory %q ignored", source)
return nil
}
glog.V(5).Infof("D %q -> %q", source, dest)
return h.CopyContents(source, dest)
return h.CopyContents(source, dest, filesToIgnore)
}

destinfo, _ := h.Stat(dest)
Expand All @@ -257,6 +262,11 @@ func doCopy(h FileSystem, source, dest string) error {
return err
}
defer destfile.Close()
_, ok := filesToIgnore[source]
if ok {
glog.V(5).Infof("File %q ignored", source)
return nil
}
glog.V(5).Infof("F %q -> %q", source, dest)
if _, err := io.Copy(destfile, sourcefile); err != nil {
return err
Expand All @@ -270,7 +280,8 @@ func doCopy(h FileSystem, source, dest string) error {
// If the destination directory does not exists, it will be created.
// The source directory itself will not be copied, only its content. If you
// want this behavior, the destination must include the source directory name.
func (h *fs) CopyContents(src string, dest string) (err error) {
// It will skip any files provided in filesToIgnore from being copied
func (h *fs) CopyContents(src string, dest string, filesToIgnore map[string]string) (err error) {
sourceinfo, err := h.Stat(src)
if err != nil {
return err
Expand All @@ -287,10 +298,11 @@ func (h *fs) CopyContents(src string, dest string) (err error) {
if err != nil {
return err
}

for _, obj := range objects {
source := path.Join(src, obj.Name())
destination := path.Join(dest, obj.Name())
if err := h.Copy(source, destination); err != nil {
if err := h.Copy(source, destination, filesToIgnore); err != nil {
return err
}
}
Expand Down
6 changes: 3 additions & 3 deletions pkg/util/fs/fs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ func helper(t *testing.T, keepSymlinks bool) {
OpenContent: "test",
}
fake.KeepSymlinks(keepSymlinks)
err := doCopy(fake, sep+"file", sep+"dest")
err := doCopy(fake, sep+"file", sep+"dest", nil)
if err != nil {
t.Error(err)
}
Expand All @@ -45,7 +45,7 @@ func helper(t *testing.T, keepSymlinks bool) {
ReadlinkName: sep + "linkdest",
}
fake.KeepSymlinks(keepSymlinks)
err = doCopy(fake, sep+"link", sep+"dest")
err = doCopy(fake, sep+"link", sep+"dest", nil)
if err != nil {
t.Error(err)
}
Expand All @@ -66,7 +66,7 @@ func helper(t *testing.T, keepSymlinks bool) {
ReadlinkName: sep + "file",
}
fake.KeepSymlinks(keepSymlinks)
err = doCopy(fake, sep+"link", sep+"dest")
err = doCopy(fake, sep+"link", sep+"dest", nil)
if err != nil {
t.Error(err)
}
Expand Down

0 comments on commit 3bdb78d

Please sign in to comment.