Skip to content

Commit

Permalink
Added blank lines to improve readability
Browse files Browse the repository at this point in the history
Changed local_settings path to enable tox tests to work
Enabled db user to create databases to enable tests to work
Moved nginx site config to sites-available and created a link to sites-enabled
  • Loading branch information
desecho committed Mar 17, 2016
1 parent 55fc189 commit 03e21da
Show file tree
Hide file tree
Showing 10 changed files with 49 additions and 16 deletions.
6 changes: 1 addition & 5 deletions defaults/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,17 @@
django_name: django
# Whether the project is in python 3
django_ispython3: yes
# python executable name. re-used in other variables below
django_pythonexec: "{{ django_ispython3 | ternary('python3.4', 'python2.7') }}"
# Root path of the django project
django_path: "/opt/{{ django_name }}"
# Domain name under which the project is served. Ends up in ALLOWED_HOSTS.
django_domain_name: "{{ django_name }}.vagrant.dev"
# Path of the virtualenv where we install python dependencies
django_env: "{{ django_path }}/env"
# Path for the site-packages folder inside the virtualenv (you normally don't have to change this
# unless it's the wrong path). This is where we go place the "local_settings.py" file.
django_sitepackages_path: "{{ django_env }}/lib/{{ django_pythonexec }}/site-packages"
# Path where the source of the project is placed.
django_project_path: "{{ django_path }}/project"
# Path, inside the project's source, where "manage.py" lives.
django_src_path: "{{ django_project_path }}/src"
django_settings_path: "{{ django_src_path }}/demo"
# Path for STATIC_ROOT
django_static_path: "{{ django_path }}/static"
# Path for MEDIA_ROOT
Expand Down
2 changes: 1 addition & 1 deletion handlers/main.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
---
- name: nginx restart
service: name=nginx state=restarted

- name: wsgi restart
service: name=django-wsgi state=restarted

15 changes: 13 additions & 2 deletions tasks/django.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,21 @@
- libjpeg-dev # pillow
- libpng12-dev # pillow
- gettext

- name: Ensure py2 packages needed for Django are present
apt: name={{ item }} state=latest
with_items:
- python-dev
- python-virtualenv
when: not django_ispython3

- name: Ensure py3 packages needed for Django are present
apt: name={{ item }} state=latest
with_items:
- python3-dev
- python3-venv
when: django_ispython3

- name: Ensure required folders are there
file: path={{ item }} state=directory owner={{ django_user }} group={{ django_user }}
with_items:
Expand All @@ -28,37 +31,45 @@
- name: Create a virtualenv (py3) for django
command: "/usr/bin/python3 -m venv --system-site-packages {{ django_env }} creates={{ django_env }}"
when: django_ispython3

- name: Create a virtualenv (py2) for django
command: "virtualenv {{ django_env }} --system-site-packages creates={{ django_env }}"
when: not django_ispython3

- name: Install django requirements
command: "{{ django_env }}/bin/pip install -r {{ django_requirements_file }}"
register: pip_install_result
changed_when: "'Successfully installed' in pip_install_result.stdout"

- name: Create local_settings.py
template: src=local_settings.py dest={{ django_path }}/local_settings.py
notify:
- wsgi restart
- wsgi restart

- name: Symlink local_settings.py into our venv's site-packages
file:
path: "{{ django_sitepackages_path }}/local_settings.py"
path: "{{ django_settings_path }}/local_settings.py"
state: link
src: "{{ django_path }}/local_settings.py"

- name: Migrate
django_manage:
app_path: "{{ django_src_path }}"
command: migrate
virtualenv: "{{ django_env }}"

- name: Load fixtures
django_manage:
app_path: "{{ django_src_path }}"
command: "loaddata {{ item }}"
virtualenv: "{{ django_env }}"
with_items: "{{ django_fixtures_to_load }}"

- name: Collect static
django_manage:
app_path: "{{ django_src_path }}"
command: collectstatic
link: yes
virtualenv: "{{ django_env }}"
become: yes
become_user: "{{ django_user }}"
2 changes: 2 additions & 0 deletions tasks/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,10 @@
fail:
msg: "django_dbtype must be either 'postgres' or 'mariadb'"
when: "django_dbtype not in ['postgres', 'mariadb']"

- include: postgres.yml
when: django_dbtype == 'postgres'

- include: mariadb.yml
when: django_dbtype == 'mariadb'

Expand Down
3 changes: 3 additions & 0 deletions tasks/mariadb.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,13 @@
- mariadb-client
- libmysqlclient-dev # for Python's mysqlclient pkg
- python-mysqldb # for ansible's mysql module

- name: Ensure that mariadb is started
service: name=mysql state=started enabled=yes

- name: Ensure that we have a DB
mysql_db: "name={{ django_dbname }}"

- name: Ensure that we have a DB user
mysql_user:
name: "{{ django_dbuser }}"
Expand Down
3 changes: 3 additions & 0 deletions tasks/mariadb_importdump.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,20 @@
stat:
path: "{{ django_initialdump_path }}"
register: localdump_stat_result

- name: Import local dump into mysql db
mysql_db:
name: "{{ django_dbname }}"
state: import
target: "{{ django_initialdump_path }}"
when: localdump_stat_result.stat.exists

- name: Copy DB dump over
copy:
src: "{{ django_initialdump_path }}"
dest: "/tmp/{{ django_initialdump_path | basename }}"
when: not localdump_stat_result.stat.exists

- name: Import copied dump into mysql db
mysql_db:
name: "{{ django_dbname }}"
Expand Down
14 changes: 11 additions & 3 deletions tasks/nginx.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,20 @@
apt: name={{ item }} state=latest
with_items:
- nginx

- name: Delete default site
file: path=/etc/nginx/sites-enabled/default state=absent
notify: nginx restart

- name: Create nginx sites config
template: src={{item}}.conf dest=/etc/nginx/sites-enabled/{{item}}.conf
with_items: "{{nginx_config_files}}"
template: src={{ item }}.conf dest=/etc/nginx/sites-available/{{ item }}.conf
with_items: "{{ nginx_config_files }}"

- name: Create symbolic links for sites config to enable sites
file:
path: "/etc/nginx/sites-enabled/{{ item }}.conf"
state: link
src: "/etc/nginx/sites-available/{{ item }}.conf"
with_items: "{{ nginx_config_files }}"
notify:
- nginx restart

7 changes: 6 additions & 1 deletion tasks/postgres.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,22 +7,27 @@
- postgresql-server-dev-all
- libpq-dev # for psycopg
- python-psycopg2 # for ansible's postgresql module

- name: Ensure that postgresql is started
service: name=postgresql state=started enabled=yes

- name: Is postgres REALLY started?
shell: pg_isready
ignore_errors: yes
register: pg_isready_result
# This is due to a weird locale error happening when apt-get installing postgres over SSH. For some
# weird locale reason, apt-get is unable to create the clusters and asks to run the command below
# manually. If we run it, it works (strangely). So there we go, we run it.

- name: Ensure that our cluster is created
shell: "pg_createcluster 9.4 main --start"
when: pg_isready_result|failed
- block:
- name: Ensure that we have a DB
postgresql_db: "name={{ django_dbname }}"

- name: Ensure that we have a DB user
postgresql_user: "name={{ django_dbuser }} db={{ django_dbname }} password={{ django_dbpass }} priv=ALL"
postgresql_user: "name={{ django_dbuser }} db={{ django_dbname }} password={{ django_dbpass }} priv=ALL role_attr_flags=CREATEDB"

become: yes
become_user: postgres
1 change: 1 addition & 0 deletions tasks/srcsync.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
dest: "{{ django_project_path }}"
accept_hostkey: yes
recursive: no

- name: Fix synced src's permissions
file:
path: "{{ django_src_path }}"
Expand Down
12 changes: 8 additions & 4 deletions tasks/wsgi.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,23 @@
- uwsgi
- uwsgi-plugin-python
- uwsgi-plugin-python3

- name: Create uWSGI config
template: src=uwsgi.ini dest={{ django_path }}/uwsgi.ini
notify:
- wsgi restart
- nginx restart
- wsgi restart
- nginx restart

- name: Setup systemd service
template: src=uwsgi_systemd dest=/etc/systemd/system/django-wsgi.service owner=root group=root mode=0644
register: wsgi_service
notify:
- wsgi restart
- nginx restart
- wsgi restart
- nginx restart

- name: Reload systemd because we've just updated a service
shell: systemctl daemon-reload
when: wsgi_service|changed

- name: Ensure that the wsgi service is enabled
service: name=django-wsgi enabled=yes

0 comments on commit 03e21da

Please sign in to comment.