-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathexecute.go
74 lines (67 loc) · 1.68 KB
/
execute.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
package main
import (
b64 "encoding/base64"
"encoding/json"
"fmt"
shell "github.com/ipfs/go-ipfs-api"
"io/ioutil"
"log"
"os"
"os/exec"
"time"
)
type Message struct {
Action string `json:"action"`
Data struct {
Event string `json:"event"`
Code string `json:"code"`
} `json:"data"`
}
func decodeUnwrap(encoded string) string {
uEnv, _ := b64.URLEncoding.DecodeString(encoded)
return string(uEnv)
}
func writeCodeToTempFile(code string) {
path := "/tmp/43556789705456847598/" // build the temp DIR
if _, err := os.Stat(path); os.IsNotExist(err) {
os.Mkdir(path, os.ModePerm)
}
// write the function file body to tmp
err := ioutil.WriteFile("/tmp/43556789705456847598/lambda_function.py", []byte(code), 0644)
if err != nil {
panic(err)
}
}
func executeLambdaDocker(event string) string {
cmd := "docker"
args := []string{
"run", "--rm", "-v", "/tmp/43556789705456847598:/var/task",
"lambci/lambda:python3.7", "lambda_function.lambda_handler", event,
}
out, err := exec.Command(cmd, args...).Output()
if err != nil {
log.Fatal(err)
}
return string(out)
}
func ListenForExecute() {
// Where your local node is running on localhost:5001
sh := shell.NewShell("localhost:5001")
sub, _ := sh.PubSubSubscribe("test")
for true {
r, _ := sub.Next()
var msg Message
err := json.Unmarshal(r.Data, &msg)
if err != nil {
fmt.Println(err)
}
// fmt.Println(msg) // shows what we parsed out
code := decodeUnwrap(msg.Data.Code)
event := decodeUnwrap(msg.Data.Event)
writeCodeToTempFile(code)
out := executeLambdaDocker(event)
// fmt.Println(out) // shows Lambda Docker Resp - not logs
sh.PubSubPublish("test-response", out)
time.Sleep(time.Second)
}
}