forked from contraband/autopilot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
autopilot.go
326 lines (273 loc) · 8.53 KB
/
autopilot.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
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
package main
import (
"encoding/json"
"errors"
"flag"
"fmt"
"os"
"strings"
"github.com/cloudfoundry/cli/plugin"
"github.com/concourse/autopilot/rewind"
)
func fatalIf(err error) {
if err != nil {
fmt.Fprintln(os.Stdout, "error:", err)
os.Exit(1)
}
}
func main() {
plugin.Start(&AutopilotPlugin{})
}
type AutopilotPlugin struct{}
func venerableAppName(appName string) string {
return fmt.Sprintf("%s-venerable", appName)
}
func rollbackAppName(appName string) string {
return fmt.Sprintf("%s-rollback", appName)
}
func getActionsForRollback(appName string, appRepo *ApplicationRepo, args []string) []rewind.Action {
return []rewind.Action{
//Rename live app
{
Forward: func() error {
return appRepo.RenameApplication(appName, rollbackAppName(appName))
},
ReversePrevious: func() error {
return appRepo.RenameApplication(rollbackAppName(appName), appName)
},
},
//Rename venerable app
{
Forward: func() error {
return appRepo.RenameApplication(venerableAppName(appName), appName)
},
ReversePrevious: func() error {
return appRepo.RenameApplication(appName, venerableAppName(appName))
},
},
//Start rollback app
{
Forward: func() error {
return appRepo.StartApplication(appName)
},
},
//Delete rolled back app
{
Forward: func() error {
return appRepo.DeleteApplication(rollbackAppName(appName))
},
},
}
}
func getActionsForPush(appRepo *ApplicationRepo, args []string) []rewind.Action {
appName, manifestPath, appPath, options, err := ParseArgs(args)
fatalIf(err)
appExists, err := appRepo.DoesAppExist(appName)
fatalIf(err)
if appExists {
return getActionsForExistingApp(appRepo, appName, manifestPath, appPath, options)
} else {
return getActionsForNewApp(appRepo, appName, manifestPath, appPath)
}
}
func getActionsForExistingApp(appRepo *ApplicationRepo, appName, manifestPath, appPath string, options AutopilotOptions) []rewind.Action {
return []rewind.Action{
// delete old version if it still exists
{
Forward: func() error {
appExists, err := appRepo.DoesAppExist(venerableAppName(appName))
fatalIf(err)
if(appExists) {
fmt.Println("Found old version of app running, deleting.")
return appRepo.DeleteApplication(venerableAppName(appName))
} else {
return nil
}
},
},
// rename
{
Forward: func() error {
return appRepo.RenameApplication(appName, venerableAppName(appName))
},
},
// push
{
Forward: func() error {
return appRepo.PushApplication(appName, manifestPath, appPath)
},
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.DeleteApplication(appName)
return appRepo.RenameApplication(venerableAppName(appName), appName)
},
},
// delete/stop
{
Forward: func() error {
if(options.KeepExisting){
fmt.Println("Stopping old version of app. Remove the --keep-existing-app flag to delete it automatically.")
return appRepo.StopApplication(venerableAppName(appName))
} else {
fmt.Println("Deleting old version of app. Use the --keep-existing-app flag to preserve it.")
return appRepo.DeleteApplication(venerableAppName(appName))
}
},
},
}
}
func getActionsForNewApp(appRepo *ApplicationRepo, appName, manifestPath, appPath string) []rewind.Action {
return []rewind.Action{
// push
{
Forward: func() error {
return appRepo.PushApplication(appName, manifestPath, appPath)
},
},
}
}
func (plugin AutopilotPlugin) Run(cliConnection plugin.CliConnection, args []string) {
appRepo := NewApplicationRepo(cliConnection)
appName := args[1]
var actionList []rewind.Action
var successMessage string
if(args[0] == "zero-downtime-push") {
actionList = getActionsForPush(appRepo, args)
successMessage = "A new version of your application has successfully been pushed!"
} else if (args[0] == "zero-downtime-rollback") {
appExists, err := appRepo.DoesAppExist(appName)
fatalIf(err)
venerableAppExists, err := appRepo.DoesAppExist(venerableAppName(appName))
fatalIf(err)
if(!appExists){
fatalIf(errors.New(fmt.Sprintf("Live version of app \"%s\" not found, cannot rollback.", appName)))
}
if(!venerableAppExists){
fatalIf(errors.New(fmt.Sprintf("Venerable version of \"%s\" not found, cannot rollback. Make sure you push with the " +
"--keep-existing-app flag to leave the venerable version behind.", appName)))
}
actionList = getActionsForRollback(appName, appRepo, args)
successMessage = "Your application has been successfully rolled back!"
}
actions := rewind.Actions{
Actions: actionList,
RewindFailureMessage: "Oh no. Something's gone wrong. I've tried to roll back but you should check to see if everything is OK.",
}
err := actions.Execute()
fatalIf(err)
fmt.Println()
fmt.Println(successMessage)
fmt.Println()
err = appRepo.ListApplications()
fatalIf(err)
}
func (AutopilotPlugin) GetMetadata() plugin.PluginMetadata {
return plugin.PluginMetadata{
Name: "autopilot",
Version: plugin.VersionType{
Major: 0,
Minor: 0,
Build: 3,
},
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 application-to-replace \\ \n \t-f path/to/new_manifest.yml \\ \n \t-p path/to/new/path",
},
},
{
Name:"zero-downtime-rollback",
HelpText: "Perform a zero-downtime rollback to the previous version of the application. Requires that the previous, 'venerable' version of the app still exists." +
"Use the --keep-existing-app flag when performing a zero-downtime-push to ensure this.",
UsageDetails:plugin.Usage{
Usage:"$cf zero-downtime-rollback application-to-revert",
},
},
},
}
}
func ParseArgs(args []string) (string, string, string, AutopilotOptions, error) {
flags := flag.NewFlagSet("zero-downtime-push", flag.ContinueOnError)
manifestPath := flags.String("f", "", "path to an application manifest")
appPath := flags.String("p", "", "path to application files")
keepVenerable := flags.Bool("keep-existing-app", false, "keep existing app running")
err := flags.Parse(args[2:])
if err != nil {
return "", "", "", AutopilotOptions{}, err
}
appName := args[1]
if *manifestPath == "" {
return "", "", "", AutopilotOptions{}, ErrNoManifest
}
options := AutopilotOptions{KeepExisting: *keepVenerable}
return appName, *manifestPath, *appPath, options, nil
}
var ErrNoManifest = errors.New("a manifest is required to push this application")
type ApplicationRepo struct {
conn plugin.CliConnection
}
type AutopilotOptions struct {
KeepExisting bool
}
func NewApplicationRepo(conn plugin.CliConnection) *ApplicationRepo {
return &ApplicationRepo{
conn: conn,
}
}
func (repo *ApplicationRepo) RenameApplication(oldName, newName string) error {
_, err := repo.conn.CliCommand("rename", oldName, newName)
return err
}
func (repo *ApplicationRepo) PushApplication(appName, manifestPath, appPath string) error {
args := []string{"push", appName, "-f", manifestPath}
if appPath != "" {
args = append(args, "-p", appPath)
}
_, err := repo.conn.CliCommand(args...)
return err
}
func (repo *ApplicationRepo) DeleteApplication(appName string) error {
_, err := repo.conn.CliCommand("delete", appName, "-f")
return err
}
func (repo *ApplicationRepo) StartApplication(appName string) error {
_, err := repo.conn.CliCommand("start", appName)
return err
}
func (repo *ApplicationRepo) StopApplication(appName string) error {
_, err := repo.conn.CliCommand("stop", appName)
return err
}
func (repo *ApplicationRepo) ListApplications() error {
_, err := repo.conn.CliCommand("apps")
return err
}
func (repo *ApplicationRepo) DoesAppExist(appName string) (bool, error) {
space, err := repo.conn.GetCurrentSpace()
if err != nil {
return false, err
}
path := fmt.Sprintf(`v2/apps?q=name:%s&q=space_guid:%s`, appName, space.Guid)
result, err := repo.conn.CliCommandWithoutTerminalOutput("curl", path)
if err != nil {
return false, err
}
jsonResp := strings.Join(result, "")
output := make(map[string]interface{})
err = json.Unmarshal([]byte(jsonResp), &output)
if err != nil {
return false, err
}
totalResults, ok := output["total_results"]
if !ok {
return false, errors.New("Missing total_results from api response")
}
count, ok := totalResults.(float64)
if !ok {
return false, fmt.Errorf("total_results didn't have a number %v", totalResults)
}
return count == 1, nil
}