-
Notifications
You must be signed in to change notification settings - Fork 927
/
create_buildpack_command.go
138 lines (117 loc) · 4.86 KB
/
create_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
package v7
import (
"io/ioutil"
"os"
"time"
"code.cloudfoundry.org/cli/api/cloudcontroller/ccerror"
"code.cloudfoundry.org/cli/api/cloudcontroller/ccv3"
"code.cloudfoundry.org/cli/command/flag"
"code.cloudfoundry.org/cli/command/translatableerror"
"code.cloudfoundry.org/cli/util/download"
"code.cloudfoundry.org/clock"
"code.cloudfoundry.org/cli/actor/sharedaction"
"code.cloudfoundry.org/cli/actor/v7action"
"code.cloudfoundry.org/cli/command"
"code.cloudfoundry.org/cli/command/v7/shared"
"code.cloudfoundry.org/cli/types"
)
//go:generate counterfeiter . CreateBuildpackActor
type CreateBuildpackActor interface {
CreateBuildpack(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 CreateBuildpackCommand struct {
RequiredArgs flag.CreateBuildpackArgs `positional-args:"Yes"`
usage interface{} `usage:"CF_NAME create-buildpack BUILDPACK PATH POSITION [--disable]\n\nTIP:\n Path 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."`
relatedCommands interface{} `related_commands:"buildpacks, push"`
Disable bool `long:"disable" description:"Disable the buildpack from being used for staging"`
UI command.UI
Config command.Config
ProgressBar v7action.SimpleProgressBar
SharedActor command.SharedActor
Actor CreateBuildpackActor
}
func (cmd *CreateBuildpackCommand) 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 CreateBuildpackCommand) Execute(args []string) error {
err := cmd.SharedActor.CheckTarget(false, false)
if err != nil {
return err
}
user, err := cmd.Config.CurrentUser()
if err != nil {
return err
}
cmd.UI.DisplayTextWithFlavor("Creating buildpack {{.BuildpackName}} as {{.Username}}...", map[string]interface{}{
"Username": user.Name,
"BuildpackName": cmd.RequiredArgs.Buildpack,
})
downloader := download.NewDownloader(time.Second * 30)
tmpDirPath, err := ioutil.TempDir("", "buildpack-dir-")
if err != nil {
return err
}
defer os.RemoveAll(tmpDirPath)
pathToBuildpackBits, err := cmd.Actor.PrepareBuildpackBits(string(cmd.RequiredArgs.Path), tmpDirPath, downloader)
if err != nil {
return err
}
createdBuildpack, warnings, err := cmd.Actor.CreateBuildpack(v7action.Buildpack{
Name: cmd.RequiredArgs.Buildpack,
Position: types.NullInt{IsSet: true, Value: cmd.RequiredArgs.Position},
Enabled: types.NullBool{IsSet: true, Value: !cmd.Disable},
})
cmd.UI.DisplayWarningsV7(warnings)
if err != nil {
return err
}
cmd.UI.DisplayOK()
cmd.UI.DisplayTextWithFlavor("Uploading buildpack {{.BuildpackName}} as {{.Username}}...", map[string]interface{}{
"Username": user.Name,
"BuildpackName": cmd.RequiredArgs.Buildpack,
})
jobURL, warnings, err := cmd.Actor.UploadBuildpack(createdBuildpack.GUID, pathToBuildpackBits, cmd.ProgressBar)
if _, ok := err.(ccerror.InvalidAuthTokenError); ok {
cmd.UI.DisplayWarningsV7([]string{"Failed to upload buildpack due to auth token expiration, retrying..."})
jobURL, warnings, err = cmd.Actor.UploadBuildpack(createdBuildpack.GUID, pathToBuildpackBits, cmd.ProgressBar)
}
cmd.UI.DisplayWarningsV7(warnings)
if err != nil {
return cmd.wrapWithTip(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.DisplayWarningsV7(warnings)
if err != nil {
return cmd.wrapWithTip(err)
}
cmd.UI.DisplayOK()
return nil
}
func (cmd CreateBuildpackCommand) wrapWithTip(err error) error {
return translatableerror.TipDecoratorError{
BaseError: err,
Tip: "A buildpack with name '{{.BuildpackName}}' and nil stack has been created. Use '{{.CfDeleteBuildpackCommand}}' to delete it or '{{.CfUpdateBuildpackCommand}}' to try again.",
TipKeys: map[string]interface{}{
"BuildpackName": cmd.RequiredArgs.Buildpack,
"CfDeleteBuildpackCommand": cmd.Config.BinaryName() + " delete-buildpack",
"CfUpdateBuildpackCommand": cmd.Config.BinaryName() + " update-buildpack",
},
}
}