Skip to content

Commit 79dfe89

Browse files
author
OpenShift Bot
committed
Merge pull request #460 from gabemontero/457
Merged by openshift-bot
2 parents 4c24fc7 + 2b26067 commit 79dfe89

File tree

8 files changed

+93
-7
lines changed

8 files changed

+93
-7
lines changed

cmd/s2i/main.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ import (
2525
"github.com/openshift/source-to-image/pkg/docker"
2626
"github.com/openshift/source-to-image/pkg/errors"
2727
"github.com/openshift/source-to-image/pkg/run"
28+
"github.com/openshift/source-to-image/pkg/tar"
2829
"github.com/openshift/source-to-image/pkg/util"
2930
"github.com/openshift/source-to-image/pkg/version"
3031
)
@@ -171,6 +172,7 @@ $ s2i build . centos/ruby-22-centos7 hello-world-app
171172
buildCmd.Flags().StringVarP(&(cfg.Ref), "ref", "r", "", "Specify a ref to check-out")
172173
buildCmd.Flags().StringVarP(&(cfg.AssembleUser), "assemble-user", "", "", "Specify the user to run assemble with")
173174
buildCmd.Flags().StringVarP(&(cfg.ContextDir), "context-dir", "", "", "Specify the sub-directory inside the repository with the application sources")
175+
buildCmd.Flags().StringVarP(&(cfg.ExcludeRegExp), "exclude", "", tar.DefaultExclusionPattern.String(), "Regular expression for selecting files from the source tree to exclude from the build, where the default excludes the '.git' directory (see https://golang.org/pkg/regexp for syntax, but note that \"\" will be interpreted as allow all files and exclude no files)")
174176
buildCmd.Flags().StringVarP(&(cfg.ScriptsURL), "scripts-url", "s", "", "Specify a URL for the assemble and run scripts")
175177
buildCmd.Flags().StringVar(&(oldScriptsFlag), "scripts", "", "DEPRECATED: Specify a URL for the assemble and run scripts")
176178
buildCmd.Flags().BoolVar(&(useConfig), "use-config", false, "Store command line options to .stifile")

contrib/bash/s2i

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -212,6 +212,7 @@ _s2i_build()
212212
two_word_flags+=("-e")
213213
flags+=("--environment-file=")
214214
two_word_flags+=("-E")
215+
flags+=("--exclude=")
215216
flags+=("--force-pull")
216217
flags+=("--incremental")
217218
flags+=("--incremental-pull-policy=")

docs/cli.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,7 @@ that image and add them to the tar streamed to the container into `/artifacts`.
7878
| `--incremental` | Try to perform an incremental build |
7979
| `-e (--env)` | Environment variables to be passed to the builder eg. `NAME=VALUE,NAME2=VALUE2,...` |
8080
| `-E (--environment-file)` | Specify the path to the file with environment |
81+
| `--exclude` | Regular expression for selecting files from the source tree to exclude from the build, where the default excludes the '.git' directory (see https://golang.org/pkg/regexp for syntax, but note that \"\" will be interpreted as allow all files and exclude no files) |
8182
| `--force-pull` | Always pull the builder image, even if it is present locally (defaults to true) |
8283
| `--run` | Launch the resulting image after a successful build. All output from the image is being printed to help determine image's validity. In case of a long running image you will have to Ctrl-C to exit both s2i and the running container. (defaults to false) |
8384
| `-r (--ref)` | A branch/tag that the build should use instead of MASTER (applies only to Git source) |

pkg/api/types.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -188,6 +188,10 @@ type Config struct {
188188
// ScriptDownloadProxyConfig optionally specifies the http and https proxy
189189
// to use when downloading scripts
190190
ScriptDownloadProxyConfig *ProxyConfig
191+
192+
// ExcludeRegExp contains a string representation of the regular expression desired for
193+
// deciding which files to exclude from the tar stream
194+
ExcludeRegExp string
191195
}
192196

193197
type ProxyConfig struct {

pkg/build/strategies/layered/layered.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import (
99
"os"
1010
"path"
1111
"path/filepath"
12+
"regexp"
1213
"time"
1314

1415
"github.com/golang/glog"
@@ -35,11 +36,13 @@ func New(config *api.Config, scripts build.ScriptsHandler, overrides build.Overr
3536
if err != nil {
3637
return nil, err
3738
}
39+
tarHandler := tar.New()
40+
tarHandler.SetExclusionPattern(regexp.MustCompile(config.ExcludeRegExp))
3841
return &Layered{
3942
docker: d,
4043
config: config,
4144
fs: util.NewFileSystem(),
42-
tar: tar.New(),
45+
tar: tarHandler,
4346
scripts: scripts,
4447
}, nil
4548
}

pkg/build/strategies/sti/sti.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,8 @@ func New(req *api.Config, overrides build.Overrides) (*STI, error) {
8484
}
8585

8686
inst := scripts.NewInstaller(req.BuilderImage, req.ScriptsURL, req.ScriptDownloadProxyConfig, docker, req.PullAuthentication)
87+
tarHandler := tar.New()
88+
tarHandler.SetExclusionPattern(regexp.MustCompile(req.ExcludeRegExp))
8789

8890
b := &STI{
8991
installer: inst,
@@ -92,7 +94,7 @@ func New(req *api.Config, overrides build.Overrides) (*STI, error) {
9294
incrementalDocker: incrementalDocker,
9395
git: git.New(),
9496
fs: util.NewFileSystem(),
95-
tar: tar.New(),
97+
tar: tarHandler,
9698
callbackInvoker: util.NewCallbackInvoker(),
9799
requiredScripts: []string{api.Assemble, api.Run},
98100
optionalScripts: []string{api.SaveArtifacts},

pkg/tar/tar.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,9 @@ import (
2222
// connections in which it would wait for a long time to untar and nothing would happen
2323
const defaultTimeout = 30 * time.Second
2424

25-
// defaultExclusionPattern is the pattern of files that will not be included in a tar
25+
// DefaultExclusionPattern is the pattern of files that will not be included in a tar
2626
// file when creating one. By default it is any file inside a .git metadata directory
27-
var defaultExclusionPattern = regexp.MustCompile("((^\\.git\\/)|(\\/.git\\/)|(\\/.git$))")
27+
var DefaultExclusionPattern = regexp.MustCompile("((^\\.git\\/)|(\\/.git\\/)|(\\/.git$))")
2828

2929
// Tar can create and extract tar files used in an STI build
3030
type Tar interface {
@@ -74,7 +74,7 @@ type Tar interface {
7474
// New creates a new Tar
7575
func New() Tar {
7676
return &stiTar{
77-
exclude: defaultExclusionPattern,
77+
exclude: DefaultExclusionPattern,
7878
timeout: defaultTimeout,
7979
}
8080
}
@@ -174,7 +174,7 @@ func (t *stiTar) CreateTarFile(base, dir string) (string, error) {
174174
}
175175

176176
func (t *stiTar) shouldExclude(path string) bool {
177-
return t.exclude != nil && t.exclude.MatchString(path)
177+
return t.exclude != nil && t.exclude.String() != "" && t.exclude.MatchString(path)
178178
}
179179

180180
// CreateTarStream calls CreateTarStreamWithLogging with a nil logger

pkg/tar/tar_test.go

Lines changed: 74 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77
"io/ioutil"
88
"os"
99
"path/filepath"
10+
"regexp"
1011
"sync"
1112
"testing"
1213
"time"
@@ -119,7 +120,7 @@ func verifyTarFile(t *testing.T, filename string, files []fileDesc, links []link
119120
}
120121

121122
if len(filesToVerify) > 0 || len(linksToVerify) > 0 {
122-
t.Errorf("Did not find all expected files in tar: %v, %v", filesToVerify, linksToVerify)
123+
t.Errorf("Did not find all expected files in tar: fileToVerify %v, linksToVerify %v", filesToVerify, linksToVerify)
123124
}
124125
}
125126

@@ -192,6 +193,78 @@ func TestCreateTar(t *testing.T) {
192193
verifyTarFile(t, tarFile, testFiles, testLinks)
193194
}
194195

196+
func TestCreateTarIncludeDotGit(t *testing.T) {
197+
th := New()
198+
th.SetExclusionPattern(regexp.MustCompile("test3.txt"))
199+
tempDir, err := ioutil.TempDir("", "testtar")
200+
defer os.RemoveAll(tempDir)
201+
if err != nil {
202+
t.Fatalf("Cannot create temp directory for test: %v", err)
203+
}
204+
modificationDate := time.Date(2011, time.March, 5, 23, 30, 1, 0, time.UTC)
205+
testFiles := []fileDesc{
206+
{"dir01/dir02/test1.txt", modificationDate, 0700, "Test1 file content", false, ""},
207+
{"dir01/test2.git", modificationDate, 0660, "Test2 file content", false, ""},
208+
{"dir01/dir03/test3.txt", modificationDate, 0444, "Test3 file content", true, ""},
209+
{"dir01/.git/hello.txt", modificationDate, 0600, "Allow .git content", false, ""},
210+
}
211+
if err := createTestFiles(tempDir, testFiles); err != nil {
212+
t.Fatalf("Cannot create test files: %v", err)
213+
}
214+
testLinks := []linkDesc{
215+
{"link/okfilelink", "../dir01/dir02/test1.txt"},
216+
{"link/errfilelink", "../dir01/missing.target"},
217+
{"link/okdirlink", "../dir01/dir02"},
218+
{"link/okdirlink2", "../dir01/.git"},
219+
}
220+
if err := createTestLinks(tempDir, testLinks); err != nil {
221+
t.Fatalf("Cannot create link files: %v", err)
222+
}
223+
224+
tarFile, err := th.CreateTarFile("", tempDir)
225+
defer os.Remove(tarFile)
226+
if err != nil {
227+
t.Fatalf("Unable to create new tar upload file: %v", err)
228+
}
229+
verifyTarFile(t, tarFile, testFiles, testLinks)
230+
}
231+
232+
func TestCreateTarEmptyRegexp(t *testing.T) {
233+
th := New()
234+
th.SetExclusionPattern(regexp.MustCompile(""))
235+
tempDir, err := ioutil.TempDir("", "testtar")
236+
defer os.RemoveAll(tempDir)
237+
if err != nil {
238+
t.Fatalf("Cannot create temp directory for test: %v", err)
239+
}
240+
modificationDate := time.Date(2011, time.March, 5, 23, 30, 1, 0, time.UTC)
241+
testFiles := []fileDesc{
242+
{"dir01/dir02/test1.txt", modificationDate, 0700, "Test1 file content", false, ""},
243+
{"dir01/test2.git", modificationDate, 0660, "Test2 file content", false, ""},
244+
{"dir01/dir03/test3.txt", modificationDate, 0444, "Test3 file content", false, ""},
245+
{"dir01/.git/hello.txt", modificationDate, 0600, "Allow .git content", false, ""},
246+
}
247+
if err := createTestFiles(tempDir, testFiles); err != nil {
248+
t.Fatalf("Cannot create test files: %v", err)
249+
}
250+
testLinks := []linkDesc{
251+
{"link/okfilelink", "../dir01/dir02/test1.txt"},
252+
{"link/errfilelink", "../dir01/missing.target"},
253+
{"link/okdirlink", "../dir01/dir02"},
254+
{"link/okdirlink2", "../dir01/.git"},
255+
}
256+
if err := createTestLinks(tempDir, testLinks); err != nil {
257+
t.Fatalf("Cannot create link files: %v", err)
258+
}
259+
260+
tarFile, err := th.CreateTarFile("", tempDir)
261+
defer os.Remove(tarFile)
262+
if err != nil {
263+
t.Fatalf("Unable to create new tar upload file: %v", err)
264+
}
265+
verifyTarFile(t, tarFile, testFiles, testLinks)
266+
}
267+
195268
func createTestTar(files []fileDesc, writer io.Writer) error {
196269
tw := tar.NewWriter(writer)
197270
defer tw.Close()

0 commit comments

Comments
 (0)