forked from cloudfoundry/cli
/
update_buildpack_command.go
261 lines (220 loc) · 9.09 KB
/
update_buildpack_command.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
package v7
import (
"io/ioutil"
"os"
"time"
"code.cloudfoundry.org/cli/actor/sharedaction"
"code.cloudfoundry.org/cli/actor/v7action"
"code.cloudfoundry.org/cli/api/cloudcontroller/ccerror"
"code.cloudfoundry.org/cli/api/cloudcontroller/ccv3"
"code.cloudfoundry.org/cli/command"
"code.cloudfoundry.org/cli/command/flag"
"code.cloudfoundry.org/cli/command/translatableerror"
"code.cloudfoundry.org/cli/command/v7/shared"
"code.cloudfoundry.org/cli/types"
"code.cloudfoundry.org/cli/util/configv3"
"code.cloudfoundry.org/cli/util/download"
"code.cloudfoundry.org/clock"
)
//go:generate counterfeiter . UpdateBuildpackActor
type UpdateBuildpackActor interface {
UpdateBuildpackByNameAndStack(buildpackName string, buildpackStack string, buildpack v7action.Buildpack) (v7action.Buildpack, v7action.Warnings, error)
UploadBuildpack(guid string, pathToBuildpackBits string, progressBar v7action.SimpleProgressBar) (ccv3.JobURL, v7action.Warnings, error)
PrepareBuildpackBits(inputPath string, tmpDirPath string, downloader v7action.Downloader) (string, error)
PollUploadBuildpackJob(jobURL ccv3.JobURL) (v7action.Warnings, error)
}
type UpdateBuildpackCommand struct {
RequiredArgs flag.BuildpackName `positional-args:"Yes"`
usage interface{} `usage:"CF_NAME update-buildpack BUILDPACK [-p PATH | -s STACK | --assign-stack NEW_STACK] [-i POSITION] [--enable|--disable] [--lock|--unlock] [--rename]\n\nTIP:\nPath should be a zip file, a url to a zip file, or a local directory. Position is a positive integer, sets priority, and is sorted from lowest to highest.\n\nUse '--assign-stack' with caution. Associating a buildpack with a stack that it does not support may result in undefined behavior. Additionally, changing this association once made may require a local copy of the buildpack.\n\n"`
relatedCommands interface{} `related_commands:"buildpacks, create-buildpack, delete-buildpack"`
NewStack string `long:"assign-stack" description:"Assign a stack to a buildpack that does not have a stack association"`
Disable bool `long:"disable" description:"Disable the buildpack from being used for staging"`
Enable bool `long:"enable" description:"Enable the buildpack to be used for staging"`
Lock bool `long:"lock" description:"Lock the buildpack to prevent updates"`
Path flag.PathWithExistenceCheckOrURL `long:"path" short:"p" description:"Path to directory or zip file"`
Position types.NullInt `long:"position" short:"i" description:"The order in which the buildpacks are checked during buildpack auto-detection"`
NewName string `long:"rename" description:"Rename an existing buildpack"`
CurrentStack string `long:"stack" short:"s" description:"Specify stack to disambiguate buildpacks with the same name"`
Unlock bool `long:"unlock" description:"Unlock the buildpack to enable updates"`
UI command.UI
Config command.Config
ProgressBar v7action.SimpleProgressBar
SharedActor command.SharedActor
Actor UpdateBuildpackActor
}
func (cmd *UpdateBuildpackCommand) Setup(config command.Config, ui command.UI) error {
cmd.UI = ui
cmd.Config = config
sharedActor := sharedaction.NewActor(config)
cmd.SharedActor = sharedActor
ccClient, uaaClient, err := shared.GetNewClientsAndConnectToCF(config, ui, "")
if err != nil {
return err
}
cmd.Actor = v7action.NewActor(ccClient, config, sharedActor, uaaClient, clock.NewClock())
cmd.ProgressBar = v7action.NewProgressBar()
return nil
}
func (cmd UpdateBuildpackCommand) Execute(args []string) error {
var buildpackBitsPath, tmpDirPath string
user, err := cmd.validateSetup()
if err != nil {
return err
}
cmd.printInitialText(user.Name)
if cmd.Path != "" {
buildpackBitsPath, tmpDirPath, err = cmd.prepareBuildpackBits()
if err != nil {
return err
}
defer os.RemoveAll(tmpDirPath)
}
updatedBuildpack, err := cmd.updateBuildpack()
if err != nil {
return err
}
if buildpackBitsPath != "" {
return cmd.uploadBits(user, updatedBuildpack, buildpackBitsPath)
}
return nil
}
func (cmd UpdateBuildpackCommand) validateSetup() (configv3.User, error) {
var user configv3.User
err := cmd.validateFlagCombinations()
if err != nil {
return user, err
}
err = cmd.SharedActor.CheckTarget(false, false)
if err != nil {
return user, err
}
return cmd.Config.CurrentUser()
}
func (cmd UpdateBuildpackCommand) prepareBuildpackBits() (string, string, error) {
downloader := download.NewDownloader(time.Second * 30)
tmpDirPath, err := ioutil.TempDir("", "buildpack-dir-")
if err != nil {
return "", "", err
}
buildpackBits, err := cmd.Actor.PrepareBuildpackBits(string(cmd.Path), tmpDirPath, downloader)
return buildpackBits, tmpDirPath, err
}
func (cmd UpdateBuildpackCommand) updateBuildpack() (v7action.Buildpack, error) {
var desiredBuildpack v7action.Buildpack
desiredBuildpack.Enabled = types.NullBool{IsSet: cmd.Enable || cmd.Disable, Value: cmd.Enable}
desiredBuildpack.Locked = types.NullBool{IsSet: cmd.Lock || cmd.Unlock, Value: cmd.Lock}
desiredBuildpack.Position = cmd.Position
if cmd.NewStack != "" {
desiredBuildpack.Stack = cmd.NewStack
}
if cmd.NewName != "" {
desiredBuildpack.Name = cmd.NewName
}
updatedBuildpack, warnings, err := cmd.Actor.UpdateBuildpackByNameAndStack(
cmd.RequiredArgs.Buildpack,
cmd.CurrentStack,
desiredBuildpack,
)
cmd.UI.DisplayWarnings(warnings)
if err != nil {
return updatedBuildpack, err
}
cmd.UI.DisplayOK()
return updatedBuildpack, nil
}
func (cmd UpdateBuildpackCommand) uploadBits(user configv3.User, updatedBuildpack v7action.Buildpack, buildpackBitsPath string) error {
cmd.UI.DisplayTextWithFlavor("Uploading buildpack {{.Buildpack}} as {{.Username}}...", map[string]interface{}{
"Buildpack": cmd.RequiredArgs.Buildpack,
"Username": user.Name,
})
jobURL, warnings, err := cmd.Actor.UploadBuildpack(
updatedBuildpack.GUID,
buildpackBitsPath,
cmd.ProgressBar,
)
if _, ok := err.(ccerror.InvalidAuthTokenError); ok {
cmd.UI.DisplayWarnings([]string{"Failed to upload buildpack due to auth token expiration, retrying..."})
jobURL, warnings, err = cmd.Actor.UploadBuildpack(updatedBuildpack.GUID, buildpackBitsPath, cmd.ProgressBar)
}
cmd.UI.DisplayWarnings(warnings)
if err != nil {
return err
}
cmd.UI.DisplayOK()
cmd.UI.DisplayTextWithFlavor("Processing uploaded buildpack {{.BuildpackName}}...", map[string]interface{}{
"BuildpackName": cmd.RequiredArgs.Buildpack,
})
warnings, err = cmd.Actor.PollUploadBuildpackJob(jobURL)
cmd.UI.DisplayWarnings(warnings)
if err != nil {
return err
}
cmd.UI.DisplayOK()
return nil
}
func (cmd UpdateBuildpackCommand) printInitialText(userName string) {
var originalBuildpackName = cmd.RequiredArgs.Buildpack
var buildpackName = originalBuildpackName
if cmd.NewName != "" {
buildpackName = cmd.NewName
cmd.UI.DisplayTextWithFlavor("Renaming buildpack {{.Buildpack}} to {{.DesiredBuildpackName}} as {{.CurrentUser}}...\n", map[string]interface{}{
"Buildpack": originalBuildpackName,
"CurrentUser": userName,
"DesiredBuildpackName": cmd.NewName,
})
}
switch {
case cmd.NewStack != "":
cmd.UI.DisplayTextWithFlavor("Assigning stack {{.Stack}} to {{.Buildpack}} as {{.CurrentUser}}...", map[string]interface{}{
"Buildpack": buildpackName,
"CurrentUser": userName,
"Stack": cmd.NewStack,
})
if cmd.Position.IsSet || cmd.Lock || cmd.Unlock || cmd.Enable || cmd.Disable {
cmd.UI.DisplayTextWithFlavor("\nUpdating buildpack {{.Buildpack}} with stack {{.Stack}} as {{.CurrentUser}}...", map[string]interface{}{
"Buildpack": buildpackName,
"CurrentUser": userName,
"Stack": cmd.NewStack,
})
}
case cmd.CurrentStack == "":
cmd.UI.DisplayTextWithFlavor("Updating buildpack {{.Buildpack}} as {{.CurrentUser}}...", map[string]interface{}{
"Buildpack": buildpackName,
"CurrentUser": userName,
})
default:
cmd.UI.DisplayTextWithFlavor("Updating buildpack {{.Buildpack}} with stack {{.Stack}} as {{.CurrentUser}}...", map[string]interface{}{
"Buildpack": buildpackName,
"CurrentUser": userName,
"Stack": cmd.CurrentStack,
})
}
}
func (cmd UpdateBuildpackCommand) validateFlagCombinations() error {
if cmd.Lock && cmd.Unlock {
return translatableerror.ArgumentCombinationError{
Args: []string{"--lock", "--unlock"},
}
}
if cmd.Enable && cmd.Disable {
return translatableerror.ArgumentCombinationError{
Args: []string{"--enable", "--disable"},
}
}
if cmd.Path != "" && cmd.Lock {
return translatableerror.ArgumentCombinationError{
Args: []string{"--path", "--lock"},
}
}
if cmd.Path != "" && cmd.NewStack != "" {
return translatableerror.ArgumentCombinationError{
Args: []string{"--path", "--assign-stack"},
}
}
if cmd.CurrentStack != "" && cmd.NewStack != "" {
return translatableerror.ArgumentCombinationError{
Args: []string{"--stack", "--assign-stack"},
}
}
return nil
}