|
4 | 4 | package main |
5 | 5 |
|
6 | 6 | import ( |
7 | | - "context" |
8 | | - "fmt" |
9 | | - "net" |
10 | | - "os" |
11 | | - "runtime/debug" |
12 | | - |
13 | | - "github.com/jessevdk/go-flags" |
14 | | - "go.amzn.com/lambda/interop" |
15 | | - "go.amzn.com/lambda/rapidcore" |
16 | | - |
17 | | - log "github.com/sirupsen/logrus" |
| 7 | + "github.com/aws/aws-lambda-runtime-interface-emulator/internal/lambda/rie" |
18 | 8 | ) |
19 | 9 |
|
20 | | -const ( |
21 | | - optBootstrap = "/opt/bootstrap" |
22 | | - runtimeBootstrap = "/var/runtime/bootstrap" |
23 | | -) |
24 | | - |
25 | | -type options struct { |
26 | | - LogLevel string `long:"log-level" description:"The level of AWS Lambda Runtime Interface Emulator logs to display. Can also be set by the environment variable 'LOG_LEVEL'. Defaults to the value 'info'."` |
27 | | - InitCachingEnabled bool `long:"enable-init-caching" description:"Enable support for Init Caching"` |
28 | | - // Do not have a default value so we do not need to keep it in sync with the default value in lambda/rapidcore/sandbox_builder.go |
29 | | - RuntimeAPIAddress string `long:"runtime-api-address" description:"The address of the AWS Lambda Runtime API to communicate with the Lambda execution environment."` |
30 | | - RuntimeInterfaceEmulatorAddress string `long:"runtime-interface-emulator-address" default:"0.0.0.0:8080" description:"The address for the AWS Lambda Runtime Interface Emulator to accept HTTP request upon."` |
31 | | -} |
32 | | - |
33 | 10 | func main() { |
34 | | - // More frequent GC reduces the tail latencies, equivalent to export GOGC=33 |
35 | | - debug.SetGCPercent(33) |
36 | | - |
37 | | - opts, args := getCLIArgs() |
38 | | - |
39 | | - logLevel := "info" |
40 | | - |
41 | | - // If you specify an option by using a parameter on the CLI command line, it overrides any value from either the corresponding environment variable. |
42 | | - // |
43 | | - // https://docs.aws.amazon.com/cli/latest/userguide/cli-configure-envvars.html |
44 | | - if opts.LogLevel != "" { |
45 | | - logLevel = opts.LogLevel |
46 | | - } else if envLogLevel, envLogLevelSet := os.LookupEnv("LOG_LEVEL"); envLogLevelSet { |
47 | | - logLevel = envLogLevel |
48 | | - } |
49 | | - |
50 | | - rapidcore.SetLogLevel(logLevel) |
51 | | - |
52 | | - if opts.RuntimeAPIAddress != "" { |
53 | | - _, _, err := net.SplitHostPort(opts.RuntimeAPIAddress) |
54 | | - |
55 | | - if err != nil { |
56 | | - log.WithError(err).Fatalf("The command line value for \"--runtime-api-address\" is not a valid network address %q.", opts.RuntimeAPIAddress) |
57 | | - } |
58 | | - } |
59 | | - |
60 | | - _, _, err := net.SplitHostPort(opts.RuntimeInterfaceEmulatorAddress) |
61 | | - |
62 | | - if err != nil { |
63 | | - log.WithError(err).Fatalf("The command line value for \"--runtime-interface-emulator-address\" is not a valid network address %q.", opts.RuntimeInterfaceEmulatorAddress) |
64 | | - } |
65 | | - |
66 | | - bootstrap, handler := getBootstrap(args, opts) |
67 | | - sandbox := rapidcore. |
68 | | - NewSandboxBuilder(). |
69 | | - AddShutdownFunc(context.CancelFunc(func() { os.Exit(0) })). |
70 | | - SetExtensionsFlag(true). |
71 | | - SetInitCachingFlag(opts.InitCachingEnabled) |
72 | | - |
73 | | - if len(handler) > 0 { |
74 | | - sandbox.SetHandler(handler) |
75 | | - } |
76 | | - |
77 | | - if opts.RuntimeAPIAddress != "" { |
78 | | - sandbox.SetRuntimeAPIAddress(opts.RuntimeAPIAddress) |
79 | | - } |
80 | | - |
81 | | - sandboxContext, internalStateFn := sandbox.Create() |
82 | | - // Since we have not specified a custom interop server for standalone, we can |
83 | | - // directly reference the default interop server, which is a concrete type |
84 | | - sandbox.DefaultInteropServer().SetSandboxContext(sandboxContext) |
85 | | - sandbox.DefaultInteropServer().SetInternalStateGetter(internalStateFn) |
86 | | - |
87 | | - startHTTPServer(opts.RuntimeInterfaceEmulatorAddress, sandbox, bootstrap) |
88 | | -} |
89 | | - |
90 | | -func getCLIArgs() (options, []string) { |
91 | | - var opts options |
92 | | - parser := flags.NewParser(&opts, flags.IgnoreUnknown) |
93 | | - args, err := parser.ParseArgs(os.Args) |
94 | | - |
95 | | - if err != nil { |
96 | | - log.WithError(err).Fatal("Failed to parse command line arguments:", os.Args) |
97 | | - } |
98 | | - |
99 | | - return opts, args |
100 | | -} |
101 | | - |
102 | | -func isBootstrapFileExist(filePath string) bool { |
103 | | - file, err := os.Stat(filePath) |
104 | | - return !os.IsNotExist(err) && !file.IsDir() |
105 | | -} |
106 | | - |
107 | | -func getBootstrap(args []string, opts options) (interop.Bootstrap, string) { |
108 | | - var bootstrapLookupCmd []string |
109 | | - var handler string |
110 | | - currentWorkingDir := "/var/task" // default value |
111 | | - |
112 | | - if len(args) <= 1 { |
113 | | - // set default value to /var/task/bootstrap, but switch to the other options if it doesn't exist |
114 | | - bootstrapLookupCmd = []string{ |
115 | | - fmt.Sprintf("%s/bootstrap", currentWorkingDir), |
116 | | - } |
117 | | - |
118 | | - if !isBootstrapFileExist(bootstrapLookupCmd[0]) { |
119 | | - var bootstrapCmdCandidates = []string{ |
120 | | - optBootstrap, |
121 | | - runtimeBootstrap, |
122 | | - } |
123 | | - |
124 | | - for i, bootstrapCandidate := range bootstrapCmdCandidates { |
125 | | - if isBootstrapFileExist(bootstrapCandidate) { |
126 | | - bootstrapLookupCmd = []string{bootstrapCmdCandidates[i]} |
127 | | - break |
128 | | - } |
129 | | - } |
130 | | - } |
131 | | - |
132 | | - // handler is used later to set an env var for Lambda Image support |
133 | | - handler = "" |
134 | | - } else if len(args) > 1 { |
135 | | - |
136 | | - bootstrapLookupCmd = args[1:] |
137 | | - |
138 | | - if cwd, err := os.Getwd(); err == nil { |
139 | | - currentWorkingDir = cwd |
140 | | - } |
141 | | - |
142 | | - if len(args) > 2 { |
143 | | - // Assume last arg is the handler |
144 | | - handler = args[len(args)-1] |
145 | | - } |
146 | | - |
147 | | - log.Infof("exec '%s' (cwd=%s, handler=%s)", args[1], currentWorkingDir, handler) |
148 | | - |
149 | | - } else { |
150 | | - log.Panic("insufficient arguments: bootstrap not provided") |
151 | | - } |
152 | | - |
153 | | - return NewSimpleBootstrap(bootstrapLookupCmd, currentWorkingDir), handler |
| 11 | + rie.Run() |
154 | 12 | } |
0 commit comments