-
Notifications
You must be signed in to change notification settings - Fork 3
/
global.go
301 lines (262 loc) · 8.67 KB
/
global.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
package main
import (
"fmt"
"os"
"os/exec"
"os/signal"
"regexp"
"strings"
"github.com/mattermost/mattermost-server/model"
"github.com/pelletier/go-toml"
)
var client *model.Client4
var webSocketClient *model.WebSocketClient
var botUser *model.User
var botTeam *model.Team
var debuggingChannel *model.Channel
//Splash is a splash
const Splash = `
┬┌─┬ ┬┌┐ ┌─┐┬─┐┌┐┌┌─┐┌┬┐┌─┐┌─┐
├┴┐│ │├┴┐├┤ ├┬┘│││├┤ │ ├┤ └─┐
┴ ┴└─┘└─┘└─┘┴└─┘└┘└─┘ ┴ └─┘└─┘
┌┬┐┌─┐┌┬┐┌┬┐┌─┐┬─┐┌┬┐┌─┐┌─┐┌┬┐
│││├─┤ │ │ ├┤ ├┬┘││││ │└─┐ │
┴ ┴┴ ┴ ┴ ┴ └─┘┴└─┴ ┴└─┘└─┘ ┴
`
// Config is a struc of config
type Config struct {
host string
kubectlPath string
botName string
channelName string
teamName string
userLogin string
userPassword string
}
//LoadConfig load the toml file into the lib
func LoadConfig(ConfigPath string) *toml.Tree {
config, err := toml.LoadFile(ConfigPath)
if err != nil {
fmt.Printf("%s %s\n", ConfigPath, "is unreadable (check the file path or lint it on https://www.tomllint.com)")
os.Exit(1)
}
return config
}
//ParseConfig set the Config object with the actual value of the toml
func ParseConfig(config *toml.Tree) Config {
keysArray := config.Keys()
if StringInSlice("general", keysArray) == false {
fmt.Printf("%s %s\n", "The config file don't get any [general] section in", *configPath)
os.Exit(1)
}
conf := Config{
botName: config.Get("general.bot_name").(string),
kubectlPath: config.Get("general.kubectl_path").(string),
host: config.Get("mattermost.host").(string),
channelName: config.Get("mattermost.channel_name").(string),
teamName: config.Get("mattermost.team_name").(string),
userLogin: config.Get("mattermost.user_login").(string),
userPassword: config.Get("mattermost.user_password").(string),
}
return conf
}
// MakeSureServerIsRunning ensure the server is running
func MakeSureServerIsRunning() {
if props, resp := client.GetOldClientConfig(""); resp.Error != nil {
println("There was a problem pinging the Mattermost server. Are you sure it's running?")
PrintError(resp.Error)
os.Exit(1)
} else {
println("Server detected and is running version " + props["Version"] + "\n")
}
}
// LoginAsTheBotUser login as the bot
func LoginAsTheBotUser(email string, password string) {
if user, resp := client.Login(email, password); resp.Error != nil {
println("There was a problem logging into the Mattermost server. Are you sure ran the setup steps from the README.md?")
PrintError(resp.Error)
os.Exit(1)
} else {
botUser = user
}
}
// FindBotTeam is use to find the team
func FindBotTeam(teamName string) {
if team, resp := client.GetTeamByName(teamName, ""); resp.Error != nil {
println("We failed to get the initial load")
println("or we do not appear to be a member of the team '" + teamName + "'")
PrintError(resp.Error)
os.Exit(1)
} else {
botTeam = team
}
}
// CreateBotDebuggingChannelIfNeeded create the channel
func CreateBotDebuggingChannelIfNeeded(channelName string) {
if rchannel, resp := client.GetChannelByName(channelName, botTeam.Id, ""); resp.Error != nil {
println("We failed to get the channels")
PrintError(resp.Error)
} else {
debuggingChannel = rchannel
return
}
// Looks like we need to create the logging channel
channel := &model.Channel{}
channel.Name = channelName
channel.DisplayName = channelName
channel.Purpose = "This is used as a channel for Kubernetes interactions"
channel.Type = model.CHANNEL_OPEN
channel.TeamId = botTeam.Id
if rchannel, resp := client.CreateChannel(channel); resp.Error != nil {
println("We failed to create the channel " + channelName)
PrintError(resp.Error)
} else {
debuggingChannel = rchannel
println("Looks like this might be the first run so we've created the channel " + channelName)
}
}
// SendMsgToDebuggingChannel send msg if its ok
func SendMsgToDebuggingChannel(msg string, replyToID string) {
post := &model.Post{}
post.ChannelId = debuggingChannel.Id
post.Message = msg
post.RootId = replyToID
if _, resp := client.CreatePost(post); resp.Error != nil {
println("We failed to send a message to the channel")
PrintError(resp.Error)
}
}
// HandleWebSocketResponse handle the socket
func HandleWebSocketResponse(event *model.WebSocketEvent) {
HandleMsgFromDebuggingChannel(event)
}
// HandleMsgFromDebuggingChannel handle the msg
func HandleMsgFromDebuggingChannel(event *model.WebSocketEvent) {
// If this isn't the debugging channel then lets ingore it
if event.Broadcast.ChannelId != debuggingChannel.Id {
return
}
// Lets only reponded to messaged posted events
if event.Event != model.WEBSOCKET_EVENT_POSTED {
return
}
post := model.PostFromJson(strings.NewReader(event.Data["post"].(string)))
if post != nil {
// ignore my events
if post.UserId == botUser.Id {
return
}
if matched, _ := regexp.MatchString(KubeWord, post.Message); matched {
words := strings.Fields(post.Message)
message := strings.TrimSpace(post.Message)
cmd := CheckBeforeExec(words, message)
if len(cmd) > 0 && cmd != "command forbidden" {
fmt.Printf("responding to -> %s", post.Message)
cmdOut := ExecKubectl(cmd)
if cmdOut != "" && len(cmdOut) > 0 {
SendMsgToDebuggingChannel(cmdOut, post.Id)
fmt.Printf(" <- Sent. \n")
return
}
}
if cmd == "command forbidden" {
SendMsgToDebuggingChannel(cmd, post.Id)
return
}
}
// if you see any word matching 'alive' then respond
if matched, _ := regexp.MatchString(`(?:^|\W)alive(?:$|\W)`, post.Message); matched {
SendMsgToDebuggingChannel("Yes I'm running", post.Id)
return
}
if matched, _ := regexp.MatchString(`(?:^|\W)help(?:$|\W)`, post.Message); matched {
SendMsgToDebuggingChannel("!k [namespace] verb [ressource]", post.Id)
return
}
// if you see any word matching 'up' then respond
if matched, _ := regexp.MatchString(`(?:^|\W)up(?:$|\W)`, post.Message); matched {
SendMsgToDebuggingChannel("Yes I'm running", post.Id)
return
}
// if you see any word matching 'running' then respond
if matched, _ := regexp.MatchString(`(?:^|\W)running(?:$|\W)`, post.Message); matched {
SendMsgToDebuggingChannel("Yes I'm running", post.Id)
return
}
// if you see any word matching 'hello' then respond
if matched, _ := regexp.MatchString(`(?:^|\W)Hello(?:$|\W)`, post.Message); matched {
SendMsgToDebuggingChannel("Hello my friend !", post.Id)
return
}
}
//SendMsgToDebuggingChannel("I did not understand you!", post.Id)
}
// CheckBeforeExec - Check stuffs before exec.
func CheckBeforeExec(words []string, lastmsg string) string {
var cmd string
if words[0] == KubeWord && len(words) >= 3 {
confToml := LoadConfig(*configPath)
conf := ParseConfig(confToml)
kubectlAndNs := fmt.Sprintf(conf.kubectlPath + " -n")
cmd = strings.Replace(lastmsg, KubeWord, kubectlAndNs, -1)
// If it contain "all" namespace
if words[1] == "all" {
cmd = cmd + " --all-namespaces"
}
if !StringInSlice(words[2], ValidVerbs) {
fmt.Printf("error -> command unavailable <- %+v \n", lastmsg)
cmd = "command forbidden"
}
// Match TRUSTED words (get, scale ...)
if words[2] == "logs" && StringInSlice("-f", words) {
fmt.Printf("error -> command unavailable <- %+v \n", lastmsg)
cmd = "command forbidden"
}
if words[2] == "exec" && StringInSlice("-it", words) {
fmt.Printf("error -> command unavailable <- %+v \n", lastmsg)
cmd = "command forbidden"
}
}
return cmd
}
// ExecKubectl - Launch and format kubectl cmd.
func ExecKubectl(cmd string) string {
var cl string
args := strings.Split(cmd, " ")
out, err := exec.Command(args[0], args[1:]...).Output()
if err == nil {
result := fmt.Sprintf("``` \n %s ```", out)
cl = strings.Replace(result, "\n\n", "\n", -1)
}
return cl
}
// PrintError print the connexions error
func PrintError(err *model.AppError) {
println("\tError Details:")
println("\t\t" + err.Message)
println("\t\t" + err.Id)
println("\t\t" + err.DetailedError)
}
// SetupGracefulShutdown preapre the graceful shut
func SetupGracefulShutdown(botName string) {
c := make(chan os.Signal, 1)
signal.Notify(c, os.Interrupt)
go func() {
for range c {
if webSocketClient != nil {
webSocketClient.Close()
}
SendMsgToDebuggingChannel("_"+botName+" has **stopped** running_", "")
os.Exit(0)
}
}()
}
//StringInSlice check if a string is in a []string
func StringInSlice(a string, list []string) bool {
for _, b := range list {
if b == a {
return true
}
}
return false
}