-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathmain.go
More file actions
355 lines (304 loc) · 9.29 KB
/
Copy pathmain.go
File metadata and controls
355 lines (304 loc) · 9.29 KB
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
// Copyright 2025 LiveKit, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package main
import (
"context"
"fmt"
"os"
"strings"
"time"
"github.com/livekit/protocol/livekit"
"github.com/livekit/protocol/logger"
"github.com/livekit/server-sdk-go/v2/pkg/cloudagents"
"github.com/slack-go/slack"
)
var (
log logger.Logger
lkUrl, lkApiKey, lkApiSecret string
)
func main() {
zl, _ := logger.NewZapLogger(&logger.Config{
JSON: true,
Level: "debug",
})
log = zl.WithValues()
logger.SetLogger(log, "cloud-agents-github-plugin")
operation := os.Getenv("INPUT_OPERATION")
if operation == "" {
log.Errorw("OPERATION is not set", nil)
os.Exit(1)
}
region := os.Getenv("INPUT_REGION")
if region == "" {
log.Infow("REGION is not set, defaulting to nearest region.")
}
workingDir := os.Getenv("INPUT_WORKING_DIRECTORY")
if workingDir == "" {
workingDir = "."
}
log.Infow("Running in", "path", workingDir)
agentIds := strings.Split(os.Getenv("INPUT_AGENT_IDS"), ",")
if len(agentIds) > 0 {
log.Infow("Using agent IDs from INPUT_AGENT_IDS", "agentIds", agentIds)
}
timeout := os.Getenv("INPUT_TIMEOUT")
if timeout == "" {
timeout = "5m"
}
timeoutDuration, err := time.ParseDuration(timeout)
if err != nil {
log.Errorw("Invalid timeout", err)
os.Exit(1)
}
// get all the env vars that are prefixed with SECRET_
secrets := make([]*livekit.AgentSecret, 0)
for _, env := range os.Environ() {
if strings.HasPrefix(env, "SECRET_") {
// ignore the SECRET_LIST env var
if strings.HasPrefix(env, "SECRET_LIST=") {
continue
}
secretParts := strings.SplitN(strings.TrimPrefix(env, "SECRET_"), "=", 2)
secretName := secretParts[0]
secretValue := strings.TrimSpace(secretParts[1])
log.Infow("Loading secret", "secret", secretName)
if secretName == "LIVEKIT_URL" || secretName == "LIVEKIT_API_KEY" || secretName == "LIVEKIT_API_SECRET" {
switch secretName {
case "LIVEKIT_URL":
lkUrl = secretValue
case "LIVEKIT_API_KEY":
lkApiKey = secretValue
case "LIVEKIT_API_SECRET":
lkApiSecret = secretValue
}
}
secrets = append(secrets, &livekit.AgentSecret{
Name: secretName,
Value: []byte(secretValue),
})
}
}
if lkUrl == "" || lkApiKey == "" || lkApiSecret == "" {
// try to load directly from the env first instead of the SECRET_ prefix
lkUrl = strings.TrimSpace(os.Getenv("LIVEKIT_URL"))
lkApiKey = strings.TrimSpace(os.Getenv("LIVEKIT_API_KEY"))
lkApiSecret = strings.TrimSpace(os.Getenv("LIVEKIT_API_SECRET"))
if lkUrl == "" || lkApiKey == "" || lkApiSecret == "" {
log.Errorw("LIVEKIT_URL, LIVEKIT_API_KEY, and LIVEKIT_API_SECRET must be set", nil)
os.Exit(1)
}
}
// some use cases require a list of secrets to be passed in as a comma separated list of SECRET_NAME=SECRET_VALUE
if os.Getenv("SECRET_LIST") != "" {
secretList := strings.Split(os.Getenv("SECRET_LIST"), ",")
for _, secret := range secretList {
secretParts := strings.SplitN(secret, "=", 2)
if len(secretParts) != 2 {
log.Errorw("Invalid secret format", nil, "secret", secret)
os.Exit(1)
}
secretName := strings.TrimSpace(secretParts[0])
secretValue := strings.TrimSpace(secretParts[1])
log.Infow("Loading secret from SECRET_LIST", "secret", secretName)
secrets = append(secrets, &livekit.AgentSecret{
Name: secretName,
Value: []byte(secretValue),
})
}
}
client, err := cloudagents.New(
cloudagents.WithProject(lkUrl, lkApiKey, lkApiSecret),
cloudagents.WithLogger(log),
)
if err != nil {
log.Errorw("Failed to create agent client", err)
os.Exit(1)
}
// get the subdomain from the lkUrl
subdomain := strings.Split(lkUrl, ".")[0]
if len(secrets) == 0 {
log.Infow("No secrets loaded")
}
switch operation {
case "create":
createAgent(client, subdomain, secrets, workingDir, region)
case "deploy":
deployAgent(client, secrets, workingDir)
case "status":
err := agentStatus(client, workingDir)
if err != nil {
log.Errorw("Failed to get agent status", err)
os.Exit(1)
}
case "status-retry":
log.Debugw("Starting agent status retry", "timeout", timeoutDuration)
err := agentStatusRetry(client, workingDir, timeoutDuration)
if err != nil {
log.Errorw("Failed to get agent status", err)
os.Exit(1)
}
log.Infow("Agent status check completed", "status", "running")
case "delete":
deleteAgent(client, workingDir)
case "delete-multi":
deleteAgentMulti(client, agentIds)
default:
log.Errorw("Invalid operation", nil, "operation", operation)
os.Exit(1)
}
}
func sendSlackNotification(message string) {
slackToken := os.Getenv("SLACK_TOKEN")
slackChannel := os.Getenv("SLACK_CHANNEL")
if slackToken == "" || slackChannel == "" {
log.Infow("Slack notification skipped - token or channel not configured")
return
}
api := slack.New(slackToken)
_, _, err := api.PostMessage(
slackChannel,
slack.MsgOptionText(message, false),
)
if err != nil {
log.Errorw("Failed to send Slack notification", err)
} else {
log.Infow("Slack notification sent", "channel", slackChannel)
}
}
func agentStatusRetry(client *cloudagents.Client, workingDir string, timeoutDuration time.Duration) error {
startTime := time.Now()
for {
err := agentStatus(client, workingDir)
if err == nil {
return nil
}
if time.Since(startTime) >= timeoutDuration {
return fmt.Errorf("timeout reached after %v", timeoutDuration)
}
log.Infow("Failed to get running agent", "error", err)
time.Sleep(5 * time.Second)
}
}
func agentStatus(client *cloudagents.Client, workingDir string) error {
lkConfig, exists, err := LoadTOMLFile(workingDir, LiveKitTOMLFile)
if err != nil {
return err
}
if !exists {
return fmt.Errorf("livekit.toml not found")
}
log.Infow("Getting agent status", "agent", lkConfig.Agent.ID)
res, err := client.ListAgents(context.Background(), &livekit.ListAgentsRequest{
AgentId: lkConfig.Agent.ID,
})
if err != nil {
return fmt.Errorf("failed to get agent")
}
if len(res.Agents) == 0 {
return fmt.Errorf("agent not found")
}
for _, agent := range res.Agents {
for _, regionalAgent := range agent.AgentDeployments {
if regionalAgent.Status != "Running" {
sendSlackNotification(fmt.Sprintf("Agent %s is not running", lkConfig.Agent.ID))
return fmt.Errorf("agent id %s is not running %s", lkConfig.Agent.ID, regionalAgent.Status)
}
}
}
log.Infow("Agent status", "agent", lkConfig.Agent.ID, "status", res.Agents[0].AgentDeployments[0].Status)
return nil
}
func deployAgent(client *cloudagents.Client, secrets []*livekit.AgentSecret, workingDir string) {
lkConfig, exists, err := LoadTOMLFile(workingDir, LiveKitTOMLFile)
if err != nil {
log.Errorw("Failed to load livekit.toml", err)
os.Exit(1)
}
if !exists {
log.Errorw("livekit.toml not found", nil)
os.Exit(1)
}
if err := client.DeployAgent(
context.Background(),
lkConfig.Agent.ID,
os.DirFS(workingDir),
secrets,
[]string{LiveKitTOMLFile},
); err != nil {
log.Errorw("Failed to deploy agent", err)
os.Exit(1)
}
log.Infow("Agent deployed", "agent", lkConfig.Agent.ID)
}
func createAgent(client *cloudagents.Client, subdomain string, secrets []*livekit.AgentSecret, workingDir string, region string) {
if _, err := os.Stat(fmt.Sprintf("%s/%s", workingDir, LiveKitTOMLFile)); err == nil {
log.Infow("livekit.toml already exists", "path", fmt.Sprintf("%s/%s", workingDir, LiveKitTOMLFile))
os.Exit(0)
}
lkConfig := NewLiveKitTOML(subdomain).WithDefaultAgent()
var regions []string
if region != "" {
regions = []string{region}
}
resp, err := client.CreateAgent(
context.Background(),
os.DirFS(workingDir),
secrets,
regions,
[]string{LiveKitTOMLFile},
)
if err != nil {
log.Errorw("Failed to create agent", err)
os.Exit(1)
}
lkConfig.Agent.ID = resp.AgentId
if err := lkConfig.SaveTOMLFile(workingDir, LiveKitTOMLFile); err != nil {
log.Errorw("Failed to save livekit.toml", err)
os.Exit(1)
}
log.Infow("Agent created", "agent", resp.AgentId)
}
func deleteAgent(client *cloudagents.Client, workingDir string) {
lkConfig, exists, err := LoadTOMLFile(workingDir, LiveKitTOMLFile)
if err != nil {
log.Errorw("Failed to load livekit.toml", err)
os.Exit(1)
}
if !exists {
log.Errorw("livekit.toml not found", nil)
os.Exit(1)
}
req := &livekit.DeleteAgentRequest{
AgentId: lkConfig.Agent.ID,
}
_, err = client.DeleteAgent(context.Background(), req)
if err != nil {
log.Errorw("Failed to delete agent", err)
os.Exit(1)
}
log.Infow("Agent deleted", "agent", lkConfig.Agent.ID)
}
func deleteAgentMulti(client *cloudagents.Client, agentIds []string) {
for _, agentId := range agentIds {
req := &livekit.DeleteAgentRequest{
AgentId: agentId,
}
_, err := client.DeleteAgent(context.Background(), req)
if err != nil {
log.Errorw("Failed to delete agent", err)
os.Exit(1)
}
log.Infow("Agent deleted", "agent", agentId)
}
}