-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.go
279 lines (237 loc) · 7.37 KB
/
main.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
package main
import (
"errors"
"fmt"
"os"
"runtime"
"sync"
"time"
"github.com/cloudfoundry/cli/cf/terminal"
"github.com/cloudfoundry/cli/flags"
"github.com/cloudfoundry/cli/flags/flag"
"github.com/cloudfoundry/cli/plugin"
"github.com/simonleung8/cli-stack-changer/apps"
"github.com/simonleung8/cli-stack-changer/instances"
"github.com/simonleung8/cli-stack-changer/orgs"
"github.com/simonleung8/cli-stack-changer/spaces"
)
type StackChanger struct {
ui terminal.UI
}
func (c *StackChanger) GetMetadata() plugin.PluginMetadata {
return plugin.PluginMetadata{
Name: "Stack-Changer",
Version: plugin.VersionType{
Major: 1,
Minor: 1,
Build: 0,
},
Commands: []plugin.Command{
{
Name: "stack-change",
HelpText: "Update stacks for apps from lucid64 to cflinuxfs2. Restart started apps.",
UsageDetails: plugin.Usage{
Usage: `cf stack-change [-o {org}] [-s {space}] [-p {parallel}]`,
},
},
{
Name: "stack-list",
HelpText: "List all apps running on stack lucid64.",
UsageDetails: plugin.Usage{
Usage: "cf stack-list [-o {org}] [-s {space}]",
},
},
},
}
}
func main() {
plugin.Start(new(StackChanger))
}
func (cmd *StackChanger) Run(cliConnection plugin.CliConnection, args []string) {
cmd.ui = terminal.NewUI(os.Stdin, terminal.NewTeePrinter())
appsObj := apps.NewApps(cliConnection)
instancesObj := instances.NewInstances(cliConnection)
fc := flags.NewFlagContext(setupFlags())
err := fc.Parse(args[1:]...)
if err != nil {
cmd.exitWithError(err)
}
prompt := "Getting all apps with lucid64 stack"
if fc.IsSet("o") {
prompt += " in org " + terminal.EntityNameColor(fc.String("o"))
}
if fc.IsSet("s") {
prompt += "/space " + terminal.EntityNameColor(fc.String("s"))
}
cmd.ui.Say(prompt + "...")
if args[0] == "stack-change" {
allApps := cmd.getApps(cliConnection, fc, appsObj)
cmd.ui.Say(terminal.SuccessColor("OK"))
cmd.ui.Say("")
i := 0
j := 10 //default throttle
if fc.IsSet("p") {
j = fc.Int("p")
if j < 1 || j > 100 {
cmd.ui.Failed("`-p` has to be in the range of 1 - 100")
}
}
for i < len(allApps.Resources) {
if i+j >= len(allApps.Resources) {
j = len(allApps.Resources) - i
}
cmd.ui.Say(fmt.Sprintf("Total %d found, %d processed. Batch processing %d at a time ...", len(allApps.Resources), i, j))
cmd.updateAndRestart(appsObj, instancesObj, allApps.Resources[i:i+j], cliConnection)
i = i + j
}
} else if args[0] == "stack-list" {
allApps := cmd.getApps(cliConnection, fc, appsObj)
cmd.ui.Say(terminal.SuccessColor("OK"))
cmd.ui.Say("")
cmd.ui.Say(fmt.Sprintf("Total %d found ...", len(allApps.Resources)))
cmd.printTable(allApps.Resources)
}
}
func setupFlags() map[string]flags.FlagSet {
fs := make(map[string]flags.FlagSet)
fs["o"] = &cliFlags.StringFlag{Name: "o", Usage: ""}
fs["s"] = &cliFlags.StringFlag{Name: "s", Usage: ""}
fs["p"] = &cliFlags.IntFlag{Name: "p", Usage: ""}
return fs
}
func (cmd *StackChanger) getOrgs(cliConnection plugin.CliConnection, fc flags.FlagContext) []orgs.OrgModel {
o := orgs.NewOrgs(cliConnection)
if fc.IsSet("o") {
oneOrg, err := o.GetOrg(fc.String("o"))
if err != nil {
cmd.exitWithError(err)
}
return []orgs.OrgModel{oneOrg}
} else {
allOrgs, err := o.GetAllOrgs()
if err != nil {
cmd.exitWithError(err)
}
return allOrgs
}
}
func (cmd *StackChanger) getApps(cliConnection plugin.CliConnection, fc flags.FlagContext, a apps.Apps) apps.AppsModel {
var allApps apps.AppsModel
var err error
var oneOrg orgs.OrgModel
if fc.IsSet("s") {
if !fc.IsSet("o") {
cmd.exitWithError(errors.New(fmt.Sprintf("Please provide the organization which space '%s' belongs to\n", fc.String("s"))))
}
s := spaces.NewSpaces(cliConnection)
spaceGuid, err := s.GetSpaceGuid(fc)
if err != nil {
cmd.exitWithError(err)
}
allApps, err = a.GetLucid64AppsFromSpace(spaceGuid)
if err != nil {
cmd.exitWithError(err)
}
} else if fc.IsSet("o") {
o := orgs.NewOrgs(cliConnection)
oneOrg, err = o.GetOrg(fc.String("o"))
if err != nil {
cmd.exitWithError(err)
}
allApps, err = a.GetLucid64AppsFromOrg(oneOrg.Metadata.Guid)
if err != nil {
cmd.exitWithError(err)
}
} else {
allApps, err = a.GetLucid64Apps()
if err != nil {
cmd.exitWithError(err)
}
}
return allApps
}
func (cmd *StackChanger) updateAndRestart(appsObj apps.Apps, instancesObj instances.Instances, allApps []apps.AppModel, cliConnection plugin.CliConnection) {
var wg sync.WaitGroup
cmd.printTable(allApps)
for _, a := range allApps {
wg.Add(1)
go func(app apps.AppModel, allApps []apps.AppModel, cliConnection plugin.CliConnection) {
defer wg.Done()
appsObj := apps.NewApps(cliConnection)
if app.Entity.State == "STARTED" {
err := appsObj.UpdateStackAndStopApp(app.Metadata.Guid)
if err != nil {
allApps = updateAppState(allApps, app.Entity.Name, "Error Updating stack: "+err.Error())
cmd.reprintTable(allApps, app.Entity.Name, "Error Updating stack: "+err.Error())
return
}
allApps = updateAppState(allApps, app.Entity.Name, "Updated, Restarting")
cmd.reprintTable(allApps, app.Entity.Name, "Updated, Restarting")
err = appsObj.RestartApp(app.Metadata.Guid)
if err != nil {
allApps = updateAppState(allApps, app.Entity.Name, "Updated, Error Restarting")
cmd.reprintTable(allApps, app.Entity.Name, "Updated, Error Restarting")
return
}
err = instancesObj.IsAnyInstancesStarted(app.Metadata.Guid, 600*time.Second)
if err != nil {
allApps = updateAppState(allApps, app.Entity.Name, "Updated, Timeout Restarting")
cmd.reprintTable(allApps, app.Entity.Name, "Updated, Timeout Restarting")
return
}
allApps = updateAppState(allApps, app.Entity.Name, "Updated, Restarted ")
cmd.reprintTable(allApps, app.Entity.Name, "Updated, Restarted ")
} else {
err := appsObj.UpdateStack(app.Metadata.Guid)
if err != nil {
allApps = updateAppState(allApps, app.Entity.Name, "Error Updating stack: "+err.Error())
cmd.reprintTable(allApps, app.Entity.Name, "Error Updating stack: "+err.Error())
return
}
allApps = updateAppState(allApps, app.Entity.Name, "Updated, Done")
cmd.reprintTable(allApps, app.Entity.Name, "Updated, Done")
}
}(a, allApps, cliConnection)
}
wg.Wait()
if runtime.GOOS != "windows" {
fmt.Printf("\033[2B")
}
fmt.Printf("\n\n")
}
func (cmd *StackChanger) printTable(allApps []apps.AppModel) {
table := terminal.NewTable(cmd.ui, []string{"name", "guid", "state"})
for _, a := range allApps {
table.Add(a.Entity.Name, a.Metadata.Guid, a.Entity.State)
}
table.Print()
}
func (cmd *StackChanger) reprintTable(allApps []apps.AppModel, updatedApp string, msg string) {
n := len(allApps) + 1
if runtime.GOOS == "windows" {
fmt.Printf("\n\n")
} else {
fmt.Printf("\033[%dA", n)
}
table := terminal.NewTable(cmd.ui, []string{"name", "guid", "state"})
for _, a := range allApps {
if a.Entity.Name != updatedApp {
table.Add(a.Entity.Name, a.Metadata.Guid, a.Entity.State)
} else {
table.Add(updatedApp, a.Metadata.Guid, msg)
}
}
table.Print()
}
func (c *StackChanger) exitWithError(err error) {
c.ui.Failed("Error: " + err.Error())
}
func updateAppState(allApps []apps.AppModel, updatedApp string, msg string) []apps.AppModel {
for i, _ := range allApps {
if allApps[i].Entity.Name == updatedApp {
allApps[i].Entity.State = msg
return allApps
}
}
return allApps
}