forked from cloudfoundry/cli
-
Notifications
You must be signed in to change notification settings - Fork 0
/
buildpack.go
277 lines (227 loc) · 7 KB
/
buildpack.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
package v7action
import (
"archive/zip"
"io"
"os"
"path/filepath"
"code.cloudfoundry.org/cli/actor/actionerror"
"code.cloudfoundry.org/cli/api/cloudcontroller/ccerror"
"code.cloudfoundry.org/cli/api/cloudcontroller/ccv3"
"code.cloudfoundry.org/cli/util"
)
type Buildpack ccv3.Buildpack
type JobURL ccv3.JobURL
//go:generate counterfeiter . Downloader
type Downloader interface {
Download(url string, tmpDirPath string) (string, error)
}
func (actor Actor) GetBuildpacks() ([]Buildpack, Warnings, error) {
ccv3Buildpacks, warnings, err := actor.CloudControllerClient.GetBuildpacks(ccv3.Query{
Key: ccv3.OrderBy,
Values: []string{ccv3.PositionOrder},
})
var buildpacks []Buildpack
for _, buildpack := range ccv3Buildpacks {
buildpacks = append(buildpacks, Buildpack(buildpack))
}
return buildpacks, Warnings(warnings), err
}
// GetBuildpackByNameAndStack returns a buildpack with the provided name and
// stack. If `buildpackStack` is not specified, and there are multiple
// buildpacks with the same name, it will return the one with no stack, if
// present.
func (actor Actor) GetBuildpackByNameAndStack(buildpackName string, buildpackStack string) (Buildpack, Warnings, error) {
var (
ccv3Buildpacks []ccv3.Buildpack
warnings ccv3.Warnings
err error
)
if buildpackStack == "" {
ccv3Buildpacks, warnings, err = actor.CloudControllerClient.GetBuildpacks(ccv3.Query{
Key: ccv3.NameFilter,
Values: []string{buildpackName},
})
} else {
ccv3Buildpacks, warnings, err = actor.CloudControllerClient.GetBuildpacks(
ccv3.Query{
Key: ccv3.NameFilter,
Values: []string{buildpackName},
},
ccv3.Query{
Key: ccv3.StackFilter,
Values: []string{buildpackStack},
},
)
}
if err != nil {
return Buildpack{}, Warnings(warnings), err
}
if len(ccv3Buildpacks) == 0 {
return Buildpack{}, Warnings(warnings), actionerror.BuildpackNotFoundError{BuildpackName: buildpackName, StackName: buildpackStack}
}
if len(ccv3Buildpacks) > 1 {
for _, buildpack := range ccv3Buildpacks {
if buildpack.Stack == "" {
return Buildpack(buildpack), Warnings(warnings), nil
}
}
return Buildpack{}, Warnings(warnings), actionerror.MultipleBuildpacksFoundError{BuildpackName: buildpackName}
}
return Buildpack(ccv3Buildpacks[0]), Warnings(warnings), err
}
func (actor Actor) CreateBuildpack(buildpack Buildpack) (Buildpack, Warnings, error) {
ccv3Buildpack, warnings, err := actor.CloudControllerClient.CreateBuildpack(
ccv3.Buildpack(buildpack),
)
return Buildpack(ccv3Buildpack), Warnings(warnings), err
}
func (actor Actor) UpdateBuildpackByNameAndStack(buildpackName string, buildpackStack string, buildpack Buildpack) (Buildpack, Warnings, error) {
var warnings Warnings
foundBuildpack, getWarnings, err := actor.GetBuildpackByNameAndStack(buildpackName, buildpackStack)
warnings = append(warnings, getWarnings...)
if err != nil {
return Buildpack{}, warnings, err
}
buildpack.GUID = foundBuildpack.GUID
updatedBuildpack, updateWarnings, err := actor.CloudControllerClient.UpdateBuildpack(ccv3.Buildpack(buildpack))
warnings = append(warnings, updateWarnings...)
if err != nil {
return Buildpack{}, warnings, err
}
return Buildpack(updatedBuildpack), warnings, nil
}
func (actor Actor) UploadBuildpack(guid string, pathToBuildpackBits string, progressBar SimpleProgressBar) (ccv3.JobURL, Warnings, error) {
wrappedReader, size, err := progressBar.Initialize(pathToBuildpackBits)
if err != nil {
return "", Warnings{}, err
}
jobURL, warnings, err := actor.CloudControllerClient.UploadBuildpack(guid, pathToBuildpackBits, wrappedReader, size)
if err != nil {
// TODO: Do we actually want to convert this error? Is this the right place?
if e, ok := err.(ccerror.BuildpackAlreadyExistsForStackError); ok {
return "", Warnings(warnings), actionerror.BuildpackAlreadyExistsForStackError{Message: e.Message}
}
return "", Warnings(warnings), err
}
// TODO: Should we defer the terminate instead?
progressBar.Terminate()
return jobURL, Warnings(warnings), nil
}
func (actor *Actor) PrepareBuildpackBits(inputPath string, tmpDirPath string, downloader Downloader) (string, error) {
if util.IsHTTPScheme(inputPath) {
pathToDownloadedBits, err := downloader.Download(inputPath, tmpDirPath)
if err != nil {
return "", err
}
return pathToDownloadedBits, nil
}
if filepath.Ext(inputPath) == ".zip" {
return inputPath, nil
}
info, err := os.Stat(inputPath)
if err != nil {
return "", err
}
if info.IsDir() {
var empty bool
empty, err = isEmptyDirectory(inputPath)
if err != nil {
return "", err
}
if empty {
return "", actionerror.EmptyBuildpackDirectoryError{Path: inputPath}
}
archive := filepath.Join(tmpDirPath, filepath.Base(inputPath)) + ".zip"
err = Zipit(inputPath, archive, "")
if err != nil {
return "", err
}
return archive, nil
}
return inputPath, nil
}
func isEmptyDirectory(name string) (bool, error) {
f, err := os.Open(name)
if err != nil {
return false, err
}
defer f.Close()
_, err = f.Readdirnames(1)
if err == io.EOF {
return true, nil
}
return false, err
}
// Zipit zips the source into a .zip file in the target dir
func Zipit(source, target, prefix string) error {
// Thanks to Svett Ralchev
// http://blog.ralch.com/tutorial/golang-working-with-zip/
zipfile, err := os.Create(target)
if err != nil {
return err
}
defer zipfile.Close()
if prefix != "" {
_, err = io.WriteString(zipfile, prefix)
if err != nil {
return err
}
}
archive := zip.NewWriter(zipfile)
defer archive.Close()
err = filepath.Walk(source, func(path string, info os.FileInfo, err error) error {
if err != nil {
return err
}
if path == source {
return nil
}
header, err := zip.FileInfoHeader(info)
if err != nil {
return err
}
header.Name, err = filepath.Rel(source, path)
if err != nil {
return err
}
header.Name = filepath.ToSlash(header.Name)
if info.IsDir() {
header.Name += "/"
header.SetMode(info.Mode())
} else {
header.Method = zip.Deflate
header.SetMode(fixMode(info.Mode()))
}
writer, err := archive.CreateHeader(header)
if err != nil {
return err
}
if info.IsDir() {
return nil
}
file, err := os.Open(path)
if err != nil {
return err
}
defer file.Close()
_, err = io.Copy(writer, file)
return err
})
return err
}
func (actor Actor) DeleteBuildpackByNameAndStack(buildpackName string, buildpackStack string) (Warnings, error) {
var allWarnings Warnings
buildpack, getBuildpackWarnings, err := actor.GetBuildpackByNameAndStack(buildpackName, buildpackStack)
allWarnings = append(allWarnings, getBuildpackWarnings...)
if err != nil {
return allWarnings, err
}
jobURL, deleteBuildpackWarnings, err := actor.CloudControllerClient.DeleteBuildpack(buildpack.GUID)
allWarnings = append(allWarnings, deleteBuildpackWarnings...)
if err != nil {
return allWarnings, err
}
pollWarnings, err := actor.CloudControllerClient.PollJob(jobURL)
allWarnings = append(allWarnings, pollWarnings...)
return allWarnings, err
}