This repository has been archived by the owner on Dec 3, 2021. It is now read-only.
/
main.go
313 lines (269 loc) 路 7.43 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
package main
import (
"context"
"fmt"
"os"
"github.com/fatih/color"
"github.com/golang/protobuf/jsonpb"
"github.com/golang/protobuf/ptypes/empty"
"github.com/nre-learning/syringe/config"
cli "github.com/urfave/cli"
grpc "google.golang.org/grpc"
api "github.com/nre-learning/syringe/api/exp"
pb "github.com/nre-learning/syringe/api/exp/generated"
)
func main() {
app := cli.NewApp()
app.Name = "syrctl"
app.Version = buildInfo["buildVersion"]
app.Usage = "Command-line tool to interact with Syringe, the scheduler for Antidote"
var host, port string
// global level flags
app.Flags = []cli.Flag{
cli.StringFlag{
Name: "H, host",
Usage: "syringed hostname",
Value: "127.0.0.1",
Destination: &host,
},
cli.StringFlag{
Name: "P, port",
Usage: "syringed port",
Value: "50099",
Destination: &port,
},
}
app.Commands = []cli.Command{
{
Name: "validate",
Aliases: []string{"validate"},
Usage: "syrctl validate <CURRICULUM DIRECTORY>",
Action: func(c *cli.Context) {
_, err := api.ImportCurriculum(&config.SyringeConfig{
Tier: "local",
CurriculumDir: c.Args().First(),
})
if err != nil {
color.Red("Some curriculum resources failed to validate.")
os.Exit(1)
} else {
color.Green("All detected curriculum resources imported successfully.")
os.Exit(0)
}
},
},
{
Name: "whitelist",
Aliases: []string{"wl"},
Usage: "syrctl whitelist <subcommand>",
Subcommands: []cli.Command{
{
Name: "show",
Usage: "Show whitelisted sessions",
Action: func(c *cli.Context) {
// TODO(mierdin): Add security options
conn, err := grpc.Dial(fmt.Sprintf("%s:%s", host, port), grpc.WithInsecure())
if err != nil {
fmt.Println(err)
}
defer conn.Close()
client := pb.NewLiveLessonsServiceClient(conn)
whitelist, err := client.GetGCWhitelist(context.Background(), &empty.Empty{})
if err != nil {
fmt.Println(err)
os.Exit(1)
}
sessions := whitelist.Sessions
if len(sessions) == 0 {
fmt.Println("No exempt sessions found.")
os.Exit(0)
}
fmt.Println("EXEMPT SESSIONS")
for i := range sessions {
fmt.Println(sessions[i].Id)
}
},
},
{
Name: "add",
Usage: "Add session to GC whitelist",
Action: func(c *cli.Context) {
sid := c.Args().First()
if sid == "" {
fmt.Println("Please provide session ID to add to whitelist")
os.Exit(1)
}
// TODO(mierdin): Add security options
conn, err := grpc.Dial(fmt.Sprintf("%s:%s", host, port), grpc.WithInsecure())
if err != nil {
fmt.Println(err)
}
defer conn.Close()
client := pb.NewLiveLessonsServiceClient(conn)
client.AddSessiontoGCWhitelist(context.Background(), &pb.Session{Id: sid})
if err != nil {
fmt.Println(err)
os.Exit(1)
}
fmt.Printf("%s added to whitelist\n", sid)
},
},
{
Name: "remove",
Usage: "Remove session from GC whitelist",
Action: func(c *cli.Context) {
sid := c.Args().First()
if sid == "" {
fmt.Println("Please provide session ID to remove from whitelist")
os.Exit(1)
}
// TODO(mierdin): Add security options
conn, err := grpc.Dial(fmt.Sprintf("%s:%s", host, port), grpc.WithInsecure())
if err != nil {
fmt.Println(err)
}
defer conn.Close()
client := pb.NewLiveLessonsServiceClient(conn)
_, err = client.RemoveSessionFromGCWhitelist(context.Background(), &pb.Session{Id: sid})
if err != nil {
fmt.Println(err)
os.Exit(1)
}
fmt.Printf("%s removed from whitelist\n", sid)
},
},
},
},
{
Name: "livelesson",
Aliases: []string{"ll"},
Usage: "syrctl livelesson <subcommand>",
Subcommands: []cli.Command{
{
Name: "list",
Usage: "List livelessons",
Action: func(c *cli.Context) {
// TODO(mierdin): Add security options
conn, err := grpc.Dial(fmt.Sprintf("%s:%s", host, port), grpc.WithInsecure())
if err != nil {
fmt.Println(err)
}
defer conn.Close()
client := pb.NewLiveLessonsServiceClient(conn)
liveLessons, err := client.ListLiveLessons(context.Background(), &empty.Empty{})
if err != nil {
fmt.Println(err)
os.Exit(1)
}
for i := range liveLessons.Items {
fmt.Println(liveLessons.Items[i].LessonUUID)
}
},
},
{
Name: "get",
Usage: "get a Livelesson",
Action: func(c *cli.Context) {
uuid := c.Args().First()
if uuid == "" {
fmt.Println("Please provide livelesson ID to get")
os.Exit(1)
}
// TODO(mierdin): Add security options
conn, err := grpc.Dial(fmt.Sprintf("%s:%s", host, port), grpc.WithInsecure())
if err != nil {
fmt.Println(err)
}
defer conn.Close()
client := pb.NewLiveLessonsServiceClient(conn)
ll, err := client.GetLiveLesson(context.Background(), &pb.LessonUUID{Id: uuid})
if err != nil {
fmt.Println(err)
os.Exit(1)
}
jpbm := jsonpb.Marshaler{}
fmt.Println(jpbm.MarshalToString(ll))
},
},
{
Name: "kill",
Usage: "Kill a livelesson",
Action: func(c *cli.Context) {
uuid := c.Args().First()
if uuid == "" {
fmt.Println("Please provide livelesson ID to kill")
os.Exit(1)
}
// TODO(mierdin): Add security options
conn, err := grpc.Dial(fmt.Sprintf("%s:%s", host, port), grpc.WithInsecure())
if err != nil {
fmt.Println(err)
}
defer conn.Close()
client := pb.NewLiveLessonsServiceClient(conn)
_, err = client.KillLiveLesson(context.Background(), &pb.LessonUUID{Id: uuid})
if err != nil {
fmt.Println(err)
os.Exit(1)
}
fmt.Printf("Livelesson %s killed.\n", uuid)
},
},
},
},
{
Name: "kubelab",
Aliases: []string{"kl"},
Usage: "syrctl kubelab <subcommand>",
Subcommands: []cli.Command{
{
Name: "list",
Usage: "List kubelabs",
Action: func(c *cli.Context) {
// TODO(mierdin): Add security options
conn, err := grpc.Dial(fmt.Sprintf("%s:%s", host, port), grpc.WithInsecure())
if err != nil {
fmt.Println(err)
}
defer conn.Close()
client := pb.NewKubeLabServiceClient(conn)
kubeLabs, err := client.ListKubeLabs(context.Background(), &empty.Empty{})
if err != nil {
fmt.Println(err)
os.Exit(1)
}
for uuid := range kubeLabs.Items {
fmt.Println(uuid)
}
},
},
{
Name: "get",
Usage: "get a Kubelab",
Action: func(c *cli.Context) {
uuid := c.Args().First()
if uuid == "" {
fmt.Println("Please provide kubelab ID to get")
os.Exit(1)
}
// TODO(mierdin): Add security options
conn, err := grpc.Dial(fmt.Sprintf("%s:%s", host, port), grpc.WithInsecure())
if err != nil {
fmt.Println(err)
}
defer conn.Close()
client := pb.NewKubeLabServiceClient(conn)
kubeLab, err := client.GetKubeLab(context.Background(), &pb.KubeLabUuid{Id: uuid})
if err != nil {
fmt.Println(err)
os.Exit(1)
}
jpbm := jsonpb.Marshaler{}
fmt.Println(jpbm.MarshalToString(kubeLab))
},
},
},
},
}
app.Run(os.Args)
}