mirror of
https://github.com/fnproject/fn.git
synced 2022-10-28 21:29:17 +03:00
Fix run env vars passed in via command line to test locally and updated docs to match.
This commit is contained in:
committed by
Reed Allman
parent
7b408468fa
commit
af918fdfe9
3
Makefile
3
Makefile
@@ -8,8 +8,7 @@ build:
|
||||
go build -o functions
|
||||
|
||||
test:
|
||||
go test -v $(shell go list ./... | grep -v vendor | grep -v examples | grep -v tool | grep -v fn)
|
||||
cd fn && $(MAKE) test
|
||||
./test.sh
|
||||
|
||||
test-datastore:
|
||||
cd api/datastore && go test -v ./...
|
||||
|
||||
@@ -162,8 +162,9 @@ func (s *Server) serve(ctx context.Context, c *gin.Context, appName string, foun
|
||||
var stdout bytes.Buffer // TODO: should limit the size of this, error if gets too big. akin to: https://golang.org/pkg/io/#LimitReader
|
||||
|
||||
envVars := map[string]string{
|
||||
"METHOD": c.Request.Method,
|
||||
"ROUTE": found.Path,
|
||||
"METHOD": c.Request.Method,
|
||||
"APP_NAME": appName,
|
||||
"ROUTE": found.Path,
|
||||
"REQUEST_URL": fmt.Sprintf("%v//%v%v", func() string {
|
||||
if c.Request.TLS == nil {
|
||||
return "http"
|
||||
|
||||
@@ -32,10 +32,12 @@ To read in the function body, just read from STDIN.
|
||||
You will also have access to a set of environment variables.
|
||||
|
||||
* REQUEST_URL - the full URL for the request
|
||||
* ROUTE - the matched route
|
||||
* METHOD - the HTTP method for the request
|
||||
* APP_NAME - the name of the application that matched this route, eg: `myapp`
|
||||
* ROUTE - the matched route, eg: `/hello`
|
||||
* METHOD - the HTTP method for the request, eg: `GET` or `POST`
|
||||
* HEADER_X - the HTTP headers that were set for this request. Replace X with the upper cased name of the header and replace dashes in the header with underscores.
|
||||
* X - any configuration values you've set for the Application or the Route. Replace X with the upper cased name of the config variable you set. Ex: `minio_secret=secret` will be exposed via MINIO_SECRET env var
|
||||
* X - any [configuration values](https://gitlab.oracledx.com/odx/functions/blob/master/fn/README.md#application-level-configuration) you've set
|
||||
for the Application or the Route. Replace X with the upper cased name of the config variable you set. Ex: `minio_secret=secret` will be exposed via MINIO_SECRET env var.
|
||||
|
||||
Warning: these may change before release.
|
||||
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
FROM funcy/ruby:dev
|
||||
|
||||
WORKDIR /function
|
||||
ADD Gemfile /function/
|
||||
ADD Gemfile* /function/
|
||||
RUN bundle install
|
||||
|
||||
ADD . /function/
|
||||
|
||||
@@ -1 +0,0 @@
|
||||
0.0.1
|
||||
@@ -1,5 +0,0 @@
|
||||
#!/bin/bash
|
||||
set -ex
|
||||
|
||||
# build image
|
||||
docker build -t username/func-checker .
|
||||
@@ -1,3 +1,4 @@
|
||||
name: username/func-checker
|
||||
build:
|
||||
- ./build.sh
|
||||
version: 0.0.1
|
||||
entrypoint: ruby function.rb
|
||||
runtime: ruby
|
||||
|
||||
@@ -1,6 +1,11 @@
|
||||
require 'json'
|
||||
require 'uri'
|
||||
|
||||
puts "Running checker..."
|
||||
|
||||
payload = STDIN.read
|
||||
puts "payload #{payload}"
|
||||
p ENV
|
||||
if payload != ""
|
||||
payload = JSON.parse(payload)
|
||||
|
||||
@@ -13,4 +18,24 @@ if payload != ""
|
||||
end
|
||||
end
|
||||
puts "all good"
|
||||
end
|
||||
end
|
||||
|
||||
# Also check for expected env vars: https://gitlab.oracledx.com/odx/functions/blob/master/docs/writing.md#inputs
|
||||
e = ENV["REQUEST_URL"]
|
||||
puts e
|
||||
uri = URI.parse(e)
|
||||
if !uri.scheme.start_with?('http')
|
||||
raise "invalid REQUEST_URL, does not start with http"
|
||||
end
|
||||
e = ENV["METHOD"]
|
||||
if !(e == "GET" || e == "POST" || e == "DELETE" || e == "PATCH" || e == "PUT")
|
||||
raise "Invalid METHOD: #{e}"
|
||||
end
|
||||
e = ENV["APP_NAME"]
|
||||
if e == nil || e == ''
|
||||
raise "No APP_NAME found"
|
||||
end
|
||||
e = ENV["ROUTE"]
|
||||
if e == nil || e == ''
|
||||
raise "No ROUTE found"
|
||||
end
|
||||
|
||||
@@ -1,9 +1,8 @@
|
||||
#!/bin/bash
|
||||
set -x
|
||||
|
||||
./build.sh
|
||||
set -ex
|
||||
|
||||
PAYLOAD='{"env_vars": {"FOO": "bar"}}'
|
||||
|
||||
# test it
|
||||
echo $PAYLOAD | docker run --rm -i -e TEST=1 -e FOO=bar username/func-checker
|
||||
: ${FN:="fn"}
|
||||
echo $PAYLOAD | $FN run -e FOO=bar
|
||||
|
||||
@@ -45,7 +45,6 @@ func (b *buildcmd) build(c *cli.Context) error {
|
||||
return err
|
||||
}
|
||||
|
||||
fmt.Fprintln(verbwriter, "building", fn)
|
||||
ff, err := buildfunc(verbwriter, fn)
|
||||
if err != nil {
|
||||
return err
|
||||
|
||||
@@ -96,7 +96,10 @@ func dockerbuild(verbwriter io.Writer, path string, ff *funcfile) error {
|
||||
}
|
||||
|
||||
fmt.Printf("Building image %v\n", ff.FullName())
|
||||
cmd := exec.Command("docker", "build", "-t", ff.FullName(), ".")
|
||||
cmd := exec.Command("docker", "build",
|
||||
"-t", ff.FullName(),
|
||||
"--build-arg", "HTTP_PROXY",
|
||||
".")
|
||||
cmd.Dir = dir
|
||||
cmd.Stderr = os.Stderr
|
||||
cmd.Stdout = os.Stdout
|
||||
@@ -206,6 +209,7 @@ func extractEnvConfig(configs []string) map[string]string {
|
||||
}
|
||||
|
||||
func dockerpush(ff *funcfile) error {
|
||||
fmt.Println("Pushing to docker registry...")
|
||||
cmd := exec.Command("docker", "push", ff.FullName())
|
||||
cmd.Stderr = os.Stderr
|
||||
cmd.Stdout = os.Stdout
|
||||
|
||||
@@ -4,12 +4,12 @@ import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"io"
|
||||
"log"
|
||||
"os"
|
||||
"path"
|
||||
"path/filepath"
|
||||
"time"
|
||||
|
||||
"github.com/Sirupsen/logrus"
|
||||
functions "github.com/iron-io/functions_go"
|
||||
"github.com/iron-io/functions_go/models"
|
||||
"github.com/urfave/cli"
|
||||
@@ -76,9 +76,8 @@ func (p *deploycmd) scan(c *cli.Context) error {
|
||||
var walked bool
|
||||
wd, err := os.Getwd()
|
||||
if err != nil {
|
||||
logrus.Fatalln("Couldn't get current directory:", err)
|
||||
log.Fatalln("Couldn't get working directory:", err)
|
||||
}
|
||||
// logrus.Infoln("wd:", wd)
|
||||
|
||||
err = filepath.Walk(wd, func(path string, info os.FileInfo, err error) error {
|
||||
if path != wd && info.IsDir() {
|
||||
@@ -134,7 +133,6 @@ func (p *deploycmd) deploy(c *cli.Context, funcFilePath string) error {
|
||||
dirName := "/" + path.Base(path.Dir(funcFilePath))
|
||||
funcfile.Path = &dirName
|
||||
}
|
||||
logrus.Infof("funcfile %+v", funcfile)
|
||||
|
||||
if p.skippush {
|
||||
return nil
|
||||
@@ -148,6 +146,7 @@ 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)
|
||||
if err := resetBasePath(p.Configuration); err != nil {
|
||||
return fmt.Errorf("error setting endpoint: %v", err)
|
||||
}
|
||||
|
||||
29
fn/run.go
29
fn/run.go
@@ -63,14 +63,11 @@ func (r *runCmd) run(c *cli.Context) error {
|
||||
return runff(image, 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, restrictedEnv []string, links []string) error {
|
||||
func runff(image string, stdin io.Reader, stdout, stderr io.Writer, method string, envVars []string, links []string) error {
|
||||
sh := []string{"docker", "run", "--rm", "-i"}
|
||||
|
||||
var env []string
|
||||
detectedEnv := os.Environ()
|
||||
if len(restrictedEnv) > 0 {
|
||||
detectedEnv = restrictedEnv
|
||||
}
|
||||
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 {
|
||||
@@ -79,13 +76,13 @@ func runff(image string, stdin io.Reader, stdout, stderr io.Writer, method strin
|
||||
method = "POST"
|
||||
}
|
||||
}
|
||||
sh = append(sh, "-e", kvEq("METHOD", method))
|
||||
|
||||
for _, e := range detectedEnv {
|
||||
shellvar, envvar := extractEnvVar(e)
|
||||
sh = append(sh, shellvar...)
|
||||
env = append(env, envvar)
|
||||
}
|
||||
// Add expected env vars that service will add
|
||||
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"))
|
||||
// add user defined envs
|
||||
runEnv = append(runEnv, envVars...)
|
||||
|
||||
for _, l := range links {
|
||||
sh = append(sh, "--link", l)
|
||||
@@ -96,12 +93,16 @@ func runff(image string, stdin io.Reader, stdout, stderr io.Writer, method strin
|
||||
env = append(env, fmt.Sprint(e, "=", os.Getenv(e)))
|
||||
}
|
||||
|
||||
for _, e := range runEnv {
|
||||
sh = append(sh, "-e", e)
|
||||
}
|
||||
|
||||
sh = append(sh, image)
|
||||
cmd := exec.Command(sh[0], sh[1:]...)
|
||||
cmd.Stdin = stdin
|
||||
cmd.Stdout = stdout
|
||||
cmd.Stderr = stderr
|
||||
cmd.Env = env
|
||||
// cmd.Env = env
|
||||
return cmd.Run()
|
||||
}
|
||||
|
||||
|
||||
14
test.sh
Executable file
14
test.sh
Executable file
@@ -0,0 +1,14 @@
|
||||
# Top level test script to start all other tests
|
||||
|
||||
set -ex
|
||||
|
||||
go test -v $(go list ./... | grep -v vendor | grep -v examples | grep -v tool | grep -v fn)
|
||||
cd fn && make build && make test
|
||||
# TODO: should we install fn here to use throughout?
|
||||
FN="$(pwd)/fn"
|
||||
cd ..
|
||||
|
||||
# TODO: Test a bunch of the examples using fn test when ready
|
||||
# checker tests env vars
|
||||
cd examples/checker
|
||||
./test.sh
|
||||
Reference in New Issue
Block a user