Skip to content

Commit 12a863a

Browse files
committed
Migrate from dispatch.
1 parent 310ae75 commit 12a863a

7 files changed

Lines changed: 111 additions & 0 deletions

File tree

‎Dockerfile‎

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
FROM vmware/photon2:20180302
2+
3+
RUN tdnf install -y python3-3.6.1-9.ph2 python3-pip-3.6.1-9.ph2 && pip3 install --upgrade pip setuptools
4+
5+
ARG IMAGE_TEMPLATE=/image-template
6+
ARG FUNCTION_TEMPLATE=/function-template
7+
8+
LABEL io.dispatchframework.imageTemplate="${IMAGE_TEMPLATE}" \
9+
io.dispatchframework.functionTemplate="${FUNCTION_TEMPLATE}"
10+
11+
COPY image-template ${IMAGE_TEMPLATE}/
12+
COPY function-template ${FUNCTION_TEMPLATE}/
13+
14+
ENV FUNCTION_MODULE=/function-server/function/handler.py PORT=8080
15+
EXPOSE ${PORT}
16+
17+
COPY function-server /function-server/
18+
19+
WORKDIR /function-server
20+
RUN pip install -r requirements.txt
21+
22+
CMD ["python3", "main.py"]

‎build.sh‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
#!/bin/sh
2+
set -e -x
3+
4+
cd $(dirname $0)
5+
6+
docker build -t vmware/dispatch-python3-base:0.0.2-dev1 .
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
def handle(context, payload):
2+
return payload

‎function-server/main.py‎

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
import json
2+
import signal
3+
import sys
4+
import io
5+
6+
import falcon
7+
import waitress
8+
9+
from function import handler
10+
11+
12+
class Health(object):
13+
@staticmethod
14+
def on_get(req, res):
15+
res.body = '{}'
16+
17+
18+
def read_logs(text_io):
19+
text_io.seek(0)
20+
return [line.rstrip('\n') for line in text_io]
21+
22+
23+
def exec_function(req, res):
24+
msg = None
25+
if req.content_length:
26+
msg = json.load(req.stream)
27+
28+
r = None
29+
err = None
30+
31+
stderr = io.StringIO()
32+
old_stderr = sys.stderr
33+
34+
try:
35+
sys.stderr = stderr
36+
r = handler.handle(msg['context'], msg['payload'])
37+
except Exception as e:
38+
err = e
39+
finally:
40+
sys.stderr = old_stderr
41+
stderr.flush()
42+
43+
res.body = json.dumps({'context': {'error': err, 'logs': read_logs(stderr)}, 'payload': r}, ensure_ascii=False)
44+
45+
46+
def signal_handler(signum, frame):
47+
sys.exit(0)
48+
49+
50+
signal.signal(signal.SIGINT, signal_handler)
51+
signal.signal(signal.SIGTERM, signal_handler)
52+
53+
app = falcon.API()
54+
app.add_route('/healthz', Health())
55+
app.add_sink(exec_function, '/')
56+
57+
waitress.serve(app, threads=1)

‎function-server/requirements.txt‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
falcon==1.4.1
2+
python-mimeparse==1.6.0
3+
six==1.11.0
4+
waitress==1.1.0

‎function-template/Dockerfile‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
ARG IMAGE
2+
FROM ${IMAGE}
3+
4+
ARG FUNCTION_SRC=function.txt
5+
6+
# FUNCTION_MODULE env var is set in the base-image
7+
COPY ${FUNCTION_SRC} ${FUNCTION_MODULE}

‎image-template/Dockerfile‎

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
ARG BASE_IMAGE
2+
FROM ${BASE_IMAGE}
3+
4+
ARG SYSTEM_PACKAGES_FILE=system-packages.txt
5+
COPY ${SYSTEM_PACKAGES_FILE} .
6+
7+
RUN packages=$(cat ${SYSTEM_PACKAGES_FILE} | sed 's/ /-/' | uniq | paste -d' ' -); \
8+
[ -n "${packages}" ] && tdnf install -y ${packages} || :
9+
10+
ARG PACKAGES_FILE=packages.txt
11+
COPY ${PACKAGES_FILE} requirements.txt
12+
13+
RUN pj=$(cat requirements.txt); [ -n "${pj}" ] && pip install -r requirements.txt || :

0 commit comments

Comments
 (0)