mirror of
https://github.com/fnproject/fn.git
synced 2022-10-28 21:29:17 +03:00
Fixes async payload passing for #68.
This commit is contained in:
28
fn/calls.go
28
fn/calls.go
@@ -1,12 +1,12 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"context"
|
||||
"fmt"
|
||||
|
||||
"github.com/funcy/functions_go/models"
|
||||
fnclient "github.com/funcy/functions_go/client"
|
||||
apicall "github.com/funcy/functions_go/client/call"
|
||||
"github.com/funcy/functions_go/models"
|
||||
"github.com/urfave/cli"
|
||||
)
|
||||
|
||||
@@ -40,14 +40,14 @@ func calls() cli.Command {
|
||||
}
|
||||
|
||||
func printCalls(calls []*models.Call) {
|
||||
for _, call := range calls{
|
||||
for _, call := range calls {
|
||||
fmt.Println(fmt.Sprintf(
|
||||
"ID: %v\n" +
|
||||
"App: %v\n" +
|
||||
"Route: %v\n" +
|
||||
"Created At: %v\n" +
|
||||
"Started At: %v\n" +
|
||||
"Completed At: %v\n" +
|
||||
"ID: %v\n"+
|
||||
"App: %v\n"+
|
||||
"Route: %v\n"+
|
||||
"Created At: %v\n"+
|
||||
"Started At: %v\n"+
|
||||
"Completed At: %v\n"+
|
||||
"Status: %v\n",
|
||||
call.ID, call.AppName, call.Path, call.CreatedAt,
|
||||
call.StartedAt, call.CompletedAt, call.Status))
|
||||
@@ -55,17 +55,16 @@ func printCalls(calls []*models.Call) {
|
||||
}
|
||||
|
||||
func (call *callsCmd) get(ctx *cli.Context) error {
|
||||
call_id := ctx.Args().Get(0)
|
||||
callID := ctx.Args().Get(0)
|
||||
params := apicall.GetCallsCallParams{
|
||||
Call: call_id,
|
||||
Call: callID,
|
||||
Context: context.Background(),
|
||||
}
|
||||
resp, err := call.client.Call.GetCallsCall(¶ms)
|
||||
if err != nil {
|
||||
switch err.(type) {
|
||||
case *apicall.GetCallsCallNotFound:
|
||||
return fmt.Errorf("error: %v", err.(
|
||||
*apicall.GetCallsCallNotFound).Payload.Error.Message)
|
||||
return fmt.Errorf("error: %v", err.(*apicall.GetCallsCallNotFound).Payload.Error.Message)
|
||||
}
|
||||
return fmt.Errorf("unexpected error: %v", err)
|
||||
|
||||
@@ -85,8 +84,7 @@ func (call *callsCmd) list(ctx *cli.Context) error {
|
||||
if err != nil {
|
||||
switch err.(type) {
|
||||
case *apicall.GetCallsCallNotFound:
|
||||
return fmt.Errorf("error: %v", err.(
|
||||
*apicall.GetCallsCallNotFound).Payload.Error.Message)
|
||||
return fmt.Errorf("error: %v", err.(*apicall.GetCallsCallNotFound).Payload.Error.Message)
|
||||
}
|
||||
return fmt.Errorf("unexpected error: %v", err)
|
||||
|
||||
|
||||
11
fn/deploy.go
11
fn/deploy.go
@@ -129,9 +129,8 @@ func (p *deploycmd) deploy(c *cli.Context, funcFilePath string) error {
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if funcfile.Path == nil || *funcfile.Path == "" {
|
||||
dirName := "/" + path.Base(path.Dir(funcFilePath))
|
||||
funcfile.Path = &dirName
|
||||
if funcfile.Path == "" {
|
||||
funcfile.Path = "/" + path.Base(path.Dir(funcFilePath))
|
||||
}
|
||||
|
||||
if p.skippush {
|
||||
@@ -146,18 +145,18 @@ func (p *deploycmd) deploy(c *cli.Context, funcFilePath string) error {
|
||||
}
|
||||
|
||||
func (p *deploycmd) route(c *cli.Context, ff *funcfile) error {
|
||||
fmt.Printf("Updating route, setting %s -> %s...", ff.Path, ff.Name)
|
||||
fmt.Printf("Updating route %s using image %s...\n", ff.Path, ff.FullName())
|
||||
fmt.Printf("%+v\ntype: %v\n", ff, *ff.Type)
|
||||
if err := resetBasePath(p.Configuration); err != nil {
|
||||
return fmt.Errorf("error setting endpoint: %v", err)
|
||||
}
|
||||
|
||||
routesCmd := routesCmd{client: apiClient()}
|
||||
rt := &models.Route{}
|
||||
|
||||
if err := routeWithFuncFile(c, ff, rt); err != nil {
|
||||
return fmt.Errorf("error getting route with funcfile: %s", err)
|
||||
}
|
||||
return routesCmd.patchRoute(c, p.appName, *ff.Path, rt)
|
||||
return routesCmd.patchRoute(c, p.appName, ff.Path, rt)
|
||||
}
|
||||
|
||||
func expandEnvConfig(configs map[string]string) map[string]string {
|
||||
|
||||
@@ -45,7 +45,7 @@ type funcfile struct {
|
||||
Config map[string]string `yaml:"config,omitempty" json:"config,omitempty"`
|
||||
Build []string `yaml:"build,omitempty" json:"build,omitempty"`
|
||||
Tests []fftest `yaml:"tests,omitempty" json:"tests,omitempty"`
|
||||
Path *string `yaml:"path,omitempty" json:"path,omitempty"`
|
||||
Path string `yaml:"path,omitempty" json:"path,omitempty"`
|
||||
}
|
||||
|
||||
func (ff *funcfile) FullName() string {
|
||||
|
||||
13
fn/init.go
13
fn/init.go
@@ -50,6 +50,7 @@ type initFnCmd struct {
|
||||
entrypoint string
|
||||
cmd string
|
||||
format string
|
||||
typeS string
|
||||
}
|
||||
|
||||
func initFn() cli.Command {
|
||||
@@ -83,6 +84,12 @@ func initFn() cli.Command {
|
||||
Destination: &a.format,
|
||||
Value: "",
|
||||
},
|
||||
cli.StringFlag{
|
||||
Name: "type",
|
||||
Usage: "sync or async",
|
||||
Destination: &a.typeS,
|
||||
Value: "",
|
||||
},
|
||||
},
|
||||
}
|
||||
}
|
||||
@@ -124,10 +131,14 @@ func (a *initFnCmd) init(c *cli.Context) error {
|
||||
Entrypoint: a.entrypoint,
|
||||
Cmd: a.cmd,
|
||||
Format: ffmt,
|
||||
Type: &a.typeS,
|
||||
}
|
||||
if ff.Type != nil && *ff.Type == "" {
|
||||
ff.Type = nil
|
||||
}
|
||||
|
||||
_, path := appNamePath(ff.FullName())
|
||||
ff.Path = &path
|
||||
ff.Path = path
|
||||
|
||||
if err := encodeFuncfileYAML("func.yaml", ff); err != nil {
|
||||
return err
|
||||
|
||||
@@ -180,7 +180,7 @@ func createFunctionYaml(opts createImageOptions, functionName string) error {
|
||||
|
||||
funcDesc := &funcfile{
|
||||
Name: opts.Name,
|
||||
Path: &path,
|
||||
Path: path,
|
||||
Config: opts.Config,
|
||||
Version: "0.0.1",
|
||||
Runtime: &opts.Base,
|
||||
|
||||
@@ -296,9 +296,13 @@ func routeWithFuncFile(c *cli.Context, ff *funcfile, rt *fnmodels.Route) error {
|
||||
to := int32(ff.Timeout.Seconds())
|
||||
rt.Timeout = &to
|
||||
}
|
||||
if rt.Path == "" && ff.Path != nil {
|
||||
rt.Path = *ff.Path
|
||||
if rt.Path == "" && ff.Path != "" {
|
||||
rt.Path = ff.Path
|
||||
}
|
||||
if rt.Type == nil && ff.Type != nil && *ff.Type != "" {
|
||||
rt.Type = *ff.Type
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
|
||||
24
fn/run.go
24
fn/run.go
@@ -48,22 +48,28 @@ func (r *runCmd) run(c *cli.Context) error {
|
||||
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()
|
||||
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
|
||||
}
|
||||
image = ff.FullName()
|
||||
} else {
|
||||
ff = &funcfile{
|
||||
Name: image,
|
||||
}
|
||||
}
|
||||
|
||||
return runff(image, stdin(), os.Stdout, os.Stderr, c.String("method"), c.StringSlice("e"), c.StringSlice("link"))
|
||||
return runff(ff, stdin(), os.Stdout, os.Stderr, c.String("method"), c.StringSlice("e"), c.StringSlice("link"))
|
||||
}
|
||||
|
||||
func runff(image string, stdin io.Reader, stdout, stderr io.Writer, method string, envVars []string, links []string) error {
|
||||
// TODO: THIS SHOULD USE THE RUNNER DRIVERS FROM THE SERVER SO IT'S ESSENTIALLY THE SAME PROCESS (MINUS DATABASE AND ALL THAT)
|
||||
func runff(ff *funcfile, stdin io.Reader, stdout, stderr io.Writer, method string, envVars []string, links []string) error {
|
||||
sh := []string{"docker", "run", "--rm", "-i"}
|
||||
|
||||
var env []string // env for the shelled out docker run command
|
||||
@@ -80,7 +86,7 @@ func runff(image string, stdin io.Reader, stdout, stderr io.Writer, method strin
|
||||
runEnv = append(runEnv, kvEq("METHOD", method))
|
||||
runEnv = append(runEnv, kvEq("REQUEST_URL", "http://localhost:8080/myapp/hello"))
|
||||
runEnv = append(runEnv, kvEq("APP_NAME", "myapp"))
|
||||
runEnv = append(runEnv, kvEq("ROUTE", "/hello"))
|
||||
runEnv = append(runEnv, kvEq("ROUTE", "/hello")) // TODO: should we change this to PATH ?
|
||||
// add user defined envs
|
||||
runEnv = append(runEnv, envVars...)
|
||||
|
||||
@@ -97,7 +103,13 @@ func runff(image string, stdin io.Reader, stdout, stderr io.Writer, method strin
|
||||
sh = append(sh, "-e", e)
|
||||
}
|
||||
|
||||
sh = append(sh, image)
|
||||
if ff.Type != nil && *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?
|
||||
}
|
||||
|
||||
sh = append(sh, ff.FullName())
|
||||
cmd := exec.Command(sh[0], sh[1:]...)
|
||||
cmd.Stdin = stdin
|
||||
cmd.Stdout = stdout
|
||||
|
||||
@@ -68,8 +68,8 @@ func (t *testcmd) test(c *cli.Context) error {
|
||||
target := ff.FullName()
|
||||
runtest := runlocaltest
|
||||
if t.remote != "" {
|
||||
if ff.Path == nil || *ff.Path == "" {
|
||||
return errors.New("execution of tests on remote server demand that this function to have a `path`.")
|
||||
if ff.Path == "" {
|
||||
return errors.New("execution of tests on remote server demand that this function has a `path`.")
|
||||
}
|
||||
if err := resetBasePath(t.Configuration); err != nil {
|
||||
return fmt.Errorf("error setting endpoint: %v", err)
|
||||
@@ -80,7 +80,7 @@ func (t *testcmd) test(c *cli.Context) error {
|
||||
}
|
||||
|
||||
u, err := url.Parse("../")
|
||||
u.Path = path.Join(u.Path, "r", t.remote, *ff.Path)
|
||||
u.Path = path.Join(u.Path, "r", t.remote, ff.Path)
|
||||
target = baseURL.ResolveReference(u).String()
|
||||
runtest = runremotetest
|
||||
}
|
||||
@@ -134,7 +134,8 @@ func runlocaltest(target string, in, expectedOut, expectedErr *string, env map[s
|
||||
restrictedEnv = append(restrictedEnv, k)
|
||||
}
|
||||
|
||||
if err := runff(target, stdin, &stdout, &stderr, "", restrictedEnv, nil); err != nil {
|
||||
ff := &funcfile{Name: target}
|
||||
if err := runff(ff, stdin, &stdout, &stderr, "", restrictedEnv, nil); err != nil {
|
||||
return fmt.Errorf("%v\nstdout:%s\nstderr:%s\n", err, stdout.String(), stderr.String())
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user