Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
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
38 changes: 38 additions & 0 deletions gunicorn/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
ARCH_LIBDIR ?= /lib/$(shell $(CC) -dumpmachine)

ifeq ($(DEBUG),1)
GRAMINE_LOG_LEVEL = debug
else
GRAMINE_LOG_LEVEL = error
endif

.PHONY: all
all: gunicorn.manifest
ifeq ($(SGX),1)
all: gunicorn.manifest.sgx gunicorn.sig
endif

gunicorn.manifest: gunicorn.manifest.template
gramine-manifest \
-Dlog_level=$(GRAMINE_LOG_LEVEL) \
-Darch_libdir=$(ARCH_LIBDIR) \
-Dentrypoint=$(realpath $(shell sh -c "command -v gunicorn")) \
-Dpython_exe_path=$(realpath $(shell sh -c "command -v python3")) \
$< >$@

# Make on Ubuntu <= 20.04 doesn't support "Rules with Grouped Targets" (`&:`)
gunicorn.manifest.sgx gunicorn.sig: sgx_sign
@:

.INTERMEDIATE: sgx_sign
sgx_sign: gunicorn.manifest
gramine-sgx-sign \
--manifest $< \
--output $<.sgx

.PHONY: clean
clean:
$(RM) -rf *.token *.sig *.manifest *.manifest.sgx __pycache__

.PHONY: distclean
distclean: clean
35 changes: 35 additions & 0 deletions gunicorn/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
# Gunicorn example

This directory contains an example for running Gunicorn in Gramine, including the
Makefile and a template for generating the manifest.

# Generating the manifest

## Installing prerequisites

Please run the following command to install Gunicorn and its dependencies on Ubuntu 22.04:
```
sudo apt-get install python3 python3-flask gunicorn
```

## Building for Linux

Run `make` (non-debug) or `make DEBUG=1` (debug) in the directory.

## Building for SGX

Run `make SGX=1` (non-debug) or `make SGX=1 DEBUG=1` (debug) in the directory.

# Running Gunicorn with Gramine

Here's an example of running Gunicorn under Gramine:

Without SGX:
```
gramine-direct gunicorn --workers 1 --timeout 600 main:app
```

With SGX:
```
gramine-sgx gunicorn --workers 1 --timeout 600 main:app
```
40 changes: 40 additions & 0 deletions gunicorn/gunicorn.manifest.template
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
loader.entrypoint = "file:{{ gramine.libos }}"
libos.entrypoint = "{{ entrypoint }}"

loader.log_level = "{{ log_level }}"

loader.insecure__use_cmdline_argv = true

loader.env.LD_LIBRARY_PATH = "/lib:{{ arch_libdir }}:/usr/{{ arch_libdir }}"

fs.mounts = [
{ path = "{{ arch_libdir }}", uri = "file:{{ arch_libdir }}" },
{ path = "{{ entrypoint }}", uri = "file:{{ entrypoint }}" },
{ path = "/lib", uri = "file:{{ gramine.runtimedir() }}" },
{% for path in python.get_sys_path(python_exe_path) %}
{ path = "{{ path }}", uri = "file:{{ path }}" },
{% endfor %}
{ path = "/usr/bin/python3", uri = "file:/usr/bin/python3" },
{ path = "/usr/{{ arch_libdir }}", uri = "file:/usr/{{ arch_libdir }}" },
{ type = "tmpfs", path = "/tmp" },
]

sgx.edmm_enable = {{ 'true' if env.get('EDMM', '0') == '1' else 'false' }}
sgx.enclave_size = "512M"
sgx.max_threads = 64

sgx.trusted_files = [
"file:{{ arch_libdir }}/",
"file:{{ entrypoint }}",
"file:{{ gramine.libos }}",
"file:{{ gramine.runtimedir() }}/",
"file:main.py",
{% for path in python.get_sys_path(python_exe_path) %}
"file:{{ path }}{{ '/' if path.is_dir() else '' }}",
{% endfor %}
"file:/usr/bin/python3",
"file:/usr/{{ arch_libdir }}/",
]

# BSD (flock) locks are currently experimental
sys.experimental__enable_flock = true
12 changes: 12 additions & 0 deletions gunicorn/main.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
from flask import Flask, jsonify, request

app = Flask(__name__)

@app.route('/hello', methods=['GET'])
def helloworld():
if(request.method == 'GET'):
data = {"data": "Hello World"}
return jsonify(data)

if __name__ == '__main__':
app.run(debug=True)