-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathroot.go
More file actions
365 lines (334 loc) · 10.5 KB
/
Copy pathroot.go
File metadata and controls
365 lines (334 loc) · 10.5 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
356
357
358
359
360
361
362
363
364
365
package cmd
import (
"context"
"fmt"
"os"
"time"
"github.com/urfave/cli/v2"
"go.uber.org/zap"
"github.com/lambda-feedback/shimmy/config"
"github.com/lambda-feedback/shimmy/util/conf"
"github.com/lambda-feedback/shimmy/util/logging"
)
var (
appName = "shimmy"
appUsage = `A shim for running arbitrary, language-agnostic evaluation
functions on arbitrary, serverless platforms.`
rootApp = &cli.App{
Name: appName,
Usage: appUsage,
HideHelpCommand: true,
Args: true,
DefaultCommand: "run",
Flags: []cli.Flag{
// general flags
&cli.StringFlag{
Name: "log-level",
Usage: "set the log level. Options: debug, info, warn, error, panic, fatal.",
EnvVars: []string{"LOG_LEVEL"},
},
&cli.StringFlag{
Name: "log-format",
Usage: "set the log format. Options: production, development.",
EnvVars: []string{"LOG_FORMAT"},
},
// auth flags
&cli.StringFlag{
Name: "auth-key",
Usage: "the secret key for the application.",
Category: "auth",
EnvVars: []string{"AUTH_KEY"},
},
// shim flags
&cli.StringFlag{
Name: "interface",
Aliases: []string{"i"},
Usage: "the interface to use for worker process communication. Options: rpc, file.",
Value: "rpc",
Category: "function",
EnvVars: []string{"FUNCTION_INTERFACE"},
},
&cli.StringFlag{
Name: "command",
Aliases: []string{"c"},
Usage: "the command to invoke to start the worker process.",
Category: "function",
EnvVars: []string{"FUNCTION_COMMAND"},
Required: true,
},
&cli.StringFlag{
Name: "cwd",
Aliases: []string{"d"},
Usage: "the working directory for the worker process.",
Category: "function",
EnvVars: []string{"FUNCTION_WORKING_DIR"},
},
&cli.StringSliceFlag{
Name: "arg",
Aliases: []string{"a"},
Usage: "additional arguments for to the worker process.",
Category: "function",
EnvVars: []string{"FUNCTION_ARGS"},
},
&cli.StringSliceFlag{
Name: "env",
Aliases: []string{"e"},
Usage: "additional environment variables for the worker process.",
Category: "function",
EnvVars: []string{"FUNCTION_ENV"},
},
&cli.IntFlag{
Name: "max-workers",
Aliases: []string{"n"},
Usage: "the maximum number of worker processes to run concurrently.",
DefaultText: "number of CPU cores",
Value: 0,
Category: "function",
EnvVars: []string{"FUNCTION_MAX_PROCS"},
},
&cli.DurationFlag{
Name: "worker-stop-timeout",
Usage: "the duration to wait for a worker process to stop.",
Value: 5 * time.Second,
Category: "worker",
EnvVars: []string{"FUNCTION_WORKER_STOP_TIMEOUT"},
},
&cli.DurationFlag{
Name: "worker-send-timeout",
Usage: "the timeout for a single message send operation.",
Value: 30 * time.Second,
Category: "worker",
EnvVars: []string{"FUNCTION_WORKER_SEND_TIMEOUT"},
},
&cli.DurationFlag{
Name: "worker-start-timeout",
Usage: "the duration to wait for the application to start (worker process boot + first successful RPC dial).",
Value: 15 * time.Second,
Category: "worker",
EnvVars: []string{"FUNCTION_WORKER_START_TIMEOUT"},
},
&cli.StringFlag{
Name: "rpc-transport",
Aliases: []string{"t"},
Usage: "the transport to use for the RPC interface. Options: stdio, ipc, http, tcp, ws.",
Value: "stdio",
EnvVars: []string{"FUNCTION_RPC_TRANSPORT"},
Category: "rpc",
},
&cli.StringFlag{
Name: "rpc-transport-ipc-endpoint",
Usage: "the IPC endpoint to use for the IPC transport. Default: /tmp/eval.sock",
EnvVars: []string{"FUNCTION_RPC_TRANSPORT_IPC_ENDPOINT"},
Category: "rpc",
},
&cli.StringFlag{
Name: "rpc-transport-http-url",
Usage: "the url to use for the HTTP transport. Default: http://127.0.0.1:7321",
EnvVars: []string{"FUNCTION_RPC_TRANSPORT_HTTP_URL"},
Value: "http://127.0.0.1:7321",
Category: "rpc",
},
&cli.StringFlag{
Name: "rpc-transport-ws-url",
Usage: "the url to use for the WebSocket transport. Default: ws://127.0.0.1:7321",
EnvVars: []string{"FUNCTION_RPC_TRANSPORT_WS_URL"},
Value: "ws://127.0.0.1:7321",
Category: "rpc",
},
&cli.StringFlag{
Name: "rpc-transport-tcp-address",
Usage: "the address to use for the TCP transport. Default: 127.0.0.1:7321",
EnvVars: []string{"FUNCTION_RPC_TRANSPORT_TCP_ADDRESS"},
Value: "127.0.0.1:7321",
Category: "rpc",
},
// sandbox flags
&cli.BoolFlag{
Name: "sandbox",
Usage: "enable nsjail sandboxing for worker processes (Linux only).",
Value: false,
Category: "sandbox",
EnvVars: []string{"SANDBOX_ENABLED"},
},
&cli.StringFlag{
Name: "sandbox-nsjail-path",
Usage: "path to the nsjail binary.",
Value: "/usr/sbin/nsjail",
Category: "sandbox",
EnvVars: []string{"SANDBOX_NSJAIL_PATH"},
},
&cli.StringSliceFlag{
Name: "sandbox-ro-bind",
Usage: "host path to bind-mount read-only inside the sandbox (repeatable).",
Category: "sandbox",
EnvVars: []string{"SANDBOX_RO_BINDS"},
},
&cli.StringSliceFlag{
Name: "sandbox-rw-bind",
Usage: "host path to bind-mount read-write inside the sandbox (repeatable).",
Category: "sandbox",
EnvVars: []string{"SANDBOX_RW_BINDS"},
},
&cli.StringSliceFlag{
Name: "sandbox-tmpfs",
Usage: "path inside the sandbox to mount as tmpfs (repeatable).",
Category: "sandbox",
EnvVars: []string{"SANDBOX_TMPFS"},
},
&cli.IntFlag{
Name: "sandbox-cpu-time",
Usage: "CPU time limit in seconds for worker processes (0 = unlimited).",
Value: 0,
Category: "sandbox",
EnvVars: []string{"SANDBOX_CPU_TIME_LIMIT"},
},
&cli.IntFlag{
Name: "sandbox-memory-mb",
Usage: "memory (address space) limit in megabytes for worker processes (0 = unlimited).",
Value: 0,
Category: "sandbox",
EnvVars: []string{"SANDBOX_MEMORY_LIMIT"},
},
&cli.IntFlag{
Name: "sandbox-max-fds",
Usage: "maximum open file descriptors for worker processes (0 = nsjail default).",
Value: 0,
Category: "sandbox",
EnvVars: []string{"SANDBOX_MAX_FDS"},
},
&cli.BoolFlag{
Name: "sandbox-disable-network",
Usage: "disable network access inside the sandbox.",
Value: false,
Category: "sandbox",
EnvVars: []string{"SANDBOX_DISABLE_NETWORK"},
},
&cli.BoolFlag{
Name: "sandbox-seccomp",
Usage: "enable seccomp syscall filtering inside the sandbox.",
Value: false,
Category: "sandbox",
EnvVars: []string{"SANDBOX_SECCOMP"},
},
},
Before: func(ctx *cli.Context) error {
// create the logger
log, err := createLogger(ctx)
if err != nil {
return err
}
// inject logger into cli context
ctx.Context = logging.ContextWithLogger(ctx.Context, log)
// create the config
cfg, err := parseRootConfig(ctx)
if err != nil {
return err
}
// inject the config into the cli context
ctx.Context = conf.ContextWithConfig(ctx.Context, cfg)
return nil
},
After: func(ctx *cli.Context) error {
// flush the logger
if log, err := logging.LoggerFromContext(ctx.Context); err == nil {
log.Sync()
}
return nil
},
}
)
func init() {
cli.VersionFlag = &cli.BoolFlag{
Name: "version",
Usage: "print the version",
DisableDefaultText: true,
}
}
type ExecuteParams struct {
Version string
Compiled time.Time
}
func Execute(params ExecuteParams) {
rootApp.Version = params.Version
rootApp.Compiled = params.Compiled
run(context.Background(), os.Args)
}
func run(ctx context.Context, args []string) {
err := rootApp.RunContext(ctx, args)
// if app exited without error, return
if err == nil {
return
}
fmt.Printf("exit error: %s\n", err.Error())
// if app exited with ExitError, exit with given exit code
// otherwise, exit with exit code 1
os.Exit(1)
}
func createLogger(ctx *cli.Context) (*zap.Logger, error) {
level := getLogLevelFromCLI(ctx)
format := getLogFormatFromCLI(ctx)
var config zap.Config
switch format {
case "production":
config = zap.NewProductionConfig()
case "development":
config = zap.NewDevelopmentConfig()
default:
return nil, fmt.Errorf("invalid log format: %s", format)
}
config.InitialFields = map[string]any{}
config.Level = level
return config.Build()
}
func getLogFormatFromCLI(ctx *cli.Context) string {
if format := ctx.String("log-format"); format != "" {
return format
}
return "development"
}
func getLogLevelFromCLI(ctx *cli.Context) zap.AtomicLevel {
if atom, err := zap.ParseAtomicLevel(ctx.String("log-level")); err == nil {
return atom
}
return zap.NewAtomicLevelAt(zap.InfoLevel)
}
func parseRootConfig(ctx *cli.Context) (config.Config, error) {
// map cli flags to config fields
cliMap := map[string]string{
"auth-key": "auth.key",
"max-workers": "runtime.max_workers",
"command": "runtime.cmd",
"cwd": "runtime.cwd",
"arg": "runtime.arg",
"env": "runtime.env",
"interface": "runtime.io.interface",
"rpc-transport": "runtime.io.rpc.transport",
"rpc-transport-ipc-endpoint": "runtime.io.rpc.ipc.endpoint",
"rpc-transport-http-url": "runtime.io.rpc.http.url",
"rpc-transport-ws-url": "runtime.io.rpc.ws.url",
"rpc-transport-tcp-address": "runtime.io.rpc.tcp.address",
"worker-send-timeout": "runtime.send.timeout",
"worker-stop-timeout": "runtime.stop.timeout",
"worker-start-timeout": "start_timeout",
// sandbox
"sandbox": "runtime.sandbox.enabled",
"sandbox-nsjail-path": "runtime.sandbox.nsjail_path",
"sandbox-ro-bind": "runtime.sandbox.ro_binds",
"sandbox-rw-bind": "runtime.sandbox.rw_binds",
"sandbox-tmpfs": "runtime.sandbox.tmpfs",
"sandbox-cpu-time": "runtime.sandbox.cpu_time_limit",
"sandbox-memory-mb": "runtime.sandbox.memory_limit",
"sandbox-max-fds": "runtime.sandbox.max_fds",
"sandbox-disable-network": "runtime.sandbox.disable_network",
"sandbox-seccomp": "runtime.sandbox.seccomp",
}
// parse config using env
cfg, err := conf.Parse[config.Config](conf.ParseOptions{
Cli: ctx,
CliMap: cliMap,
})
if err != nil {
return config.Config{}, err
}
return cfg, err
}