forked from cloudfoundry-attic/diego-cli-plugin
-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.go
318 lines (271 loc) · 7.78 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
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
package main
import (
"fmt"
"os"
"github.com/cloudfoundry-incubator/diego-cli-plugin/diego_support"
"github.com/cloudfoundry-incubator/diego-cli-plugin/docker"
"github.com/cloudfoundry-incubator/diego-cli-plugin/utils"
"github.com/cloudfoundry/cli/plugin"
)
type DiegoBeta struct{}
func (c *DiegoBeta) GetMetadata() plugin.PluginMetadata {
return plugin.PluginMetadata{
Name: "Diego-Beta",
Version: plugin.VersionType{
Major: 1,
Minor: 1,
Build: 0,
},
Commands: []plugin.Command{
{
Name: "enable-diego",
HelpText: "enable Diego support for an app",
UsageDetails: plugin.Usage{
Usage: "cf enable-diego APP_NAME",
},
},
{
Name: "disable-diego",
HelpText: "disable Diego support for an app",
UsageDetails: plugin.Usage{
Usage: "cf disable-diego APP_NAME",
},
},
{
Name: "has-diego-enabled",
HelpText: "Check if Diego support is enabled for an app",
UsageDetails: plugin.Usage{
Usage: "cf has-diego-enabled APP_NAME",
},
},
{
Name: "set-health-check",
HelpText: "set health_check_type flag to either port or none",
UsageDetails: plugin.Usage{
Usage: `cf set-health-check APP_NAME port /
cf set-health-check APP_NAME none`,
},
},
{
Name: "get-health-check",
HelpText: "get health_check_type flag of an app",
UsageDetails: plugin.Usage{
Usage: "cf get-health-check APP_NAME",
},
},
{
Name: "docker-push",
HelpText: "push a docker image from docker hub as an app",
UsageDetails: plugin.Usage{
Usage: `cf docker-push APP_NAME DOCKER_IMAGE [OPTIONS]
Example
cf docker-push testapp test/docker-path -c start.sh
cf docker-push testapp test/docker-path -c start.sh --no-start
Options
-c : Startup command, set to 'null' to reset to default start command.
--no-start : Do not start an app after pushing
--no-route : Do not map a route to this app and remove routes from previous pushes of this app
`,
},
},
},
}
}
func main() {
plugin.Start(new(DiegoBeta))
}
func (c *DiegoBeta) Run(cliConnection plugin.CliConnection, args []string) {
if args[0] == "enable-diego" && len(args) == 2 {
c.toggleDiegoSupport(true, cliConnection, args[1])
} else if args[0] == "disable-diego" && len(args) == 2 {
c.toggleDiegoSupport(false, cliConnection, args[1])
} else if args[0] == "has-diego-enabled" && len(args) == 2 {
c.isDiegoEnabled(cliConnection, args[1])
} else if args[0] == "docker-push" && len(args) >= 3 {
c.dockerPush(cliConnection, args)
} else if args[0] == "set-health-check" && len(args) == 3 && (args[2] == "port" || args[2] == "none") {
c.setHealthCheck(cliConnection, args[1], args[2])
} else if args[0] == "get-health-check" && len(args) == 2 {
c.getHealthCheck(cliConnection, args[1])
} else {
c.showUsage(args)
}
}
func (c *DiegoBeta) showUsage(args []string) {
for _, cmd := range c.GetMetadata().Commands {
if cmd.Name == args[0] {
fmt.Println("Invalid Usage: \n", cmd.UsageDetails.Usage)
}
}
}
func (c *DiegoBeta) toggleDiegoSupport(on bool, cliConnection plugin.CliConnection, appName string) {
d := diego_support.NewDiegoSupport(cliConnection)
u := utils.NewUtils(cliConnection)
appGuid, err, output := u.GetAppGuid(appName)
if err != nil {
exitWithError(err, output)
}
if output, err = d.SetDiegoFlag(appGuid, on); err != nil {
exitWithError(err, output)
}
fmt.Printf("Diego support for %s is set to %t\n\n", appName, on)
}
func (c *DiegoBeta) isDiegoEnabled(cliConnection plugin.CliConnection, appName string) {
var err error
var output []string
var appGuid string
d := diego_support.NewDiegoSupport(cliConnection)
u := utils.NewUtils(cliConnection)
if appGuid, err, output = u.GetAppGuid(appName); err != nil {
exitWithError(err, output)
}
var result bool
if result, err, output = d.HasDiegoEnabled(appGuid); err != nil {
exitWithError(err, output)
}
fmt.Println(result)
}
func (c *DiegoBeta) dockerPush(cliConnection plugin.CliConnection, args []string) {
var err error
var space, spaceGuid, appGuid string
var output []string
u := utils.NewUtils(cliConnection)
appName := args[1]
appGuid, err, _ = u.GetAppGuid(appName)
if err == nil {
_, err = u.UpdateApp(appGuid, "docker_image", args[2])
if err != nil {
exitWithError(err, output)
}
cliConnection.CliCommand("restart", appName)
return
}
if space, err, output = u.GetTargetSpace(); err != nil {
exitWithError(err, output)
}
if spaceGuid, err, output = u.GetSpaceGuid(space); err != nil {
exitWithError(err, output)
}
dockerImg := args[2]
d := docker.NewDocker(cliConnection)
//creating app
fmt.Println("Creating app", appName, "...")
if output, err = d.CreateApp(appName, dockerImg, spaceGuid); err != nil {
exitWithError(err, output)
}
appGuid, err, output = u.GetAppGuid(appName)
if err != nil {
exitWithError(err, output)
}
sayOk()
if isFlagExist(args[3:], "-c") {
command := getFlagValue(args, "-c")
fmt.Println("Updating start command: " + command)
if command == "null" {
command = ""
}
if output, err = u.UpdateApp(appGuid, "command", command); err != nil {
exitWithError(err, output)
}
sayOk()
}
//creating route
var domain string
if isFlagExist(args[3:], "--no-route") {
fmt.Println("Removing app routes if any ...")
if output, err = u.DetachAppRoutes(appGuid); err != nil {
exitWithError(err, output)
}
sayOk()
return
} else {
fmt.Println("Creating route for", appName, "...")
if domain, err, output = u.FindDomain(); err != nil {
exitWithError(err, output)
}
if output, err = u.CreateRoute(space, domain, appName); err != nil {
exitWithError(err, output)
}
fmt.Println("Route " + appName + "." + domain + " created")
sayOk()
}
//mapping route
fmt.Println("Mapping route to", appName, "...")
if output, err = u.MapRoute(appName, domain, appName); err != nil {
exitWithError(err, output)
}
fmt.Println("Mapped " + appName + "." + domain + " route to " + appName)
sayOk()
//starting app
if isFlagExist(args[3:], "--no-start") {
fmt.Println("Stop operation before starting '" + appName + "'")
sayOk()
} else {
fmt.Println("Start app", appName, "...")
if output, err = u.StartApp(appName); err != nil {
exitWithError(err, output)
}
}
}
func (c *DiegoBeta) setHealthCheck(cliConnection plugin.CliConnection, appName string, value string) {
u := utils.NewUtils(cliConnection)
appGuid, err, output := u.GetAppGuid(appName)
if err != nil {
exitWithError(err, output)
}
fmt.Println("Setting health_check_type for " + appName + " to '" + value + "'")
if output, err = u.UpdateApp(appGuid, "health_check_type", value); err != nil {
exitWithError(err, output)
}
sayOk()
}
func (c *DiegoBeta) getHealthCheck(cliConnection plugin.CliConnection, appName string) {
u := utils.NewUtils(cliConnection)
appGuid, err, output := u.GetAppGuid(appName)
if err != nil {
exitWithError(err, output)
}
fmt.Println("Getting health_check_type for " + appName)
healthCheckType, output, err := u.GetHealthCheck(appGuid)
if err != nil {
exitWithError(err, output)
}
sayOk()
fmt.Println("health_check_type for "+appName+":", healthCheckType)
}
func exitWithError(err error, output []string) {
sayFailed()
fmt.Println("Error: ", err)
for _, str := range output {
fmt.Println(str)
}
os.Exit(1)
}
func isFlagExist(args []string, flag string) bool {
for _, arg := range args {
if arg == flag {
return true
}
}
return false
}
func getFlagValue(args []string, flag string) string {
for i, arg := range args {
if arg == flag {
if len(args) >= i+1 {
return args[i+1]
}
break
}
}
return ""
}
func say(message string, color uint, bold int) string {
return fmt.Sprintf("\033[%d;%dm%s\033[0m", bold, color, message)
}
func sayOk() {
fmt.Println(say("Ok\n", 32, 1))
}
func sayFailed() {
fmt.Println(say("FAILED", 31, 1))
}