Skip to content

Commit

Permalink
fix: switch to gunicorn WSGI server
Browse files Browse the repository at this point in the history
  • Loading branch information
satterly committed Apr 7, 2024
1 parent 2a246f8 commit 3cab70f
Show file tree
Hide file tree
Showing 11 changed files with 73 additions and 93 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,5 @@ contrib/kubernetes/helm/alerta/charts
.idea/*

/.bundle/

/venv/
10 changes: 3 additions & 7 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
FROM python:3.9-slim-buster
FROM python:3.10-slim-buster

ENV PYTHONUNBUFFERED 1
ENV PIP_DISABLE_PIP_VERSION_CHECK=1
Expand All @@ -15,11 +15,7 @@ ENV WEBUI_VERSION=8.7.0
ENV NGINX_WORKER_PROCESSES=1
ENV NGINX_WORKER_CONNECTIONS=1024

ENV UWSGI_PROCESSES=5
ENV UWSGI_LISTEN=100
ENV UWSGI_BUFFER_SIZE=8192
ENV UWSGI_MAX_WORKER_LIFETIME=30
ENV UWSGI_WORKER_LIFETIME_DELTA=3
ENV GUNICORN_WORKERS=5

ENV HEARTBEAT_SEVERITY=major
ENV HK_EXPIRED_DELETE_HRS=2
Expand Down Expand Up @@ -77,7 +73,7 @@ COPY requirements*.txt /app/
# hadolint ignore=DL3013
RUN pip install --no-cache-dir pip virtualenv jinja2 && \
python3 -m venv /venv && \
/venv/bin/pip install --no-cache-dir --upgrade setuptools && \
/venv/bin/pip install --no-cache-dir --upgrade setuptools wheel && \
/venv/bin/pip install --no-cache-dir --requirement /app/requirements.txt && \
/venv/bin/pip install --no-cache-dir --requirement /app/requirements-docker.txt
ENV PATH $PATH:/venv/bin
Expand Down
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -126,8 +126,8 @@ API to ease deployment more generally:
`NGINX_WORKER_CONNECTIONS`
- maximum number of simultaneous connections that can be opened by a worker process (default:`1024`)

`UWSGI_PROCESSES`
- number of processes for uWSGI (default:`5`)
`GUNICORN_WORKERS`
- number of worker processes for Gunicorn (default:`5`)

`UWSGI_LISTEN`
- max number of concurrent connections (default:`100`)
Expand Down
14 changes: 14 additions & 0 deletions config/templates/app/gunicorn.conf.py.j2
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@

bind = '127.0.0.1:29000'

chdir = '/app'
daemon = False
raw_env = [
'SCRIPT_NAME=/api',
]
worker_tmp_dir = '/dev/shm'
workers = {{ env.GUNICORN_WORKERS }}

{%- if env.DEBUG %}
loglevel = 'debug'
{%- endif %}
11 changes: 5 additions & 6 deletions config/templates/app/nginx.conf.j2
Original file line number Diff line number Diff line change
Expand Up @@ -47,13 +47,12 @@ http {
access_log /dev/stdout main;

location /api {
include /etc/nginx/uwsgi_params;
uwsgi_pass backend;
proxy_pass http://backend;

uwsgi_param Host $host;
uwsgi_param X-Real-IP $remote_addr;
uwsgi_param X-Forwarded-For $proxy_add_x_forwarded_for;
uwsgi_param X-Forwarded-Proto $http_x_forwarded_proto;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $http_x_forwarded_proto;
}

root /web;
Expand Down
6 changes: 4 additions & 2 deletions config/templates/app/supervisord.conf.j2
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,10 @@ logfile=/tmp/supervisord.log
loglevel={{ env.SUPERVISORD_LOG_LEVEL|lower or 'debug' }}
pidfile=/tmp/supervisord.pid

[program:uwsgi]
command=/venv/bin/uwsgi --ini /app/uwsgi.ini
[program:gunicorn]
command=/venv/bin/gunicorn wsgi:app -c /app/gunicorn.conf.py
autostart=true
autorestart=true
redirect_stderr=true

[program:nginx]
Expand Down
34 changes: 0 additions & 34 deletions config/templates/app/uwsgi.ini.j2

This file was deleted.

2 changes: 1 addition & 1 deletion contrib/kubernetes/backend/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ RUN apt-get update && apt-get install -y \

RUN pip install --no-cache-dir virtualenv && \
virtualenv --python=python3 /venv && \
/venv/bin/pip install uwsgi alerta alerta-server==$VERSION
/venv/bin/pip install gunicorn alerta alerta-server==$VERSION

ENV PATH $PATH:/venv/bin

Expand Down
13 changes: 7 additions & 6 deletions docker-entrypoint.sh
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ ALERTA_CONF_FILE=${ALERTA_CONF_FILE:-/app/alerta.conf}
ALERTA_SVR_CONF_FILE=${ALERTA_SVR_CONF_FILE:-/app/alertad.conf}
ALERTA_WEB_CONF_FILE=${ALERTA_WEB_CONF_FILE:-/web/config.json}
NGINX_CONF_FILE=/app/nginx.conf
UWSGI_CONF_FILE=/app/uwsgi.ini
GUNICORN_CONF_FILE=/app/gunicorn.conf.py
SUPERVISORD_CONF_FILE=/app/supervisord.conf

ADMIN_USER=${ADMIN_USERS%%,*}
Expand Down Expand Up @@ -67,12 +67,13 @@ if [ ! -f "${NGINX_CONF_FILE}" ]; then
echo "# Create nginx configuration file."
python3 -c "${JINJA2}" < ${NGINX_CONF_FILE}.j2 >${NGINX_CONF_FILE}
fi
cat ${NGINX_CONF_FILE}
nginx -t -c ${NGINX_CONF_FILE}

# Generate uWSGI config, if not supplied.
if [ ! -f "${UWSGI_CONF_FILE}" ]; then
echo "# Create uWSGI configuration file."
python3 -c "${JINJA2}" < ${UWSGI_CONF_FILE}.j2 >${UWSGI_CONF_FILE}
# Generate Gunicorn config, if not supplied.
if [ ! -f "${GUNICORN_CONF_FILE}" ]; then
echo "# Create Gunicorn configuration file."
python3 -c "${JINJA2}" < ${GUNICORN_CONF_FILE}.j2 >${GUNICORN_CONF_FILE}
fi

# Generate web config, if not supplied.
Expand All @@ -88,7 +89,7 @@ echo Alerta Client ${CLIENT_VERSION}
echo Alerta WebUI ${WEBUI_VERSION}

nginx -v
echo uwsgi $(uwsgi --version)
gunicorn --version
mongo --version | grep MongoDB
psql --version
python3 --version
Expand Down
2 changes: 1 addition & 1 deletion requirements-docker.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
lxml==4.9.2
pysaml2==7.2.1
python-ldap==3.4.3
uWSGI==2.0.21
gunicorn==21.2.0
68 changes: 34 additions & 34 deletions tests/spec/api_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,44 +26,44 @@
expect(JSON.parse(result.body)['provider']).to eq("basic")
end
end
context "/management" do
context "get properties" do
result = Client.get "#{GATEWAY_BASE_URL}/management/properties", {'x-api-key' => 'demo-key'}
it "return 200" do
expect(result.code).to eq(200)
end
it "X-Forwarded-For header is set" do
expect(result.body).to include("X-Forwarded-For")
end
end
context "/management" do
context "get properties" do
result = Client.get "#{GATEWAY_BASE_URL}/management/properties", {'x-api-key' => 'demo-key'}
it "return 200" do
expect(result.code).to eq(200)
end
it "HTTP_X_FORWARDED_FOR envvar is set" do
expect(result.body).to include("HTTP_X_FORWARDED_FOR")
end
end
context "get healthcheck" do
result = Client.get "#{GATEWAY_BASE_URL}/management/gtg"
it "return 200" do
expect(result.code).to eq(200)
end
context "get healthcheck" do
result = Client.get "#{GATEWAY_BASE_URL}/management/gtg"
it "return 200" do
expect(result.code).to eq(200)
end
it "good-to-go is OK" do
expect(result.body).to eq("OK")
end
it "response header does not contain nginx version" do
expect(result.headers[:server]).to eq("nginx")
end
it "good-to-go is OK" do
expect(result.body).to eq("OK")
end
it "response header does not contain nginx version" do
expect(result.headers[:server]).to eq("nginx")
end
end
end
context "/alerts" do
context "get without auth" do
result = Client.get "#{GATEWAY_BASE_URL}/alerts"
it "return 401" do
expect(result.code).to eq(401)
end
end
context "/alerts" do
context "get without auth" do
result = Client.get "#{GATEWAY_BASE_URL}/alerts"
it "return 401" do
expect(result.code).to eq(401)
end
context "get" do
result = Client.get "#{GATEWAY_BASE_URL}/alerts?api-key=demo-key"
it "return 200" do
expect(result.code).to eq(200)
end
context "get" do
result = Client.get "#{GATEWAY_BASE_URL}/alerts?api-key=demo-key"
it "return 200" do
expect(result.code).to eq(200)
end
it "total is 0" do
expect(JSON.parse(result.body)['total']).to eq(0)
end
it "total is 0" do
expect(JSON.parse(result.body)['total']).to eq(0)
end
end
end
Expand Down

0 comments on commit 3cab70f

Please sign in to comment.