Skip to content

Commit

Permalink
Add part 1 of Lab 6, rename space-counter function
Browse files Browse the repository at this point in the history
Signed-off-by: Alex Ellis (VMware) <alexellis2@gmail.com>
  • Loading branch information
alexellis committed Mar 1, 2018
1 parent ad0c735 commit 7e414b1
Show file tree
Hide file tree
Showing 6 changed files with 75 additions and 36 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ Disclaimer: this workshop is a work-in-progress - labs 2-5 are ready.
## [Lab 3 - Introduction to Functions](./lab3.md)

* Scaffold or generate a new function
* Build the space-counter function
* Build the astronaut-finder function
* Add dependencies with `pip`
* Troubleshooting: verbose output with `write_debug`
* Troubleshooting: find the container's logs
Expand Down
6 changes: 3 additions & 3 deletions space-counter.yml → astronaut-finder.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ provider:
gateway: http://localhost:8080

functions:
space-counter:
astronaut-finder:
lang: python
handler: ./space-counter
image: space-counter
handler: ./astronaut-finder
image: astronaut-finder
5 changes: 3 additions & 2 deletions space-counter/handler.py → astronaut-finder/handler.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import requests
import random

def handle(st):
def handle(req):
r = requests.get("http://api.open-notify.org/astros.json")
result = r.json()

index = random.randint(0, len(result["people"])-1)
name = result["people"][index]["name"]

print (name + " is in space")
print("%s is in space" % name)
File renamed without changes.
56 changes: 28 additions & 28 deletions lab3.md
Original file line number Diff line number Diff line change
Expand Up @@ -116,35 +116,35 @@ Functions can be invoked via a `GET` or `POST` method only.

Test out the function with `faas-cli invoke`, check `faas-cli invoke --help` for more options.

### Example function: space-counter
### Example function: astronaut-finder

We'll create a function that pulls in a random name of someone in space aboard the International Space Station (ISS).
We'll create a function called `astronaut-finder` that pulls in a random name of someone in space aboard the International Space Station (ISS).

```
$ faas new --lang python space-counter
$ faas new --lang python astronaut-finder
```

This will write three files for us:

```
./space-counter/handler.py
./astronaut-finder/handler.py
```

The handler for the function - you get a `req` object with the raw request and can print the result of the function to the console.

```
./space-counter/requirements.txt
./astronaut-finder/requirements.txt
```

This file lists any `pip` modules you want to install, such as `requests` or `urllib`

```
./space-counter.yml
./astronaut-finder.yml
```

This file is used to manage the function - it has the name of the function, the Docker image and any other customisations needed.

* Edit `./space-counter/requirements.txt`
* Edit `./astronaut-finder/requirements.txt`

```
requests
Expand Down Expand Up @@ -182,24 +182,24 @@ def handle(req):
Now build the function:

```
$ faas build -f ./space-counter.yml
$ faas build -f ./astronaut-finder.yml
```

> Tip: If you rename space-counter.yml to `stack.yml` then you can leave off the `-f` argument. `stack.yml` is the default file-name for the CLI.
> Tip: If you rename astronaut-finder.yml to `stack.yml` then you can leave off the `-f` argument. `stack.yml` is the default file-name for the CLI.
Deploy the function:

```
$ faas deploy -f ./space-counter.yml
$ faas deploy -f ./astronaut-finder.yml
```

Invoke the function

```
$ echo | faas invoke space-counter
$ echo | faas invoke astronaut-finder
Anton Shkaplerov is in space
$ echo | faas invoke space-counter
$ echo | faas invoke astronaut-finder
Joe Acaba is in space
```

Expand All @@ -208,9 +208,9 @@ Joe Acaba is in space
You can find out high-level information on every invocation of your function via the container's logs:

```
$ docker service logs -f space-counter
space-counter.1.1e1ujtsijf6b@nuc | 2018/02/21 14:53:25 Forking fprocess.
space-counter.1.1e1ujtsijf6b@nuc | 2018/02/21 14:53:26 Wrote 18 Bytes - Duration: 0.063269 seconds
$ docker service logs -f astronaut-finder
astronaut-finder.1.1e1ujtsijf6b@nuc | 2018/02/21 14:53:25 Forking fprocess.
astronaut-finder.1.1e1ujtsijf6b@nuc | 2018/02/21 14:53:26 Wrote 18 Bytes - Duration: 0.063269 seconds
```

## Troubleshooting: verbose output with `write_debug`
Expand All @@ -225,34 +225,34 @@ provider:
gateway: http://localhost:8080
functions:
space-counter:
astronaut-finder:
lang: python
handler: ./space-counter
image: space-counter
handler: ./astronaut-finder
image: astronaut-finder
```

Edit your YAML file for the function and add an "environment" section.

```
space-counter:
astronaut-finder:
lang: python
handler: ./space-counter
image: space-counter
handler: ./astronaut-finder
image: astronaut-finder
environment:
write_debug: true
```

Now deploy your function again with `faas-cli deploy -f ./space-counter.yml`.
Now deploy your function again with `faas-cli deploy -f ./astronaut-finder.yml`.

Invoke the function and then checkout the logs again to view the function responses:

```
$ docker service logs -f space-counter
space-counter.1.1e1ujtsijf6b@nuc | 2018/02/21 14:53:25 Forking fprocess.
space-counter.1.szobw9pt3m60@nuc | 2018/02/26 14:49:57 Query
space-counter.1.szobw9pt3m60@nuc | 2018/02/26 14:49:57 Path /function/hello-openfaas
space-counter.1.1e1ujtsijf6b@nuc | 2018/02/21 14:53:26 Hello World
space-counter.1.1e1ujtsijf6b@nuc | 2018/02/21 14:53:26 Duration: 0.063269 seconds
$ docker service logs -f astronaut-finder
astronaut-finder.1.1e1ujtsijf6b@nuc | 2018/02/21 14:53:25 Forking fprocess.
astronaut-finder.1.szobw9pt3m60@nuc | 2018/02/26 14:49:57 Query
astronaut-finder.1.szobw9pt3m60@nuc | 2018/02/26 14:49:57 Path /function/hello-openfaas
astronaut-finder.1.1e1ujtsijf6b@nuc | 2018/02/21 14:53:26 Hello World
astronaut-finder.1.1e1ujtsijf6b@nuc | 2018/02/21 14:53:26 Duration: 0.063269 seconds
```

### Make use of custom templates
Expand Down
42 changes: 40 additions & 2 deletions lab6.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,44 @@
# Lab 6 - Chain or combine Functions into workflows

* Make use of another function
* Director pattern
## Call one function from another

The easiest way to call one function from another is make a call over HTTP back to the API Gateway in OpenFaaS. This call does not need to know the external domain name or IP address, it can simply refer to the API Gateway as `gateway`.

Example:

In Lab 3 we introduced the requests module and used it to call a remote API to get the name of an astronaut aboard the ISS. We can use the same technique to call another function deployed on OpenFaaS.

* Go to the *Function Store* and deploy the *Sentiment Analysis* function.

The Sentiment Analysis function will take any string and give whether it's subjective and positive or negative in a JSON response. The key field we want is `polarity`.

```
echo -n "California is great, it's always sunny there." | faas invoke sentimentanalysis
{"polarity": 0.8, "sentence_count": 1, "subjectivity": 0.75}
```

So the result shows us that our test sentence was both very subjective (75%) and very positive (80%).

The following code can be used to call the *Sentiment Analysis* function or any other function:

```
test_sentence = "California is great, it's always sunny there."
r = requests.get("http://gateway:8080/function/sentimentanalysis", text= test_sentence)
```

Since the result is always in JSON format we can make use of the helper function `.json()` to convert the response:

```
result = r.json()
if result["polarity" > 0.45]:
print("That was probably positive")
else:
print("That was neutral or negative")
```

Try generating a new function in Python putting it all together, remember to add `requests` to your `requirements.txt` file.

## The Director pattern


Now move onto [Lab 7](lab7.md)

0 comments on commit 7e414b1

Please sign in to comment.