Skip to content

Commit

Permalink
Merge pull request #49 from cchurch/uwsgi-support
Browse files Browse the repository at this point in the history
Add uWSGI support.
  • Loading branch information
copelco authored Sep 24, 2019
2 parents e1ee740 + 6dffb92 commit cf71704
Show file tree
Hide file tree
Showing 8 changed files with 130 additions and 13 deletions.
1 change: 1 addition & 0 deletions CHANGES.rst
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ v 0.9.25 on ???????????
* Change ``include`` to ``include_tasks``.
* Add ``app_packages`` variable for specifying additional system packages to be
installed.
* Add support for using uWSGI instead of gunicorn.

v 0.9.24 on Jul 5, 2019
-----------------------
Expand Down
9 changes: 9 additions & 0 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,15 @@ The following variables are used by the ``tequila-django`` role:
before running it, then set this to the relative path of that subdirectory.
- ``wsgi_module`` **default:** ``{{ project_name }}.wsgi`` - allow
configuring an alternate path to the project's wsgi module.
- ``use_uwsgi`` **default:** ``false`` - use uWSGI instead of gunicorn to run
the web app.
- ``uwsgi_ini_path`` **default:** ``"{{ root_dir }}/uwsgi.ini"`` - path to the
uWSGI configuration file for this app.
- ``uwsgi_processes`` **default:** ``10`` - number of uWSGI worker processes to
run.
- ``uwsgi_extra_ini_settings`` **default:** ``""`` - string containing extra
options to set in the uWSGI configuration file. Each line in the string should
normally contain ``key = value`` pairs.
- ``project_port`` **default:** 8000 - what port Django listens on
- ``app_packages`` **default:** ``[]`` - additional system packages to install
in addition to the ``default_app_packages`` (refer to ``defaults/main.yml``
Expand Down
2 changes: 2 additions & 0 deletions defaults/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ celery_camera_class: "django_celery_monitor.camera.Camera"
project_subdir: ""
django_dir: "{{ source_dir ~ '/' ~ project_subdir if project_subdir != '' else source_dir }}"
wsgi_module: "{{ project_name }}.wsgi"
use_uwsgi: false
uwsgi_ini_path: "{{ root_dir }}/uwsgi.ini"
project_port: 8000
app_packages: []
default_app_packages:
Expand Down
18 changes: 17 additions & 1 deletion tasks/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -252,5 +252,21 @@
- include_tasks: celery.yml
when: is_worker or is_celery_beat

- include_tasks: web.yml
- include_tasks: web-gunicorn.yml
when: is_web and not use_uwsgi | default(false)

- include_tasks: web-uwsgi.yml
when: is_web and use_uwsgi | default(false)

# Note: we want the collectstatic step to happen at the very end of
# the roles section for the current playbook, so that it'll still
# happen in the order needed even if the playbook has another role
# after tequila-django, e.g. geerlingguy/nodejs and tequila-nodejs.
# Thus, it is moved out into handlers.

- name: trigger collectstatic
command: /bin/true
notify:
- collectstatic
run_once: "{{ cloud_staticfiles }}"
when: is_web
12 changes: 0 additions & 12 deletions tasks/web.yml → tasks/web-gunicorn.yml
Original file line number Diff line number Diff line change
Expand Up @@ -48,15 +48,3 @@
# handle that, since they're supposed to be idempotent.
# For now, just ignore errors:
ignore_errors: true

# Note: we want the collectstatic step to happen at the very end of
# the roles section for the current playbook, so that it'll still
# happen in the order needed even if the playbook has another role
# after tequila-django, e.g. geerlingguy/nodejs and tequila-nodejs.
# Thus, it is moved out into handlers.

- name: trigger collectstatic
command: /bin/true
notify:
- collectstatic
run_once: "{{ cloud_staticfiles }}"
60 changes: 60 additions & 0 deletions tasks/web-uwsgi.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
---

- name: remove uwsgi system package
apt:
pkg: uwsgi
state: absent

- name: install uwsgi
pip:
name: uwsgi
state: present
version: "{{ uwsgi_version|default(omit) }}"
virtualenv: "{{ venv_dir }}"
virtualenv_python: /usr/bin/python{{ python_version }}
become_user: "{{ project_user }}"
vars:
ansible_ssh_pipelining: true

- name: configure uwsgi service
template:
src: uwsgi.service.j2
dest: /etc/systemd/system/{{ project_name }}-uwsgi.service
owner: root
group: root
mode: 0644

- name: configure uwsgi ini
template:
src: uwsgi.ini.j2
dest: "{{ uwsgi_ini_path }}"
owner: "{{ project_user }}"
group: "{{ project_user }}"
mode: 0644

- name: ensure uwsgi is enabled
systemd:
name: "{{ project_name }}-uwsgi.service"
enabled: true
daemon_reload: true

# TODO: let connections to the {{ project_port }} through the firewall if we are load-balancing.

# Use manage.sh so we use the same env vars from .env that we use on
# other invocations of django manage.py. Unfortunately that means we cannot
# use the ansible django_manage module.
# TODO: see if we could use an ansible lookup plugin to read the .env file
# and pass the values to the environment configuration item?
- name: migrate
shell: "{{ root_dir }}/manage.sh migrate --noinput -v 0"
args:
chdir: "{{ django_dir }}"
become_user: "{{ project_user }}"
run_once: true
vars:
ansible_ssh_pipelining: true

- name: restart uwsgi
systemd:
name: "{{ project_name }}-uwsgi.service"
state: restarted
21 changes: 21 additions & 0 deletions templates/uwsgi.ini.j2
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
{{ ansible_managed | comment }}

[uwsgi]

enable-threads = true
single-interpreter = true
master = true
processes = {{ uwsgi_processes | default(10) }}
vacuum = true
uid = {{ project_user }}
gid = {{ project_user }}
die-on-term = true

http = :{{ project_port }}
socket = /tmp/{{ project_name }}.sock
chmod-socket = 664

chdir = {{ source_dir }}
module = {{ wsgi_module }}

{{ uwsgi_extra_ini_settings | default('') }}
20 changes: 20 additions & 0 deletions templates/uwsgi.service.j2
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
{{ ansible_managed | comment }}

[Unit]
Description={{ project_name }} uWSGI
After=syslog.target

[Service]
Environment=DJANGO_SETTINGS_MODULE={{ project_settings }}
ExecStart={{ django_dir }}/dotenv.sh \
{% if use_newrelic %}{{ venv_dir }}/bin/newrelic-admin run-program {% endif %} \
{{ venv_dir }}/bin/uwsgi \
--ini {{ uwsgi_ini_path }}
Restart=on-failure
KillSignal=SIGQUIT
Type=notify
StandardError=syslog
NotifyAccess=all

[Install]
WantedBy=multi-user.target

0 comments on commit cf71704

Please sign in to comment.