Skip to content

Create debian templates for the python 3 flask templates. #30

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 8, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 56 additions & 0 deletions template/python3-flask-debian/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
FROM openfaas/of-watchdog:0.7.2 as watchdog
FROM python:3.7-slim-buster

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

ARG ADDITIONAL_PACKAGE
# Alternatively use ADD https:// (which will not be cached by Docker builder)

RUN apt-get -qy update && apt-get -qy install gcc make ${ADDITIONAL_PACKAGE}

# Add non root user
RUN addgroup --system app && adduser app --system --ingroup app
RUN chown app /home/app

USER app

ENV PATH=$PATH:/home/app/.local/bin

WORKDIR /home/app/

COPY index.py .
COPY requirements.txt .

USER root
RUN pip install -r requirements.txt

#build function directory and install user specified componenets.
USER app

RUN mkdir -p function
RUN touch ./function/__init__.py
WORKDIR /home/app/function/
COPY function/requirements.txt .
RUN pip install --user -r requirements.txt

WORKDIR /home/app/

#install function code
USER root

COPY function function
RUN chown -R app:app ./

#configure WSGI server and healthcheck
USER app

ENV fprocess="python index.py"

ENV cgi_headers="true"
ENV mode="http"
ENV upstream_url="http://127.0.0.1:5000"

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

CMD ["fwatchdog"]
Empty file.
7 changes: 7 additions & 0 deletions template/python3-flask-debian/function/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.
32 changes: 32 additions & 0 deletions template/python3-flask-debian/index.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
# Copyright (c) Alex Ellis 2017. All rights reserved.
# Licensed under the MIT license. See LICENSE file in the project root for full license information.

from flask import Flask, request
from function import handler
#from gevent.wsgi import WSGIServer
from gevent.pywsgi import WSGIServer

app = Flask(__name__)

@app.before_request
def fix_transfer_encoding():
"""
Sets the "wsgi.input_terminated" environment flag, thus enabling
Werkzeug to pass chunked requests as streams. The gunicorn server
should set this, but it's not yet been implemented.
"""

transfer_encoding = request.headers.get("Transfer-Encoding", None)
if transfer_encoding == u"chunked":
request.environ["wsgi.input_terminated"] = True

@app.route("/", defaults={"path": ""}, methods=["POST", "GET"])
@app.route("/<path:path>", methods=["POST", "GET"])
def main_route(path):
ret = handler.handle(request.get_data())
return ret

if __name__ == '__main__':

http_server = WSGIServer(('0.0.0.0', 5000), app)
http_server.serve_forever()
3 changes: 3 additions & 0 deletions template/python3-flask-debian/requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
flask
gevent

2 changes: 2 additions & 0 deletions template/python3-flask-debian/template.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
language: python3-flask-debian
fprocess: python index.py
9 changes: 9 additions & 0 deletions template/python3-flask/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ COPY --from=watchdog /fwatchdog /usr/bin/fwatchdog
RUN chmod +x /usr/bin/fwatchdog

ARG ADDITIONAL_PACKAGE
# Alternatively use ADD https:// (which will not be cached by Docker builder)

RUN apk --no-cache add musl-dev gcc make ${ADDITIONAL_PACKAGE}

# Add non root user
Expand All @@ -19,8 +21,11 @@ WORKDIR /home/app/

COPY index.py .
COPY requirements.txt .

USER root
RUN pip install -r requirements.txt

#build function directory and install user specified componenets.
USER app

RUN mkdir -p function
Expand All @@ -31,9 +36,13 @@ RUN pip install --user -r requirements.txt

WORKDIR /home/app/

#install function code
USER root

COPY function function
RUN chown -R app:app ./

#configure WSGI server and healthcheck
USER app

ENV fprocess="python index.py"
Expand Down
49 changes: 49 additions & 0 deletions template/python3-http-debian/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
FROM openfaas/of-watchdog:0.7.2 as watchdog
FROM python:3.7-slim-buster

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

ARG ADDITIONAL_PACKAGE
# Alternatively use ADD https:// (which will not be cached by Docker builder)

RUN apt-get -qy update && apt-get -qy install ${ADDITIONAL_PACKAGE}

# Add non root user
RUN addgroup --system app && adduser app --system --ingroup app
RUN chown app /home/app

USER app

ENV PATH=$PATH:/home/app/.local/bin

WORKDIR /home/app/

COPY index.py .
COPY requirements.txt .
USER root
RUN pip install -r requirements.txt
USER app

RUN mkdir -p function
RUN touch ./function/__init__.py
WORKDIR /home/app/function/
COPY function/requirements.txt .
RUN pip install --user -r requirements.txt

WORKDIR /home/app/

USER root
COPY function function
RUN chown -R app:app ./
USER app

# Set up of-watchdog for HTTP mode
ENV fprocess="python index.py"
ENV cgi_headers="true"
ENV mode="http"
ENV upstream_url="http://127.0.0.1:5000"

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

CMD ["fwatchdog"]
5 changes: 5 additions & 0 deletions template/python3-http-debian/function/handler.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
def handle(event, context):
return {
"statusCode": 200,
"body": "Hello from OpenFaaS!"
}
Empty file.
69 changes: 69 additions & 0 deletions template/python3-http-debian/index.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
#!/usr/bin/env python
from flask import Flask, request, jsonify
from waitress import serve
import os

from function import handler

app = Flask(__name__)

class Event:
def __init__(self):
self.body = request.get_data()
self.headers = request.headers
self.method = request.method
self.query = request.args
self.path = request.path

class Context:
def __init__(self):
self.hostname = os.environ['HOSTNAME']

def format_status_code(resp):
if 'statusCode' in resp:
return resp['statusCode']

return 200

def format_body(resp):
if 'body' not in resp:
return ""
elif type(resp['body']) == dict:
return jsonify(resp['body'])
else:
return str(resp['body'])

def format_headers(resp):
if 'headers' not in resp:
return []
elif type(resp['headers']) == dict:
headers = []
for key in resp['headers'].keys():
header_tuple = (key, resp['headers'][key])
headers.append(header_tuple)
return headers

return resp['headers']

def format_response(resp):
if resp == None:
return ('', 200)

statusCode = format_status_code(resp)
body = format_body(resp)
headers = format_headers(resp)

return (body, statusCode, headers)

@app.route('/', defaults={'path': ''}, methods=['GET', 'PUT', 'POST', 'PATCH', 'DELETE'])
@app.route('/<path:path>', methods=['GET', 'PUT', 'POST', 'PATCH', 'DELETE'])
def call_handler(path):
event = Event()
context = Context()
response_data = handler.handle(event, context)

resp = format_response(response_data)
return resp

if __name__ == '__main__':
serve(app, host='0.0.0.0', port=5000)
2 changes: 2 additions & 0 deletions template/python3-http-debian/requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
flask
waitress
2 changes: 2 additions & 0 deletions template/python3-http-debian/template.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
language: python3-http-debian
fprocess: python index.py
7 changes: 6 additions & 1 deletion template/python3-http/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ RUN chmod +x /usr/bin/fwatchdog

ARG ADDITIONAL_PACKAGE
# Alternatively use ADD https:// (which will not be cached by Docker builder)

RUN apk --no-cache add ${ADDITIONAL_PACKAGE}

# Add non root user
Expand All @@ -22,6 +23,8 @@ COPY index.py .
COPY requirements.txt .
USER root
RUN pip install -r requirements.txt

#build function directory and install user specified componenets.
USER app

RUN mkdir -p function
Expand All @@ -32,12 +35,14 @@ RUN pip install --user -r requirements.txt

WORKDIR /home/app/

#install function code
USER root
COPY function function
RUN chown -R app:app ./

#configure WSGI server and healthcheck
USER app

# Set up of-watchdog for HTTP mode
ENV fprocess="python index.py"
ENV cgi_headers="true"
ENV mode="http"
Expand Down