Skip to content

Commit a252c82

Browse files
committed
Adapt new init changes (#30)
1 parent b86f257 commit a252c82

File tree

4 files changed

+85
-13
lines changed

4 files changed

+85
-13
lines changed

cmd/localstack/awsutil.go

+4-4
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ import (
1111
"fmt"
1212
log "github.com/sirupsen/logrus"
1313
"go.amzn.com/lambda/interop"
14-
"go.amzn.com/lambda/rapidcore"
1514
"go.amzn.com/lambda/rapidcore/env"
1615
"golang.org/x/sys/unix"
1716
"io"
@@ -34,7 +33,7 @@ func isBootstrapFileExist(filePath string) bool {
3433
return !os.IsNotExist(err) && !file.IsDir()
3534
}
3635

37-
func getBootstrap(args []string) (*rapidcore.Bootstrap, string) {
36+
func getBootstrap(args []string) (interop.Bootstrap, string) {
3837
var bootstrapLookupCmd []string
3938
var handler string
4039
currentWorkingDir := "/var/task" // default value
@@ -89,7 +88,7 @@ func getBootstrap(args []string) (*rapidcore.Bootstrap, string) {
8988
}
9089
}
9190

92-
return rapidcore.NewBootstrapSingleCmd(bootstrapLookupCmd, currentWorkingDir, ""), handler
91+
return NewSimpleBootstrap(bootstrapLookupCmd, currentWorkingDir), handler
9392
}
9493

9594
func PrintEndReports(invokeId string, initDuration string, memorySize string, invokeStart time.Time, timeoutDuration time.Duration, w io.Writer) {
@@ -205,7 +204,7 @@ func getSubFoldersInList(prefix string, pathList []string) (oldFolders []string,
205204
return
206205
}
207206

208-
func InitHandler(sandbox Sandbox, functionVersion string, timeout int64, bs interop.Bootstrap) (time.Time, time.Time) {
207+
func InitHandler(sandbox Sandbox, functionVersion string, timeout int64, bs interop.Bootstrap, accountId string) (time.Time, time.Time) {
209208
additionalFunctionEnvironmentVariables := map[string]string{}
210209

211210
// Add default Env Vars if they were not defined. This is a required otherwise 1p Python2.7, Python3.6, and
@@ -231,6 +230,7 @@ func InitHandler(sandbox Sandbox, functionVersion string, timeout int64, bs inte
231230
AwsKey: os.Getenv("AWS_ACCESS_KEY_ID"),
232231
AwsSecret: os.Getenv("AWS_SECRET_ACCESS_KEY"),
233232
AwsSession: os.Getenv("AWS_SESSION_TOKEN"),
233+
AccountID: accountId,
234234
XRayDaemonAddress: GetenvWithDefault("AWS_XRAY_DAEMON_ADDRESS", "127.0.0.1:2000"),
235235
FunctionName: GetenvWithDefault("AWS_LAMBDA_FUNCTION_NAME", "test_function"),
236236
FunctionVersion: functionVersion,

cmd/localstack/custom_interop.go

+8-8
Original file line numberDiff line numberDiff line change
@@ -194,23 +194,23 @@ func NewCustomInteropServer(lsOpts *LsOpts, delegate interop.Server, logCollecto
194194
return server
195195
}
196196

197-
func (c *CustomInteropServer) SendResponse(invokeID string, headers map[string]string, reader io.Reader, trailers http.Header, request *interop.CancellableRequest) error {
197+
func (c *CustomInteropServer) SendResponse(invokeID string, resp *interop.StreamableInvokeResponse) error {
198198
log.Traceln("SendResponse called")
199-
return c.delegate.SendResponse(invokeID, headers, reader, trailers, request)
199+
return c.delegate.SendResponse(invokeID, resp)
200200
}
201201

202-
func (c *CustomInteropServer) SendErrorResponse(invokeID string, response *interop.ErrorResponse) error {
202+
func (c *CustomInteropServer) SendErrorResponse(invokeID string, resp *interop.ErrorInvokeResponse) error {
203203
log.Traceln("SendErrorResponse called")
204-
return c.delegate.SendErrorResponse(invokeID, response)
204+
return c.delegate.SendErrorResponse(invokeID, resp)
205205
}
206206

207207
// SendInitErrorResponse writes error response during init to a shared memory and sends GIRD FAULT.
208-
func (c *CustomInteropServer) SendInitErrorResponse(invokeID string, response *interop.ErrorResponse) error {
208+
func (c *CustomInteropServer) SendInitErrorResponse(resp *interop.ErrorInvokeResponse) error {
209209
log.Traceln("SendInitErrorResponse called")
210-
if err := c.localStackAdapter.SendStatus(Error, response.Payload); err != nil {
210+
if err := c.localStackAdapter.SendStatus(Error, resp.Payload); err != nil {
211211
log.Fatalln("Failed to send init error to LocalStack " + err.Error() + ". Exiting.")
212212
}
213-
return c.delegate.SendInitErrorResponse(invokeID, response)
213+
return c.delegate.SendInitErrorResponse(resp)
214214
}
215215

216216
func (c *CustomInteropServer) GetCurrentInvokeID() string {
@@ -248,7 +248,7 @@ func (c *CustomInteropServer) Reset(reason string, timeoutMs int64) (*statejson.
248248
return c.delegate.Reset(reason, timeoutMs)
249249
}
250250

251-
func (c *CustomInteropServer) AwaitRelease() (*statejson.InternalStateDescription, error) {
251+
func (c *CustomInteropServer) AwaitRelease() (*statejson.ReleaseResponse, error) {
252252
log.Traceln("AwaitRelease called")
253253
return c.delegate.AwaitRelease()
254254
}

cmd/localstack/main.go

+4-1
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ type LsOpts struct {
1616
InteropPort string
1717
RuntimeEndpoint string
1818
RuntimeId string
19+
AccountId string
1920
InitTracingPort string
2021
User string
2122
CodeArchives string
@@ -41,6 +42,7 @@ func InitLsOpts() *LsOpts {
4142
// required
4243
RuntimeEndpoint: GetEnvOrDie("LOCALSTACK_RUNTIME_ENDPOINT"),
4344
RuntimeId: GetEnvOrDie("LOCALSTACK_RUNTIME_ID"),
45+
AccountId: GetenvWithDefault("LOCALSTACK_FUNCTION_ACCOUNT_ID", "000000000000"),
4446
// optional with default
4547
InteropPort: GetenvWithDefault("LOCALSTACK_INTEROP_PORT", "9563"),
4648
InitTracingPort: GetenvWithDefault("LOCALSTACK_RUNTIME_TRACING_PORT", "9564"),
@@ -72,6 +74,7 @@ func UnsetLsEnvs() {
7274
"LOCALSTACK_ENABLE_XRAY_TELEMETRY",
7375
"LOCALSTACK_INIT_LOG_LEVEL",
7476
"LOCALSTACK_POST_INVOKE_WAIT_MS",
77+
"LOCALSTACK_FUNCTION_ACCOUNT_ID",
7578

7679
// Docker container ID
7780
"HOSTNAME",
@@ -230,7 +233,7 @@ func main() {
230233
// start runtime init. It is important to start `InitHandler` synchronously because we need to ensure the
231234
// notification channels and status fields are properly initialized before `AwaitInitialized`
232235
log.Debugln("Starting runtime init.")
233-
InitHandler(sandbox.LambdaInvokeAPI(), GetEnvOrDie("AWS_LAMBDA_FUNCTION_VERSION"), int64(invokeTimeoutSeconds), bootstrap) // TODO: replace this with a custom init
236+
InitHandler(sandbox.LambdaInvokeAPI(), GetEnvOrDie("AWS_LAMBDA_FUNCTION_VERSION"), int64(invokeTimeoutSeconds), bootstrap, lsOpts.AccountId) // TODO: replace this with a custom init
234237

235238
log.Debugln("Awaiting initialization of runtime init.")
236239
if err := interopServer.delegate.AwaitInitialized(); err != nil {

cmd/localstack/simple_bootstrap.go

+69
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
2+
// SPDX-License-Identifier: Apache-2.0
3+
4+
package main
5+
6+
import (
7+
"fmt"
8+
"os"
9+
"path/filepath"
10+
11+
"go.amzn.com/lambda/fatalerror"
12+
"go.amzn.com/lambda/interop"
13+
"go.amzn.com/lambda/rapidcore/env"
14+
)
15+
16+
// the type implement a simpler version of the Bootstrap
17+
// this is useful in the Standalone Core implementation.
18+
type simpleBootstrap struct {
19+
cmd []string
20+
workingDir string
21+
}
22+
23+
func NewSimpleBootstrap(cmd []string, currentWorkingDir string) interop.Bootstrap {
24+
if currentWorkingDir == "" {
25+
// use the root directory as the default working directory
26+
currentWorkingDir = "/"
27+
}
28+
29+
// a single candidate command makes it automatically valid
30+
return &simpleBootstrap{
31+
cmd: cmd,
32+
workingDir: currentWorkingDir,
33+
}
34+
}
35+
36+
func (b *simpleBootstrap) Cmd() ([]string, error) {
37+
return b.cmd, nil
38+
}
39+
40+
// Cwd returns the working directory of the bootstrap process
41+
// The path is validated against the chroot identified by `root`
42+
func (b *simpleBootstrap) Cwd() (string, error) {
43+
if !filepath.IsAbs(b.workingDir) {
44+
return "", fmt.Errorf("the working directory '%s' is invalid, it needs to be an absolute path", b.workingDir)
45+
}
46+
47+
// evaluate the path relatively to the domain's mnt namespace root
48+
if _, err := os.Stat(b.workingDir); os.IsNotExist(err) {
49+
return "", fmt.Errorf("the working directory doesn't exist: %s", b.workingDir)
50+
}
51+
52+
return b.workingDir, nil
53+
}
54+
55+
// Env returns the environment variables available to
56+
// the bootstrap process
57+
func (b *simpleBootstrap) Env(e *env.Environment) map[string]string {
58+
return e.RuntimeExecEnv()
59+
}
60+
61+
// ExtraFiles returns the extra file descriptors apart from 1 & 2 to be passed to runtime
62+
func (b *simpleBootstrap) ExtraFiles() []*os.File {
63+
return make([]*os.File, 0)
64+
}
65+
66+
func (b *simpleBootstrap) CachedFatalError(err error) (fatalerror.ErrorType, string, bool) {
67+
// not implemented as it is not needed in Core but we need to fullfil the interface anyway
68+
return fatalerror.ErrorType(""), "", false
69+
}

0 commit comments

Comments
 (0)