This repository has been archived by the owner on Jan 20, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathpuppeteer.go
266 lines (247 loc) · 8.79 KB
/
puppeteer.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
package main
import (
"code.cloudfoundry.org/cli/plugin"
"fmt"
"github.com/happytobi/cf-puppeteer/arguments"
"github.com/happytobi/cf-puppeteer/cf"
v2 "github.com/happytobi/cf-puppeteer/cf/v2"
"github.com/happytobi/cf-puppeteer/rewind"
"github.com/happytobi/cf-puppeteer/ui"
"os"
"strings"
)
func fatalIf(err error) {
if err != nil {
ui.Failed("error: %s", err)
os.Exit(1)
}
}
func main() {
plugin.Start(&CfPuppeteerPlugin{})
}
type CfPuppeteerPlugin struct{}
func venerableAppName(appName string) string {
return fmt.Sprintf("%s-venerable", appName)
}
func getActionsForApp(appRepo *ApplicationRepo, parsedArguments *arguments.ParserArguments) []rewind.Action {
venName := venerableAppName(parsedArguments.AppName)
puppeteerPush := cf.NewApplicationPush(appRepo.conn, appRepo.traceLogging)
var err error
var curApp *v2.AppResourcesEntity
var venApp *v2.AppResourcesEntity
return []rewind.Action{
// get info about current app
{
Forward: func() error {
curApp, err = appRepo.v2Resources.GetAppMetadata(parsedArguments.AppName)
if err != nil {
if err == v2.ErrAppNotFound {
curApp = nil
} else {
return err
}
}
return nil
},
},
// get info about ven app
{
Forward: func() error {
venApp, err = appRepo.v2Resources.GetAppMetadata(venName)
if err != nil {
if err == v2.ErrAppNotFound {
venApp = nil
} else {
return err
}
}
return nil
},
},
// rename any existing app such so that next step can push to a clear space
{
Forward: func() error {
// If there is no current app running, that's great, we're done here
if curApp == nil {
return nil
}
// If current app isn't started, then we'll just delete it, and we're done => only if route switching was not used
if curApp.Entity.State != "STARTED" && parsedArguments.AddRoutes == false {
return appRepo.v2Resources.DeleteApplication(parsedArguments.AppName)
}
// Do we have a ven app that will stop a rename? -> normal workflow only if we dont run the add routes mode
if venApp != nil && parsedArguments.AddRoutes == false {
// Finally, since the current app claims to be healthy, we'll delete the venerable app, and rename the current over the top
err = appRepo.v2Resources.DeleteApplication(venName)
if err != nil {
return err
}
}
if parsedArguments.AddRoutes == false {
return appRepo.v2Resources.RenameApplication(parsedArguments.AppName, venName)
}
return nil
},
},
// push
{
Forward: func() error {
venAppExists := venApp != nil
space, err := appRepo.conn.GetCurrentSpace()
if err != nil {
return err
}
if parsedArguments.AddRoutes == false {
return puppeteerPush.PushApplication(venName, venAppExists, space.Guid, parsedArguments)
}
return nil
},
//When upload fails the new application will be deleted and ven app will be renamed
ReversePrevious: func() error {
ui.FailedMessage("error while uploading / deploying the application... roll everything back")
_ = appRepo.v2Resources.DeleteApplication(parsedArguments.AppName)
_ = appRepo.v2Resources.RenameApplication(venName, parsedArguments.AppName)
return nil
},
},
// start
{
Forward: func() error {
if parsedArguments.NoStart == false {
return appRepo.v2Resources.StartApplication(parsedArguments.AppName)
}
return nil
},
ReversePrevious: func() error {
if parsedArguments.ShowCrashLogs {
//print logs before application delete
ui.Say("show crash logs")
_ = appRepo.v2Resources.ShowCrashLogs(parsedArguments.AppName)
}
// If the app cannot start we'll have a lingering application
// We delete this application so that the rename can succeed
_ = appRepo.v2Resources.DeleteApplication(parsedArguments.AppName)
return appRepo.v2Resources.RenameApplication(venName, parsedArguments.AppName)
},
},
//switch routes because new application was started correct
{
Forward: func() error {
//switch route only is application was started and route switch option was set
ui.Say("check if routes should be added or switched from existing one")
if parsedArguments.NoStart == false && parsedArguments.NoRoute == false {
venAppExists := venApp != nil
return puppeteerPush.SwitchRoutes(venName, venAppExists, parsedArguments.AppName, parsedArguments.Manifest.ApplicationManifests[0].Routes, parsedArguments.LegacyPush)
}
ui.Say("nothing to do")
return nil
},
ReversePrevious: func() error {
// If the app cannot start we'll have a lingering application
// We delete this application so that the rename can succeed
_ = appRepo.v2Resources.DeleteApplication(parsedArguments.AppName)
return appRepo.v2Resources.RenameApplication(venName, parsedArguments.AppName)
},
},
//check vor venerable application again -> because venerable action was set correct and ven app could exist now.
{
Forward: func() error {
venApp, err = appRepo.v2Resources.GetAppMetadata(venName)
if err != nil {
if err == v2.ErrAppNotFound {
venApp = nil
} else {
return err
}
}
return nil
},
},
// delete
{
Forward: func() error {
//if venerableAction was set to stop
if strings.ToLower(parsedArguments.VenerableAction) == "stop" && venApp != nil {
return appRepo.v2Resources.StopApplication(venName)
} else if strings.ToLower(parsedArguments.VenerableAction) == "delete" && venApp != nil {
return appRepo.v2Resources.DeleteApplication(venName)
}
//do nothing with the ven app
return nil
},
},
}
}
func (plugin CfPuppeteerPlugin) Run(cliConnection plugin.CliConnection, args []string) {
// only handle if actually invoked, else it can't be uninstalled cleanly
if args[0] != "zero-downtime-push" {
return
}
var traceLogging bool
if os.Getenv("CF_TRACE") == "true" {
traceLogging = true
}
appRepo := NewApplicationRepo(cliConnection, traceLogging)
parsedArguments, err := arguments.ParseArgs(args)
fatalIf(err)
fatalIf((&rewind.Actions{
Actions: getActionsForApp(appRepo, parsedArguments),
RewindFailureMessage: "Oh no. Something's gone wrong. I've tried to roll back but you should check to see if everything is OK.",
}).Execute())
ui.Say("")
ui.Say("A new version of your application has successfully been pushed!")
ui.Say("")
_ = appRepo.v2Resources.ListApplications()
}
// GetMetadata get plugin metadata
func (CfPuppeteerPlugin) GetMetadata() plugin.PluginMetadata {
return plugin.PluginMetadata{
Name: "cf-puppeteer",
Version: plugin.VersionType{
Major: 1,
Minor: 2,
Build: 2,
},
Commands: []plugin.Command{
{
Name: "zero-downtime-push",
HelpText: "Perform a zero-downtime push of an application over the top of an old one",
UsageDetails: plugin.Usage{
Usage: "$ cf zero-downtime-push [<App-Name>] -f <Manifest.yml> [options]",
Options: map[string]string{
"f": "path to application manifest",
"p": "path to application files",
"s": "name of the stack to use",
"t": "push timeout (in seconds)",
"-env": "add environment key value pairs dynamic; can specify multiple times",
"-venerable-action": "option to delete, stop or do nothing with venerable application - default is delete",
"-health-check-type": "type of health check to perform",
"-health-check-http-endpoint": "endpoint for the 'http' health check type",
"-invocation-timeout": "timeout (in seconds) that controls individual health check invocations",
"-show-crash-log": "Show recent logs when applications crashes while the deployment",
"-process": "use health check type process",
"-legacy-push": "use legacy push instead of new v3 api",
"-no-route": "deploy new application without adding routes",
"-route-only": "only add routes from manifest to application",
"-no-start": "don't start application after deployment; venerable action will none",
"-docker-image": "docker image url",
"-docker-username": "docker repository username; used with password from env CF_DOCKER_PASSWORD",
"-vars-file": "path to a variable substitution file for manifest",
},
},
},
},
}
}
type ApplicationRepo struct {
conn plugin.CliConnection
traceLogging bool
v2Resources v2.Resources
}
func NewApplicationRepo(conn plugin.CliConnection, traceLogging bool) *ApplicationRepo {
return &ApplicationRepo{
conn: conn,
traceLogging: traceLogging,
v2Resources: v2.NewV2Resources(conn, traceLogging),
}
}