Skip to content

Commit 38cbf36

Browse files
committed
Initial commit
0 parents  commit 38cbf36

File tree

13 files changed

+505
-0
lines changed

13 files changed

+505
-0
lines changed

.gitignore

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
### JetBrains
2+
# Covers JetBrains IDEs: IntelliJ, RubyMine, PhpStorm, AppCode, PyCharm, CLion, Android Studio
3+
## Directory-based project format:
4+
.idea/

CHANGELOG

Whitespace-only changes.

CONTRIBUTING.rst

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
============
2+
Contributing
3+
============
4+
5+
Any contributions are much appreciated.
6+
7+
Authors
8+
-------
9+
10+
1. George Karakostas <gckarakostas@gmail.com>

LICENSE

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
Copyright (c) George Karakostas and individual contributors.
2+
All rights reserved.
3+
4+
Redistribution and use in source and binary forms, with or without modification,
5+
are permitted provided that the following conditions are met:
6+
7+
1. Redistributions of source code must retain the above copyright notice,
8+
this list of conditions and the following disclaimer.
9+
10+
2. Redistributions in binary form must reproduce the above copyright
11+
notice, this list of conditions and the following disclaimer in the
12+
documentation and/or other materials provided with the distribution.
13+
14+
3. Neither the name of Wtower/ansible-django-deploy nor the names of its contributors may be used
15+
to endorse or promote products derived from this software without
16+
specific prior written permission.
17+
18+
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
19+
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
20+
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
21+
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
22+
ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
23+
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
24+
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
25+
ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26+
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
27+
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

README.md

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
django-deploy
2+
=============
3+
4+
Ansible role to deploy a Django app to Plesk.
5+
6+
Add the role `django-deploy`.
7+
8+
Required variables
9+
------------------
10+
11+
(passed as extra variables):
12+
13+
- `project`: **Django project name**, will also be used as virtualenv name.
14+
15+
Optional variables
16+
------------------
17+
18+
(passed as extra variables):
19+
20+
- `loaddata`: A list of the dump files to load data from, eg `[project, auth]`, omit to skip loaddata.
21+
- `newrelic`: New Relic license key. Omit to skip New Relic. Recommended to add a default in playbook.
22+
23+
Required Configuration variables
24+
--------------------------------
25+
26+
(better to keep in configuration file as shown in examples):
27+
28+
- `conf`: A dictionary to help organize variables, containing:
29+
- `conf.app_name`: The application name (human description), used in New Relic too.
30+
- `conf.domain_name`: Domain name to be used in Apache conf. Omit to skip configure Apache in Plesk.
31+
- `conf.dev_path`: The local path of the project, eg ~/workspace/python/myproject.
32+
- `conf.host_path`: The remote path, where the wsgi index script lies, eg /var/www/vhosts/subscription/domain.
33+
- `conf.host_user`: The remote user to whom to transfer ownership after operations.
34+
35+
Optional Configuration variables
36+
--------------------------------
37+
38+
- `conf.name`: The application name (machine name), the same as the Python app. Default: `{{ project }}`
39+
- `conf.python_version`: The python version to install (major.minor), default: `3.5`.
40+
- `conf.wsgi_script`: The wsgi script, default: `index.wsgi`.
41+
- `conf.compile_msgs`: Set to `true` in order to compile i18n messages.
42+
- `conf.memcached`: Set to `true` in order to clear cache.
43+
44+
Other default variables
45+
-----------------------
46+
47+
- `ansible_user_id`: The user id with access to mysql and mysql database.
48+
- `virtualenv_dir`: Virtualenvs home directory. Set to `false` to disable virtualenv. Default: `/var/virtualenvs`.
49+
- `django_settings`: The Django settings module (.py) to run manage.py from, default: `settings_live`.
50+
- `plesk_server_group`: Plesk server group, default: `psaserv`.
51+
- `plesk_account_group`: Plesk account group, default: `psacln`.
52+
53+
Playbook examples
54+
-----------------
55+
56+
See the folder `examples/`:
57+
58+
- `django_deploy.yml`: playbook example
59+
- `django_sites.yml`: configuration example

examples/django_deploy.yml

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
---
2+
# Deploy Django
3+
#
4+
# Extra variables
5+
# - host
6+
# - project
7+
# - loaddata
8+
9+
- hosts: "{{ host }}"
10+
11+
gather_facts: no
12+
13+
vars_files:
14+
- ../conf/django_sites.yml
15+
16+
vars:
17+
conf: "{{ django_sites[project] }}"
18+
newrelic: 36bbf1bbae9a3790c9c254xxxxxx
19+
20+
pre_tasks:
21+
- name: Checking if disabled
22+
fail: msg="Site [{{ project }}] is disabled."
23+
when: django_sites[project].disabled is defined
24+
25+
roles:
26+
- django_deploy

examples/django_sites.yml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
---
2+
django_sites:
3+
myproject:
4+
name: My Project
5+
domain_name: myproject.com
6+
host_path: /var/www/vhosts/mysite.com/myproject.com
7+
python_version: 3.4

handlers/main.yml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
---
2+
- name: Reconfigure Plesk domain
3+
shell: "/usr/local/psa/admin/sbin/httpdmng --reconfigure-domain {{ conf.domain_name }}"
4+
become: true
5+
6+
- name: Restart Apache
7+
service: name=apache2 state=restarted
8+
become: true

meta/main.yml

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
---
2+
galaxy_info:
3+
author: George Karakostas
4+
description: Deploy Django on Plesk
5+
company: 9dev
6+
license: BSD
7+
min_ansible_version: 2.1
8+
platforms:
9+
- name: Ubuntu
10+
versions:
11+
- lucid
12+
- maverick
13+
- natty
14+
- oneiric
15+
- precise
16+
- quantal
17+
- raring
18+
- saucy
19+
- trusty
20+
- utopic
21+
- vivid
22+
- wily
23+
galaxy_tags:
24+
- system
25+
- development
26+
- web
27+
28+
dependencies: []

tasks/main.yml

Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
---
2+
# Deploys a Django app to Plesk
3+
4+
- name: Set Python executable
5+
set_fact: python="`which python{{ conf.python_version | default('3.5') }}`"
6+
7+
- name: Make virtualenv
8+
shell: "source ~/.bash_profile && mkvirtualenv {{ project }} --python={{ python }} --no-site-packages"
9+
args:
10+
executable: /bin/bash
11+
creates: "{{ virtualenv_dir }}/{{ project }}"
12+
register: mkvirtualenv
13+
failed_when: 'mkvirtualenv.changed and "New python executable" not in mkvirtualenv.stdout'
14+
15+
- name: Configure Apache
16+
template: src="vhost.conf.j2" dest="/var/www/vhosts/{{ conf.domain_name }}/conf/vhost.conf" backup=yes
17+
become: true
18+
when: conf.domain_name is defined
19+
notify:
20+
- Reconfigure Plesk domain
21+
- Restart Apache
22+
23+
- name: Configure New Relic
24+
template: src="newrelic.ini.j2" dest="{{ virtualenv_dir }}/{{ project }}.newrelic.ini" backup=yes
25+
when: newrelic is defined
26+
27+
- name: Open ownership
28+
file: name="{{ conf.host_path }}" owner="{{ ansible_user_id }}" mode=0775 state=directory recurse=yes
29+
become: true
30+
31+
- name: Sync files
32+
synchronize:
33+
src: "{{ conf.dev_path }}/"
34+
dest: "{{ conf.host_path }}"
35+
delete: yes
36+
rsync_opts:
37+
- "--exclude=.*"
38+
39+
- name: Upgrade pip
40+
pip:
41+
name: "{{ item }}"
42+
state: latest
43+
virtualenv: "{{ virtualenv_dir }}/{{ project }}"
44+
executable: "{{ virtualenv_dir }}/{{ project }}/bin/pip"
45+
with_items:
46+
- pip
47+
- setuptools
48+
49+
- name: Install pip requirements
50+
pip:
51+
requirements: "{{ conf.host_path }}/requirements.txt"
52+
virtualenv: "{{ virtualenv_dir }}/{{ project }}"
53+
executable: "{{ virtualenv_dir }}/{{ project }}/bin/pip"
54+
55+
- name: Set Django admin executable
56+
set_fact: manage="{{ virtualenv_dir }}/{{ project }}/bin/python {{ conf.host_path }}/manage.py"
57+
58+
- name: Set Django admin settings
59+
set_fact: settings="--settings={{ conf.name | default(project) }}.{{ django_settings }}"
60+
61+
- name: Migrate
62+
shell: "{{ manage }} migrate {{ settings }}"
63+
register: migrate_result
64+
changed_when: '"No migrations to apply." not in migrate_result.stdout'
65+
66+
- name: Load data
67+
shell: "{{ manage }} loaddata {{ item }} {{ settings }}"
68+
when: loaddata is defined and loaddata == 'true'
69+
with_items: conf.data
70+
register: loaddata_results
71+
failed_when: '"Installed" not in loaddata_results.stdout'
72+
73+
- name: Collect static
74+
shell: "{{ manage }} collectstatic --noinput {{ settings }}"
75+
76+
- name: Compile messages
77+
shell: "{{ manage }} compilemessages {{ settings }}"
78+
when: conf.compile_msgs is defined and conf.compile_msgs
79+
80+
- name: Clear cache
81+
shell: "{{ manage }} cache_clear {{ settings }}"
82+
when: conf.memcached is defined and conf.memcached
83+
84+
- name: Close ownership to Plesk account group
85+
file:
86+
name: "{{ conf.host_path }}"
87+
owner: "{{ conf.host_user }}"
88+
group: "{{ plesk_account_group }}"
89+
mode: 0775
90+
state: directory
91+
recurse: yes
92+
become: true
93+
94+
- name: Close ownership to Plesk server group
95+
file:
96+
name: "{{ conf.host_path }}"
97+
owner: "{{ conf.host_user }}"
98+
group: "{{ plesk_server_group }}"
99+
state: directory
100+
become: true

0 commit comments

Comments
 (0)