Skip to content

Commit

Permalink
Merge pull request #35 from caktus/django_subdir
Browse files Browse the repository at this point in the history
Support django project in subdir, and a few other things
  • Loading branch information
dpoirier authored Aug 6, 2018
2 parents 3df7aff + 9541924 commit 65ee6b8
Show file tree
Hide file tree
Showing 10 changed files with 67 additions and 22 deletions.
8 changes: 6 additions & 2 deletions CHANGES.rst
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,14 @@ Tequila-django

Changes

v X.Y.Z on MMM dd, YYYY
v 0.9.16 on Aug 6, 2018
-----------------------

* TBD
* New variable ``project_subdir`` to accomodate projects where
the Django project (``manage.py`` etc.) are in a subdirectory of
the repository.
* New variable ``wsgi_module`` to accomodate projects where the
package of the wsgi module is not ``{{ project_name }}.wsgi``.

v 0.9.15 on July 31, 2018
--------------------------
Expand Down
6 changes: 6 additions & 0 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,12 @@ The following variables are used by the ``tequila-django`` role:
- ``github_deploy_key`` **required if source_is_local is false**
- ``local_project_dir`` **required if source_is_local**
- ``extra_env`` **default:** empty dict
- ``project_subdir`` **default:** ``""`` - if a project's main source
directory is a subdir of the git repo checkout top directory, e.g.
manage.py is not in the top directory and you have to cd to a subdirectory
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.
- ``project_port`` **default:** 8000 - what port Django listens on

The ``extra_env`` variable is a dict of keys and values that is
Expand Down
5 changes: 4 additions & 1 deletion defaults/main.yml
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
---
python_version: "2.7"
root_dir: "/var/www/{{ project_name }}"
project_user: "{{ project_name }}"
source_dir: "{{ root_dir }}/src"
venv_dir: "{{ root_dir }}/env"
ssh_dir: "/home/{{ project_user }}/.ssh"
requirements_file: "{{ source_dir }}/requirements/{{ env_name }}.txt"
project_user: "{{ project_name }}"
project_settings: "{{ project_name }}.settings.deploy"
db_name: "{{ project_name }}_{{ env_name }}"
db_user: "{{ project_name }}_{{ env_name }}"
Expand All @@ -22,4 +22,7 @@ is_worker: false
extra_env: {}
celery_events: false
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"
project_port: 8000
12 changes: 8 additions & 4 deletions handlers/main.yml
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
---
# 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: collectstatic
django_manage:
command: collectstatic
app_path: "{{ source_dir }}"
virtualenv: "{{ venv_dir }}"
shell: "{{ root_dir }}/manage.sh collectstatic --noinput"
args:
chdir: "{{ django_dir }}"
become_user: "{{ project_user }}"
vars:
ansible_ssh_pipelining: true
22 changes: 18 additions & 4 deletions tasks/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -177,32 +177,46 @@
when: use_newrelic

- name: set up the project python path
copy: content="{{ source_dir }}"
copy: content="{{ django_dir }}"
dest={{ venv_dir }}/lib/python{{ python_version }}/site-packages/project.pth
owner={{ project_user }}
group={{ project_user }}

# THIS needs to come AFTER syncing the source to the server,
# since that might remove files like .env that weren't in the
# source directory locally.
# .env should be in the Django dir in case the Django program
# looks for it in its own top directory.
- name: create/update .env file
template: >
dest={{ source_dir }}/.env
dest={{ django_dir }}/.env
owner={{ project_user }}
group={{ project_user }}
mode=400
src=envfile.j2
# dotenv.sh is in the Django dir since that's where .env is.
- name: add the dotenv.sh helper script
copy: src=dotenv.sh
dest={{ source_dir }}/dotenv.sh
dest={{ django_dir }}/dotenv.sh
owner={{ project_user }}
group={{ project_user }}
mode=700

# manage.sh is at the very top level, I guess for convenience?
- name: copy shell script wrapper for manage.py
template: src=manage.sh
dest={{ root_dir }}/manage.sh
owner={{ project_user }}
group={{ project_user }}
mode=700

# We're not using django_manage at the moment in tequila, but the cost of
# keeping this is trivial, and it'll avoid mysterious errors if we start using
# django_manage again:
- name: make manage.py executable (because django_manage expects it)
file: >
path={{ source_dir }}/manage.py
path={{ django_dir }}/manage.py
mode=0755
become_user: "{{ project_user }}"
vars:
Expand Down
18 changes: 14 additions & 4 deletions tasks/web.yml
Original file line number Diff line number Diff line change
Expand Up @@ -22,18 +22,28 @@

# 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
django_manage:
command: migrate --noinput -v 0
app_path: "{{ source_dir }}"
virtualenv: "{{ venv_dir }}"
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 gunicorn
supervisorctl: name={{ project_name }}-server state=restarted
# If your program isn't running, telling supervisorctl to restart
# it is an error. I would think that the Ansible module would
# handle that, since they're supposed to be idempotent.
# For now, just ignore errors:
ignore_errors: true

- name: copy shell script wrapper for manage.py
template: src=manage.sh
Expand Down
4 changes: 2 additions & 2 deletions templates/celery.conf
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
[program:{{ project_name }}-celery-{{ name }}]
command={{ source_dir }}/dotenv.sh {% if use_newrelic %}{{ venv_dir }}/bin/newrelic-admin run-program {% endif %}{{ venv_dir }}/bin/celery -A {{ project_name }} {{ command }} {{ flags }}
command={{ django_dir }}/dotenv.sh {% if use_newrelic %}{{ venv_dir }}/bin/newrelic-admin run-program {% endif %}{{ venv_dir }}/bin/celery -A {{ project_name }} {{ command }} {{ flags }}
user={{ project_user }}
directory={{ source_dir }}
directory={{ django_dir }}
autostart=true
autorestart=true
stopasgroup=false
Expand Down
3 changes: 2 additions & 1 deletion templates/envfile.j2
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
{# template for .env file #}
ENVIRONMENT='{{ env_name }}'
PYTHONUNBUFFERED=1
PATH=/usr/local/bin:$PATH
{# the geerlingguy.nodejs role's npm installs global programs in /usr/local/lib/npm/bin #}
PATH=/usr/local/bin:/usr/local/lib/npm/bin:$PATH
DJANGO_SETTINGS_MODULE='{{ project_settings }}'
SECRET_KEY='{{ secret_key }}'
DB_NAME='{{ db_name }}'
Expand Down
7 changes: 5 additions & 2 deletions templates/gunicorn.conf
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
[program:{{ project_name }}-server]
command={{ source_dir }}/dotenv.sh {% if use_newrelic %}{{ venv_dir }}/bin/newrelic-admin run-program {% endif %}{{ venv_dir }}/bin/gunicorn {{ project_name }}.wsgi:application --bind=0.0.0.0:{{ project_port }} --workers={{ gunicorn_num_workers }} {% if gunicorn_num_threads is defined %}--threads={{ gunicorn_num_threads }}{% endif %}
command={{ django_dir }}/dotenv.sh {% if use_newrelic %}{{ venv_dir }}/bin/newrelic-admin run-program {% endif %}{{ venv_dir }}/bin/gunicorn {{ wsgi_module }}:application --log-syslog --bind=0.0.0.0:{{ project_port }} --workers={{ gunicorn_num_workers }} {% if gunicorn_num_threads is defined %}--threads={{ gunicorn_num_threads }}{% endif %}

; The blank line before this is required. Do not remove it please.
; Otherwise the command= and user= lines get joined into one.
; (Maybe due to the previous line ending with Jinja2 ``endif``?)
user={{ project_user }}
directory={{ source_dir }}
directory={{ django_dir }}
autostart=true
autorestart=true
stopasgroup=false
Expand Down
4 changes: 2 additions & 2 deletions templates/manage.sh
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
# Shell script to run a management command
cd {{ source_dir }}
{{ venv_dir }}/bin/python {{ source_dir }}/manage.py $@
cd {{ django_dir }}
./dotenv.sh {{ venv_dir }}/bin/python {{ django_dir }}/manage.py $@

0 comments on commit 65ee6b8

Please sign in to comment.