forked from aws/aws-lambda-runtime-interface-emulator
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmain.go
248 lines (223 loc) · 8 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
// main entrypoint of init
// initial structure based upon /cmd/aws-lambda-rie/main.go
package main
import (
"context"
log "github.com/sirupsen/logrus"
"go.amzn.com/lambda/interop"
"go.amzn.com/lambda/rapidcore"
"os"
"runtime/debug"
"strconv"
"strings"
)
type LsOpts struct {
InteropPort string
RuntimeEndpoint string
RuntimeId string
AccountId string
InitTracingPort string
User string
CodeArchives string
HotReloadingPaths []string
FileWatcherStrategy string
ChmodPaths string
LocalstackIP string
InitLogLevel string
EdgePort string
EnableXRayTelemetry string
PostInvokeWaitMS string
MaxPayloadSize string
}
func GetEnvOrDie(env string) string {
result, found := os.LookupEnv(env)
if !found {
panic("Could not find environment variable for: " + env)
}
return result
}
func InitLsOpts() *LsOpts {
return &LsOpts{
// required
RuntimeEndpoint: GetEnvOrDie("LOCALSTACK_RUNTIME_ENDPOINT"),
RuntimeId: GetEnvOrDie("LOCALSTACK_RUNTIME_ID"),
AccountId: GetenvWithDefault("LOCALSTACK_FUNCTION_ACCOUNT_ID", "000000000000"),
// optional with default
InteropPort: GetenvWithDefault("LOCALSTACK_INTEROP_PORT", "9563"),
InitTracingPort: GetenvWithDefault("LOCALSTACK_RUNTIME_TRACING_PORT", "9564"),
User: GetenvWithDefault("LOCALSTACK_USER", "sbx_user1051"),
InitLogLevel: GetenvWithDefault("LOCALSTACK_INIT_LOG_LEVEL", "warn"),
EdgePort: GetenvWithDefault("EDGE_PORT", "4566"),
MaxPayloadSize: GetenvWithDefault("LOCALSTACK_MAX_PAYLOAD_SIZE", "6291556"),
// optional or empty
CodeArchives: os.Getenv("LOCALSTACK_CODE_ARCHIVES"),
HotReloadingPaths: strings.Split(GetenvWithDefault("LOCALSTACK_HOT_RELOADING_PATHS", ""), ","),
FileWatcherStrategy: os.Getenv("LOCALSTACK_FILE_WATCHER_STRATEGY"),
EnableXRayTelemetry: os.Getenv("LOCALSTACK_ENABLE_XRAY_TELEMETRY"),
LocalstackIP: os.Getenv("LOCALSTACK_HOSTNAME"),
PostInvokeWaitMS: os.Getenv("LOCALSTACK_POST_INVOKE_WAIT_MS"),
ChmodPaths: GetenvWithDefault("LOCALSTACK_CHMOD_PATHS", "[]"),
}
}
// UnsetLsEnvs unsets environment variables specific to LocalStack to achieve better runtime parity with AWS
func UnsetLsEnvs() {
unsetList := [...]string{
// LocalStack internal
"LOCALSTACK_RUNTIME_ENDPOINT",
"LOCALSTACK_RUNTIME_ID",
"LOCALSTACK_INTEROP_PORT",
"LOCALSTACK_RUNTIME_TRACING_PORT",
"LOCALSTACK_USER",
"LOCALSTACK_CODE_ARCHIVES",
"LOCALSTACK_HOT_RELOADING_PATHS",
"LOCALSTACK_ENABLE_XRAY_TELEMETRY",
"LOCALSTACK_INIT_LOG_LEVEL",
"LOCALSTACK_POST_INVOKE_WAIT_MS",
"LOCALSTACK_FUNCTION_ACCOUNT_ID",
"LOCALSTACK_MAX_PAYLOAD_SIZE",
"LOCALSTACK_CHMOD_PATHS",
// Docker container ID
"HOSTNAME",
// User
"HOME",
}
for _, envKey := range unsetList {
if err := os.Unsetenv(envKey); err != nil {
log.Warnln("Could not unset environment variable:", envKey, err)
}
}
}
func main() {
// we're setting this to the same value as in the official RIE
debug.SetGCPercent(33)
// configuration parsing
lsOpts := InitLsOpts()
UnsetLsEnvs()
// set up logging following the Logrus logging levels: https://github.com/sirupsen/logrus#level-logging
log.SetReportCaller(true)
// https://docs.aws.amazon.com/xray/latest/devguide/xray-daemon-configuration.html
xRayLogLevel := "info"
switch lsOpts.InitLogLevel {
case "trace":
log.SetFormatter(&log.JSONFormatter{})
log.SetLevel(log.TraceLevel)
xRayLogLevel = "debug"
case "debug":
log.SetLevel(log.DebugLevel)
xRayLogLevel = "debug"
case "info":
log.SetLevel(log.InfoLevel)
case "warn":
log.SetLevel(log.WarnLevel)
xRayLogLevel = "warn"
case "error":
log.SetLevel(log.ErrorLevel)
xRayLogLevel = "error"
case "fatal":
log.SetLevel(log.FatalLevel)
xRayLogLevel = "error"
case "panic":
log.SetLevel(log.PanicLevel)
xRayLogLevel = "error"
default:
log.Fatal("Invalid value for LOCALSTACK_INIT_LOG_LEVEL")
}
// patch MaxPayloadSize
payloadSize, err := strconv.Atoi(lsOpts.MaxPayloadSize)
if err != nil {
log.Panicln("Please specify a number for LOCALSTACK_MAX_PAYLOAD_SIZE")
}
interop.MaxPayloadSize = payloadSize
// download code archive if env variable is set
if err := DownloadCodeArchives(lsOpts.CodeArchives); err != nil {
log.Fatal("Failed to download code archives: " + err.Error())
}
if err := AdaptFilesystemPermissions(lsOpts.ChmodPaths); err != nil {
log.Warnln("Could not change file mode of code directories:", err)
}
// parse CLI args
bootstrap, handler := getBootstrap(os.Args)
// Switch to non-root user and drop root privileges
if IsRootUser() && lsOpts.User != "" && lsOpts.User != "root" {
uid := 993
gid := 990
AddUser(lsOpts.User, uid, gid)
if err := os.Chown("/tmp", uid, gid); err != nil {
log.Warnln("Could not change owner of directory /tmp:", err)
}
UserLogger().Debugln("Process running as root user.")
err := DropPrivileges(lsOpts.User)
if err != nil {
log.Warnln("Could not drop root privileges.", err)
} else {
UserLogger().Debugln("Process running as non-root user.")
}
}
// file watcher for hot-reloading
fileWatcherContext, cancelFileWatcher := context.WithCancel(context.Background())
logCollector := NewLogCollector()
localStackLogsEgressApi := NewLocalStackLogsEgressAPI(logCollector)
tracer := NewLocalStackTracer()
// build sandbox
sandbox := rapidcore.
NewSandboxBuilder().
//SetTracer(tracer).
AddShutdownFunc(func() {
log.Debugln("Stopping file watcher")
cancelFileWatcher()
}).
SetExtensionsFlag(true).
SetInitCachingFlag(true).
SetLogsEgressAPI(localStackLogsEgressApi).
SetTracer(tracer)
// xray daemon
endpoint := "http://" + lsOpts.LocalstackIP + ":" + lsOpts.EdgePort
xrayConfig := initConfig(endpoint, xRayLogLevel)
d := initDaemon(xrayConfig, lsOpts.EnableXRayTelemetry == "1")
sandbox.AddShutdownFunc(func() {
log.Debugln("Shutting down xray daemon")
d.stop()
log.Debugln("Flushing segments in xray daemon")
d.close()
})
runDaemon(d) // async
defaultInterop := sandbox.DefaultInteropServer()
interopServer := NewCustomInteropServer(lsOpts, defaultInterop, logCollector)
sandbox.SetInteropServer(interopServer)
if len(handler) > 0 {
sandbox.SetHandler(handler)
}
exitChan := make(chan struct{})
sandbox.AddShutdownFunc(func() {
exitChan <- struct{}{}
})
// initialize all flows and start runtime API
sandboxContext, internalStateFn := sandbox.Create()
// Populate our custom interop server
interopServer.SetSandboxContext(sandboxContext)
interopServer.SetInternalStateGetter(internalStateFn)
// get timeout
invokeTimeoutEnv := GetEnvOrDie("AWS_LAMBDA_FUNCTION_TIMEOUT") // TODO: collect all AWS_* env parsing
invokeTimeoutSeconds, err := strconv.Atoi(invokeTimeoutEnv)
if err != nil {
log.Fatalln(err)
}
go RunHotReloadingListener(interopServer, lsOpts.HotReloadingPaths, fileWatcherContext, lsOpts.FileWatcherStrategy)
// start runtime init. It is important to start `InitHandler` synchronously because we need to ensure the
// notification channels and status fields are properly initialized before `AwaitInitialized`
log.Debugln("Starting runtime init.")
InitHandler(sandbox.LambdaInvokeAPI(), GetEnvOrDie("AWS_LAMBDA_FUNCTION_VERSION"), int64(invokeTimeoutSeconds), bootstrap, lsOpts.AccountId) // TODO: replace this with a custom init
log.Debugln("Awaiting initialization of runtime init.")
if err := interopServer.delegate.AwaitInitialized(); err != nil {
// Error cases: ErrInitDoneFailed or ErrInitResetReceived
log.Errorln("Runtime init failed to initialize: " + err.Error() + ". Exiting.")
// NOTE: Sending the error status to LocalStack is handled beforehand in the custom_interop.go through the
// callback SendInitErrorResponse because it contains the correct error response payload.
return
}
log.Debugln("Completed initialization of runtime init. Sending status ready to LocalStack.")
if err := interopServer.localStackAdapter.SendStatus(Ready, []byte{}); err != nil {
log.Fatalln("Failed to send status ready to LocalStack " + err.Error() + ". Exiting.")
}
<-exitChan
}