Per language HOWTO's on writing functions (#208)

* fnctl: improve UX for howto's

* doc: go function howto

* fnctl: show the progress of build calls

* doc: php function howto

* doc: fix Go HOWTO

* doc: Node Functions HOWTO

* doc: Python Functions HOWTO

* doc: improve README files

* doc: ccirello -> USERNAME

* docs: fix Python example

Used an idiomatic method (isatty) to decide whether stdin must
be read or not.

* doc: fix go example

* fnctl: fix docker push output
This commit is contained in:
C Cirello
2016-11-01 16:19:16 -07:00
committed by Seif Lotfy سيف لطفي
parent c8e4a9b82b
commit 5b3971060b
31 changed files with 311 additions and 10 deletions

5
docs/howto/go/.gitignore vendored Normal file
View File

@@ -0,0 +1,5 @@
vendor/
/hello
/go
/app
/__uberscript__

6
docs/howto/go/Dockerfile Normal file
View File

@@ -0,0 +1,6 @@
FROM iron/go
WORKDIR /app
ADD . /app
ENTRYPOINT ["./hello"]

40
docs/howto/go/README.md Normal file
View File

@@ -0,0 +1,40 @@
## Quick Example for a Go Function (3 minutes)
This example will show you how to test and deploy Go (Golang) code to IronFunctions.
### 1. Prepare the `functions.yaml` file:
At functions.yaml you will find:
```yml
app: goapp
route: /hello
image: USERNAME/hello:0.0.1
build:
- docker run --rm -v "$PWD":/go/src/ -w /go/src/ -e "GOPATH=/go/src/vendor:/go" iron/go:dev go build -o hello
```
The important step here is to ensure you replace `USERNAME` with your Docker Hub account name. Some points of note:
the application name is `goapp` and the route for incoming requests is `/hello`. These informations are relevant for
the moment you try to test this function.
### 2. Build:
```sh
fnctl publish
```
`-v` is optional, but it allows you to see how this function is being built.
### 3. Queue jobs for your function
Now you can start jobs on your function. Let's quickly queue up a job to try it out.
```sh
cat hello.payload.json | fnctl run goapp /hello
```
Here's a curl example to show how easy it is to do in any language:
```sh
curl -H "Content-Type: application/json" -X POST -d @hello.payload.json http://localhost:8080/r/goapp/hello
```

View File

@@ -0,0 +1,5 @@
app: goapp
route: /hello
image: USERNAME/hello:0.0.1
build:
- docker run --rm -v "$PWD":/go/src/ -w /go/src/ -e "GOPATH=/go/src/vendor:/go" iron/go:dev go build -o hello

17
docs/howto/go/hello.go Normal file
View File

@@ -0,0 +1,17 @@
package main
import (
"encoding/json"
"fmt"
"os"
)
type Person struct {
Name string
}
func main() {
p := &Person{Name: "World"}
json.NewDecoder(os.Stdin).Decode(p)
fmt.Println("Hello", p.Name, "!!!")
}

View File

@@ -0,0 +1,3 @@
{
"name": "Johnny"
}

1
docs/howto/node/.gitignore vendored Normal file
View File

@@ -0,0 +1 @@
node_modules/

View File

@@ -0,0 +1,6 @@
FROM iron/node
WORKDIR /app
ADD . /app
ENTRYPOINT ["node", "hello.js"]

40
docs/howto/node/README.md Normal file
View File

@@ -0,0 +1,40 @@
## Quick Example for a NodeJS Function (4 minutes)
This example will show you how to test and deploy Go (Golang) code to IronFunctions.
### 1. Prepare the `functions.yaml` file:
At functions.yaml you will find:
```yml
app: nodeapp
route: /hello
image: USERNAME/hello:0.0.1
build:
- docker run --rm -v "$PWD":/worker -w /worker iron/node:dev npm install
```
The important step here is to ensure you replace `USERNAME` with your Docker Hub account name. Some points of note:
the application name is `nodeapp` and the route for incoming requests is `/hello`. These informations are relevant for
the moment you try to test this function.
### 2. Build:
```sh
fnctl publish
```
`-v` is optional, but it allows you to see how this function is being built.
### 3. Queue jobs for your function
Now you can start jobs on your function. Let's quickly queue up a job to try it out.
```sh
cat hello.payload.json | fnctl run nodeapp /hello
```
Here's a curl example to show how easy it is to do in any language:
```sh
curl -H "Content-Type: application/json" -X POST -d @hello.payload.json http://localhost:8080/r/nodeapp/hello
```

View File

@@ -0,0 +1,5 @@
app: nodeapp
route: /hello
image: USERNAME/hello:0.0.1
build:
- docker run --rm -v "$PWD":/worker -w /worker iron/node:dev npm install

9
docs/howto/node/hello.js Normal file
View File

@@ -0,0 +1,9 @@
name = "World";
fs = require('fs');
try {
obj = JSON.parse(fs.readFileSync('/dev/stdin').toString())
if (obj.name != "") {
name = obj.name
}
} catch(e) {}
console.log("Hello", name, "from Node!");

View File

@@ -0,0 +1,3 @@
{
"name": "Johnny"
}

View File

@@ -0,0 +1,4 @@
{
"dependencies": {
}
}

1
docs/howto/php/.gitignore vendored Normal file
View File

@@ -0,0 +1 @@
vendor/

View File

@@ -0,0 +1,6 @@
FROM iron/php
WORKDIR /app
ADD . /app
ENTRYPOINT ["php", "hello.php"]

40
docs/howto/php/README.md Normal file
View File

@@ -0,0 +1,40 @@
## Quick Example for a PHP Function (4 minutes)
This example will show you how to test and deploy Go (Golang) code to IronFunctions.
### 1. Prepare the `functions.yaml` file:
At functions.yaml you will find:
```yml
app: phpapp
route: /hello
image: USERNAME/hello:0.0.1
build:
- docker run --rm -v "$PWD":/worker -w /worker iron/php:dev composer install
```
The important step here is to ensure you replace `USERNAME` with your Docker Hub account name. Some points of note:
the application name is `phpapp` and the route for incoming requests is `/hello`. These informations are relevant for
the moment you try to test this function.
### 2. Build:
```sh
fnctl publish
```
`-v` is optional, but it allows you to see how this function is being built.
### 3. Queue jobs for your function
Now you can start jobs on your function. Let's quickly queue up a job to try it out.
```sh
cat hello.payload.json | fnctl run phpapp /hello
```
Here's a curl example to show how easy it is to do in any language:
```sh
curl -H "Content-Type: application/json" -X POST -d @hello.payload.json http://localhost:8080/r/phpapp/hello
```

View File

@@ -0,0 +1,4 @@
{
"require": {
}
}

View File

@@ -0,0 +1,5 @@
app: phpapp
route: /hello
image: USERNAME/hello:0.0.1
build:
- docker run --rm -v "$PWD":/worker -w /worker iron/php:dev composer install

View File

@@ -0,0 +1,3 @@
{
"name": "Johnny"
}

10
docs/howto/php/hello.php Normal file
View File

@@ -0,0 +1,10 @@
<?php
require 'vendor/autoload.php';
stream_set_blocking(STDIN, 0);
$payload = json_decode(file_get_contents("php://stdin"), true);
if (isset($payload['name'])) {
echo "Hello ", $payload['name'],"!\n\n";
} else {
echo "Hello World!\n\n";
}

1
docs/howto/python/.gitignore vendored Normal file
View File

@@ -0,0 +1 @@
packages/

View File

@@ -0,0 +1,6 @@
FROM iron/python:2
WORKDIR /app
ADD . /app
ENTRYPOINT ["python", "hello.py"]

View File

@@ -0,0 +1,40 @@
## Quick Example for a Python Function (4 minutes)
This example will show you how to test and deploy Go (Golang) code to IronFunctions.
### 1. Prepare the `functions.yaml` file:
At functions.yaml you will find:
```yml
app: pythonapp
route: /hello
image: USERNAME/hello:0.0.1
build:
- docker run --rm -v "$PWD":/worker -w /worker iron/python:2-dev pip install -t packages -r requirements.txt
```
The important step here is to ensure you replace `USERNAME` with your Docker Hub account name. Some points of note:
the application name is `pythonapp` and the route for incoming requests is `/hello`. These informations are relevant for
the moment you try to test this function.
### 2. Build:
```sh
fnctl publish
```
`-v` is optional, but it allows you to see how this function is being built.
### 3. Queue jobs for your function
Now you can start jobs on your function. Let's quickly queue up a job to try it out.
```sh
cat hello.payload.json | fnctl run pythonapp /hello
```
Here's a curl example to show how easy it is to do in any language:
```sh
curl -H "Content-Type: application/json" -X POST -d @hello.payload.json http://localhost:8080/r/pythonapp/hello
```

View File

@@ -0,0 +1,5 @@
app: pythonapp
route: /hello
image: USERNAME/hello:0.0.1
build:
- docker run --rm -v "$PWD":/worker -w /worker iron/python:2-dev pip install -t packages -r requirements.txt

View File

@@ -0,0 +1,3 @@
{
"name": "Johnny"
}

View File

@@ -0,0 +1,12 @@
import sys
sys.path.append("packages")
import os
import json
name = "World"
if not os.isatty(sys.stdin.fileno()):
obj = json.loads(sys.stdin.read())
if obj["name"] != "":
name = obj["name"]
print "Hello", name, "!!!"

View File

View File

@@ -215,9 +215,10 @@ func (c commoncmd) localbuild(path string, steps []string) error {
for _, cmd := range steps {
exe := exec.Command("/bin/sh", "-c", cmd)
exe.Dir = filepath.Dir(path)
out, err := exe.CombinedOutput()
fmt.Fprintf(c.verbwriter, "- %s:\n%s\n", cmd, out)
if err != nil {
exe.Stderr = c.verbwriter
exe.Stdout = c.verbwriter
fmt.Fprintf(c.verbwriter, "- %s:\n", cmd)
if err := exe.Run(); err != nil {
return fmt.Errorf("error running command %v (%v)", cmd, err)
}
}
@@ -226,9 +227,10 @@ func (c commoncmd) localbuild(path string, steps []string) error {
}
func (c commoncmd) dockerbuild(path, image string) error {
out, err := exec.Command("docker", "build", "-t", image, filepath.Dir(path)).CombinedOutput()
fmt.Fprintf(c.verbwriter, "%s\n", out)
if err != nil {
cmd := exec.Command("docker", "build", "-t", image, filepath.Dir(path))
cmd.Stderr = c.verbwriter
cmd.Stdout = c.verbwriter
if err := cmd.Run(); err != nil {
return fmt.Errorf("error running docker build: %v", err)
}

View File

@@ -24,6 +24,7 @@ func main() {
lambda(),
publish(),
routes(),
run(),
}
app.Run(os.Args)
}

View File

@@ -84,12 +84,12 @@ func (p *publishcmd) publish(path string) error {
}
func (p publishcmd) dockerpush(image string) error {
out, err := exec.Command("docker", "push", image).CombinedOutput()
fmt.Fprintf(p.verbwriter, "%s\n", out)
if err != nil {
cmd := exec.Command("docker", "push", image)
cmd.Stderr = p.verbwriter
cmd.Stdout = p.verbwriter
if err := cmd.Run(); err != nil {
return fmt.Errorf("error running docker push: %v", err)
}
return nil
}

18
fnctl/run.go Normal file
View File

@@ -0,0 +1,18 @@
package main
import (
"github.com/iron-io/functions_go"
"github.com/urfave/cli"
)
func run() cli.Command {
r := routesCmd{RoutesApi: functions.NewRoutesApi()}
return cli.Command{
Name: "run",
Usage: "run function",
ArgsUsage: "fnclt run appName /path",
Flags: append(confFlags(&r.Configuration), []cli.Flag{}...),
Action: r.run,
}
}