Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Latest commit

 

History

History
History
130 lines (107 loc) · 3.1 KB

File metadata and controls

130 lines (107 loc) · 3.1 KB
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
package lambda
import (
"bytes"
"context"
"encoding/json"
"fmt"
"io"
"os"
"strings"
"github.com/docker/docker/api/types"
"github.com/docker/docker/api/types/container"
"github.com/docker/docker/api/types/network"
"github.com/docker/docker/client"
"github.com/docker/docker/pkg/stdcopy"
"github.com/lbernardo/lambda-local/model"
)
type ContentRequest struct {
Body string `json:"body"`
PathParameters map[string]string `json:"pathparameters"`
}
func PullImageDocker(runtime string) {
fmt.Println("Prepare image docker")
imageName := "lambci/lambda:" + runtime
ctx := context.Background()
cli, err := client.NewClientWithOpts(client.FromEnv, client.WithAPIVersionNegotiation())
if err != nil {
panic(err)
}
reader, err := cli.ImagePull(ctx, imageName, types.ImagePullOptions{})
if err != nil {
panic(err)
}
io.Copy(os.Stdout, reader)
}
func ReplaceEnvironment(env string) string {
return strings.ReplaceAll(env, "${opt:stage, self:provider.stage}", "dev")
}
func ExecuteDockerLambda(volume string, net string, handler string, runtime string, environment map[string]string, body io.ReadCloser, parameters map[string]string) (model.Result, string) {
var result model.Result
var output bytes.Buffer
var contentRequest ContentRequest
buf := new(bytes.Buffer)
buf.ReadFrom(body)
bodyStr := buf.String()
var strEnv []string
imageName := "lambci/lambda:" + runtime
for n, env := range environment {
strEnv = append(strEnv, n+"="+ReplaceEnvironment(env))
}
ctx := context.Background()
cli, err := client.NewClientWithOpts(client.FromEnv, client.WithAPIVersionNegotiation())
if err != nil {
panic(err)
}
bodyStr = strings.ReplaceAll(bodyStr, "\t", "")
bodyStr = strings.ReplaceAll(bodyStr, "\n", "")
contentRequest.Body = bodyStr
contentRequest.PathParameters = parameters
jsonRequest, _ := json.Marshal(contentRequest)
var executeCommand []string
executeCommand = append(executeCommand, handler)
executeCommand = append(executeCommand, string(jsonRequest))
// Network config
networkingConfig := &network.NetworkingConfig{}
if net != "" {
networkingConfig.EndpointsConfig = map[string]*network.EndpointSettings{
net: &network.EndpointSettings{},
}
}
resp, err := cli.ContainerCreate(ctx, &container.Config{
Image: imageName,
Cmd: executeCommand,
Env: strEnv,
}, &container.HostConfig{
Binds: []string{volume + ":/var/task"},
}, networkingConfig, "")
if err != nil {
panic(err)
}
err = cli.ContainerStart(ctx, resp.ID, types.ContainerStartOptions{})
if err != nil {
panic(err)
}
statusCh, errCh := cli.ContainerWait(ctx, resp.ID, container.WaitConditionNotRunning)
select {
case err := <-errCh:
if err != nil {
panic(err)
}
case <-statusCh:
}
reader, err := cli.ContainerLogs(ctx, resp.ID, types.ContainerLogsOptions{
ShowStdout: true,
ShowStderr: true,
})
if err != nil {
panic(err)
}
stdcopy.StdCopy(&output, os.Stderr, reader)
str := output.String()
err = json.Unmarshal([]byte(str), &result)
if err != nil {
fmt.Println(err)
}
cli.ContainerRemove(ctx, resp.ID, types.ContainerRemoveOptions{})
return result, str
}
Morty Proxy This is a proxified and sanitized view of the page, visit original site.