/
commands.go
262 lines (224 loc) · 6.75 KB
/
commands.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
package main
import (
"context"
"fmt"
"strconv"
"strings"
"time"
"github.com/Azure/azure-sdk-for-go/sdk/azidentity"
"github.com/Azure/azure-sdk-for-go/sdk/resourcemanager/compute/armcompute"
"github.com/bwmarrin/discordgo"
log "github.com/sirupsen/logrus"
)
// startCommand is the command handler to start an azure VM.
func startCommand(s *discordgo.Session, m *discordgo.MessageCreate) {
targetVM, err := getTargetVMFromCommand(m.Content)
if err != nil {
response := fmt.Sprintf("Failed to stop the VM: %v", err)
respond(s, m.ChannelID, response)
return
}
log.Infof("Received start command for VM: %v", targetVM)
resourceGroup, err := getResourceGroupFromConfig(targetVM)
if err != nil {
response := fmt.Sprintf("Failed to start the VM: %v", err)
respond(s, m.ChannelID, response)
return
}
vmName, err := getVMNameFromConfig(targetVM)
if err != nil {
response := fmt.Sprintf("Failed to start the VM: %v", err)
respond(s, m.ChannelID, response)
return
}
subscriptionId, err := getSubscriptionIdFromConfig(targetVM)
if err != nil {
response := fmt.Sprintf("Failed to start the VM: %v", err)
respond(s, m.ChannelID, response)
return
}
respond(s, m.ChannelID, "Starting VM "+targetVM+"...")
cred, err := azidentity.NewDefaultAzureCredential(nil)
if err != nil {
log.Fatalf("failed to obtain a credential: %v", err)
respondError(s, m.ChannelID)
return
}
client, err := armcompute.NewVirtualMachinesClient(subscriptionId, cred, nil)
if err != nil {
log.Errorf("failed to create VM client: %v", err)
respondError(s, m.ChannelID)
return
}
poller, err := client.BeginStart(
context.Background(),
resourceGroup,
vmName,
nil,
)
if err != nil {
log.Errorf("failed to obtain a response: %v", err)
respondError(s, m.ChannelID)
return
}
_, err = poller.PollUntilDone(context.Background(), nil)
if err != nil {
log.Errorf("failed to start the vm: %v", err)
response := fmt.Sprintf("Failed to start the VM: %v", err)
respond(s, m.ChannelID, response)
return
}
log.Infof("VM %v started.", targetVM)
respond(s, m.ChannelID, "VM "+targetVM+" started.")
}
func getTargetVMFromCommand(content string) (string, error) {
if len(strings.Split(content, " ")) < 2 {
return "", fmt.Errorf("no VM name provided")
}
if len(strings.Split(content, " ")) > 2 {
return "", fmt.Errorf("too many arguments")
}
targetVM := strings.Split(content, " ")[1]
return targetVM, nil
}
// stopCommand is the command handler to stop an azure VM.
func stopCommand(s *discordgo.Session, m *discordgo.MessageCreate) {
targetVM, err := getTargetVMFromCommand(m.Content)
if err != nil {
response := fmt.Sprintf("Failed to stop the VM: %v", err)
respond(s, m.ChannelID, response)
return
}
log.Infof("Received stop command for VM: %v", targetVM)
resourceGroup, err := getResourceGroupFromConfig(targetVM)
if err != nil {
response := fmt.Sprintf("Failed to stop the VM: %v", err)
respond(s, m.ChannelID, response)
return
}
vmName, err := getVMNameFromConfig(targetVM)
if err != nil {
response := fmt.Sprintf("Failed to stop the VM: %v", err)
respond(s, m.ChannelID, response)
return
}
subscriptionId, err := getSubscriptionIdFromConfig(targetVM)
if err != nil {
response := fmt.Sprintf("Failed to stop the VM: %v", err)
respond(s, m.ChannelID, response)
return
}
respond(s, m.ChannelID, "Stopping VM "+targetVM+"...")
cred, err := azidentity.NewDefaultAzureCredential(nil)
if err != nil {
log.Errorf("failed to obtain a credential: %v", err)
respondError(s, m.ChannelID)
return
}
client, err := armcompute.NewVirtualMachinesClient(subscriptionId, cred, nil)
if err != nil {
log.Errorf("failed to create VM client: %v", err)
respondError(s, m.ChannelID)
return
}
poller, err := client.BeginDeallocate(
context.Background(),
resourceGroup,
vmName,
nil,
)
if err != nil {
log.Errorf("failed to obtain a response: %v", err)
respondError(s, m.ChannelID)
return
}
_, err = poller.PollUntilDone(context.Background(), nil)
if err != nil {
log.Errorf("failed to stop the vm: %v", err)
response := fmt.Sprintf("Failed to stop the VM: %v", err)
respond(s, m.ChannelID, response)
return
}
log.Infof("VM %v stopped.", targetVM)
respond(s, m.ChannelID, "VM "+targetVM+" stopped.")
}
func stateCommand(s *discordgo.Session, m *discordgo.MessageCreate) {
targetVM, err := getTargetVMFromCommand(m.Content)
if err != nil {
response := fmt.Sprintf("Failed to stop the VM: %v", err)
respond(s, m.ChannelID, response)
return
}
log.Infof("Received state command for VM: %v", targetVM)
resourceGroup, err := getResourceGroupFromConfig(targetVM)
if err != nil {
response := fmt.Sprintf("Failed to get state for the VM: %v", err)
respond(s, m.ChannelID, response)
return
}
vmName, err := getVMNameFromConfig(targetVM)
if err != nil {
response := fmt.Sprintf("Failed to get state for the VM: %v", err)
respond(s, m.ChannelID, response)
return
}
subscriptionId, err := getSubscriptionIdFromConfig(targetVM)
if err != nil {
response := fmt.Sprintf("Failed to get state for the VM: %v", err)
respond(s, m.ChannelID, response)
return
}
respond(s, m.ChannelID, fmt.Sprintf("Getting State for VM %v...", targetVM))
cred, err := azidentity.NewDefaultAzureCredential(nil)
if err != nil {
log.Errorf("failed to obtain a credential: %v", err)
respondError(s, m.ChannelID)
return
}
client, err := armcompute.NewVirtualMachinesClient(subscriptionId, cred, nil)
if err != nil {
log.Errorf("failed to create VM client: %v", err)
respondError(s, m.ChannelID)
return
}
ctx := context.Background()
res, err := client.InstanceView(ctx,
resourceGroup,
vmName,
nil)
if err != nil {
log.Fatalf("failed to finish the request: %v", err)
}
log.Infof("Got info for VM %v.", targetVM)
status := ""
for k, r := range res.Statuses {
if k == 0 {
status = fmt.Sprintf(status+"%v | ", *r.DisplayStatus)
} else if k == 1 {
status = fmt.Sprintf(status+"%v", *r.DisplayStatus)
}
}
respond(s, m.ChannelID, fmt.Sprintf("VM Info: \n%v", status))
}
// pingCommand is the command handler to ping the bot.
func pingCommand(s *discordgo.Session, m *discordgo.MessageCreate) {
now := time.Now()
latency := ""
if timestampFieldExists(m) {
diff := m.Timestamp.Sub(now)
latency = "(" + strconv.Itoa(int(diff.Milliseconds())) + " ms)"
}
respond(s, m.ChannelID, "Pong! "+latency)
}
// helpCommand is the command handler to show the help message.
func helpCommand(s *discordgo.Session, m *discordgo.MessageCreate) {
messageContent := `azure-bot, an open source Discord Bot.
Available Commands:
$stopvm <vm_name> Stops a VM
$startvm <vm_name> Starts a VM
$vmstate <vm_name> Shows state of the VM.
$ping
Proudly maintained by Alex https://github.com/alex4108/azure-bot
`
respond(s, m.ChannelID, messageContent)
}