-
Notifications
You must be signed in to change notification settings - Fork 20
/
run.go
209 lines (184 loc) · 5.1 KB
/
run.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
package main
import (
"bytes"
"errors"
"fmt"
"io"
"io/ioutil"
"net/http"
"os"
"os/exec"
"strings"
"github.com/urfave/cli"
)
const (
DefaultFormat = "default"
HttpFormat = "http"
LocalTestURL = "http://localhost:8080/myapp/hello"
)
func run() cli.Command {
r := runCmd{}
return cli.Command{
Name: "run",
Usage: "run a function locally",
ArgsUsage: "[username/image:tag]",
Flags: append(runflags(), []cli.Flag{}...),
Action: r.run,
}
}
type runCmd struct{}
func runflags() []cli.Flag {
return []cli.Flag{
cli.StringSliceFlag{
Name: "env, e",
Usage: "select environment variables to be sent to function",
},
cli.StringSliceFlag{
Name: "link",
Usage: "select container links for the function",
},
cli.StringFlag{
Name: "method",
Usage: "http method for function",
},
cli.StringFlag{
Name: "format",
Usage: "format to use. `default` and `http` (hot) formats currently supported.",
},
cli.IntFlag{
Name: "runs",
Usage: "for hot functions only, will call the function `runs` times in a row.",
},
cli.Uint64Flag{
Name: "memory",
Usage: "RAM to allocate for function, Units: MB",
},
}
}
func (r *runCmd) run(c *cli.Context) error {
// First, build it
err := c.App.Command("build").Run(c)
if err != nil {
return err
}
var ff *funcfile
// if image name is passed in, it will run that image
image := c.Args().First()
if image == "" {
ff, err = loadFuncfile()
if err != nil {
if _, ok := err.(*notFoundError); ok {
return errors.New("error: image name is missing or no function file found")
}
return err
}
} else {
ff = &funcfile{
Name: image,
}
}
// means no memory specified through CLI args
// memory from func.yaml applied
if c.Uint64("memory") != 0 {
ff.Memory = c.Uint64("memory")
}
return runff(ff, stdin(), os.Stdout, os.Stderr, c.String("method"), c.StringSlice("e"), c.StringSlice("link"), c.String("format"), c.Int("runs"))
}
// TODO: share all this stuff with the Docker driver in server or better yet, actually use the Docker driver
func runff(ff *funcfile, stdin io.Reader, stdout, stderr io.Writer, method string, envVars []string, links []string, format string, runs int) error {
sh := []string{"docker", "run", "--rm", "-i", fmt.Sprintf("--memory=%dm", ff.Memory)}
var err error
var env []string // env for the shelled out docker run command
var runEnv []string // env to pass into the container via -e's
if method == "" {
if stdin == nil {
method = "GET"
} else {
method = "POST"
}
}
if format == "" {
format = DefaultFormat
}
// Add expected env vars that service will add
runEnv = append(runEnv, kvEq("METHOD", method))
runEnv = append(runEnv, kvEq("REQUEST_URL", LocalTestURL))
runEnv = append(runEnv, kvEq("APP_NAME", "myapp"))
runEnv = append(runEnv, kvEq("ROUTE", "/hello")) // TODO: should we change this to PATH ?
runEnv = append(runEnv, kvEq("FN_FORMAT", format))
runEnv = append(runEnv, kvEq("MEMORY_MB", fmt.Sprintf("%d", ff.Memory)))
// add user defined envs
runEnv = append(runEnv, envVars...)
for _, l := range links {
sh = append(sh, "--link", l)
}
dockerenv := []string{"DOCKER_TLS_VERIFY", "DOCKER_HOST", "DOCKER_CERT_PATH", "DOCKER_MACHINE_NAME"}
for _, e := range dockerenv {
env = append(env, fmt.Sprint(e, "=", os.Getenv(e)))
}
for _, e := range runEnv {
sh = append(sh, "-e", e)
}
if runs <= 0 {
runs = 1
}
if ff.Type != "" && ff.Type == "async" {
// if async, we'll run this in a separate thread and wait for it to complete
// reqID := id.New().String()
// I'm starting to think maybe `fn run` locally should work the same whether sync or async? Or how would we allow to test the output?
}
body := "" // used for hot functions
if format == HttpFormat {
// let's swap out stdin for http formatted message
input := []byte("")
if stdin != nil {
input, err = ioutil.ReadAll(stdin)
if err != nil {
return fmt.Errorf("error reading from stdin: %v", err)
}
}
var b bytes.Buffer
for i := 0; i < runs; i++ {
// making new request each time since Write closes the body
req, err := http.NewRequest(method, LocalTestURL, strings.NewReader(string(input)))
if err != nil {
return fmt.Errorf("error creating http request: %v", err)
}
err = req.Write(&b)
b.Write([]byte("\n"))
}
if err != nil {
return fmt.Errorf("error writing to byte buffer: %v", err)
}
body = b.String()
// fmt.Println("body:", s)
stdin = strings.NewReader(body)
}
sh = append(sh, ff.ImageName())
cmd := exec.Command(sh[0], sh[1:]...)
cmd.Stdin = stdin
cmd.Stdout = stdout
cmd.Stderr = stderr
// cmd.Env = env
return cmd.Run()
}
func extractEnvVar(e string) ([]string, string) {
kv := strings.Split(e, "=")
name := toEnvName("HEADER", kv[0])
sh := []string{"-e", name}
var v string
if len(kv) > 1 {
v = kv[1]
} else {
v = os.Getenv(kv[0])
}
return sh, kvEq(name, v)
}
func kvEq(k, v string) string {
return fmt.Sprintf("%s=%s", k, v)
}
// From server.toEnvName()
func toEnvName(envtype, name string) string {
name = strings.ToUpper(strings.Replace(name, "-", "_", -1))
return fmt.Sprintf("%s_%s", envtype, name)
}