forked from openshift/origin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
sourcelookup.go
376 lines (330 loc) · 9.76 KB
/
sourcelookup.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
package app
import (
"fmt"
"io/ioutil"
"net/url"
"path/filepath"
"regexp"
"strings"
"github.com/docker/docker/builder/parser"
"github.com/openshift/origin/pkg/generate/dockerfile"
"github.com/openshift/origin/pkg/generate/git"
"github.com/openshift/origin/pkg/generate/source"
)
var (
argumentGit = regexp.MustCompile("^(http://|https://|git@|git://).*(?:#([a-zA-Z0-9]*))?$")
argumentGitProtocol = regexp.MustCompile("^(git@|git://)")
)
type Dockerfile interface {
AST() *parser.Node
Contents() string
}
func NewDockerfileFromFile(path string) (Dockerfile, error) {
data, err := ioutil.ReadFile(path)
if err != nil {
return nil, err
}
if len(data) == 0 {
return nil, fmt.Errorf("Dockerfile %q is empty", path)
}
return NewDockerfile(string(data))
}
func NewDockerfile(contents string) (Dockerfile, error) {
if len(contents) == 0 {
return nil, fmt.Errorf("Dockerfile is empty")
}
node, err := parser.Parse(strings.NewReader(contents))
if err != nil {
return nil, err
}
return dockerfileContents{node, contents}, nil
}
type dockerfileContents struct {
ast *parser.Node
contents string
}
func (d dockerfileContents) AST() *parser.Node {
return d.ast
}
func (d dockerfileContents) Contents() string {
return d.contents
}
// IsPossibleSourceRepository checks whether the provided string is a source repository or not
func IsPossibleSourceRepository(s string) bool {
return IsRemoteRepository(s) || isDirectory(s)
}
// IsRemoteRepository checks whether the provided string is a remote repository or not
func IsRemoteRepository(s string) bool {
return argumentGit.MatchString(s) || argumentGitProtocol.MatchString(s)
}
// SourceRepository represents a code repository that may be the target of a build.
type SourceRepository struct {
location string
url url.URL
localDir string
remoteURL *url.URL
contextDir string
info *SourceRepositoryInfo
usedBy []ComponentReference
buildWithDocker bool
ignoreRepository bool
binary bool
}
// NewSourceRepository creates a reference to a local or remote source code repository from
// a URL or path.
func NewSourceRepository(s string) (*SourceRepository, error) {
location, err := git.ParseRepository(s)
if err != nil {
return nil, err
}
return &SourceRepository{
location: s,
url: *location,
}, nil
}
// NewSourceRepositoryForDockerfile creates a source repository that is set up to use
// the contents of a Dockerfile as the input of the build.
func NewSourceRepositoryForDockerfile(contents string) (*SourceRepository, error) {
dockerfile, err := NewDockerfile(contents)
if err != nil {
return nil, err
}
return &SourceRepository{
buildWithDocker: true,
ignoreRepository: true,
info: &SourceRepositoryInfo{
Dockerfile: dockerfile,
},
}, nil
}
// NewBinarySourceRepository creates a source repository that is configured for binary
// input.
func NewBinarySourceRepository() *SourceRepository {
return &SourceRepository{
binary: true,
ignoreRepository: true,
}
}
// UsedBy sets up which component uses the source repository
func (r *SourceRepository) UsedBy(ref ComponentReference) {
r.usedBy = append(r.usedBy, ref)
}
// Remote checks whether the source repository is remote
func (r *SourceRepository) Remote() bool {
return r.url.Scheme != "file"
}
// InUse checks if the source repository is in use
func (r *SourceRepository) InUse() bool {
return len(r.usedBy) > 0
}
// BuildWithDocker specifies that the source repository was built with Docker
func (r *SourceRepository) BuildWithDocker() {
r.buildWithDocker = true
}
// IsDockerBuild checks if the source repository was built with Docker
func (r *SourceRepository) IsDockerBuild() bool {
return r.buildWithDocker
}
func (r *SourceRepository) String() string {
return r.location
}
// Detect clones source locally if not already local and runs code detection
// with the given detector.
func (r *SourceRepository) Detect(d Detector) error {
if r.info != nil {
return nil
}
path, err := r.LocalPath()
if err != nil {
return err
}
r.info, err = d.Detect(path)
if err != nil {
return err
}
return nil
}
// SetInfo sets the source repository info. This is to facilitate certain tests.
func (r *SourceRepository) SetInfo(info *SourceRepositoryInfo) {
r.info = info
}
// Info returns the source repository info generated on code detection
func (r *SourceRepository) Info() *SourceRepositoryInfo {
return r.info
}
// LocalPath returns the local path of the source repository
func (r *SourceRepository) LocalPath() (string, error) {
if len(r.localDir) > 0 {
return r.localDir, nil
}
switch {
case r.url.Scheme == "file":
r.localDir = filepath.Join(r.url.Path, r.contextDir)
default:
gitRepo := git.NewRepository()
var err error
if r.localDir, err = ioutil.TempDir("", "gen"); err != nil {
return "", err
}
localURL := r.url
ref := localURL.Fragment
localURL.Fragment = ""
if err = gitRepo.Clone(r.localDir, localURL.String()); err != nil {
return "", fmt.Errorf("cannot clone repository %s: %v", localURL.String(), err)
}
if len(ref) > 0 {
if err = gitRepo.Checkout(r.localDir, ref); err != nil {
return "", fmt.Errorf("cannot checkout ref %s of repository %s: %v", ref, localURL.String(), err)
}
}
r.localDir = filepath.Join(r.localDir, r.contextDir)
}
return r.localDir, nil
}
// RemoteURL returns the remote URL of the source repository
func (r *SourceRepository) RemoteURL() (*url.URL, error) {
if r.remoteURL != nil {
return r.remoteURL, nil
}
switch r.url.Scheme {
case "file":
gitRepo := git.NewRepository()
remote, _, err := gitRepo.GetOriginURL(r.url.Path)
if err != nil {
return nil, err
}
ref := gitRepo.GetRef(r.url.Path)
if len(ref) > 0 {
remote = fmt.Sprintf("%s#%s", remote, ref)
}
if r.remoteURL, err = url.Parse(remote); err != nil {
return nil, err
}
default:
r.remoteURL = &r.url
}
return r.remoteURL, nil
}
// SetContextDir sets the context directory to use for the source repository
func (r *SourceRepository) SetContextDir(dir string) {
r.contextDir = dir
}
// ContextDir returns the context directory of the source repository
func (r *SourceRepository) ContextDir() string {
return r.contextDir
}
// SourceRepositories is a list of SourceRepository objects
type SourceRepositories []*SourceRepository
func (rr SourceRepositories) String() string {
repos := []string{}
for _, r := range rr {
repos = append(repos, r.String())
}
return strings.Join(repos, ",")
}
// NotUsed returns the list of SourceRepositories that are not used
func (rr SourceRepositories) NotUsed() SourceRepositories {
notUsed := SourceRepositories{}
for _, r := range rr {
if !r.InUse() {
notUsed = append(notUsed, r)
}
}
return notUsed
}
// SourceRepositoryInfo contains info about a source repository
type SourceRepositoryInfo struct {
Path string
Types []SourceLanguageType
Dockerfile Dockerfile
}
// Terms returns which languages the source repository was
// built with
func (info *SourceRepositoryInfo) Terms() []string {
terms := []string{}
for i := range info.Types {
terms = append(terms, info.Types[i].Term())
}
return terms
}
// SourceLanguageType contains info about the type of the language
// a source repository is built in
type SourceLanguageType struct {
Platform string
Version string
}
// Term returns a search term for the given source language type
// the term will be in the form of language:version
func (t *SourceLanguageType) Term() string {
if len(t.Version) == 0 {
return t.Platform
}
return fmt.Sprintf("%s:%s", t.Platform, t.Version)
}
// Detector is an interface for detecting information about a
// source repository
type Detector interface {
Detect(dir string) (*SourceRepositoryInfo, error)
}
// SourceRepositoryEnumerator implements the Detector interface
type SourceRepositoryEnumerator struct {
Detectors source.Detectors
Tester dockerfile.Tester
}
// ErrNoLanguageDetected is the error returned when no language can be detected by all
// source code detectors.
var ErrNoLanguageDetected = fmt.Errorf("No language matched the source repository")
// Detect extracts source code information about the provided source repository
func (e SourceRepositoryEnumerator) Detect(dir string) (*SourceRepositoryInfo, error) {
info := &SourceRepositoryInfo{
Path: dir,
}
for _, d := range e.Detectors {
if detected, ok := d(dir); ok {
info.Types = append(info.Types, SourceLanguageType{
Platform: detected.Platform,
Version: detected.Version,
})
}
}
if path, ok, err := e.Tester.Has(dir); err == nil && ok {
dockerfile, err := NewDockerfileFromFile(path)
if err != nil {
return nil, err
}
info.Dockerfile = dockerfile
}
if info.Dockerfile == nil && len(info.Types) == 0 {
return nil, ErrNoLanguageDetected
}
return info, nil
}
// StrategyAndSourceForRepository returns the build strategy and source code reference
// of the provided source repository
// TODO: user should be able to choose whether to download a remote source ref for
// more info
func StrategyAndSourceForRepository(repo *SourceRepository, image *ImageRef) (*BuildStrategyRef, *SourceRef, error) {
if image == nil {
return nil, nil, fmt.Errorf("an image ref is required to generate a strategy and sourceref")
}
strategy := &BuildStrategyRef{
Base: image,
IsDockerBuild: repo.IsDockerBuild(),
}
source := &SourceRef{
Binary: repo.binary,
}
if repo.ignoreRepository && repo.Info() != nil && repo.Info().Dockerfile != nil {
source.DockerfileContents = repo.Info().Dockerfile.Contents()
}
if !repo.ignoreRepository {
remoteURL, err := repo.RemoteURL()
if err != nil {
return nil, nil, fmt.Errorf("cannot obtain remote URL for repository at %s", repo.location)
}
source.URL = remoteURL
source.Ref = remoteURL.Fragment
source.ContextDir = repo.ContextDir()
}
return strategy, source, nil
}