/
misc.go
147 lines (132 loc) · 3.53 KB
/
misc.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
package main
import (
"archive/zip"
"bytes"
"crypto/rand"
"fmt"
"go/build"
"io/ioutil"
"log"
"os"
"os/exec"
"path/filepath"
"strings"
"github.com/jhoonb/archivex"
)
func gopathMounts(dir string) (string, []string) {
gopath := "/go"
srcDirs := build.Default.SrcDirs()
for i := range srcDirs {
gopath += fmt.Sprintf(":%s/path%d", dir, i)
}
return gopath, srcDirs
}
func packageName(pkgImport string) (name string) {
for _, src := range build.Default.SrcDirs() {
pkg, err := build.Import(pkgImport, src, build.ImportComment)
if err != nil || pkg.IsCommand() || pkg.Goroot {
continue
}
name = pkg.Name
return
}
log.Fatalln("package not found in $GOPATH:", pkgImport)
return
}
func packageDir(pkgImport string) (dir string) {
for _, src := range build.Default.SrcDirs() {
pkg, err := build.Import(pkgImport, src, build.ImportComment)
if err != nil || pkg.IsCommand() || pkg.Goroot {
continue
}
dir = pkg.Dir
return
}
log.Fatalln("package not found in $GOPATH:", pkgImport)
return
}
func getExecPath(name string) string {
out, err := exec.LookPath("docker")
if err != nil {
log.Fatalf("executable file %s not found in $PATH", name)
return ""
}
return strings.TrimSpace(out)
}
func makeZip(main []byte, mainPath, libPath string, other ...string) []byte {
buf := new(bytes.Buffer)
zipper := &archivex.ZipFile{
Name: "source.zip",
Writer: zip.NewWriter(buf),
}
if err := zipper.Add(mainPath, main); err != nil {
log.Fatalln(err)
}
if err := zipper.AddFile(libPath); err != nil {
log.Fatalf("error adding file '%s' to zip archive: %v", main, err)
}
for _, name := range other {
info, err := os.Stat(name)
if err != nil {
log.Fatalln(err)
}
if info.IsDir() {
err = zipper.AddAll(name, false)
} else {
err = zipper.AddFile(name)
}
if err != nil {
log.Fatalln(err)
}
}
zipper.Close()
return buf.Bytes()
}
func getModuleSource(packageName, packageFunc string) []byte {
buf := new(bytes.Buffer)
fmt.Fprintln(buf, `
import module
import json
class Encoder(json.JSONEncoder):
def default(self, o):
if o.__class__.__name__ == 'CognitoIdentity': # very wow much reflection
return dict(cognito_identity_id=o.cognito_identity_id,
cognito_identity_pool_id=o.cognito_identity_pool_id)
else:
return o.__dict__
`)
fmt.Fprintln(buf, "lambda_handler = module.lambda_handler()")
fmt.Fprintf(buf, "def %s(event, context):\n", packageFunc)
fmt.Fprintln(buf, " return lambda_handler(json.dumps(event), json.dumps(context, cls=Encoder))")
return buf.Bytes()
}
func getTempDir() string {
tag := make([]byte, 8)
_, err := rand.Read(tag)
if err != nil {
return filepath.Join(os.TempDir(), "go-lambda")
}
return filepath.Join(os.TempDir(), fmt.Sprintf("go-lambda-%x", tag))
}
func buildModuleBridge(tempDir, pkg, packageFunc string) {
if err := os.MkdirAll(tempDir, 0755); err != nil {
log.Fatalln(err)
}
defer os.RemoveAll(tempDir)
err := ioutil.WriteFile(filepath.Join(tempDir, "module.c"), MustAsset("module/module.c"), 0644)
if err != nil {
log.Fatalln(err)
}
goAsset := MustAsset("module/module.go")
funcRef := fmt.Sprintf(`&lambda.%s`, strings.Title(packageFunc))
goAsset = bytes.Replace(goAsset, []byte(`&lambda.Handler`), []byte(funcRef), -1)
if isNative() {
pkgRef := fmt.Sprintf(`lambda "%s"`, pkg)
goAsset = bytes.Replace(goAsset, []byte(`lambda "in"`), []byte(pkgRef), -1)
}
err = ioutil.WriteFile(filepath.Join(tempDir, "module.go"), goAsset, 0644)
if err != nil {
log.Fatalln(err)
}
runBuild(pkg, tempDir)
}