diff --git a/examples/Dockerfile.glide b/examples/Dockerfile.glide new file mode 100644 index 000000000..996a8b7cf --- /dev/null +++ b/examples/Dockerfile.glide @@ -0,0 +1,8 @@ +FROM iron/go:dev + +RUN mkdir -p /go/src/github.com/Masterminds +ENV GOPATH=/go +RUN cd /go/src/github.com/Masterminds && git clone https://github.com/Masterminds/glide.git && cd glide && go build +RUN cp /go/src/github.com/Masterminds/glide/glide /bin + +ENTRYPOINT ["glide"] \ No newline at end of file diff --git a/examples/blog/.gitignore b/examples/blog/.gitignore index e69de29bb..d11e5fd5a 100644 --- a/examples/blog/.gitignore +++ b/examples/blog/.gitignore @@ -0,0 +1 @@ +/func \ No newline at end of file diff --git a/examples/blog/Dockerfile b/examples/blog/Dockerfile index 43b5ae40d..f5f8ab200 100644 --- a/examples/blog/Dockerfile +++ b/examples/blog/Dockerfile @@ -1,8 +1,5 @@ -FROM iron/go:dev +FROM iron/go -ADD . $GOPATH/src/github.com/iron-io/functions/examples/blog -WORKDIR $GOPATH/src/github.com/iron-io/functions/examples/blog +ADD func . -RUN go get . - -ENTRYPOINT ["go", "run", "main.go"] \ No newline at end of file +ENTRYPOINT ["./func"] \ No newline at end of file diff --git a/examples/blog/README.md b/examples/blog/README.md index 6e54f5be4..47dc1a23f 100644 --- a/examples/blog/README.md +++ b/examples/blog/README.md @@ -1,8 +1,11 @@ # Blog API Example +A simple serverless blog API + ## Requirements - Remote MongoDB instance (for example heroku) +- Running IronFunctions API ## Development @@ -13,18 +16,19 @@ USERNAME=YOUR_DOCKER_HUB_USERNAME # build it -docker build -t $USERNAME/functions-blog . +docker run --rm -v "$PWD":/go/src/github.com/treeder/hello -w /go/src/github.com/treeder/hello iron/go:dev go build -o function +docker build -t $USERNAME/func-blog . ``` -### Publishing it +### Publishing to DockerHub ``` # tagging docker run --rm -v "$PWD":/app treeder/bump patch -docker tag $USERNAME/functions-blog:latest $USERNAME/functions-blog:`cat VERSION` +docker tag $USERNAME/func-blog:latest $USERNAME/func-blog:`cat VERSION` # pushing to docker hub -docker push $USERNAME/functions-blog +docker push $USERNAME/func-blog ``` ## Running it on IronFunctions @@ -43,7 +47,13 @@ FUNCAPI=YOUR_FUNCTIONS_ADDRESS MONGODB=YOUR_MONGODB_ADDRESS ``` -### Creating our blog application in your IronFunctions +### Testing image + +``` +./test.sh +``` + +### Running with IronFunctions With this command we are going to create an application with name `blog` and also defining the app configuration `DB`. @@ -65,7 +75,7 @@ Now, we can create our blog routes: ``` curl -X POST --data '{ "route": { - "image": "'$USERNAME'/functions-blog", + "image": "'$USERNAME'/func-blog", "path": "/posts" } }' http://$FUNCAPI/v1/apps/blog/routes @@ -74,7 +84,7 @@ curl -X POST --data '{ ``` curl -X POST --data '{ "route": { - "image": "'$USERNAME'/functions-blog", + "image": "'$USERNAME'/func-blog", "path": "/posts/:id" } }' http://$FUNCAPI/v1/apps/blog/routes @@ -83,15 +93,15 @@ curl -X POST --data '{ ``` curl -X POST --data '{ "route": { - "image": "'$USERNAME'/functions-blog", + "image": "'$USERNAME'/func-blog", "path": "/token" } }' http://$FUNCAPI/v1/apps/blog/routes ``` -### Testing your Blog +#### Testing function -Now that we created our IronFunction route, lets test our routes +Now that we created our IronFunction route, let's test our routes ``` curl -X POST http://$FUNCAPI/r/blog/posts @@ -99,9 +109,9 @@ curl -X POST http://$FUNCAPI/r/blog/posts This command should return `{"error":"Invalid authentication"}` because we aren't sending any token. -## Authentication +#### Authentication -### Creating a blog user +##### Creating a blog user First let's create our blog user. In this example an user `test` with password `test`. @@ -109,7 +119,7 @@ First let's create our blog user. In this example an user `test` with password ` docker run --rm -e CONFIG_DB=$MONGODB -e NEWUSER='{ "username": "test", "password": "test" }' $USERNAME/functions-blog ``` -### Getting authorization token +##### Getting authorization token Now, to get authorized to post in our Blog API endpoints we must request a new token with a valid user. @@ -129,6 +139,6 @@ Let's save that token in the environment BLOG_TOKEN=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJleHAiOiIyMDE2LTA5LTAxVDAwOjQzOjMxLjQwNjY5NTIxNy0wMzowMCIsInVzZXIiOiJ0ZXN0In0.aPKdH3QPauutFsFbSdQyF6q1hqTAas_BCbSYi5mFiSU ``` -### Posting in your blog +##### Posting in your blog -curl -X POST --header "Authentication: JWT $BLOG_TOKEN" --data '{ "title": "My New Post", "body": "Hello world!" }' http://$FUNCAPI/r/blog/posts \ No newline at end of file +curl -X POST --header "Authorization: Bearer $BLOG_TOKEN" --data '{ "title": "My New Post", "body": "Hello world!", "user": "test" }' http://$FUNCAPI/r/blog/posts \ No newline at end of file diff --git a/examples/blog/build.sh b/examples/blog/build.sh new file mode 100755 index 000000000..198caaf19 --- /dev/null +++ b/examples/blog/build.sh @@ -0,0 +1,12 @@ +#!/bin/bash +set -ex + +FUNCPKG=$(pwd | sed "s|$GOPATH/src/||") + +# glide image to install dependencies +../build-glide.sh +docker run --rm -v "$PWD":/go/src/$FUNCPKG -w /go/src/$FUNCPKG glide up + +# build image +docker run --rm -v "$PWD":/go/src/$FUNCPKG -w /go/src/$FUNCPKG iron/go:dev go build -o func +docker build -t iron/func-blog . \ No newline at end of file diff --git a/examples/blog/main.go b/examples/blog/function.go similarity index 100% rename from examples/blog/main.go rename to examples/blog/function.go diff --git a/examples/blog/functions.yml b/examples/blog/functions.yml new file mode 100644 index 000000000..5dd678f12 --- /dev/null +++ b/examples/blog/functions.yml @@ -0,0 +1,3 @@ +image: iron/func-blog +build: + - ./build.sh \ No newline at end of file diff --git a/examples/blog/glide.lock b/examples/blog/glide.lock new file mode 100644 index 000000000..3061b12b4 --- /dev/null +++ b/examples/blog/glide.lock @@ -0,0 +1,18 @@ +hash: 5ed6b2d92ab59777802bcfbd57690d5cfb128160bca6cfa799abeb28a8baad4d +updated: 2016-11-01T02:15:49.751792317Z +imports: +- name: github.com/dgrijalva/jwt-go + version: d2709f9f1f31ebcda9651b03077758c1f3a0018c +- name: golang.org/x/crypto + version: 9477e0b78b9ac3d0b03822fd95422e2fe07627cd + subpackages: + - bcrypt + - blowfish +- name: gopkg.in/mgo.v2 + version: 3f83fa5005286a7fe593b055f0d7771a7dce4655 + subpackages: + - bson + - internal/json + - internal/sasl + - internal/scram +testImports: [] diff --git a/examples/blog/glide.yaml b/examples/blog/glide.yaml new file mode 100644 index 000000000..e934de875 --- /dev/null +++ b/examples/blog/glide.yaml @@ -0,0 +1,10 @@ +package: github.com/iron-io/functions/examples/blog +import: +- package: github.com/dgrijalva/jwt-go + version: ^3.0.0 +- package: golang.org/x/crypto + subpackages: + - bcrypt +- package: gopkg.in/mgo.v2 + subpackages: + - bson diff --git a/examples/blog/routes/post_create.go b/examples/blog/routes/post_create.go index 3d72861b8..dca60a148 100644 --- a/examples/blog/routes/post_create.go +++ b/examples/blog/routes/post_create.go @@ -12,13 +12,12 @@ import ( func HandlePostCreate(db *database.Database, auth map[string]interface{}) { var post *models.Post - err := json.Unmarshal([]byte(os.Getenv("PAYLOAD")), &post) - if err != nil { - fmt.Println("Invalid post") + if err := json.NewDecoder(os.Stdin).Decode(&post); err != nil { + fmt.Printf("Couldn't decode post JSON: %v\n", err) return } - post, err = db.SavePost(post) + post, err := db.SavePost(post) if err != nil { fmt.Println("Couldn't save that post") return diff --git a/examples/blog/routes/server.go b/examples/blog/routes/server.go index 26770ed86..c202eb287 100644 --- a/examples/blog/routes/server.go +++ b/examples/blog/routes/server.go @@ -13,8 +13,6 @@ import ( "golang.org/x/crypto/bcrypt" ) -// import "github.com/iron-io/functions/examples/blog/database" - var jwtSignKey = []byte("mysecretblog") type Response map[string]interface{} @@ -32,9 +30,9 @@ func SendError(err interface{}) { func HandleToken(db *database.Database) { var login *models.User - err := json.Unmarshal([]byte(os.Getenv("PAYLOAD")), &login) - if err != nil { - fmt.Println("Missing username and password") + + if err := json.NewDecoder(os.Stdin).Decode(&login); err != nil { + fmt.Printf("Couldn't decode login JSON: %v\n", err) return } diff --git a/examples/blog/test.sh b/examples/blog/test.sh new file mode 100755 index 000000000..ed95a4b07 --- /dev/null +++ b/examples/blog/test.sh @@ -0,0 +1,16 @@ +#!/bin/bash +set -x + +./build.sh + +# test it +docker stop test-mongo-func +docker rm test-mongo-func + +docker run -p 27017:27017 --name test-mongo-func -d mongo + +echo '{ "title": "My New Post", "body": "Hello world!", "user": "test" }' | docker run --rm -i -e METHOD=POST -e ROUTE=/posts -e CONFIG_DB=mongo:27017 --link test-mongo-func:mongo -e TEST=1 iron/func-blog +docker run --rm -i -e METHOD=GET -e ROUTE=/posts -e CONFIG_DB=mongo:27017 --link test-mongo-func:mongo -e TEST=1 iron/func-blog + +docker stop test-mongo-func +docker rm test-mongo-func \ No newline at end of file diff --git a/examples/build-glide.sh b/examples/build-glide.sh new file mode 100755 index 000000000..6b9965ba8 --- /dev/null +++ b/examples/build-glide.sh @@ -0,0 +1,5 @@ +EXAMPLE=`pwd` + +cd .. +docker build -f Dockerfile.glide -t glide . +cd $EXAMPLE \ No newline at end of file diff --git a/examples/caddy-lb/VERSION b/examples/caddy-lb/VERSION new file mode 100644 index 000000000..8a9ecc2ea --- /dev/null +++ b/examples/caddy-lb/VERSION @@ -0,0 +1 @@ +0.0.1 \ No newline at end of file diff --git a/examples/caddy-lb/build.sh b/examples/caddy-lb/build.sh new file mode 100755 index 000000000..35c18663d --- /dev/null +++ b/examples/caddy-lb/build.sh @@ -0,0 +1,4 @@ +#!/bin/bash +set -ex + +docker-compose up \ No newline at end of file diff --git a/examples/caddy-lb/functions.yml b/examples/caddy-lb/functions.yml new file mode 100644 index 000000000..953a3b564 --- /dev/null +++ b/examples/caddy-lb/functions.yml @@ -0,0 +1,3 @@ +image: iron/func-caddy-lb +build: + - ./build.sh \ No newline at end of file diff --git a/examples/caddy-lb/test.sh b/examples/caddy-lb/test.sh new file mode 100755 index 000000000..8dabc7407 --- /dev/null +++ b/examples/caddy-lb/test.sh @@ -0,0 +1,3 @@ +#!/bin/bash +set -x + diff --git a/examples/checker/Dockerfile b/examples/checker/Dockerfile index a5d9a5a87..44987d673 100644 --- a/examples/checker/Dockerfile +++ b/examples/checker/Dockerfile @@ -1,9 +1,9 @@ FROM iron/ruby:dev -WORKDIR /worker -ADD Gemfile* /worker/ +WORKDIR /function +ADD Gemfile /function/ RUN bundle install -ADD . /worker/ +ADD . /function/ -ENTRYPOINT ["ruby", "checker.rb"] +ENTRYPOINT ["ruby", "function.rb"] diff --git a/examples/checker/Gemfile.lock b/examples/checker/Gemfile.lock deleted file mode 100644 index 28ab22fe5..000000000 --- a/examples/checker/Gemfile.lock +++ /dev/null @@ -1,13 +0,0 @@ -GEM - remote: https://rubygems.org/ - specs: - json (1.8.3) - -PLATFORMS - ruby - -DEPENDENCIES - json (> 1.8.2) - -BUNDLED WITH - 1.10.5 diff --git a/examples/checker/README.md b/examples/checker/README.md index dd1672638..8c0caeab3 100644 --- a/examples/checker/README.md +++ b/examples/checker/README.md @@ -1,22 +1,78 @@ -This is a worker that we can use to check inputs to the job, such as env vars. +# Environment Checker Function Image -Pass in checks via the payload: +This images compares the payload info with the header. -```json -{ - "env_vars": { - "foo": "bar" - } -} -``` +## Requirements -That will check that there is an env var called foo with the value bar passed to the task. +- IronFunctions API -## Building Image +## Development -Install [dj](https://github.com/treeder/dj/), then run: +### Building image locally ``` -docker build -t iron/checker . -docker run -e 'PAYLOAD={"env_vars": {"FOO": "bar"}}' -e "FOO=bar" iron/checker +# SET BELOW TO YOUR DOCKER HUB USERNAME +USERNAME=YOUR_DOCKER_HUB_USERNAME + +# build it +./build.sh ``` + +### Publishing to DockerHub + +``` +# tagging +docker run --rm -v "$PWD":/app treeder/bump patch +docker tag $USERNAME/func-checker:latest $USERNAME/func-checker:`cat VERSION` + +# pushing to docker hub +docker push $USERNAME/func-checker +``` + +### Testing image + +``` +./test.sh +``` + +## Running it on IronFunctions + +### Let's define some environment variables + +``` +# Set your Function server address +# Eg. 127.0.0.1:8080 +FUNCAPI=YOUR_FUNCTIONS_ADDRESS +``` + +### Running with IronFunctions + +With this command we are going to create an application with name `checker`. + +``` +curl -X POST --data '{ + "app": { + "name": "checker", + } +}' http://$FUNCAPI/v1/apps +``` + +Now, we can create our route + +``` +curl -X POST --data '{ + "route": { + "image": "'$USERNAME'/func-checker", + "path": "/check", + "config": { "TEST": "1" } + } +}' http://$FUNCAPI/v1/apps/checker/routes +``` + +#### Testing function + +Now that we created our IronFunction route, let's test our new route + +``` +curl -X POST --data '{ "env_vars": { "config_test": "1" } }' http://$FUNCAPI/r/checker/check +``` \ No newline at end of file diff --git a/examples/checker/VERSION b/examples/checker/VERSION new file mode 100644 index 000000000..8a9ecc2ea --- /dev/null +++ b/examples/checker/VERSION @@ -0,0 +1 @@ +0.0.1 \ No newline at end of file diff --git a/examples/checker/build.sh b/examples/checker/build.sh new file mode 100755 index 000000000..5d9cf9a94 --- /dev/null +++ b/examples/checker/build.sh @@ -0,0 +1,5 @@ +#!/bin/bash +set -ex + +# build image +docker build -t iron/func-checker . \ No newline at end of file diff --git a/examples/checker/checker.rb b/examples/checker/checker.rb deleted file mode 100644 index 73c2c61b5..000000000 --- a/examples/checker/checker.rb +++ /dev/null @@ -1,13 +0,0 @@ -require 'json' - -payload = JSON.parse(ENV['PAYLOAD']) - -# payload contains checks -if payload["env_vars"] - payload["env_vars"].each do |k,v| - if ENV[k] != v - raise "Env var #{k} does not match" - end - end -end -puts "all good" diff --git a/examples/checker/function.rb b/examples/checker/function.rb new file mode 100644 index 000000000..5b8e0e14e --- /dev/null +++ b/examples/checker/function.rb @@ -0,0 +1,16 @@ +require 'json' + +payload = STDIN.read +if payload != "" + payload = JSON.parse(payload) + + # payload contains checks + if payload["env_vars"] + payload["env_vars"].each do |k,v| + if ENV[k] != v + raise "Env var #{k} does not match" + end + end + end + puts "all good" +end \ No newline at end of file diff --git a/examples/checker/functions.yml b/examples/checker/functions.yml new file mode 100644 index 000000000..6ba395003 --- /dev/null +++ b/examples/checker/functions.yml @@ -0,0 +1,3 @@ +image: iron/func-checker +build: + - ./build.sh \ No newline at end of file diff --git a/examples/checker/test.sh b/examples/checker/test.sh new file mode 100755 index 000000000..a3c9ff1c7 --- /dev/null +++ b/examples/checker/test.sh @@ -0,0 +1,9 @@ +#!/bin/bash +set -x + +./build.sh + +PAYLOAD='{"env_vars": {"FOO": "bar"}}' + +# test it +echo $PAYLOAD | docker run --rm -i -e TEST=1 -e FOO=bar iron/func-checker \ No newline at end of file diff --git a/examples/echo/Dockerfile b/examples/echo/Dockerfile index 75dcd174f..e60bd3b48 100644 --- a/examples/echo/Dockerfile +++ b/examples/echo/Dockerfile @@ -1,9 +1,9 @@ FROM iron/ruby:dev -WORKDIR /worker -ADD Gemfile* /worker/ +WORKDIR /function +ADD Gemfile /function/ RUN bundle install -ADD . /worker/ +ADD . /function/ -ENTRYPOINT ["ruby", "echo.rb"] +ENTRYPOINT ["ruby", "function.rb"] \ No newline at end of file diff --git a/examples/echo/Gemfile b/examples/echo/Gemfile index 651ef62b4..e266a7a9c 100644 --- a/examples/echo/Gemfile +++ b/examples/echo/Gemfile @@ -1,4 +1,3 @@ source 'https://rubygems.org' -gem 'json', '> 1.8.2' -gem 'iron_worker', '>= 3.0.0' +gem 'json', '> 1.8.2' \ No newline at end of file diff --git a/examples/echo/Gemfile.lock b/examples/echo/Gemfile.lock deleted file mode 100644 index b3c8c257d..000000000 --- a/examples/echo/Gemfile.lock +++ /dev/null @@ -1,25 +0,0 @@ -GEM - remote: https://rubygems.org/ - specs: - iron_core (1.0.9) - rest (>= 3.0.4) - iron_worker (3.2.2) - iron_core (>= 0.5.1) - json (> 1.8.1) - rest (>= 3.0.6) - json (1.8.3) - net-http-persistent (2.9.4) - netrc (0.11.0) - rest (3.0.6) - net-http-persistent (>= 2.9.1) - netrc - -PLATFORMS - ruby - -DEPENDENCIES - iron_worker (>= 3.0.0) - json (> 1.8.2) - -BUNDLED WITH - 1.10.5 diff --git a/examples/echo/README.md b/examples/echo/README.md index 5b1c0a265..67e6f9f9a 100644 --- a/examples/echo/README.md +++ b/examples/echo/README.md @@ -1,24 +1,77 @@ -This is a worker that just echoes the "input" param in the payload. +# Echo Function Image -eg: +This images compares the payload info with the header. -This input: +## Requirements -```json -{ - "input": "Yo dawg" -} -``` +- IronFunctions API -Will output: +## Development + +### Building image locally ``` -Yo dawg +# SET BELOW TO YOUR DOCKER HUB USERNAME +USERNAME=YOUR_DOCKER_HUB_USERNAME + +# build it +./build.sh ``` -## Building Image +### Publishing to DockerHub ``` -docker build -t iron/echo . -docker run -e 'PAYLOAD={"input": "yoooo"}' iron/echo +# tagging +docker run --rm -v "$PWD":/app treeder/bump patch +docker tag $USERNAME/func-echo:latest $USERNAME/func-echo:`cat VERSION` + +# pushing to docker hub +docker push $USERNAME/func-echo ``` + +### Testing image + +``` +./test.sh +``` + +## Running it on IronFunctions + +### Let's define some environment variables + +``` +# Set your Function server address +# Eg. 127.0.0.1:8080 +FUNCAPI=YOUR_FUNCTIONS_ADDRESS +``` + +### Running with IronFunctions + +With this command we are going to create an application with name `echo`. + +``` +curl -X POST --data '{ + "app": { + "name": "echo", + } +}' http://$FUNCAPI/v1/apps +``` + +Now, we can create our route + +``` +curl -X POST --data '{ + "route": { + "image": "'$USERNAME'/func-echo", + "path": "/echo", + } +}' http://$FUNCAPI/v1/apps/echo/routes +``` + +#### Testing function + +Now that we created our IronFunction route, let's test our new route + +``` +curl -X POST --data '{"input": "yoooo"}' http://$FUNCAPI/r/echo/echo +``` \ No newline at end of file diff --git a/examples/echo/VERSION b/examples/echo/VERSION new file mode 100644 index 000000000..8a9ecc2ea --- /dev/null +++ b/examples/echo/VERSION @@ -0,0 +1 @@ +0.0.1 \ No newline at end of file diff --git a/examples/echo/build.sh b/examples/echo/build.sh new file mode 100755 index 000000000..8b35ad511 --- /dev/null +++ b/examples/echo/build.sh @@ -0,0 +1,5 @@ +#!/bin/bash +set -ex + +# build image +docker build -t iron/func-echo . \ No newline at end of file diff --git a/examples/echo/echo.rb b/examples/echo/echo.rb deleted file mode 100644 index c221ebf0e..000000000 --- a/examples/echo/echo.rb +++ /dev/null @@ -1,7 +0,0 @@ -require 'iron_worker' - -# p IronWorker.payload -# puts "#{IronWorker.payload["input"]}" - -payload = JSON.parse(ENV['PAYLOAD']) -puts payload['input'] diff --git a/examples/echo/function.rb b/examples/echo/function.rb new file mode 100644 index 000000000..aa49581c6 --- /dev/null +++ b/examples/echo/function.rb @@ -0,0 +1,11 @@ +require 'json' + +payload = STDIN.read +if payload != "" + payload = JSON.parse(payload) + + # payload contains checks + if payload["input"] + puts payload["input"] + end +end diff --git a/examples/echo/functions.yml b/examples/echo/functions.yml new file mode 100644 index 000000000..de5fcfac4 --- /dev/null +++ b/examples/echo/functions.yml @@ -0,0 +1,3 @@ +image: iron/func-echo +build: + - ./build.sh \ No newline at end of file diff --git a/examples/echo/test.sh b/examples/echo/test.sh new file mode 100755 index 000000000..ba4e41231 --- /dev/null +++ b/examples/echo/test.sh @@ -0,0 +1,9 @@ +#!/bin/bash +set -x + +./build.sh + +PAYLOAD='{"input": "yoooo"}' + +# test it +echo $PAYLOAD | docker run --rm -i -e TEST=1 iron/func-echo \ No newline at end of file diff --git a/examples/error/Gemfile b/examples/error/Gemfile index 651ef62b4..e05a90012 100644 --- a/examples/error/Gemfile +++ b/examples/error/Gemfile @@ -1,4 +1,3 @@ source 'https://rubygems.org' gem 'json', '> 1.8.2' -gem 'iron_worker', '>= 3.0.0' diff --git a/examples/error/Gemfile.lock b/examples/error/Gemfile.lock deleted file mode 100644 index b3c8c257d..000000000 --- a/examples/error/Gemfile.lock +++ /dev/null @@ -1,25 +0,0 @@ -GEM - remote: https://rubygems.org/ - specs: - iron_core (1.0.9) - rest (>= 3.0.4) - iron_worker (3.2.2) - iron_core (>= 0.5.1) - json (> 1.8.1) - rest (>= 3.0.6) - json (1.8.3) - net-http-persistent (2.9.4) - netrc (0.11.0) - rest (3.0.6) - net-http-persistent (>= 2.9.1) - netrc - -PLATFORMS - ruby - -DEPENDENCIES - iron_worker (>= 3.0.0) - json (> 1.8.2) - -BUNDLED WITH - 1.10.5 diff --git a/examples/error/README.md b/examples/error/README.md index f954cbcf7..3e210a9cf 100644 --- a/examples/error/README.md +++ b/examples/error/README.md @@ -1,9 +1,77 @@ -This is a worker that errors out (ie: exits with non-zero exit code). +# Error Function Image +This images compares the payload info with the header. -## Building Image +## Requirements + +- IronFunctions API + +## Development + +### Building image locally ``` -docker build -t iron/error . -docker run -e 'PAYLOAD={"input": "yoooo"}' iron/error +# SET BELOW TO YOUR DOCKER HUB USERNAME +USERNAME=YOUR_DOCKER_HUB_USERNAME + +# build it +./build.sh ``` + +### Publishing to DockerHub + +``` +# tagging +docker run --rm -v "$PWD":/app treeder/bump patch +docker tag $USERNAME/func-error:latest $USERNAME/func-error:`cat VERSION` + +# pushing to docker hub +docker push $USERNAME/func-error +``` + +### Testing image + +``` +./test.sh +``` + +## Running it on IronFunctions + +### Let's define some environment variables + +``` +# Set your Function server address +# Eg. 127.0.0.1:8080 +FUNCAPI=YOUR_FUNCTIONS_ADDRESS +``` + +### Running with IronFunctions + +With this command we are going to create an application with name `error`. + +``` +curl -X POST --data '{ + "app": { + "name": "error", + } +}' http://$FUNCAPI/v1/apps +``` + +Now, we can create our route + +``` +curl -X POST --data '{ + "route": { + "image": "'$USERNAME'/func-error", + "path": "/error", + } +}' http://$FUNCAPI/v1/apps/error/routes +``` + +#### Testing function + +Now that we created our IronFunction route, let's test our new route + +``` +curl -X POST --data '{"input": "yoooo"}' http://$FUNCAPI/r/error/error +``` \ No newline at end of file diff --git a/examples/error/VERSION b/examples/error/VERSION new file mode 100644 index 000000000..8a9ecc2ea --- /dev/null +++ b/examples/error/VERSION @@ -0,0 +1 @@ +0.0.1 \ No newline at end of file diff --git a/examples/error/build.sh b/examples/error/build.sh new file mode 100755 index 000000000..c93fa16b0 --- /dev/null +++ b/examples/error/build.sh @@ -0,0 +1,4 @@ +#!/bin/bash +set -ex + +docker build -t iron/func-error . \ No newline at end of file diff --git a/examples/error/error.rb b/examples/error/error.rb deleted file mode 100644 index 1ffd9c4b6..000000000 --- a/examples/error/error.rb +++ /dev/null @@ -1,8 +0,0 @@ -require 'iron_worker' - -if ENV['PAYLOAD'] && ENV['PAYLOAD'] != "" - payload = JSON.parse(ENV['PAYLOAD']) - puts payload['input'] -end - -raise "Something went terribly wrong!" diff --git a/examples/error/function.rb b/examples/error/function.rb new file mode 100644 index 000000000..a86263bda --- /dev/null +++ b/examples/error/function.rb @@ -0,0 +1,13 @@ +require 'json' + +payload = STDIN.read +if payload != "" + payload = JSON.parse(payload) + + # payload contains checks + if payload["input"] + puts payload["input"] + end +end + +raise "Something went terribly wrong!" diff --git a/examples/error/functions.yml b/examples/error/functions.yml new file mode 100644 index 000000000..34fa569f8 --- /dev/null +++ b/examples/error/functions.yml @@ -0,0 +1,3 @@ +image: iron/func-error +build: + - ./build.sh \ No newline at end of file diff --git a/examples/error/test.sh b/examples/error/test.sh new file mode 100755 index 000000000..133cf550f --- /dev/null +++ b/examples/error/test.sh @@ -0,0 +1,9 @@ +#!/bin/bash +set -x + +./build.sh + +PAYLOAD='{"input": "yoooo"}' + +# test it +echo $PAYLOAD | docker run --rm -i -e TEST=1 iron/func-error \ No newline at end of file diff --git a/examples/hello-go/.gitignore b/examples/hello-go/.gitignore index e92569d01..f16ae5300 100644 --- a/examples/hello-go/.gitignore +++ b/examples/hello-go/.gitignore @@ -1 +1,2 @@ -/hello +/func +vendor \ No newline at end of file diff --git a/examples/hello-go/Dockerfile b/examples/hello-go/Dockerfile index b1c5ce864..f5f8ab200 100644 --- a/examples/hello-go/Dockerfile +++ b/examples/hello-go/Dockerfile @@ -1,6 +1,5 @@ FROM iron/go -WORKDIR /function -ADD hello . +ADD func . -ENTRYPOINT ["./hello"] +ENTRYPOINT ["./func"] \ No newline at end of file diff --git a/examples/hello-go/README.md b/examples/hello-go/README.md index 8baccb13c..e539afc66 100644 --- a/examples/hello-go/README.md +++ b/examples/hello-go/README.md @@ -1,4 +1,77 @@ -set -ex +# Hello Function Image (Go Language) -docker run --rm -v "$PWD":/go/src/github.com/treeder/hello -w /go/src/github.com/treeder/hello iron/go:dev go build -o hello -docker build -t iron/hello . +This images compares the payload info with the header. + +## Requirements + +- IronFunctions API + +## Development + +### Building image locally + +``` +# SET BELOW TO YOUR DOCKER HUB USERNAME +USERNAME=YOUR_DOCKER_HUB_USERNAME + +# build it +./build.sh +``` + +### Publishing to DockerHub + +``` +# tagging +docker run --rm -v "$PWD":/app treeder/bump patch +docker tag $USERNAME/func-hello-go:latest $USERNAME/func-hello-go:`cat VERSION` + +# pushing to docker hub +docker push $USERNAME/func-hello-go +``` + +### Testing image + +``` +./test.sh +``` + +## Running it on IronFunctions + +### Let's define some environment variables + +``` +# Set your Function server address +# Eg. 127.0.0.1:8080 +FUNCAPI=YOUR_FUNCTIONS_ADDRESS +``` + +### Running with IronFunctions + +With this command we are going to create an application with name `hello`. + +``` +curl -X POST --data '{ + "app": { + "name": "hello", + } +}' http://$FUNCAPI/v1/apps +``` + +Now, we can create our route + +``` +curl -X POST --data '{ + "route": { + "image": "'$USERNAME'/func-hello-go", + "path": "/hello", + } +}' http://$FUNCAPI/v1/apps/hello/routes +``` + +#### Testing function + +Now that we created our IronFunction route, let's test our new route + +``` +curl -X POST --data '{"name": "Johnny"}' http://$FUNCAPI/r/hello/hello +``` \ No newline at end of file diff --git a/examples/hello-go/VERSION b/examples/hello-go/VERSION index 7bcd0e361..8a9ecc2ea 100644 --- a/examples/hello-go/VERSION +++ b/examples/hello-go/VERSION @@ -1 +1 @@ -0.0.2 \ No newline at end of file +0.0.1 \ No newline at end of file diff --git a/examples/hello-go/build.sh b/examples/hello-go/build.sh index 3a09a6fd4..61bf638d3 100755 --- a/examples/hello-go/build.sh +++ b/examples/hello-go/build.sh @@ -1,8 +1,8 @@ -set -ex +#!/bin/bash +set -ex -USERNAME=iron -IMAGE=hello +FUNCPKG=$(pwd | sed "s|$GOPATH/src/||") -# build it -docker run --rm -v "$PWD":/go/src/github.com/iron/hello -w /go/src/github.com/iron/hello iron/go:dev go build -o hello -docker build -t $USERNAME/$IMAGE . +# build image +docker run --rm -v "$PWD":/go/src/$FUNCPKG -w /go/src/$FUNCPKG iron/go:dev go build -o func +docker build -t iron/func-hello-go . \ No newline at end of file diff --git a/examples/hello-go/hello.go b/examples/hello-go/function.go similarity index 100% rename from examples/hello-go/hello.go rename to examples/hello-go/function.go diff --git a/examples/hello-go/functions.yml b/examples/hello-go/functions.yml new file mode 100644 index 000000000..28704e1cd --- /dev/null +++ b/examples/hello-go/functions.yml @@ -0,0 +1,3 @@ +image: iron/func-hello-gp +build: + - ./build.sh \ No newline at end of file diff --git a/examples/hello-go/release.sh b/examples/hello-go/release.sh deleted file mode 100755 index 1cef83f1c..000000000 --- a/examples/hello-go/release.sh +++ /dev/null @@ -1,17 +0,0 @@ -set -ex - -USERNAME=iron -IMAGE=hello - -# build it -./build.sh -# test it -echo '{"name":"Johnny"}' | docker run --rm -i $USERNAME/hello -# tag it -docker run --rm -v "$PWD":/app treeder/bump patch -version=`cat VERSION` -echo "version: $version" -docker tag $USERNAME/$IMAGE:latest $USERNAME/$IMAGE:$version -# push it -docker push $USERNAME/$IMAGE:latest -docker push $USERNAME/$IMAGE:$version diff --git a/examples/hello-go/test.sh b/examples/hello-go/test.sh new file mode 100755 index 000000000..7f3a0e581 --- /dev/null +++ b/examples/hello-go/test.sh @@ -0,0 +1,9 @@ +#!/bin/bash +set -x + +./build.sh + +PAYLOAD='{"name":"Johnny"}' + +# test it +echo $PAYLOAD | docker run --rm -i iron/func-hello-go \ No newline at end of file diff --git a/examples/hello-ruby/Dockerfile b/examples/hello-ruby/Dockerfile index efa975b05..f6c10018d 100644 --- a/examples/hello-ruby/Dockerfile +++ b/examples/hello-ruby/Dockerfile @@ -1,9 +1,9 @@ FROM iron/ruby:dev -WORKDIR /worker -ADD Gemfile* /worker/ +WORKDIR /function +ADD Gemfile* /function/ RUN bundle install -ADD . /worker/ +ADD . /function/ -ENTRYPOINT ["ruby", "hello.rb"] +ENTRYPOINT ["ruby", "function.rb"] diff --git a/examples/hello-ruby/Gemfile.lock b/examples/hello-ruby/Gemfile.lock deleted file mode 100644 index 28ab22fe5..000000000 --- a/examples/hello-ruby/Gemfile.lock +++ /dev/null @@ -1,13 +0,0 @@ -GEM - remote: https://rubygems.org/ - specs: - json (1.8.3) - -PLATFORMS - ruby - -DEPENDENCIES - json (> 1.8.2) - -BUNDLED WITH - 1.10.5 diff --git a/examples/hello-ruby/README.md b/examples/hello-ruby/README.md index c486e4cc5..3dc96fdc8 100644 --- a/examples/hello-ruby/README.md +++ b/examples/hello-ruby/README.md @@ -1,23 +1,77 @@ -This is a worker that just echoes the "input" param in the payload. +# Hello Function Image (Ruby) -eg: +This images compares the payload info with the header. -This input: +## Requirements -```json -{ - "name": "Johnny Utah" -} -``` +- IronFunctions API -Will output: +## Development + +### Building image locally ``` -Hello Johnny Utah! +# SET BELOW TO YOUR DOCKER HUB USERNAME +USERNAME=YOUR_DOCKER_HUB_USERNAME + +# build it +./build.sh ``` -## Try it +### Publishing to DockerHub ``` -echo '{"name":"Johnny"}' | docker run --rm -i iron/hello +# tagging +docker run --rm -v "$PWD":/app treeder/bump patch +docker tag $USERNAME/func-hello-ruby:latest $USERNAME/func-hello-ruby:`cat VERSION` + +# pushing to docker hub +docker push $USERNAME/func-hello-ruby ``` + +### Testing image + +``` +./test.sh +``` + +## Running it on IronFunctions + +### Let's define some environment variables + +``` +# Set your Function server address +# Eg. 127.0.0.1:8080 +FUNCAPI=YOUR_FUNCTIONS_ADDRESS +``` + +### Running with IronFunctions + +With this command we are going to create an application with name `hello`. + +``` +curl -X POST --data '{ + "app": { + "name": "hello", + } +}' http://$FUNCAPI/v1/apps +``` + +Now, we can create our route + +``` +curl -X POST --data '{ + "route": { + "image": "'$USERNAME'/func-hello-ruby", + "path": "/hello", + } +}' http://$FUNCAPI/v1/apps/hello/routes +``` + +#### Testing function + +Now that we created our IronFunction route, let's test our new route + +``` +curl -X POST --data '{"name": "Johnny"}' http://$FUNCAPI/r/hello/hello +``` \ No newline at end of file diff --git a/examples/hello-ruby/VERSION b/examples/hello-ruby/VERSION index f092e2be2..8acdd82b7 100644 --- a/examples/hello-ruby/VERSION +++ b/examples/hello-ruby/VERSION @@ -1 +1 @@ -0.0.30 +0.0.1 diff --git a/examples/hello-ruby/build.sh b/examples/hello-ruby/build.sh new file mode 100755 index 000000000..4d8d73dbc --- /dev/null +++ b/examples/hello-ruby/build.sh @@ -0,0 +1,4 @@ +#!/bin/bash +set -ex + +docker build -t iron/func-hello-ruby . \ No newline at end of file diff --git a/examples/hello-ruby/hello.rb b/examples/hello-ruby/function.rb similarity index 100% rename from examples/hello-ruby/hello.rb rename to examples/hello-ruby/function.rb diff --git a/examples/hello-ruby/functions.yml b/examples/hello-ruby/functions.yml new file mode 100644 index 000000000..e06d9139c --- /dev/null +++ b/examples/hello-ruby/functions.yml @@ -0,0 +1,3 @@ +image: iron/func-hello-ruby +build: + - ./build.sh \ No newline at end of file diff --git a/examples/hello-ruby/release.sh b/examples/hello-ruby/release.sh deleted file mode 100755 index 5a5b4415c..000000000 --- a/examples/hello-ruby/release.sh +++ /dev/null @@ -1,9 +0,0 @@ -USERNAME=iron -# build it -docker build -t $USERNAME/hello:ruby . -# test it -echo '{"name":"Johnny"}' | docker run --rm -i $USERNAME/hello -# tag it -docker run --rm -v "$PWD":/app treeder/bump patch -# push it -docker push $USERNAME/hello:ruby diff --git a/examples/hello-ruby/test.sh b/examples/hello-ruby/test.sh new file mode 100755 index 000000000..2a533a641 --- /dev/null +++ b/examples/hello-ruby/test.sh @@ -0,0 +1,9 @@ +#!/bin/bash +set -x + +./build.sh + +PAYLOAD='{"name":"Johnny"}' + +# test it +echo $PAYLOAD | docker run --rm -i -e TEST=1 iron/func-hello-ruby \ No newline at end of file diff --git a/examples/redis/.gitignore b/examples/redis/.gitignore index e69de29bb..f16ae5300 100644 --- a/examples/redis/.gitignore +++ b/examples/redis/.gitignore @@ -0,0 +1,2 @@ +/func +vendor \ No newline at end of file diff --git a/examples/redis/Dockerfile b/examples/redis/Dockerfile index bae945573..f5f8ab200 100644 --- a/examples/redis/Dockerfile +++ b/examples/redis/Dockerfile @@ -1,8 +1,5 @@ -FROM golang:1.6.2 +FROM iron/go -ADD . $GOPATH/src/func -WORKDIR $GOPATH/src/func +ADD func . -RUN go get . - -ENTRYPOINT ["go", "run", "main.go"] \ No newline at end of file +ENTRYPOINT ["./func"] \ No newline at end of file diff --git a/examples/redis/README.md b/examples/redis/README.md index a81cbbed7..efe3c2c4a 100644 --- a/examples/redis/README.md +++ b/examples/redis/README.md @@ -1,51 +1,11 @@ -# Redis GET/SET Function Example +# Redis GET/SET Function Image This function basically executes a GET/SET in a given redis server. -## How it works +## Requirements -If you send this payload: - -```json -{ - "redis": "redis:6379", - "command": "SET", - "args": ["name", "Johnny"] -} -``` - -The function will execute that command on the server and output: - -``` -OK -``` - -If you send this GET payload: - -```json -{ - "redis": "redis:6379", - "command": "GET", - "args": ["name"] -} -``` - -The function will execute that command on the server and output: - -``` -Johnny -``` - -## Payload structure - -```go -{ - redis string - redisAuth string - command string - args []string -} -``` +- Redis Server +- IronFunctions API ## Development @@ -56,34 +16,10 @@ Johnny USERNAME=YOUR_DOCKER_HUB_USERNAME # build it -docker build -t $USERNAME/func-redis . +./build.sh ``` -### Testing it - -Let's run a temporary redis server: - -``` -docker run --rm --name some-redis redis -``` - -Now let's test it - -``` -docker run --link 'some-redis:redis' -e 'PAYLOAD={ - "redis": "redis:6379", - "command": "SET", - "args": ["test", "123"] -}' $USERNAME/func-redis -``` - -Should output: - -``` -OK -``` - -### Publishing it +### Publishing to DockerHub ``` # tagging @@ -94,46 +30,82 @@ docker tag $USERNAME/func-redis:latest $USERNAME/func-redis:`cat VERSION` docker push $USERNAME/func-redis ``` -## Running it on Functions +### Testing image -First, let's define this two ENV variables +``` +./test.sh +``` + +## Running it on IronFunctions + +### Let's define some environment variables ``` # Set your Function server address # Eg. 127.0.0.1:8080 -FUNCHOST=YOUR_FUNCTIONS_ADDRESS +FUNCAPI=YOUR_FUNCTIONS_ADDRESS -# Set your redis server address -# Eg. myredishost.com:6379 -REDISHOST=YOUR_REDIS_ADDRESS +# Set your Redis server address +# Eg. redis:6379 +REDIS=YOUR_REDIS_ADDRESS -# (OPTIONAL) Set your redis server authentication -REDISAUTH=YOUR_REDIS_AUTH +# Set your Redis server auth (if required) +REDIS_AUTH=REDIS_AUTH_KEY ``` -### Creating the route inside your function server +### Running with IronFunctions -After you [start running you Function server](#), we can create our route: - -Eg. /redis/do +With this command we are going to create an application with name `redis`. ``` curl -X POST --data '{ - "name": "do", - "image": "'$USERNAME'/func-redis", - "path": "/do" -}' http://$FUNCHOST/v1/apps/redis/routes + "app": { + "name": "redis", + "config": { + "server": "'$REDIS'" + "redis_auth": "'$REDIS_AUTH'" + } + } +}' http://$FUNCAPI/v1/apps ``` -### Running our function +Now, we can create our routes -Now that we created our Function route, lets test it. +#### Route for set value ``` curl -X POST --data '{ - "redis":"'$REDISHOST'", - "redisAuth":"'$REDISAUTH'", - "command": "SET", - "args":["abc", "123"] -}' http://$FUNCHOST/redis/exec + "route": { + "image": "'$USERNAME'/func-redis", + "path": "/redis", + "config": { + "command": "SET" + } + } +}' http://$FUNCAPI/v1/apps/redis/routes +``` + +#### Route for get value + +``` +curl -X POST --data '{ + "route": { + "image": "'$USERNAME'/func-redis", + "path": "/redis", + "config": { + "command": "GET" + } + } +}' http://$FUNCAPI/v1/apps/redis/routes +``` + +#### Testing function + +Now that we created our IronFunction route, let's test our new route + +``` +curl -X POST --data '{"key": "name", "value": "Johnny"}' http://$FUNCAPI/r/redis/set +// "OK" +curl -X POST --data '{"key": "name"}' http://$FUNCAPI/r/redis/get +// "Johnny" ``` \ No newline at end of file diff --git a/examples/redis/build.sh b/examples/redis/build.sh new file mode 100755 index 000000000..d3e8997d4 --- /dev/null +++ b/examples/redis/build.sh @@ -0,0 +1,12 @@ +#!/bin/bash +set -ex + +FUNCPKG=$(pwd | sed "s|$GOPATH/src/||") + +# glide image to install dependencies +../build-glide.sh +docker run --rm -v "$PWD":/go/src/$FUNCPKG -w /go/src/$FUNCPKG glide up + +# build image +docker run --rm -v "$PWD":/go/src/$FUNCPKG -w /go/src/$FUNCPKG iron/go:dev go build -o func +docker build -t iron/func-redis . \ No newline at end of file diff --git a/examples/redis/main.go b/examples/redis/function.go similarity index 66% rename from examples/redis/main.go rename to examples/redis/function.go index b2025ae9c..e83717143 100644 --- a/examples/redis/main.go +++ b/examples/redis/function.go @@ -11,10 +11,8 @@ import ( ) type payload struct { - Redis string `json:"redis"` - RedisAuth string `json:"redisAuth"` - Command string `json:"command"` - Args []interface{} `json:"args"` + Key string `json:"key"` + Value string `json:"value"` } func main() { @@ -31,7 +29,7 @@ func main() { } // Dialing redis server - c, err := redis.Dial("tcp", pl.Redis) + c, err := redis.Dial("tcp", os.Getenv("CONFIG_SERVER")) if err != nil { log.Println("Failed to dial redis server") log.Fatal(err) @@ -39,8 +37,8 @@ func main() { } // Authenticate to redis server if exists the password - if pl.RedisAuth != "" { - if _, err := c.Do("AUTH", pl.RedisAuth); err != nil { + if os.Getenv("CONFIG_REDIS_AUTH") != "" { + if _, err := c.Do("AUTH", os.Getenv("CONFIG_REDIS_AUTH")); err != nil { log.Println("Failed to authenticate to redis server") log.Fatal(err) return @@ -48,13 +46,19 @@ func main() { } // Check if payload command is valid - if pl.Command != "GET" && pl.Command != "SET" { + if os.Getenv("CONFIG_COMMAND") != "GET" && os.Getenv("CONFIG_COMMAND") != "SET" { log.Println("Invalid command") return } // Execute command on redis server - r, err := c.Do(pl.Command, pl.Args...) + var r interface{} + if os.Getenv("CONFIG_COMMAND") == "GET" { + r, err = c.Do("GET", pl.Key) + } else if os.Getenv("CONFIG_COMMAND") == "SET" { + r, err = c.Do("SET", pl.Key, pl.Value) + } + if err != nil { log.Println("Failed to execute command on redis server") log.Fatal(err) diff --git a/examples/redis/functions.yml b/examples/redis/functions.yml new file mode 100644 index 000000000..7a63e0777 --- /dev/null +++ b/examples/redis/functions.yml @@ -0,0 +1,3 @@ +image: iron/func-redis +build: + - ./build.sh \ No newline at end of file diff --git a/examples/redis/glide.lock b/examples/redis/glide.lock new file mode 100644 index 000000000..9df405c9c --- /dev/null +++ b/examples/redis/glide.lock @@ -0,0 +1,9 @@ +hash: 8c667282524602aac09a157244397d581f19d256b95a725a4782ab3c67ebb08a +updated: 2016-11-01T02:56:55.36346607Z +imports: +- name: github.com/garyburd/redigo + version: 8873b2f1995f59d4bcdd2b0dc9858e2cb9bf0c13 + subpackages: + - internal + - redis +testImports: [] diff --git a/examples/redis/glide.yaml b/examples/redis/glide.yaml new file mode 100644 index 000000000..16c29f1ba --- /dev/null +++ b/examples/redis/glide.yaml @@ -0,0 +1,6 @@ +package: github.com/iron-io/functions/examples/redis +import: +- package: github.com/garyburd/redigo + version: ^1.0.0 + subpackages: + - redis diff --git a/examples/redis/test.sh b/examples/redis/test.sh new file mode 100755 index 000000000..eee6486ac --- /dev/null +++ b/examples/redis/test.sh @@ -0,0 +1,21 @@ +#!/bin/bash +set -x + +./build.sh + +PAYLOAD='{ + "key": "test", + "value": "123" +}' + +# test it +docker stop test-redis-func +docker rm test-redis-func + +docker run -p 6379:6379 --name test-redis-func -d redis + +echo $PAYLOAD | docker run --rm -i -e CONFIG_SERVER=redis:6379 -e CONFIG_COMMAND=SET --link test-redis-func:redis iron/func-redis +echo $PAYLOAD | docker run --rm -i -e CONFIG_SERVER=redis:6379 -e CONFIG_COMMAND=GET --link test-redis-func:redis iron/func-redis + +docker stop test-redis-func +docker rm test-redis-func \ No newline at end of file diff --git a/examples/sleeper/Dockerfile b/examples/sleeper/Dockerfile index a0cba87d5..f6c10018d 100644 --- a/examples/sleeper/Dockerfile +++ b/examples/sleeper/Dockerfile @@ -1,9 +1,9 @@ FROM iron/ruby:dev -WORKDIR /worker -ADD Gemfile* /worker/ +WORKDIR /function +ADD Gemfile* /function/ RUN bundle install -ADD . /worker/ +ADD . /function/ -ENTRYPOINT ["ruby", "sleeper.rb"] +ENTRYPOINT ["ruby", "function.rb"] diff --git a/examples/sleeper/Gemfile.lock b/examples/sleeper/Gemfile.lock deleted file mode 100644 index 28ab22fe5..000000000 --- a/examples/sleeper/Gemfile.lock +++ /dev/null @@ -1,13 +0,0 @@ -GEM - remote: https://rubygems.org/ - specs: - json (1.8.3) - -PLATFORMS - ruby - -DEPENDENCIES - json (> 1.8.2) - -BUNDLED WITH - 1.10.5 diff --git a/examples/sleeper/README.md b/examples/sleeper/README.md index a493acc58..4ee6d371b 100644 --- a/examples/sleeper/README.md +++ b/examples/sleeper/README.md @@ -1,23 +1,77 @@ -This is a worker that just echoes the "input" param in the payload. +# Sleeper Function Image -eg: +This images compares the payload info with the header. -This input: +## Requirements -```json -{ - "sleep": 5 -} -``` +- IronFunctions API -Will make this container sleep for 5 seconds. +## Development - -## Building Image - -Install [dj](https://github.com/treeder/dj/), then run: +### Building image locally ``` -docker build -t iron/sleeper . -docker run -e 'PAYLOAD={"sleep": 5}' iron/sleeper +# SET BELOW TO YOUR DOCKER HUB USERNAME +USERNAME=YOUR_DOCKER_HUB_USERNAME + +# build it +./build.sh +``` + +### Publishing to DockerHub + +``` +# tagging +docker run --rm -v "$PWD":/app treeder/bump patch +docker tag $USERNAME/func-sleeper:latest $USERNAME/func-sleeper:`cat VERSION` + +# pushing to docker hub +docker push $USERNAME/func-sleeper +``` + +### Testing image + +``` +./test.sh +``` + +## Running it on IronFunctions + +### Let's define some environment variables + +``` +# Set your Function server address +# Eg. 127.0.0.1:8080 +FUNCAPI=YOUR_FUNCTIONS_ADDRESS +``` + +### Running with IronFunctions + +With this command we are going to create an application with name `sleeper`. + +``` +curl -X POST --data '{ + "app": { + "name": "sleeper", + } +}' http://$FUNCAPI/v1/apps +``` + +Now, we can create our route + +``` +curl -X POST --data '{ + "route": { + "image": "'$USERNAME'/func-sleeper", + "path": "/sleeper", + } +}' http://$FUNCAPI/v1/apps/sleeper/routes +``` + +#### Testing function + +Now that we created our IronFunction route, let's test our new route + +``` +curl -X POST --data '{"sleep": 5}' http://$FUNCAPI/r/sleeper/sleeper ``` diff --git a/examples/sleeper/build.sh b/examples/sleeper/build.sh new file mode 100755 index 000000000..c062b83ee --- /dev/null +++ b/examples/sleeper/build.sh @@ -0,0 +1,4 @@ +#!/bin/bash +set -ex + +docker build -t iron/func-sleeper . \ No newline at end of file diff --git a/examples/sleeper/function.rb b/examples/sleeper/function.rb new file mode 100644 index 000000000..bdb56cd3d --- /dev/null +++ b/examples/sleeper/function.rb @@ -0,0 +1,14 @@ +require 'json' + +payload = STDIN.read +if payload != "" + payload = JSON.parse(payload) + + # payload contains checks + if payload["sleep"] + i = payload['sleep'].to_i + puts "Sleeping for #{i} seconds..." + sleep i + puts "I'm awake!" + end +end diff --git a/examples/sleeper/functions.yml b/examples/sleeper/functions.yml new file mode 100644 index 000000000..d3ac44833 --- /dev/null +++ b/examples/sleeper/functions.yml @@ -0,0 +1,3 @@ +image: iron/func-sleeper +build: + - ./build.sh \ No newline at end of file diff --git a/examples/sleeper/sleeper.rb b/examples/sleeper/sleeper.rb deleted file mode 100644 index 490d027cb..000000000 --- a/examples/sleeper/sleeper.rb +++ /dev/null @@ -1,8 +0,0 @@ -require 'json' - -payload = JSON.parse(ENV['PAYLOAD']) - -i = payload['sleep'].to_i -puts "Sleeping for #{i} seconds..." -sleep i -puts "I'm awake!" diff --git a/examples/sleeper/test.sh b/examples/sleeper/test.sh new file mode 100755 index 000000000..e1c80fa6c --- /dev/null +++ b/examples/sleeper/test.sh @@ -0,0 +1,9 @@ +#!/bin/bash +set -x + +./build.sh + +PAYLOAD='{"sleep": 5}' + +# test it +echo $PAYLOAD | docker run --rm -i -e TEST=1 iron/func-sleeper \ No newline at end of file diff --git a/examples/tests.sh b/examples/tests.sh new file mode 100755 index 000000000..2e206065c --- /dev/null +++ b/examples/tests.sh @@ -0,0 +1,11 @@ +#!/bin/bash + +testlist=`find -L "$@" -type f -executable -name test.sh` + +for test in $testlist +do + cd $(dirname $test) + echo "${test}" + ./test.sh + cd .. +done \ No newline at end of file diff --git a/examples/twitter/.gitignore b/examples/twitter/.gitignore new file mode 100644 index 000000000..f16ae5300 --- /dev/null +++ b/examples/twitter/.gitignore @@ -0,0 +1,2 @@ +/func +vendor \ No newline at end of file diff --git a/examples/twitter/Dockerfile b/examples/twitter/Dockerfile index 34c7c326e..f5f8ab200 100644 --- a/examples/twitter/Dockerfile +++ b/examples/twitter/Dockerfile @@ -1,8 +1,5 @@ -FROM iron/go:dev +FROM iron/go -ADD . $GOPATH/src/github.com/iron-io/functions/examples/twitter -WORKDIR $GOPATH/src/github.com/iron-io/functions/examples/twitter +ADD func . -RUN go get . - -ENTRYPOINT ["go", "run", "main.go"] \ No newline at end of file +ENTRYPOINT ["./func"] \ No newline at end of file diff --git a/examples/twitter/README.md b/examples/twitter/README.md index 0442bb280..13460a070 100644 --- a/examples/twitter/README.md +++ b/examples/twitter/README.md @@ -1,13 +1,11 @@ -# Twitter Example +# Twitter Function Image This function exemplifies an authentication in Twitter API and get latest tweets of an account. ## Requirements -You need create or configure a [Twitter App](https://apps.twitter.com/) and configure Customer Access and Access Token. - -Reference: https://dev.twitter.com/oauth/overview/application-owner-access-tokens - +- IronFunctions API +- Configure a [Twitter App](https://apps.twitter.com/) and [configure Customer Access and Access Token](https://dev.twitter.com/oauth/overview/application-owner-access-tokens). ## Development @@ -15,37 +13,51 @@ Reference: https://dev.twitter.com/oauth/overview/application-owner-access-token ``` # SET BELOW TO YOUR DOCKER HUB USERNAME -export USERNAME=YOUR_DOCKER_HUB_USERNAME +USERNAME=YOUR_DOCKER_HUB_USERNAME # build it -docker build -t $USERNAME/functions-twitter . +./build.sh ``` -### Publishing it +### Publishing to DockerHub ``` -docker push $USERNAME/functions-twitter +# tagging +docker run --rm -v "$PWD":/app treeder/bump patch +docker tag $USERNAME/func-twitter:latest $USERNAME/func-twitter:`cat VERSION` + +# pushing to docker hub +docker push $USERNAME/func-twitter +``` + +### Testing image + +``` +./test.sh ``` ## Running it on IronFunctions -You need a running IronFunctions API +### Let's define some environment variables -### First, let's define this environment variables +``` +# Set your Function server address +# Eg. 127.0.0.1:8080 +FUNCAPI=YOUR_FUNCTIONS_ADDRESS -Set your Twitter Credentials in environment variables. - -```sh -export CUSTOMER_KEY="XXXXXX" -export CUSTOMER_SECRET="XXXXXX" -export ACCESS_TOKEN="XXXXXX" -export ACCESS_SECRET="XXXXXX" +CUSTOMER_KEY="XXXXXX" +CUSTOMER_SECRET="XXXXXX" +ACCESS_TOKEN="XXXXXX" +ACCESS_SECRET="XXXXXX" ``` -### Create the application -```sh -curl -H "Content-Type: application/json" -X POST -d '{ - "app": { +### Running with IronFunctions + +With this command we are going to create an application with name `twitter`. + +``` +curl -X POST --data '{ + "app": { "name": "twitter", "config": { "CUSTOMER_KEY": "'$CUSTOMER_KEY'", @@ -54,27 +66,24 @@ curl -H "Content-Type: application/json" -X POST -d '{ "ACCESS_SECRET": "'$ACCESS_SECRET'" } } -}' http://localhost:8080/v1/apps +}' http://$FUNCAPI/v1/apps ``` -### Add the route -```sh -curl -H "Content-Type: application/json" -X POST -d '{ +Now, we can create our route + +``` +curl -X POST --data '{ "route": { - "path":"/tweets", - "image":"'$USERNAME/functions-twitter'" + "image": "'$USERNAME'/func-twitter", + "path": "/tweets", } -}' http://localhost:8080/v1/apps/twitter/routes +}' http://$FUNCAPI/v1/apps/twitter/routes ``` +#### Testing function -### Calling the function - -```sh -# Latests tweets of default account (getiron) -curl http://localhost:8080/r/twitter/tweets - -# Latests tweets of specific account -curl -X POST --data '{"username": "getiron"}' http://localhost:8080/r/twitter/tweets +Now that we created our IronFunction route, let's test our new route +``` +curl -X POST --data '{"username": "getiron"}' http://$FUNCAPI/r/twitter/tweets ``` \ No newline at end of file diff --git a/examples/twitter/VERSION b/examples/twitter/VERSION new file mode 100644 index 000000000..8a9ecc2ea --- /dev/null +++ b/examples/twitter/VERSION @@ -0,0 +1 @@ +0.0.1 \ No newline at end of file diff --git a/examples/twitter/build.sh b/examples/twitter/build.sh new file mode 100755 index 000000000..0dc577168 --- /dev/null +++ b/examples/twitter/build.sh @@ -0,0 +1,12 @@ +#!/bin/bash +set -ex + +FUNCPKG=$(pwd | sed "s|$GOPATH/src/||") + +# glide image to install dependencies +../build-glide.sh +docker run --rm -v "$PWD":/go/src/$FUNCPKG -w /go/src/$FUNCPKG glide up + +# build image +docker run --rm -v "$PWD":/go/src/$FUNCPKG -w /go/src/$FUNCPKG iron/go:dev go build -o func +docker build -t iron/func-twitter . \ No newline at end of file diff --git a/examples/twitter/main.go b/examples/twitter/function.go similarity index 100% rename from examples/twitter/main.go rename to examples/twitter/function.go diff --git a/examples/twitter/functions.yml b/examples/twitter/functions.yml new file mode 100644 index 000000000..c041ecb15 --- /dev/null +++ b/examples/twitter/functions.yml @@ -0,0 +1,3 @@ +image: iron/func-twitter +build: + - ./build.sh \ No newline at end of file diff --git a/examples/twitter/glide.lock b/examples/twitter/glide.lock new file mode 100644 index 000000000..af9561527 --- /dev/null +++ b/examples/twitter/glide.lock @@ -0,0 +1,22 @@ +hash: 749fba43b472c9a4dbbb37dfce0a6c06f5d8dc5b936dd4ca5283051ebe5447d7 +updated: 2016-11-01T02:57:50.631537738Z +imports: +- name: github.com/cenkalti/backoff + version: b02f2bbce11d7ea6b97f282ef1771b0fe2f65ef3 +- name: github.com/dghubble/go-twitter + version: fab2c1610270521a409d76c4a1f04c85eaa8702f + subpackages: + - twitter +- name: github.com/dghubble/oauth1 + version: 70562a5920ad9b6ff03ef697c0f90ae569abbd2b +- name: github.com/dghubble/sling + version: c961a4334054e64299d16f8a31bd686ee2565ae4 +- name: github.com/google/go-querystring + version: 9235644dd9e52eeae6fa48efd539fdc351a0af53 + subpackages: + - query +- name: golang.org/x/net + version: 541150ac4f433e755f5663eba5d888c2fa347343 + subpackages: + - context +testImports: [] diff --git a/examples/twitter/glide.yaml b/examples/twitter/glide.yaml new file mode 100644 index 000000000..f2ccb9e23 --- /dev/null +++ b/examples/twitter/glide.yaml @@ -0,0 +1,7 @@ +package: github.com/iron-io/functions/examples/twitter +import: +- package: github.com/dghubble/go-twitter + subpackages: + - twitter +- package: github.com/dghubble/oauth1 + version: ^0.4.0 diff --git a/examples/twitter/test.sh b/examples/twitter/test.sh new file mode 100755 index 000000000..59e170323 --- /dev/null +++ b/examples/twitter/test.sh @@ -0,0 +1,9 @@ +#!/bin/bash +set -x + +./build.sh + +PAYLOAD='{"username": "getiron"}' + +# test it +echo $PAYLOAD | docker run --rm -i iron/func-twitter \ No newline at end of file