forked from aws/aws-lambda-runtime-interface-emulator
-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathbootstrap.go
More file actions
158 lines (131 loc) · 5.1 KB
/
bootstrap.go
File metadata and controls
158 lines (131 loc) · 5.1 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
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0
package rapidcore
import (
"fmt"
"os"
"path/filepath"
"go.amzn.com/lambda/fatalerror"
"go.amzn.com/lambda/logging"
"go.amzn.com/lambda/rapid"
log "github.com/sirupsen/logrus"
)
type LogFormatter func(error) string
type BootstrapError func() (fatalerror.ErrorType, LogFormatter)
// Bootstrap represents a list of executable bootstrap
// candidates in order of priority and exec metadata
type Bootstrap struct {
orderedLookupPaths []string
validCmd []string
workingDir string
cmdCandidates [][]string
extraFiles []*os.File
bootstrapError BootstrapError
}
// NewBootstrap returns an instance of bootstrap defined by given params
func NewBootstrap(cmdCandidates [][]string, currentWorkingDir string) *Bootstrap {
var orderedLookupBootstrapPaths []string
for _, args := range cmdCandidates {
// Empty args is an error, but we want to detect it later (in Cmd() call) when we are able to report a descriptive error
if len(args) != 0 {
orderedLookupBootstrapPaths = append(orderedLookupBootstrapPaths, args[0])
}
}
if currentWorkingDir == "" {
// use the root directory as the default working directory
currentWorkingDir = "/"
}
return &Bootstrap{
orderedLookupPaths: orderedLookupBootstrapPaths,
workingDir: currentWorkingDir,
cmdCandidates: cmdCandidates,
}
}
func NewBootstrapSingleCmd(cmd []string, currentWorkingDir string) *Bootstrap {
if currentWorkingDir == "" {
// use the root directory as the default working directory
currentWorkingDir = "/"
}
// a single candidate command makes it automatically valid
return &Bootstrap{
validCmd: cmd,
workingDir: currentWorkingDir,
}
}
// locateBootstrap sets the first occurrence of an
// actual bootstrap, given a list of possible files
func (b *Bootstrap) locateBootstrap() error {
for i, bootstrapCandidate := range b.orderedLookupPaths {
if file, err := os.Stat(bootstrapCandidate); !os.IsNotExist(err) && !file.IsDir() {
b.validCmd = b.cmdCandidates[i]
return nil
}
}
log.WithField("bootstrapPathsChecked", b.orderedLookupPaths).Warn("Couldn't find valid bootstrap(s)")
return fmt.Errorf("Couldn't find valid bootstrap(s): %s", b.orderedLookupPaths)
}
// Cmd returns the args of bootstrap, where args[0]
// is the path to executable
func (b *Bootstrap) Cmd() ([]string, error) {
if len(b.validCmd) > 0 {
return b.validCmd, nil
}
if err := b.locateBootstrap(); err != nil {
return []string{}, err
}
log.Debug("Located runtime bootstrap", b.validCmd[0])
return b.validCmd, nil
}
// Env returns the environment variables available to
// the bootstrap process
func (b *Bootstrap) Env(e rapid.EnvironmentVariables) []string {
return e.RuntimeExecEnv()
}
// Cwd returns the working directory of the bootstrap process
func (b *Bootstrap) Cwd() (string, error) {
if !filepath.IsAbs(b.workingDir) {
return "", fmt.Errorf("the working directory '%s' is invalid, it needs to be an absolute path", b.workingDir)
} else if _, err := os.Stat(b.workingDir); os.IsNotExist(err) {
return "", fmt.Errorf("the working directory doesn't exist: %s", b.workingDir)
}
return b.workingDir, nil
}
// SetExtraFiles sets the extra file descriptors apart from 1 & 2 to be passed to runtime
func (b *Bootstrap) SetExtraFiles(extraFiles []*os.File) {
b.extraFiles = extraFiles
}
// ExtraFiles returns the extra file descriptors apart from 1 & 2 to be passed to runtime
func (b *Bootstrap) ExtraFiles() []*os.File {
return b.extraFiles
}
// CachedFatalError returns a bootstrap error that occurred during startup and before init
// so that it can be reported back to the customer in a later phase
func (b *Bootstrap) CachedFatalError(err error) (fatalerror.ErrorType, string, bool) {
if b.bootstrapError == nil {
return fatalerror.ErrorType(""), "", false
}
fatalError, logFunc := b.bootstrapError()
return fatalError, logFunc(err), true
}
// SetCachedFatalError sets a cached fatal error that occurred during startup and before init
// so that it can be reported back to the customer in a later phase
func (b *Bootstrap) SetCachedFatalError(bootstrapErrFn BootstrapError) {
b.bootstrapError = bootstrapErrFn
}
// BootstrapErrInvalidLCISTaskConfig represents an error while parsing LCIS task config
func BootstrapErrInvalidLCISTaskConfig(err error) BootstrapError {
return func() (fatalerror.ErrorType, LogFormatter) {
return fatalerror.InvalidTaskConfig, logging.SupernovaInvalidTaskConfigRepr(err)
}
}
// BootstrapErrInvalidLCISEntrypoint represents an invalid LCIS entrypoint error
func BootstrapErrInvalidLCISEntrypoint(entrypoint []string, cmd []string, workingdir string) BootstrapError {
return func() (fatalerror.ErrorType, LogFormatter) {
return fatalerror.InvalidEntrypoint, logging.SupernovaLaunchErrorRepr(entrypoint, cmd, workingdir)
}
}
func BootstrapErrInvalidLCISWorkingDir(entrypoint []string, cmd []string, workingdir string) BootstrapError {
return func() (fatalerror.ErrorType, LogFormatter) {
return fatalerror.InvalidWorkingDir, logging.SupernovaLaunchErrorRepr(entrypoint, cmd, workingdir)
}
}