mirror of
https://github.com/fnproject/fn.git
synced 2022-10-28 21:29:17 +03:00
Examples changes (#201)
This commit is contained in:
@@ -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"]
|
||||
|
||||
@@ -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
|
||||
@@ -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
|
||||
```
|
||||
|
||||
4
examples/sleeper/build.sh
Executable file
4
examples/sleeper/build.sh
Executable file
@@ -0,0 +1,4 @@
|
||||
#!/bin/bash
|
||||
set -ex
|
||||
|
||||
docker build -t iron/func-sleeper .
|
||||
14
examples/sleeper/function.rb
Normal file
14
examples/sleeper/function.rb
Normal file
@@ -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
|
||||
3
examples/sleeper/functions.yml
Normal file
3
examples/sleeper/functions.yml
Normal file
@@ -0,0 +1,3 @@
|
||||
image: iron/func-sleeper
|
||||
build:
|
||||
- ./build.sh
|
||||
@@ -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!"
|
||||
9
examples/sleeper/test.sh
Executable file
9
examples/sleeper/test.sh
Executable file
@@ -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
|
||||
Reference in New Issue
Block a user