/
cmdshandle.go
395 lines (375 loc) · 8.79 KB
/
cmdshandle.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
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
package main
import (
"fmt"
"os"
"os/user"
"path/filepath"
"sort"
"strconv"
"strings"
"time"
)
func hsleep(line string) {
if !strings.ContainsAny(line, " ") {
warn("Need a time interval in seconds. For example, `sleep 10s` means sleep for 10s and `sleep 200ms` means sleep for 200ms")
return
}
arg := strings.Split(line, " ")[1]
d, err := time.ParseDuration(arg)
if err != nil {
warn("Can't parse time interval:" + err.Error())
return
}
time.Sleep(d)
}
func hcd(line string) {
var targetdir string
switch {
case !strings.ContainsAny(line, " "):
usr, err := user.Current()
if err != nil {
warn(err.Error())
}
targetdir = usr.HomeDir
case strings.ContainsAny(line, " ") && strings.Split(line, " ")[1] == "-":
targetdir = prevdir
default:
targetdir = strings.Split(line, " ")[1]
}
prevdir, _ = os.Getwd()
err := os.Chdir(targetdir)
if err != nil {
warn(err.Error())
}
}
func hcurl(line string) {
if !strings.ContainsAny(line, " ") {
info("Need a target URL, for example `curl someservice` in the cluster or `curl http://example.com`")
return
}
url := strings.Split(line, " ")[1]
res, err := kubectl(false, "exec", "-it", "curljump", "curl", url)
if err != nil {
warn(fmt.Sprintf("Can't curl %s: %s", url, err.Error()))
return
}
output(res)
}
func hlocalexec(line string) {
cmd := line
args := []string{}
if strings.ContainsAny(line, " ") {
cmd = strings.Split(line, " ")[0]
args = strings.Split(line, " ")[1:]
}
res, err := shellout(true, cmd, args...)
if err != nil {
fmt.Printf("Can't execute %s locally: %s", cmd, err)
}
output(res)
}
func henv(line string) {
// user asked to list variables in the currently selected environment:
if !strings.ContainsAny(line, " ") {
tmp := []string{}
for k, v := range currentenv().evt.et {
tmp = append(tmp, fmt.Sprintf("%s=%s", k, v))
}
sort.Strings(tmp)
for _, e := range tmp {
output(e)
}
return
}
// maybe it's a list command?
if line == "env list" {
tmp := []string{}
for k := range environments {
if k != globalEnv {
tmp = append(tmp, fmt.Sprintf("%s", k))
}
}
sort.Strings(tmp)
for _, e := range tmp {
output(e)
}
return
}
// so check for a CRUD command?
if (len(strings.Split(line, " "))) != 3 {
warn("Unknown command. Must follow 'env list' or 'env create|select|delete ENV_NAME' pattern.")
return
}
cmd := strings.Split(line, " ")[1]
targetenv := strings.Split(line, " ")[2]
switch cmd {
case "create":
debug("creating new environment '" + targetenv + "'")
createenv(targetenv, true)
case "select":
debug("switching to environment '" + targetenv + "'")
err := selectenv(targetenv, true)
if err != nil {
warn(err.Error())
}
case "delete":
debug("deleting environment '" + targetenv + "'")
err := deleteenv(targetenv, true)
if err != nil {
warn(err.Error())
}
default:
warn("Unknown command. Must follow 'env list' or 'env create|select|delete ENV_NAME' pattern.")
}
}
func hkill(line string) {
if !strings.ContainsAny(line, " ") {
info("Need a target distributed process to kill")
return
}
// pre-flight check if dproc exists:
ID := strings.Split(line, " ")[1]
_, err := kubectl(true, "get", "deployment", ID)
if err != nil {
warn(fmt.Sprintf("A distributed process with the ID '%s' does not exist in current context. Try the ps command …", ID))
return
}
_, err = kubectl(true, "scale", "--replicas=0", "deployment", ID)
if err != nil {
killfail(line, err.Error())
return
}
_, err = kubectl(true, "delete", "deployment", ID)
if err != nil {
killfail(line, err.Error())
return
}
// gather info to remove from global DPT:
kubecontext, err := kubectl(true, "config", "current-context")
if err != nil {
killfail(line, err.Error())
return
}
dproc, err := dpt.getDProc(ID, kubecontext)
if err != nil {
killfail(line, err.Error())
return
}
svcname := dproc.ServiceName
debug(svcname)
_, err = kubectl(true, "delete", "service", svcname)
if err != nil {
killfail(line, err.Error())
return
}
if err != nil {
info(err.Error())
}
// finally, remove drpoc from global DPT:
dpt.removeDProc(dproc)
}
func hps(line string) {
args := ""
if strings.ContainsAny(line, " ") {
args = strings.Split(line, " ")[1]
}
var kubecontext string
switch args {
case "all":
kubecontext = ""
default:
k, err := kubectl(true, "config", "current-context")
if err != nil {
warn("Can't determine current context")
return
}
kubecontext = k
}
debug("in context " + kubecontext)
res := dpt.DumpDPT(kubecontext)
output(res)
}
func hcontexts(line string) {
if !strings.ContainsAny(line, " ") {
res, err := kubectl(true, "config", "get-contexts")
if err != nil {
warn(fmt.Sprintf("Can't list contexts: %s", err))
return
}
output(res)
return
}
targetcontext := strings.Split(line, " ")[1]
res, err := kubectl(true, "config", "use-context", targetcontext)
if err != nil {
warn(fmt.Sprintf("Can't switch context: %s", err))
return
}
output(res)
if rl != nil {
setprompt()
}
}
func hns(line string) {
if !strings.ContainsAny(line, " ") {
res, err := kubectl(true, "get", "ns")
if err != nil {
warn(fmt.Sprintf("Can't list namespaces: %s", err))
return
}
output(res)
return
}
currentcx, err := kubectl(false, "config", "current-context")
if err != nil {
warn("Can't determine current context")
return
}
targetns := strings.Split(line, " ")[1]
res, err := kubectl(true, "config", "set-context", currentcx,
"--namespace="+targetns)
if err != nil {
warn(fmt.Sprintf("Can't switch namespace: %s", err))
return
}
output(res)
if rl != nil {
setprompt()
}
}
func himg() {
res, err := kubectl(true, "get", "po", "--all-namespaces", "-o", "jsonpath=\"{..image}\"")
if err != nil {
warn(fmt.Sprintf("Can't list container images: %s", err))
return
}
seenimgs := []string{}
for _, img := range strings.Split(strings.Trim(res, "\""), " ") {
if !contains(seenimgs, img) {
seenimgs = append(seenimgs, img)
}
}
sort.Strings(seenimgs)
for _, uimg := range seenimgs {
output(uimg)
}
}
func contains(s []string, e string) bool {
for _, a := range s {
if a == e {
return true
}
}
return false
}
func launchfail(line, reason string) {
fmt.Printf("\nCan't launch %s in the cluster:\n%s\n\n", strconv.Quote(line), reason)
}
func killfail(line, reason string) {
fmt.Printf("\nCan't kill %s:\n%s\n\n", strconv.Quote(line), reason)
}
func extractsrc(line string) string {
line = strings.TrimSuffix(line, "&")
line = strings.TrimSpace(line)
debug("input line: " + line)
// a binary is standalone:
if !strings.ContainsAny(line, " ") {
_, binfile := filepath.Split(line)
debug("binary extracted: " + binfile)
return binfile
}
// … otherwise it's a script:
script := strings.Split(line, " ")[1]
_, scriptfile := filepath.Split(script)
debug("script extracted: " + scriptfile)
return scriptfile
}
func hlaunch(line string) {
// If a line doesn't start with one of the
// known environments, assume user wants to
// launch a binary:
var dpid, svcname string
src := extractsrc(line)
src = "script:" + src
switch {
case strings.HasPrefix(line, "python "):
d, s, err := launchpy(line)
if err != nil {
launchfail(line, err.Error())
return
}
dpid = d
svcname = s
case strings.HasPrefix(line, "node "):
d, s, err := launchjs(line)
if err != nil {
launchfail(line, err.Error())
return
}
dpid = d
svcname = s
case strings.HasPrefix(line, "ruby "):
d, s, err := launchrb(line)
if err != nil {
launchfail(line, err.Error())
return
}
dpid = d
svcname = s
default: // binary
d, s, err := launchbin(line)
if err != nil {
launchfail(line, err.Error())
return
}
dpid = d
svcname = s
src = "bin:" + extractsrc(line)
}
// update DPT
if strings.HasSuffix(line, "&") {
kubecontext, err := kubectl(true, "config", "current-context")
if err != nil {
launchfail(line, err.Error())
return
}
dpt.addDProc(newDProc(dpid, DProcLongRunning, kubecontext, src, svcname, currentenv().evt.get("SERVICE_PORT"), currentenv().name))
}
}
func hliterally(line string) {
if !strings.ContainsAny(line, " ") {
info("Can't execute kubectl command, not enough arguments")
return
}
l := strings.Split(line, " ")
res, _ := kubectl(true, l[1], l[2:]...)
output(res)
}
func hecho(line string) {
if !strings.ContainsAny(line, " ") {
info("No value to echo given")
return
}
echo := strings.Split(line, " ")[1]
if strings.HasPrefix(echo, "$") {
v := currentenv().evt.get(echo[1:])
if v != "" {
fmt.Println(v)
return
}
}
fmt.Println(echo)
}
func hplugins(line string) {
cmd := line
args := []string{}
if strings.ContainsAny(line, " ") {
cmd = strings.Split(line, " ")[1]
args = strings.Split(line, " ")[2:]
}
res, err := kubectl(true, cmd, args...)
if err != nil {
fmt.Printf("Can't execute %s locally: %s", cmd, err)
}
output(res)
}