forked from futuretea/harvester-auto
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
947 lines (898 loc) · 34 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
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
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
package main
import (
"context"
"errors"
"fmt"
"log"
"os"
"strings"
"github.com/fsnotify/fsnotify"
"github.com/shomali11/slacker"
"github.com/sirupsen/logrus"
"github.com/spf13/viper"
"github.com/futuretea/harvester-auto/pkg/config"
"github.com/futuretea/harvester-auto/pkg/constants"
"github.com/futuretea/harvester-auto/pkg/ctx"
"github.com/futuretea/harvester-auto/pkg/util"
)
var (
conf config.Config
userContexts = map[string]*ctx.Context{}
users = map[string]*config.User{}
)
func init() {
// config
viper.SetConfigName("config")
viper.SetConfigType("yaml")
viper.AddConfigPath(constants.ConfigDir)
if err := viper.ReadInConfig(); err != nil {
panic(fmt.Errorf("fatal error config file: %w", err))
}
if err := viper.Unmarshal(&conf); err != nil {
panic(fmt.Errorf("unable to decode into struct, %w", err))
}
go dynamicConfig()
// log
logrus.SetFormatter(&logrus.JSONFormatter{})
logrus.SetLevel(logrus.WarnLevel)
// env
for envName, envValue := range conf.Slack.Envs {
os.Setenv(envName, envValue)
}
// users
for _, user := range conf.Slack.Users {
users[user.Name] = user
}
}
func dynamicConfig() {
viper.WatchConfig()
viper.OnConfigChange(func(e fsnotify.Event) {
if err := viper.Unmarshal(&conf); err != nil {
panic(fmt.Errorf("unable to decode into struct, %w", err))
}
})
}
func getUserContext(userName string) *ctx.Context {
return userContexts[userName]
}
func setUserContext(userName string, userContext *ctx.Context) {
userContexts[userName] = userContext
}
func getUserByUserName(userName string) (*config.User, bool) {
user, exist := users[userName]
return user, exist
}
func main() {
// new bot
bot := slacker.NewClient(conf.Slack.BotToken, conf.Slack.AppToken)
authorizationFunc := func(botCtx slacker.BotContext, request slacker.Request) bool {
userName := botCtx.Event().UserName
text := botCtx.Event().Text
_, exist := getUserByUserName(userName)
if !exist {
logrus.Infof("unknown user: %s", userName)
return false
}
userContext := getUserContext(userName)
if userContext == nil {
userContext = ctx.CreateContext(0, text)
setUserContext(userName, userContext)
} else {
userContext.Record(text)
}
return true
}
// command ping
pingDefinition := &slacker.CommandDefinition{
Description: "Ping!",
Examples: []string{"ping"},
AuthorizationFunc: authorizationFunc,
Handler: func(botCtx slacker.BotContext, request slacker.Request, response slacker.ResponseWriter) {
logrus.Error(response.Reply("pong", util.ReplyOpt(botCtx)))
},
}
bot.Command("ping", pingDefinition)
// command history
historyDefinition := &slacker.CommandDefinition{
Description: "Show history",
Examples: []string{"history", "history 10"},
AuthorizationFunc: authorizationFunc,
Handler: func(botCtx slacker.BotContext, request slacker.Request, response slacker.ResponseWriter) {
userName := botCtx.Event().UserName
userContext := getUserContext(userName)
historyNumber := request.IntegerParam("historyNumber", 20)
historyLen := len(userContext.History)
var text string
if historyLen > historyNumber {
text = strings.Join(userContext.History[historyLen-historyNumber:], "\n")
} else {
if historyLen == 0 {
text = "N/A"
} else {
text = strings.Join(userContext.History, "\n")
}
}
logrus.Error(response.Reply(text, util.ReplyOpt(botCtx)))
},
}
bot.Command("history {historyNumber}", historyDefinition)
// command l
lDefinition := &slacker.CommandDefinition{
Description: "List Harvester clusters",
Examples: []string{"l"},
AuthorizationFunc: authorizationFunc,
Handler: func(botCtx slacker.BotContext, request slacker.Request, response slacker.ResponseWriter) {
user, _ := getUserByUserName(botCtx.Event().UserName)
userContext := getUserContext(botCtx.Event().UserName)
clusterID := userContext.GetClusterID()
bashCommand := fmt.Sprintf("./l.sh %d %d", user.NamespaceID, clusterID)
util.Shell2Reply(botCtx, response, bashCommand)
},
}
bot.Command("l", lDefinition)
// command c
clusterDefinition := &slacker.CommandDefinition{
Description: "Show/Set Current Harvester cluster",
Examples: []string{"c (show current cluster id)", "c 1 (set current cluster id to 1)"},
AuthorizationFunc: authorizationFunc,
Handler: func(botCtx slacker.BotContext, request slacker.Request, response slacker.ResponseWriter) {
userName := botCtx.Event().UserName
userContext := getUserContext(userName)
requestClusterID := uint8(request.IntegerParam("clusterID", 0))
if requestClusterID != 0 {
userContext.SetClusterID(requestClusterID)
}
clusterID := userContext.GetClusterID()
if clusterID == 0 {
util.ClusterNotSetReply(botCtx, response)
return
}
text := fmt.Sprintf("current cluster id is %d", clusterID)
logrus.Error(response.Reply(text, util.ReplyOpt(botCtx)))
},
}
bot.Command("c {clusterID}", clusterDefinition)
// command url
urlDefinition := &slacker.CommandDefinition{
Description: "Show Harvester cluster URLs",
Examples: []string{"url"},
AuthorizationFunc: authorizationFunc,
Handler: func(botCtx slacker.BotContext, request slacker.Request, response slacker.ResponseWriter) {
userName := botCtx.Event().UserName
user, _ := getUserByUserName(userName)
userContext := getUserContext(userName)
namespaceID := user.NamespaceID
clusterID := userContext.GetClusterID()
if clusterID == 0 {
util.ClusterNotSetReply(botCtx, response)
return
}
bashCommand := fmt.Sprintf("./url.sh %d %d", namespaceID, clusterID)
util.Shell2Reply(botCtx, response, bashCommand)
},
}
bot.Command("url", urlDefinition)
// command status
statusDefinition := &slacker.CommandDefinition{
Description: "Show Harvester cluster status",
Examples: []string{"status"},
AuthorizationFunc: authorizationFunc,
Handler: func(botCtx slacker.BotContext, request slacker.Request, response slacker.ResponseWriter) {
userName := botCtx.Event().UserName
user, _ := getUserByUserName(userName)
userContext := getUserContext(userName)
namespaceID := user.NamespaceID
clusterID := userContext.GetClusterID()
if clusterID == 0 {
util.ClusterNotSetReply(botCtx, response)
return
}
bashCommand := fmt.Sprintf("./status.sh %d %d", namespaceID, clusterID)
util.Shell2Reply(botCtx, response, bashCommand)
},
}
bot.Command("status", statusDefinition)
// command version
versionDefinition := &slacker.CommandDefinition{
Description: "Show Harvester version",
Examples: []string{"version"},
AuthorizationFunc: authorizationFunc,
Handler: func(botCtx slacker.BotContext, request slacker.Request, response slacker.ResponseWriter) {
userName := botCtx.Event().UserName
user, _ := getUserByUserName(userName)
userContext := getUserContext(userName)
namespaceID := user.NamespaceID
clusterID := userContext.GetClusterID()
if clusterID == 0 {
util.ClusterNotSetReply(botCtx, response)
return
}
bashCommand := fmt.Sprintf("./version.sh %d %d", namespaceID, clusterID)
util.Shell2Reply(botCtx, response, bashCommand)
},
}
bot.Command("version", versionDefinition)
// command name
nameDefinition := &slacker.CommandDefinition{
Description: "Show/Set Harvester name",
Examples: []string{"name", "name test-cluster"},
AuthorizationFunc: authorizationFunc,
Handler: func(botCtx slacker.BotContext, request slacker.Request, response slacker.ResponseWriter) {
name := request.StringParam("name", "")
userName := botCtx.Event().UserName
user, _ := getUserByUserName(userName)
userContext := getUserContext(userName)
namespaceID := user.NamespaceID
clusterID := userContext.GetClusterID()
if clusterID == 0 {
util.ClusterNotSetReply(botCtx, response)
return
}
bashCommand := fmt.Sprintf("./name.sh %d %d %s", namespaceID, clusterID, name)
util.Shell2Reply(botCtx, response, bashCommand)
},
}
bot.Command("name {name}", nameDefinition)
// command settings
settingsDefinition := &slacker.CommandDefinition{
Description: "Show Harvester settings",
Examples: []string{"settings"},
AuthorizationFunc: authorizationFunc,
Handler: func(botCtx slacker.BotContext, request slacker.Request, response slacker.ResponseWriter) {
userName := botCtx.Event().UserName
user, _ := getUserByUserName(userName)
userContext := getUserContext(userName)
namespaceID := user.NamespaceID
clusterID := userContext.GetClusterID()
if clusterID == 0 {
util.ClusterNotSetReply(botCtx, response)
return
}
bashCommand := fmt.Sprintf("./settings.sh %d %d", namespaceID, clusterID)
util.Shell2Reply(botCtx, response, bashCommand)
},
}
bot.Command("settings", settingsDefinition)
// command podImages
pisDefinition := &slacker.CommandDefinition{
Description: "Show Pod Images",
Examples: []string{"pis", "pis cattle-system"},
AuthorizationFunc: authorizationFunc,
Handler: func(botCtx slacker.BotContext, request slacker.Request, response slacker.ResponseWriter) {
kubeNamespace := request.StringParam("namespace", "harvester-system")
userName := botCtx.Event().UserName
user, _ := getUserByUserName(userName)
userContext := getUserContext(userName)
namespaceID := user.NamespaceID
clusterID := userContext.GetClusterID()
if clusterID == 0 {
util.ClusterNotSetReply(botCtx, response)
return
}
bashCommand := fmt.Sprintf("./pis.sh %d %d %s", namespaceID, clusterID, kubeNamespace)
util.Shell2Reply(botCtx, response, bashCommand)
},
}
bot.Command("pis {namespace}", pisDefinition)
// command pods
podsDefinition := &slacker.CommandDefinition{
Description: "kubectl get pod",
Examples: []string{"pods", "pods cattle-system"},
AuthorizationFunc: authorizationFunc,
Handler: func(botCtx slacker.BotContext, request slacker.Request, response slacker.ResponseWriter) {
kubeNamespace := request.StringParam("kubeNamespace", "harvester-system")
userName := botCtx.Event().UserName
user, _ := getUserByUserName(userName)
userContext := getUserContext(userName)
namespaceID := user.NamespaceID
clusterID := userContext.GetClusterID()
if clusterID == 0 {
util.ClusterNotSetReply(botCtx, response)
return
}
bashCommand := fmt.Sprintf("./pods.sh %d %d %s", namespaceID, clusterID, kubeNamespace)
util.Shell2Reply(botCtx, response, bashCommand)
},
}
bot.Command("pods {kubeNamespace}", podsDefinition)
// command vms
vmsDefinition := &slacker.CommandDefinition{
Description: "kubectl get vm",
Examples: []string{"vms", "vms harvester-public"},
AuthorizationFunc: authorizationFunc,
Handler: func(botCtx slacker.BotContext, request slacker.Request, response slacker.ResponseWriter) {
kubeNamespace := request.StringParam("kubeNamespace", "default")
userName := botCtx.Event().UserName
user, _ := getUserByUserName(userName)
userContext := getUserContext(userName)
namespaceID := user.NamespaceID
clusterID := userContext.GetClusterID()
if clusterID == 0 {
util.ClusterNotSetReply(botCtx, response)
return
}
bashCommand := fmt.Sprintf("./vms.sh %d %d %s", namespaceID, clusterID, kubeNamespace)
util.Shell2Reply(botCtx, response, bashCommand)
},
}
bot.Command("vms {kubeNamespace}", vmsDefinition)
// command kubeconfig
kubeconfigDefinition := &slacker.CommandDefinition{
Description: "Show Harvester cluster kubeconfig content",
Examples: []string{"kubeconfig"},
AuthorizationFunc: authorizationFunc,
Handler: func(botCtx slacker.BotContext, request slacker.Request, response slacker.ResponseWriter) {
userName := botCtx.Event().UserName
user, _ := getUserByUserName(userName)
userContext := getUserContext(userName)
namespaceID := user.NamespaceID
clusterID := userContext.GetClusterID()
if clusterID == 0 {
util.ClusterNotSetReply(botCtx, response)
return
}
bashCommand := fmt.Sprintf("./kubeconfig.sh %d %d", namespaceID, clusterID)
util.Shell2Reply(botCtx, response, bashCommand)
},
}
bot.Command("kubeconfig", kubeconfigDefinition)
// command sshconfig
sshconfigDefinition := &slacker.CommandDefinition{
Description: "Show ssh config for connecting",
Examples: []string{"sshconfig"},
AuthorizationFunc: authorizationFunc,
Handler: func(botCtx slacker.BotContext, request slacker.Request, response slacker.ResponseWriter) {
userName := botCtx.Event().UserName
user, _ := getUserByUserName(userName)
userContext := getUserContext(userName)
namespaceID := user.NamespaceID
clusterID := userContext.GetClusterID()
if clusterID == 0 {
util.ClusterNotSetReply(botCtx, response)
return
}
bashCommand := fmt.Sprintf("./sshconfig.sh %d %d", namespaceID, clusterID)
util.Shell2Reply(botCtx, response, bashCommand)
},
}
bot.Command("sshconfig", sshconfigDefinition)
// command destroy
destroyDefinition := &slacker.CommandDefinition{
Description: "Destroy Harvester cluster nodes",
Examples: []string{"destroy"},
AuthorizationFunc: authorizationFunc,
Handler: func(botCtx slacker.BotContext, request slacker.Request, response slacker.ResponseWriter) {
userName := botCtx.Event().UserName
user, _ := getUserByUserName(userName)
userContext := getUserContext(userName)
namespaceID := user.NamespaceID
if user.Mode != config.ModeRW {
util.NoPermissionReply(botCtx, response)
return
}
clusterID := userContext.GetClusterID()
if clusterID == 0 {
util.ClusterNotSetReply(botCtx, response)
return
}
bashCommand := fmt.Sprintf("./destroy.sh %d %d", namespaceID, clusterID)
util.Shell2Reply(botCtx, response, bashCommand)
},
}
bot.Command("destroy", destroyDefinition)
// command start
startDefinition := &slacker.CommandDefinition{
Description: "Start Harvester cluster nodes",
Examples: []string{"start"},
AuthorizationFunc: authorizationFunc,
Handler: func(botCtx slacker.BotContext, request slacker.Request, response slacker.ResponseWriter) {
userName := botCtx.Event().UserName
user, _ := getUserByUserName(userName)
userContext := getUserContext(userName)
namespaceID := user.NamespaceID
if user.Mode != config.ModeRW {
util.NoPermissionReply(botCtx, response)
return
}
clusterID := userContext.GetClusterID()
if clusterID == 0 {
util.ClusterNotSetReply(botCtx, response)
return
}
bashCommand := fmt.Sprintf("./start.sh %d %d", namespaceID, clusterID)
util.Shell2Reply(botCtx, response, bashCommand)
},
}
bot.Command("start", startDefinition)
// command stop
stopDefinition := &slacker.CommandDefinition{
Description: "Stop Harvester cluster nodes",
Examples: []string{"stop"},
AuthorizationFunc: authorizationFunc,
Handler: func(botCtx slacker.BotContext, request slacker.Request, response slacker.ResponseWriter) {
userName := botCtx.Event().UserName
user, _ := getUserByUserName(userName)
userContext := getUserContext(userName)
namespaceID := user.NamespaceID
if user.Mode != config.ModeRW {
util.NoPermissionReply(botCtx, response)
return
}
clusterID := userContext.GetClusterID()
if clusterID == 0 {
util.ClusterNotSetReply(botCtx, response)
return
}
bashCommand := fmt.Sprintf("./stop.sh %d %d", namespaceID, clusterID)
util.Shell2Reply(botCtx, response, bashCommand)
},
}
bot.Command("stop", stopDefinition)
// command restart
restartDefinition := &slacker.CommandDefinition{
Description: "Restart Harvester cluster nodes",
Examples: []string{"restart"},
AuthorizationFunc: authorizationFunc,
Handler: func(botCtx slacker.BotContext, request slacker.Request, response slacker.ResponseWriter) {
userName := botCtx.Event().UserName
user, _ := getUserByUserName(userName)
userContext := getUserContext(userName)
namespaceID := user.NamespaceID
if user.Mode != config.ModeRW {
util.NoPermissionReply(botCtx, response)
return
}
clusterID := userContext.GetClusterID()
if clusterID == 0 {
util.ClusterNotSetReply(botCtx, response)
return
}
bashCommand := fmt.Sprintf("./restart.sh %d %d", namespaceID, clusterID)
util.Shell2Reply(botCtx, response, bashCommand)
},
}
bot.Command("restart", restartDefinition)
// command snaps
snapsDefinition := &slacker.CommandDefinition{
Description: "List Harvester cluster snapshots",
Examples: []string{"snaps"},
AuthorizationFunc: authorizationFunc,
Handler: func(botCtx slacker.BotContext, request slacker.Request, response slacker.ResponseWriter) {
userName := botCtx.Event().UserName
user, _ := getUserByUserName(userName)
userContext := getUserContext(userName)
namespaceID := user.NamespaceID
if user.Mode != config.ModeRW {
util.NoPermissionReply(botCtx, response)
return
}
clusterID := userContext.GetClusterID()
if clusterID == 0 {
util.ClusterNotSetReply(botCtx, response)
return
}
bashCommand := fmt.Sprintf("./snaps.sh %d %d", namespaceID, clusterID)
util.Shell2Reply(botCtx, response, bashCommand)
},
}
bot.Command("snaps", snapsDefinition)
// command snap
snapDefinition := &slacker.CommandDefinition{
Description: "Snapshot Harvester cluster nodes",
Examples: []string{"snap foo"},
AuthorizationFunc: authorizationFunc,
Handler: func(botCtx slacker.BotContext, request slacker.Request, response slacker.ResponseWriter) {
userName := botCtx.Event().UserName
user, _ := getUserByUserName(userName)
userContext := getUserContext(userName)
namespaceID := user.NamespaceID
if user.Mode != config.ModeRW {
util.NoPermissionReply(botCtx, response)
return
}
clusterID := userContext.GetClusterID()
if clusterID == 0 {
util.ClusterNotSetReply(botCtx, response)
return
}
snapshotName := request.Param("snapshotName")
if snapshotName == "" {
response.ReportError(errors.New("missing snapshotName"), util.ReplyErrorOpt(botCtx))
return
}
bashCommand := fmt.Sprintf("./snap.sh %d %d %s", namespaceID, clusterID, snapshotName)
util.Shell2Reply(botCtx, response, bashCommand)
},
}
bot.Command("snap {snapshotName}", snapDefinition)
// command revert
revertDefinition := &slacker.CommandDefinition{
Description: "Revert Harvester cluster nodes",
Examples: []string{"revert foo"},
AuthorizationFunc: authorizationFunc,
Handler: func(botCtx slacker.BotContext, request slacker.Request, response slacker.ResponseWriter) {
userName := botCtx.Event().UserName
user, _ := getUserByUserName(userName)
userContext := getUserContext(userName)
namespaceID := user.NamespaceID
if user.Mode != config.ModeRW {
util.NoPermissionReply(botCtx, response)
return
}
clusterID := userContext.GetClusterID()
if clusterID == 0 {
util.ClusterNotSetReply(botCtx, response)
return
}
snapshotName := request.Param("snapshotName")
if snapshotName == "" {
response.ReportError(errors.New("missing snapshotName"), util.ReplyErrorOpt(botCtx))
return
}
bashCommand := fmt.Sprintf("./revert.sh %d %d %s", namespaceID, clusterID, snapshotName)
util.Shell2Reply(botCtx, response, bashCommand)
},
}
bot.Command("revert {snapshotName}", revertDefinition)
// command virsh
virshDefinition := &slacker.CommandDefinition{
Description: "virsh command warpper",
Examples: []string{"virsh list"},
AuthorizationFunc: authorizationFunc,
Handler: func(botCtx slacker.BotContext, request slacker.Request, response slacker.ResponseWriter) {
command := request.StringParam("command", "")
args := request.StringParam("args", "")
bashCommand := fmt.Sprintf("./virsh.sh %s %s", command, args)
util.Shell2Reply(botCtx, response, bashCommand)
},
}
bot.Command("virsh {command} {args}", virshDefinition)
// command ps
psDefinition := &slacker.CommandDefinition{
Description: "Show running jobs",
Examples: []string{"ps"},
AuthorizationFunc: authorizationFunc,
Handler: func(botCtx slacker.BotContext, request slacker.Request, response slacker.ResponseWriter) {
userName := botCtx.Event().UserName
user, _ := getUserByUserName(userName)
userContext := getUserContext(userName)
namespaceID := user.NamespaceID
clusterID := userContext.GetClusterID()
bashCommand := fmt.Sprintf("./ps.sh %d %d", namespaceID, clusterID)
util.Shell2Reply(botCtx, response, bashCommand)
},
}
bot.Command("ps", psDefinition)
// command log
logDefinition := &slacker.CommandDefinition{
Description: "Tail Job logs",
Examples: []string{"log 2c", "log 2c 100", "log 2pt", "log 2pt 100", "log 2ui", "log 2ui 100", "log sc", "log sc 100", "log up", "log up 100"},
AuthorizationFunc: authorizationFunc,
Handler: func(botCtx slacker.BotContext, request slacker.Request, response slacker.ResponseWriter) {
userName := botCtx.Event().UserName
user, _ := getUserByUserName(userName)
userContext := getUserContext(userName)
namespaceID := user.NamespaceID
clusterID := userContext.GetClusterID()
job := request.StringParam("job", "")
switch job {
case "2c", "2pt", "sc", "up":
if clusterID == 0 {
util.ClusterNotSetReply(botCtx, response)
return
}
case "2ui", "2iso":
case "":
response.ReportError(errors.New("missing job type"), util.ReplyErrorOpt(botCtx))
return
default:
response.ReportError(errors.New("invalid job type"), util.ReplyErrorOpt(botCtx))
return
}
lineNumber := request.IntegerParam("lineNumber", 20)
bashCommand := fmt.Sprintf("./log.sh %d %d %s %d", namespaceID, clusterID, job, lineNumber)
util.Shell2Reply(botCtx, response, bashCommand)
},
}
bot.Command("log {job} {lineNumber}", logDefinition)
// command kill
killDefinition := &slacker.CommandDefinition{
Description: "Kill running job",
Examples: []string{"kill 2c", "kill 2pt", "kill 2ui", "kill sc", "kill up", "kill iso"},
AuthorizationFunc: authorizationFunc,
Handler: func(botCtx slacker.BotContext, request slacker.Request, response slacker.ResponseWriter) {
userName := botCtx.Event().UserName
user, _ := getUserByUserName(userName)
userContext := getUserContext(userName)
namespaceID := user.NamespaceID
if user.Mode != config.ModeRW {
util.NoPermissionReply(botCtx, response)
return
}
clusterID := userContext.GetClusterID()
job := request.StringParam("job", "")
switch job {
case "2c", "2pt", "sc", "up":
if clusterID == 0 {
util.ClusterNotSetReply(botCtx, response)
return
}
case "2ui", "2iso":
case "":
response.ReportError(errors.New("missing job type"), util.ReplyErrorOpt(botCtx))
return
default:
response.ReportError(errors.New("invalid job type"), util.ReplyErrorOpt(botCtx))
return
}
bashCommand := fmt.Sprintf("./kill.sh %d %d %s", namespaceID, clusterID, job)
util.Shell2Reply(botCtx, response, bashCommand)
},
}
bot.Command("kill {job}", killDefinition)
// command pr2iso
pr2isoDefinition := &slacker.CommandDefinition{
Description: "Build Harvester ISO after merging PRs or checkout branches",
Examples: []string{"pr2iso 0 0"},
AuthorizationFunc: authorizationFunc,
Handler: func(botCtx slacker.BotContext, request slacker.Request, response slacker.ResponseWriter) {
userName := botCtx.Event().UserName
user, _ := getUserByUserName(userName)
namespaceID := user.NamespaceID
if user.Mode != config.ModeRW {
util.NoPermissionReply(botCtx, response)
return
}
harvesterPRs := request.StringParam("harvesterPRs", "0")
harvesterInstallerPRs := request.StringParam("harvesterInstallerPRs", "0")
bashCommand := fmt.Sprintf("./pr2iso.sh %d %s %s", namespaceID, harvesterPRs, harvesterInstallerPRs)
util.Shell2Reply(botCtx, response, bashCommand)
},
}
bot.Command("pr2iso {harvesterPRs} {harvesterInstallerPRs}", pr2isoDefinition)
// command pr2c
pr2cDefinition := &slacker.CommandDefinition{
Description: "Create a Harvester cluster after merging PRs or checkout branches, always build ISO",
Examples: []string{"pr2c 0 0"},
AuthorizationFunc: authorizationFunc,
Handler: func(botCtx slacker.BotContext, request slacker.Request, response slacker.ResponseWriter) {
userName := botCtx.Event().UserName
user, _ := getUserByUserName(userName)
userContext := getUserContext(userName)
namespaceID := user.NamespaceID
if user.Mode != config.ModeRW {
util.NoPermissionReply(botCtx, response)
return
}
clusterID := userContext.GetClusterID()
if clusterID == 0 {
util.ClusterNotSetReply(botCtx, response)
return
}
harvesterPRs := request.StringParam("harvesterPRs", "0")
harvesterInstallerPRs := request.StringParam("harvesterInstallerPRs", "0")
harvesterConfigURL := request.StringParam("harvesterConfigURL", "")
bashCommand := fmt.Sprintf("./pr2c.sh %d %d %s %s %s %t", namespaceID, clusterID, harvesterPRs, harvesterInstallerPRs, harvesterConfigURL, false)
util.Shell2Reply(botCtx, response, bashCommand)
},
}
bot.Command("pr2c {harvesterPRs} {harvesterInstallerPRs} {harvesterConfigURL}", pr2cDefinition)
// command pr2cNoBuild
pr2cNoBuildDefinition := &slacker.CommandDefinition{
Description: "Create a Harvester cluster based on PRs or branches, but use the built ISO from pr2iso/pr2c/pr2up",
Examples: []string{"pr2cNoBuild 0 0"},
AuthorizationFunc: authorizationFunc,
Handler: func(botCtx slacker.BotContext, request slacker.Request, response slacker.ResponseWriter) {
userName := botCtx.Event().UserName
user, _ := getUserByUserName(userName)
userContext := getUserContext(userName)
namespaceID := user.NamespaceID
if user.Mode != config.ModeRW {
util.NoPermissionReply(botCtx, response)
return
}
clusterID := userContext.GetClusterID()
if clusterID == 0 {
util.ClusterNotSetReply(botCtx, response)
return
}
harvesterPRs := request.StringParam("harvesterPRs", "0")
harvesterInstallerPRs := request.StringParam("harvesterInstallerPRs", "0")
harvesterConfigURL := request.StringParam("harvesterConfigURL", "")
bashCommand := fmt.Sprintf("./pr2c.sh %d %d %s %s %s %t", namespaceID, clusterID, harvesterPRs, harvesterInstallerPRs, harvesterConfigURL, true)
util.Shell2Reply(botCtx, response, bashCommand)
},
}
bot.Command("pr2cNoBuild {harvesterPRs} {harvesterInstallerPRs} {harvesterConfigURL}", pr2cNoBuildDefinition)
// command v2c
v2cDefinition := &slacker.CommandDefinition{
Description: "Create a Harvester cluster after downloading the ISO",
Examples: []string{"v2c v1.1"},
AuthorizationFunc: authorizationFunc,
Handler: func(botCtx slacker.BotContext, request slacker.Request, response slacker.ResponseWriter) {
userName := botCtx.Event().UserName
user, _ := getUserByUserName(userName)
userContext := getUserContext(userName)
namespaceID := user.NamespaceID
if user.Mode != config.ModeRW {
util.NoPermissionReply(botCtx, response)
return
}
clusterID := userContext.GetClusterID()
if clusterID == 0 {
util.ClusterNotSetReply(botCtx, response)
return
}
harvesterVersion := request.StringParam("harvesterVersion", "0")
harvesterConfigURL := request.StringParam("harvesterConfigURL", "")
bashCommand := fmt.Sprintf("./v2c.sh %d %d %s %s", namespaceID, clusterID, harvesterVersion, harvesterConfigURL)
util.Shell2Reply(botCtx, response, bashCommand)
},
}
bot.Command("v2c {harvesterVersion} {harvesterConfigURL}", v2cDefinition)
// command pr2up
pr2upDefinition := &slacker.CommandDefinition{
Description: "Upgrade a Harvester cluster after merging PRs or checkout branches, always build ISO",
Examples: []string{"pr2up 0 0"},
AuthorizationFunc: authorizationFunc,
Handler: func(botCtx slacker.BotContext, request slacker.Request, response slacker.ResponseWriter) {
userName := botCtx.Event().UserName
user, _ := getUserByUserName(userName)
userContext := getUserContext(userName)
namespaceID := user.NamespaceID
if user.Mode != config.ModeRW {
util.NoPermissionReply(botCtx, response)
return
}
clusterID := userContext.GetClusterID()
if clusterID == 0 {
util.ClusterNotSetReply(botCtx, response)
return
}
harvesterPRs := request.StringParam("harvesterPRs", "0")
harvesterInstallerPRs := request.StringParam("harvesterInstallerPRs", "0")
bashCommand := fmt.Sprintf("./pr2up.sh %d %d %s %s %t", namespaceID, clusterID, harvesterPRs, harvesterInstallerPRs, false)
util.Shell2Reply(botCtx, response, bashCommand)
},
}
bot.Command("pr2up {harvesterPRs} {harvesterInstallerPRs}", pr2upDefinition)
// command pr2upNoBuild
pr2upNoBuildDefinition := &slacker.CommandDefinition{
Description: "Upgrade a Harvester cluster based on PRs or branches, but use the built ISO from pr2up or pr2c",
Examples: []string{"pr2upNoBuild 0 0"},
AuthorizationFunc: authorizationFunc,
Handler: func(botCtx slacker.BotContext, request slacker.Request, response slacker.ResponseWriter) {
userName := botCtx.Event().UserName
user, _ := getUserByUserName(userName)
userContext := getUserContext(userName)
namespaceID := user.NamespaceID
if user.Mode != config.ModeRW {
util.NoPermissionReply(botCtx, response)
return
}
clusterID := userContext.GetClusterID()
if clusterID == 0 {
util.ClusterNotSetReply(botCtx, response)
return
}
harvesterPRs := request.StringParam("harvesterPRs", "0")
harvesterInstallerPRs := request.StringParam("harvesterInstallerPRs", "0")
bashCommand := fmt.Sprintf("./pr2up.sh %d %d %s %s %t", namespaceID, clusterID, harvesterPRs, harvesterInstallerPRs, true)
util.Shell2Reply(botCtx, response, bashCommand)
},
}
bot.Command("pr2upNoBuild {harvesterPRs} {harvesterInstallerPRs}", pr2upNoBuildDefinition)
// command v2up
v2upDefinition := &slacker.CommandDefinition{
Description: "Upgrade a Harvester cluster after downloading the ISO",
Examples: []string{"v2up v1.1.2-rc5"},
AuthorizationFunc: authorizationFunc,
Handler: func(botCtx slacker.BotContext, request slacker.Request, response slacker.ResponseWriter) {
userName := botCtx.Event().UserName
user, _ := getUserByUserName(userName)
userContext := getUserContext(userName)
namespaceID := user.NamespaceID
if user.Mode != config.ModeRW {
util.NoPermissionReply(botCtx, response)
return
}
clusterID := userContext.GetClusterID()
if clusterID == 0 {
util.ClusterNotSetReply(botCtx, response)
return
}
harvesterVersion := request.StringParam("harvesterVersion", "0")
bashCommand := fmt.Sprintf("./v2up.sh %d %d %s", namespaceID, clusterID, harvesterVersion)
util.Shell2Reply(botCtx, response, bashCommand)
},
}
bot.Command("v2up {harvesterVersion}", v2upDefinition)
// command scale
scaleDefinition := &slacker.CommandDefinition{
Description: "Scale Harvester nodes",
Examples: []string{"scale 2"},
AuthorizationFunc: authorizationFunc,
Handler: func(botCtx slacker.BotContext, request slacker.Request, response slacker.ResponseWriter) {
specNodeNumber := request.IntegerParam("specNodeNumber", 1)
userName := botCtx.Event().UserName
user, _ := getUserByUserName(userName)
userContext := getUserContext(userName)
namespaceID := user.NamespaceID
if user.Mode != config.ModeRW {
util.NoPermissionReply(botCtx, response)
return
}
clusterID := userContext.GetClusterID()
if clusterID == 0 {
util.ClusterNotSetReply(botCtx, response)
return
}
bashCommand := fmt.Sprintf("./scale.sh %d %d %d", namespaceID, clusterID, specNodeNumber)
util.Shell2Reply(botCtx, response, bashCommand)
},
}
bot.Command("scale {specNodeNumber}", scaleDefinition)
// command pr2pt
pr2ptDefinition := &slacker.CommandDefinition{
Description: "Patch Harvester image after merging PRs or checkout branches, always build image",
Examples: []string{"pr2pt harvester 0"},
AuthorizationFunc: authorizationFunc,
Handler: func(botCtx slacker.BotContext, request slacker.Request, response slacker.ResponseWriter) {
userName := botCtx.Event().UserName
user, _ := getUserByUserName(userName)
userContext := getUserContext(userName)
namespaceID := user.NamespaceID
if user.Mode != config.ModeRW {
util.NoPermissionReply(botCtx, response)
return
}
clusterID := userContext.GetClusterID()
if clusterID == 0 {
util.ClusterNotSetReply(botCtx, response)
return
}
repoName := request.StringParam("repoName", "harvester")
repoPRs := request.StringParam("repoPRs", "0")
bashCommand := fmt.Sprintf("./pr2pt.sh %d %d %s %s", namespaceID, clusterID, repoName, repoPRs)
util.Shell2Reply(botCtx, response, bashCommand)
},
}
bot.Command("pr2pt {repoName} {repoPRs}", pr2ptDefinition)
// command pr2ui
pr2uiDefinition := &slacker.CommandDefinition{
Description: "Build Harvester Dashboard",
Examples: []string{"pr2ui 0"},
AuthorizationFunc: authorizationFunc,
Handler: func(botCtx slacker.BotContext, request slacker.Request, response slacker.ResponseWriter) {
userName := botCtx.Event().UserName
user, _ := getUserByUserName(userName)
namespaceID := user.NamespaceID
uiPRs := request.StringParam("uiPRs", "0")
bashCommand := fmt.Sprintf("./pr2ui.sh %d %s false", namespaceID, uiPRs)
util.Shell2Reply(botCtx, response, bashCommand)
},
}
bot.Command("pr2ui {uiPRs}", pr2uiDefinition)
// command pr2uiRancher
pr2uiRancherDefinition := &slacker.CommandDefinition{
Description: "Build Rancher Dashboard",
Examples: []string{"pr2uiRancher 0"},
AuthorizationFunc: authorizationFunc,
Handler: func(botCtx slacker.BotContext, request slacker.Request, response slacker.ResponseWriter) {
userName := botCtx.Event().UserName
user, _ := getUserByUserName(userName)
namespaceID := user.NamespaceID
uiPRs := request.StringParam("uiPRs", "0")
bashCommand := fmt.Sprintf("./pr2ui.sh %d %s true", namespaceID, uiPRs)
util.Shell2Reply(botCtx, response, bashCommand)
},
}
bot.Command("pr2uiRancher {uiPRs}", pr2uiRancherDefinition)
// bot run
c, cancel := context.WithCancel(context.Background())
defer cancel()
err := bot.Listen(c)
if err != nil {
log.Fatal(err)
}
}