-
Notifications
You must be signed in to change notification settings - Fork 927
/
push.go
269 lines (221 loc) · 6.04 KB
/
push.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
package application
import (
"cf"
"cf/api"
"cf/configuration"
"cf/net"
"cf/requirements"
"cf/terminal"
"github.com/codegangsta/cli"
"os"
"strconv"
"strings"
)
type Push struct {
ui terminal.UI
config *configuration.Configuration
starter ApplicationStarter
stopper ApplicationStopper
appRepo api.ApplicationRepository
domainRepo api.DomainRepository
routeRepo api.RouteRepository
stackRepo api.StackRepository
appBitsRepo api.ApplicationBitsRepository
}
func NewPush(ui terminal.UI, config *configuration.Configuration, starter ApplicationStarter, stopper ApplicationStopper,
aR api.ApplicationRepository, dR api.DomainRepository, rR api.RouteRepository, sR api.StackRepository,
appBitsRepo api.ApplicationBitsRepository) (cmd Push) {
cmd.ui = ui
cmd.config = config
cmd.starter = starter
cmd.stopper = stopper
cmd.appRepo = aR
cmd.domainRepo = dR
cmd.routeRepo = rR
cmd.stackRepo = sR
cmd.appBitsRepo = appBitsRepo
return
}
func (cmd Push) GetRequirements(reqFactory requirements.Factory, c *cli.Context) (reqs []requirements.Requirement, err error) {
reqs = []requirements.Requirement{
reqFactory.NewLoginRequirement(),
reqFactory.NewTargetedSpaceRequirement(),
}
return
}
func (cmd Push) Run(c *cli.Context) {
var (
apiResponse net.ApiResponse
)
if len(c.Args()) != 1 {
cmd.ui.FailWithUsage(c, "push")
return
}
app, didCreate := cmd.getApp(c)
domain := cmd.domain(c)
hostName := cmd.hostName(app, c)
cmd.bindAppToRoute(app, domain, hostName, didCreate, c)
cmd.ui.Say("Uploading %s...", terminal.EntityNameColor(app.Name))
dir := cmd.path(c)
apiResponse = cmd.appBitsRepo.UploadApp(app, dir)
if apiResponse.IsNotSuccessful() {
cmd.ui.Failed(apiResponse.Message)
return
}
cmd.ui.Ok()
cmd.ui.Say("")
cmd.restart(app, c)
}
func (cmd Push) getApp(c *cli.Context) (app cf.Application, didCreate bool) {
appName := c.Args()[0]
app, apiResponse := cmd.appRepo.FindByName(appName)
if apiResponse.IsError() {
cmd.ui.Failed(apiResponse.Message)
return
}
if apiResponse.IsNotFound() {
app, apiResponse = cmd.createApp(appName, c)
if apiResponse.IsNotSuccessful() {
cmd.ui.Failed(apiResponse.Message)
return
}
didCreate = true
}
return
}
func (cmd Push) createApp(appName string, c *cli.Context) (app cf.Application, apiResponse net.ApiResponse) {
newApp := cf.Application{
Name: appName,
Instances: c.Int("i"),
Memory: memoryLimit(c.String("m")),
BuildpackUrl: c.String("b"),
Command: c.String("c"),
}
stackName := c.String("s")
if stackName != "" {
var stack cf.Stack
stack, apiResponse = cmd.stackRepo.FindByName(stackName)
if apiResponse.IsNotSuccessful() {
cmd.ui.Failed(apiResponse.Message)
return
}
newApp.Stack = stack
cmd.ui.Say("Using stack %s...", terminal.EntityNameColor(stack.Name))
}
cmd.ui.Say("Creating app %s in org %s / space %s as %s...",
terminal.EntityNameColor(appName),
terminal.EntityNameColor(cmd.config.Organization.Name),
terminal.EntityNameColor(cmd.config.Space.Name),
terminal.EntityNameColor(cmd.config.Username()),
)
app, apiResponse = cmd.appRepo.Create(newApp)
if apiResponse.IsNotSuccessful() {
cmd.ui.Failed(apiResponse.Message)
return
}
cmd.ui.Ok()
cmd.ui.Say("")
return
}
func (cmd Push) domain(c *cli.Context) (domain cf.Domain) {
var apiResponse net.ApiResponse
domainName := c.String("d")
if domainName != "" {
domain, apiResponse = cmd.domainRepo.FindByNameInCurrentSpace(domainName)
} else {
domain, apiResponse = cmd.domainRepo.FindDefaultAppDomain()
}
if apiResponse.IsNotSuccessful() {
cmd.ui.Failed(apiResponse.Message)
}
return
}
func (cmd Push) hostName(app cf.Application, c *cli.Context) (hostName string) {
if !c.Bool("no-hostname") {
hostName = c.String("n")
if hostName == "" {
hostName = app.Name
}
}
return
}
func (cmd Push) createRoute(hostName string, domain cf.Domain) (route cf.Route) {
newRoute := cf.Route{Host: hostName, Domain: domain}
cmd.ui.Say("Creating route %s...", terminal.EntityNameColor(newRoute.URL()))
route, apiResponse := cmd.routeRepo.Create(newRoute, domain)
if apiResponse.IsNotSuccessful() {
cmd.ui.Failed(apiResponse.Message)
return
}
cmd.ui.Ok()
cmd.ui.Say("")
return
}
func (cmd Push) bindAppToRoute(app cf.Application, domain cf.Domain, hostName string, didCreate bool, c *cli.Context) {
if c.Bool("no-route") {
return
}
if len(app.Routes) == 0 && didCreate == false {
cmd.ui.Say("App %s currently exists as a worker, skipping route creation", terminal.EntityNameColor(app.Name))
return
}
route, apiResponse := cmd.routeRepo.FindByHostAndDomain(hostName, domain.Name)
if apiResponse.IsNotSuccessful() {
route = cmd.createRoute(hostName, domain)
} else {
cmd.ui.Say("Using route %s", terminal.EntityNameColor(route.URL()))
}
for _, boundRoute := range app.Routes {
if boundRoute.Guid == route.Guid {
return
}
}
cmd.ui.Say("Binding %s to %s...", terminal.EntityNameColor(route.URL()), terminal.EntityNameColor(app.Name))
apiResponse = cmd.routeRepo.Bind(route, app)
if apiResponse.IsNotSuccessful() {
cmd.ui.Failed(apiResponse.Message)
return
}
cmd.ui.Ok()
cmd.ui.Say("")
}
func (cmd Push) path(c *cli.Context) (dir string) {
dir = c.String("p")
if dir == "" {
var err error
dir, err = os.Getwd()
if err != nil {
cmd.ui.Failed(err.Error())
return
}
}
return
}
func (cmd Push) restart(app cf.Application, c *cli.Context) {
updatedApp, _ := cmd.stopper.ApplicationStop(app)
cmd.ui.Say("")
if !c.Bool("no-start") {
if c.String("b") != "" {
updatedApp.BuildpackUrl = c.String("b")
}
cmd.starter.ApplicationStart(updatedApp)
}
}
func memoryLimit(arg string) (memory uint64) {
var err error
switch {
case strings.HasSuffix(arg, "M"):
trimmedArg := arg[:len(arg)-1]
memory, err = strconv.ParseUint(trimmedArg, 10, 0)
case strings.HasSuffix(arg, "G"):
trimmedArg := arg[:len(arg)-1]
memory, err = strconv.ParseUint(trimmedArg, 10, 0)
memory = memory * 1024
default:
memory, err = strconv.ParseUint(arg, 10, 0)
}
if err != nil {
memory = 128
}
return
}