From 1afa6fedae756e65467be53ba3bd46e2f9c86cc5 Mon Sep 17 00:00:00 2001 From: cheimu Date: Thu, 4 Mar 2021 18:52:42 -0800 Subject: [PATCH] cannot finish lab4 function chaining. See https://github.com/openfaas/workshop/issues/191 --- lab4/.gitignore | 2 ++ lab4/astronaut-finder.yml | 12 +++++++++ lab4/astronaut-finder/__init__.py | 0 lab4/astronaut-finder/handler.py | 10 +++++++ lab4/astronaut-finder/requirements.txt | 1 + lab4/example.yml | 15 +++++++++++ lab4/first/__init__.py | 0 lab4/first/handler.py | 7 +++++ lab4/first/requirements.txt | 0 lab4/hello-openfaas.yml | 12 +++++++++ lab4/hello-openfaas/__init__.py | 0 lab4/hello-openfaas/handler.py | 7 +++++ lab4/hello-openfaas/requirements.txt | 0 lab4/ingest-file.yml | 13 +++++++++ lab4/ingest-file/__init__.py | 0 lab4/ingest-file/handler.py | 17 ++++++++++++ lab4/ingest-file/requirements.txt | 0 lab4/message.txt | 1 + lab4/request-markdown.yml | 10 +++++++ lab4/request-markdown/__init__.py | 0 lab4/request-markdown/handler.py | 22 +++++++++++++++ lab4/request-markdown/requirements.txt | 1 + lab4/request-sentiment-analysis.yml | 10 +++++++ lab4/request-sentiment-analysis/__init__.py | 0 lab4/request-sentiment-analysis/handler.py | 24 +++++++++++++++++ .../requirements.txt | 1 + lab4/second/__init__.py | 0 lab4/second/handler.py | 7 +++++ lab4/second/requirements.txt | 0 lab4/sorter.yml | 10 +++++++ lab4/sorter/Dockerfile | 27 +++++++++++++++++++ 31 files changed, 209 insertions(+) create mode 100644 lab4/.gitignore create mode 100644 lab4/astronaut-finder.yml create mode 100644 lab4/astronaut-finder/__init__.py create mode 100644 lab4/astronaut-finder/handler.py create mode 100644 lab4/astronaut-finder/requirements.txt create mode 100644 lab4/example.yml create mode 100644 lab4/first/__init__.py create mode 100644 lab4/first/handler.py create mode 100644 lab4/first/requirements.txt create mode 100644 lab4/hello-openfaas.yml create mode 100644 lab4/hello-openfaas/__init__.py create mode 100644 lab4/hello-openfaas/handler.py create mode 100644 lab4/hello-openfaas/requirements.txt create mode 100644 lab4/ingest-file.yml create mode 100644 lab4/ingest-file/__init__.py create mode 100644 lab4/ingest-file/handler.py create mode 100644 lab4/ingest-file/requirements.txt create mode 100644 lab4/message.txt create mode 100644 lab4/request-markdown.yml create mode 100644 lab4/request-markdown/__init__.py create mode 100644 lab4/request-markdown/handler.py create mode 100644 lab4/request-markdown/requirements.txt create mode 100644 lab4/request-sentiment-analysis.yml create mode 100644 lab4/request-sentiment-analysis/__init__.py create mode 100644 lab4/request-sentiment-analysis/handler.py create mode 100644 lab4/request-sentiment-analysis/requirements.txt create mode 100644 lab4/second/__init__.py create mode 100644 lab4/second/handler.py create mode 100644 lab4/second/requirements.txt create mode 100644 lab4/sorter.yml create mode 100644 lab4/sorter/Dockerfile diff --git a/lab4/.gitignore b/lab4/.gitignore new file mode 100644 index 0000000..942d901 --- /dev/null +++ b/lab4/.gitignore @@ -0,0 +1,2 @@ +template +build diff --git a/lab4/astronaut-finder.yml b/lab4/astronaut-finder.yml new file mode 100644 index 0000000..595a374 --- /dev/null +++ b/lab4/astronaut-finder.yml @@ -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 + diff --git a/lab4/astronaut-finder/__init__.py b/lab4/astronaut-finder/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/lab4/astronaut-finder/handler.py b/lab4/astronaut-finder/handler.py new file mode 100644 index 0000000..785e8d6 --- /dev/null +++ b/lab4/astronaut-finder/handler.py @@ -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) diff --git a/lab4/astronaut-finder/requirements.txt b/lab4/astronaut-finder/requirements.txt new file mode 100644 index 0000000..663bd1f --- /dev/null +++ b/lab4/astronaut-finder/requirements.txt @@ -0,0 +1 @@ +requests \ No newline at end of file diff --git a/lab4/example.yml b/lab4/example.yml new file mode 100644 index 0000000..4a9bc68 --- /dev/null +++ b/lab4/example.yml @@ -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 + diff --git a/lab4/first/__init__.py b/lab4/first/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/lab4/first/handler.py b/lab4/first/handler.py new file mode 100644 index 0000000..a7098fa --- /dev/null +++ b/lab4/first/handler.py @@ -0,0 +1,7 @@ +def handle(req): + """handle a request to the function + Args: + req (str): request body + """ + + return req diff --git a/lab4/first/requirements.txt b/lab4/first/requirements.txt new file mode 100644 index 0000000..e69de29 diff --git a/lab4/hello-openfaas.yml b/lab4/hello-openfaas.yml new file mode 100644 index 0000000..7d39344 --- /dev/null +++ b/lab4/hello-openfaas.yml @@ -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 + diff --git a/lab4/hello-openfaas/__init__.py b/lab4/hello-openfaas/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/lab4/hello-openfaas/handler.py b/lab4/hello-openfaas/handler.py new file mode 100644 index 0000000..3c5f3c8 --- /dev/null +++ b/lab4/hello-openfaas/handler.py @@ -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"}) diff --git a/lab4/hello-openfaas/requirements.txt b/lab4/hello-openfaas/requirements.txt new file mode 100644 index 0000000..e69de29 diff --git a/lab4/ingest-file.yml b/lab4/ingest-file.yml new file mode 100644 index 0000000..0ead0dc --- /dev/null +++ b/lab4/ingest-file.yml @@ -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/" + diff --git a/lab4/ingest-file/__init__.py b/lab4/ingest-file/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/lab4/ingest-file/handler.py b/lab4/ingest-file/handler.py new file mode 100644 index 0000000..66abf0a --- /dev/null +++ b/lab4/ingest-file/handler.py @@ -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 \ No newline at end of file diff --git a/lab4/ingest-file/requirements.txt b/lab4/ingest-file/requirements.txt new file mode 100644 index 0000000..e69de29 diff --git a/lab4/message.txt b/lab4/message.txt new file mode 100644 index 0000000..fd968c8 --- /dev/null +++ b/lab4/message.txt @@ -0,0 +1 @@ +Hello function diff --git a/lab4/request-markdown.yml b/lab4/request-markdown.yml new file mode 100644 index 0000000..956a5c4 --- /dev/null +++ b/lab4/request-markdown.yml @@ -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 + diff --git a/lab4/request-markdown/__init__.py b/lab4/request-markdown/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/lab4/request-markdown/handler.py b/lab4/request-markdown/handler.py new file mode 100644 index 0000000..a28abd4 --- /dev/null +++ b/lab4/request-markdown/handler.py @@ -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 \ No newline at end of file diff --git a/lab4/request-markdown/requirements.txt b/lab4/request-markdown/requirements.txt new file mode 100644 index 0000000..663bd1f --- /dev/null +++ b/lab4/request-markdown/requirements.txt @@ -0,0 +1 @@ +requests \ No newline at end of file diff --git a/lab4/request-sentiment-analysis.yml b/lab4/request-sentiment-analysis.yml new file mode 100644 index 0000000..2fb5748 --- /dev/null +++ b/lab4/request-sentiment-analysis.yml @@ -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 + diff --git a/lab4/request-sentiment-analysis/__init__.py b/lab4/request-sentiment-analysis/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/lab4/request-sentiment-analysis/handler.py b/lab4/request-sentiment-analysis/handler.py new file mode 100644 index 0000000..5d441bf --- /dev/null +++ b/lab4/request-sentiment-analysis/handler.py @@ -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" \ No newline at end of file diff --git a/lab4/request-sentiment-analysis/requirements.txt b/lab4/request-sentiment-analysis/requirements.txt new file mode 100644 index 0000000..663bd1f --- /dev/null +++ b/lab4/request-sentiment-analysis/requirements.txt @@ -0,0 +1 @@ +requests \ No newline at end of file diff --git a/lab4/second/__init__.py b/lab4/second/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/lab4/second/handler.py b/lab4/second/handler.py new file mode 100644 index 0000000..a7098fa --- /dev/null +++ b/lab4/second/handler.py @@ -0,0 +1,7 @@ +def handle(req): + """handle a request to the function + Args: + req (str): request body + """ + + return req diff --git a/lab4/second/requirements.txt b/lab4/second/requirements.txt new file mode 100644 index 0000000..e69de29 diff --git a/lab4/sorter.yml b/lab4/sorter.yml new file mode 100644 index 0000000..f24febf --- /dev/null +++ b/lab4/sorter.yml @@ -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 + diff --git a/lab4/sorter/Dockerfile b/lab4/sorter/Dockerfile new file mode 100644 index 0000000..d430000 --- /dev/null +++ b/lab4/sorter/Dockerfile @@ -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"]