Skip to content

Commit

Permalink
cannot finish lab4 function chaining. See openfaas/workshop#191
Browse files Browse the repository at this point in the history
  • Loading branch information
cheimu committed Mar 5, 2021
1 parent 94cef98 commit 1afa6fe
Show file tree
Hide file tree
Showing 31 changed files with 209 additions and 0 deletions.
2 changes: 2 additions & 0 deletions lab4/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
template
build
12 changes: 12 additions & 0 deletions lab4/astronaut-finder.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
version: 1.0
provider:
name: openfaas
gateway: http://127.0.0.1:8080
functions:
astronaut-finder:
lang: python3
handler: ./astronaut-finder
image: ${DOCKER_USER:-dev}/astronaut-finder:latest
environment:
write_debug: true

Empty file.
10 changes: 10 additions & 0 deletions lab4/astronaut-finder/handler.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import requests
import random

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"]

return "%s is in space" % (name)
1 change: 1 addition & 0 deletions lab4/astronaut-finder/requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
requests
15 changes: 15 additions & 0 deletions lab4/example.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
version: 1.0
provider:
name: openfaas
gateway: http://127.0.0.1:8080
functions:
first:
lang: python3
handler: ./first
image: cheimu/first:latest

second:
lang: python3
handler: ./second
image: cheimu/second:latest

Empty file added lab4/first/__init__.py
Empty file.
7 changes: 7 additions & 0 deletions lab4/first/handler.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
def handle(req):
"""handle a request to the function
Args:
req (str): request body
"""

return req
Empty file added lab4/first/requirements.txt
Empty file.
12 changes: 12 additions & 0 deletions lab4/hello-openfaas.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
version: 1.0
provider:
name: openfaas
gateway: http://127.0.0.1:8080
functions:
hello-openfaas:
lang: python3
handler: ./hello-openfaas
image: cheimu/hello-openfaas:latest
environment:
combine_output: false

Empty file added lab4/hello-openfaas/__init__.py
Empty file.
7 changes: 7 additions & 0 deletions lab4/hello-openfaas/handler.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import sys
import json

def handle(req):

sys.stderr.write("This should be an error message.\n")
return json.dumps({"Hello": "OpenFaaS"})
Empty file.
13 changes: 13 additions & 0 deletions lab4/ingest-file.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
version: 1.0
provider:
name: openfaas
gateway: http://127.0.0.1:8080
functions:
ingest-file:
lang: python3
handler: ./ingest-file
image: cheimu/ingest-file:latest
readonly_root_filesystem: true
environment:
save_path: "/tmp/"

Empty file added lab4/ingest-file/__init__.py
Empty file.
17 changes: 17 additions & 0 deletions lab4/ingest-file/handler.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import os
import time

def handle(req):
# Read the path or a default from environment variable
path = os.getenv("save_path", "/home/app/")

# generate a name using the current timestamp
t = time.time()
file_name = path + str(t)

# write a file
with open(file_name, "w") as f:
f.write(req)
f.close()

return file_name
Empty file.
1 change: 1 addition & 0 deletions lab4/message.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Hello function
10 changes: 10 additions & 0 deletions lab4/request-markdown.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
version: 1.0
provider:
name: openfaas
gateway: http://127.0.0.1:8080
functions:
request-markdown:
lang: python3
handler: ./request-markdown
image: cheimu/request-markdown:latest

Empty file.
22 changes: 22 additions & 0 deletions lab4/request-markdown/handler.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import os
import requests
import sys

def handle(req):
"""handle a request to the function
Args:
req (str): request body
"""

gateway_hostname = os.getenv("gateway_hostname", "gateway") # uses a default of "gateway" for when "gateway_hostname" is not set

test_sentence = req

r = requests.get("http://" + "127.0.0.1" + ":8080/function/markdown", data= test_sentence)

if r.status_code != 200:
sys.exit("Error with markdown, expected: %d, got: %d\n" % (200, r.status_code))

result = r.json()

return result
1 change: 1 addition & 0 deletions lab4/request-markdown/requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
requests
10 changes: 10 additions & 0 deletions lab4/request-sentiment-analysis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
version: 1.0
provider:
name: openfaas
gateway: http://127.0.0.1:8080
functions:
request-sentiment-analysis:
lang: python3
handler: ./request-sentiment-analysis
image: cheimu/request-sentiment-analysis:latest

Empty file.
24 changes: 24 additions & 0 deletions lab4/request-sentiment-analysis/handler.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import os
import requests
import sys

def handle(req):
"""handle a request to the function
Args:
req (str): request body
"""

#gateway_hostname = os.getenv("gateway_hostname", "gateway") # uses a default of "gateway" for when "gateway_hostname" is not set

test_sentence = req

r = requests.get("http://127.0.0.1:8080/function/sentimentanalysis", data= test_sentence)

if r.status_code != 200:
sys.exit("Error with sentimentanalysis, expected: %d, got: %d\n" % (200, r.status_code))

result = r.json()
if result["polarity"] > 0.45:
return "That was probably positive"
else:
return "That was neutral or negative"
1 change: 1 addition & 0 deletions lab4/request-sentiment-analysis/requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
requests
Empty file added lab4/second/__init__.py
Empty file.
7 changes: 7 additions & 0 deletions lab4/second/handler.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
def handle(req):
"""handle a request to the function
Args:
req (str): request body
"""

return req
Empty file added lab4/second/requirements.txt
Empty file.
10 changes: 10 additions & 0 deletions lab4/sorter.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
version: 1.0
provider:
name: openfaas
gateway: http://127.0.0.1:8080
functions:
sorter:
lang: dockerfile
handler: ./sorter
image: cheimu/sorter:latest

27 changes: 27 additions & 0 deletions lab4/sorter/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
FROM ghcr.io/openfaas/classic-watchdog:0.1.5 as watchdog

FROM alpine:3.12

RUN mkdir -p /home/app

COPY --from=watchdog /fwatchdog /usr/bin/fwatchdog
RUN chmod +x /usr/bin/fwatchdog

# Add non root user
RUN addgroup -S app && adduser app -S -G app
RUN chown app /home/app

WORKDIR /home/app

USER app

# Populate example here - i.e. "cat", "sha512sum" or "node index.js"
ENV fprocess="sort"
# Set to true to see request in function logs
ENV write_debug="false"

EXPOSE 8080

HEALTHCHECK --interval=3s CMD [ -e /tmp/.lock ] || exit 1

CMD ["fwatchdog"]

0 comments on commit 1afa6fe

Please sign in to comment.