-
Notifications
You must be signed in to change notification settings - Fork 48
/
Copy pathsparta_main_awsbinary.go
251 lines (229 loc) · 6.96 KB
/
sparta_main_awsbinary.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
249
250
251
//go:build lambdabinary
// +build lambdabinary
package sparta
// Provides NOP implementations for functions that do not need to execute
// in the Lambda context
import (
"context"
"fmt"
"io"
"io/ioutil"
"os"
"regexp"
"runtime"
"time"
"github.com/pkg/errors"
"github.com/rs/zerolog"
"github.com/spf13/cobra"
)
var reExtractPlatInfo = regexp.MustCompile(`(\w+)=\"(.*)\"`)
// Main defines the primary handler for transforming an application into a Sparta package. The
// serviceName is used to uniquely identify your service within a region and will
// be used for subsequent updates. For provisioning, ensure that you've
// properly configured AWS credentials for the golang SDK.
// See http://docs.aws.amazon.com/sdk-for-go/api/aws/defaults.html#DefaultChainCredentials-constant
// for more information.
func Main(serviceName string,
serviceDescription string,
lambdaAWSInfos []*LambdaAWSInfo,
api *API,
site *S3Site) error {
return MainEx(serviceName,
serviceDescription,
lambdaAWSInfos,
api,
site,
nil,
false)
}
// MainEx provides an "extended" Main that supports customizing the standard Sparta
// workflow via the `workflowHooks` parameter.
func MainEx(serviceName string,
serviceDescription string,
lambdaAWSInfos []*LambdaAWSInfo,
api APIGateway,
site *S3Site,
workflowHooks *WorkflowHooks,
useCGO bool) error {
// It's possible the user attached a custom command to the
// root command. If there is no command, then just run the
// Execute command...
CommandLineOptions.Root.PersistentPreRunE = func(cmd *cobra.Command, args []string) error {
// This can only run in AWS Lambda
mainLogLevel := "info"
envVarLogLevel := os.Getenv(envVarLogLevel)
if envVarLogLevel != "" {
mainLogLevel = envVarLogLevel
}
// We never want colors in AWS because the console can't show them...
logger, loggerErr := NewLoggerForOutput(mainLogLevel, "json", true)
if loggerErr != nil {
return loggerErr
}
if logger == nil {
return errors.Errorf("Failed to initialize logger instance")
}
welcomeMessage := fmt.Sprintf("Service: %s", StampedServiceName)
logger.Info().
Str(fmt.Sprintf("%sVersion", ProperName), SpartaVersion).
Str(fmt.Sprintf("%sSHA", ProperName), SpartaGitHash[0:7]).
Str("go Version", runtime.Version()).
Str("BuildID", StampedBuildID).
Str("UTC", time.Now().UTC().Format(time.RFC3339)).
Msg(welcomeMessage)
OptionsGlobal.ServiceName = StampedServiceName
OptionsGlobal.Logger = logger
return nil
}
CommandLineOptions.Root.RunE = func(cmd *cobra.Command, args []string) (err error) {
defer func() {
if r := recover(); r != nil {
OptionsGlobal.Logger.Error().Msgf("Panic recovered: %v", r)
err = errors.Errorf(fmt.Sprintf("%v", r))
}
}()
// By default run the Execute command
err = Execute(StampedServiceName,
lambdaAWSInfos,
OptionsGlobal.Logger)
return err
}
return CommandLineOptions.Root.Execute()
}
// Delete is not available in the AWS Lambda binary
func Delete(ctx context.Context,
serviceName string,
logger *zerolog.Logger) error {
logger.Error().Msg("Delete() not supported in AWS Lambda binary")
return errors.New("Delete not supported for this binary")
}
// Build is not available in the AWS Lambda binary
func Build(ctx context.Context,
noop bool,
serviceName string,
serviceDescription string,
lambdaAWSInfos []*LambdaAWSInfo,
api APIGateway,
site *S3Site,
useCGO bool,
buildID string,
dockerFile string,
outputDirectory string,
buildTags string,
linkerFlags string,
templateWriter io.Writer,
workflowHooks *WorkflowHooks,
logger *zerolog.Logger) error {
logger.Error().Msg("Build() not supported in AWS Lambda binary")
return errors.New("Build not supported for this binary")
}
// Provision is not available in the AWS Lambda binary
func Provision(noop bool,
serviceName string,
serviceDescription string,
lambdaAWSInfos []*LambdaAWSInfo,
api *API,
site *S3Site,
s3Bucket string,
useCGO bool,
inplace bool,
buildID string,
codePipelineTrigger string,
buildTags string,
linkerFlags string,
writer io.Writer,
workflowHooks *WorkflowHooks,
logger *zerolog.Logger) error {
logger.Error().Msg("Provision() not supported in AWS Lambda binary")
return errors.New("Provision not supported for this binary")
}
// Describe is not available in the AWS Lambda binary
func Describe(serviceName string,
serviceDescription string,
lambdaAWSInfos []*LambdaAWSInfo,
api *API,
site *S3Site,
s3BucketName string,
buildTags string,
linkerFlags string,
outputWriter io.Writer,
workflowHooks *WorkflowHooks,
logger *zerolog.Logger) error {
logger.Error().Msg("Describe() not supported in AWS Lambda binary")
return errors.New("Describe not supported for this binary")
}
// Explore is an interactive command that brings up a GUI to test
// lambda functions previously deployed into AWS lambda. It's not supported in the
// AWS binary build
func Explore(serviceName string,
serviceDescription string,
lambdaAWSInfos []*LambdaAWSInfo,
api *API,
site *S3Site,
s3BucketName string,
buildTags string,
linkerFlags string,
logger *zerolog.Logger) error {
return errors.New("Explore not supported for this binary")
}
// Profile is the interactive command used to pull S3 assets locally into /tmp
// and run ppro against the cached profiles
func Profile(serviceName string,
serviceDescription string,
s3Bucket string,
httpPort int,
logger *zerolog.Logger) error {
return errors.New("Profile not supported for this binary")
}
// Status is the command that produces a simple status report for a given
// stack
func Status(ctx context.Context,
serviceName string,
serviceDescription string,
redact bool,
logger *zerolog.Logger) error {
return errors.New("Status not supported for this binary")
}
func platformLogSysInfo(lambdaFunc string, logger *zerolog.Logger) {
// Setup the files and their respective log levels
mapFilesToLoggerCall := map[zerolog.Level][]string{
zerolog.InfoLevel: {
"/proc/version",
"/etc/os-release",
},
zerolog.DebugLevel: {
"/proc/stat",
"/proc/cpuinfo",
"/proc/meminfo",
"/proc/loadavg",
"/proc/diskstats",
},
}
for eachLevel, eachList := range mapFilesToLoggerCall {
for _, eachEntry := range eachList {
data, dataErr := ioutil.ReadFile(eachEntry)
if dataErr == nil && len(data) != 0 {
entry := logger.WithLevel(eachLevel).Str("filepath", eachEntry)
match := reExtractPlatInfo.FindAllStringSubmatch(string(data), -1)
if match != nil {
for _, eachMatch := range match {
entry = entry.Str(eachMatch[1], eachMatch[2])
}
} else {
entry = entry.Str("contents", string(data))
}
entry.Msg("Host Info")
} else if dataErr != nil || len(data) <= 0 {
logger.Warn().
Str("filepath", eachEntry).
Interface("error", dataErr).
Int("length", len(data)).
Msg("Failed to read host info")
}
}
}
}
// RegisterCodePipelineEnvironment is not available during lambda execution
func RegisterCodePipelineEnvironment(environmentName string, environmentVariables map[string]string) error {
return nil
}