forked from openshift/origin
/
installer.go
196 lines (167 loc) · 4.98 KB
/
installer.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
package script
import (
"net/url"
"path/filepath"
"sync"
"github.com/golang/glog"
"github.com/openshift/source-to-image/pkg/sti/api"
"github.com/openshift/source-to-image/pkg/sti/docker"
"github.com/openshift/source-to-image/pkg/sti/errors"
"github.com/openshift/source-to-image/pkg/sti/util"
)
// Installer interface is responsible for installing scripts needed to run the build
type Installer interface {
DownloadAndInstall(scripts []api.Script, workingDir string, required bool) (bool, error)
}
// NewInstaller returns a new instance of the default Installer implementation
func NewInstaller(image, scriptsURL string, docker docker.Docker) Installer {
handler := &handler{
image: image,
scriptsURL: scriptsURL,
docker: docker,
downloader: util.NewDownloader(),
fs: util.NewFileSystem(),
}
return &installer{handler}
}
type installer struct {
handler scriptHandler
}
type handler struct {
docker docker.Docker
image string
scriptsURL string
downloader util.Downloader
fs util.FileSystem
}
type scriptHandler interface {
download(scripts []api.Script, workingDir string) (bool, error)
getPath(script api.Script, workingDir string) string
install(scriptPath string, workingDir string) error
}
type scriptInfo struct {
url *url.URL
name api.Script
}
// DownloadAndInstall downloads and installs a set of scripts using the specified
// working directory. If the required flag is specified and a particular script
// cannot be found, an error is returned, additionally the method returns information
// whether the download actually happened.
func (i *installer) DownloadAndInstall(scripts []api.Script, workingDir string, required bool) (bool, error) {
download, err := i.handler.download(scripts, workingDir)
if err != nil {
return false, err
}
if !download {
return false, nil
}
for _, script := range scripts {
scriptPath := i.handler.getPath(script, workingDir)
if required && scriptPath == "" {
return false, errors.NewScriptDownloadError(script, nil)
}
if err := i.handler.install(scriptPath, workingDir); err != nil {
return false, err
}
}
return true, nil
}
func (s *handler) download(scripts []api.Script, workingDir string) (bool, error) {
if len(scripts) == 0 {
return false, nil
}
wg := sync.WaitGroup{}
errs := make(map[api.Script]chan error)
downloads := make(map[api.Script]chan bool)
for _, s := range scripts {
errs[s] = make(chan error, 2)
downloads[s] = make(chan bool, 2)
}
downloadAsync := func(script api.Script, scriptUrl *url.URL, targetFile string) {
defer wg.Done()
download, err := s.downloader.DownloadFile(scriptUrl, targetFile)
if err != nil {
return
}
downloads[script] <- download
if !download {
return
}
if err := s.fs.Chmod(targetFile, 0700); err != nil {
errs[script] <- err
}
}
if s.scriptsURL != "" {
destDir := filepath.Join(workingDir, "/downloads/scripts")
for file, info := range s.prepareDownload(scripts, destDir, s.scriptsURL) {
wg.Add(1)
go downloadAsync(info.name, info.url, file)
}
}
defaultURL, err := s.docker.GetScriptsURL(s.image)
if err != nil {
return false, errors.NewDefaultScriptsURLError(err)
}
if defaultURL != "" {
destDir := filepath.Join(workingDir, "/downloads/defaultScripts")
for file, info := range s.prepareDownload(scripts, destDir, defaultURL) {
wg.Add(1)
go downloadAsync(info.name, info.url, file)
}
}
// Wait for the script downloads to finish
wg.Wait()
for s, d := range downloads {
if len(d) == 0 {
return false, errors.NewScriptDownloadError(s, nil)
}
if download := <-d; !download {
return false, nil
}
}
for s, e := range errs {
if len(e) > 0 {
return false, errors.NewScriptDownloadError(s, <-e)
}
}
return true, nil
}
func (s *handler) getPath(script api.Script, workingDir string) string {
locations := []string{
"downloads/scripts",
"upload/src/.sti/bin",
"downloads/defaultScripts",
}
descriptions := []string{
"user provided url",
"application source",
"default url reference in the image",
}
for i, location := range locations {
path := filepath.Join(workingDir, location, string(script))
glog.V(2).Infof("Looking for %s script at %s", script, path)
if s.fs.Exists(path) {
glog.V(2).Infof("Found %s script from %s.", script, descriptions[i])
return path
}
}
return ""
}
func (s *handler) install(path string, workingDir string) error {
script := filepath.Base(path)
return s.fs.Rename(path, filepath.Join(workingDir, "upload/scripts", script))
}
// prepareScriptDownload turns the script name into proper URL
func (s *handler) prepareDownload(scripts []api.Script, targetDir, baseURL string) map[string]scriptInfo {
s.fs.MkdirAll(targetDir)
info := make(map[string]scriptInfo)
for _, script := range scripts {
url, err := url.Parse(baseURL + "/" + string(script))
if err != nil {
glog.Warningf("Unable to parse script URL: %s/%s", baseURL, script)
continue
}
info[targetDir+"/"+string(script)] = scriptInfo{url, script}
}
return info
}