Skip to content

Commit

Permalink
Update the role
Browse files Browse the repository at this point in the history
  • Loading branch information
klen committed Mar 9, 2016
1 parent 7d190d6 commit 65280c2
Show file tree
Hide file tree
Showing 20 changed files with 126 additions and 97 deletions.
1 change: 0 additions & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ python: "2.7"
before_install:
- sudo apt-get update -qq
- sudo apt-get install -qq python-apt python-pycurl
- git clone https://github.com/Stouts/Stouts.deploy roles/Stouts.deploy
- git clone https://github.com/Stouts/Stouts.nginx roles/Stouts.nginx
- git clone https://github.com/Stouts/Stouts.python roles/Stouts.python
- env
Expand Down
25 changes: 12 additions & 13 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ Ansible role which deploys wsgi application (uwsgi/gunicorn, nginx, virtualenv)

#### Requirements & Dependencies

- [Stouts.deploy](https://github.com/Stouts/Stouts.deploy)
- [Stouts.python](https://github.com/Stouts/Stouts.python)
- [Stouts.nginx](https://github.com/Stouts/Stouts.nginx)

Expand All @@ -26,28 +25,27 @@ The role variables and default values.
```yaml
wsgi_enabled: yes # Enable the role

wsgi_user: "{{deploy_user}}" # An user to run WSGI applications
wsgi_group: "{{deploy_group}}" # A group to run WSGI applications
wsgi_user: "{{deploy_user|default(ansible_user)}}" # An user to run WSGI applications
wsgi_group: "{{deploy_group|default(wsgi_user)}}" # A group to run WSGI applications
wsgi_server: uwsgi # A server which provide wsgi integration (uwsgi/gunicorn)
wsgi_proxy: nginx # A proxy http server (nginx)
wsgi_proxy_params: |
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header Host $http_host;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
wsgi_reload: no # Reload servers when code changes

wsgi_pip_packages: [] # Pip packages to install

wsgi_applications: # Setup your wsgi application here
- name: "{{deploy_app_name}}"
pip_requirements: "{{wsgi_app_dir}}/requirements.txt"

# Default directories
wsgi_run_dir: "{{deploy_run_dir}}"
wsgi_etc_dir: "{{deploy_etc_dir}}"
wsgi_log_dir: "{{deploy_log_dir}}"
wsgi_app_dir: "{{deploy_src_dir}}"
- name: "{{deploy_app_name|default('wsgi')}}"

# Default virtualenv options
wsgi_virtualenv: "{{deploy_dir}}/env"
wsgi_virtualenv: "{{deploy_dir|default('/opt/wsgi')}}/env"
wsgi_virtualenv_python: python

wsgi_service: upstart

# UWSGI default options (can be redefined for each wsgi app)
wsgi_uwsgi_enable_threads: no
wsgi_uwsgi_read_timeout: 300
Expand All @@ -57,6 +55,7 @@ wsgi_uwsgi_no_orphans: yes
wsgi_uwsgi_options: []

# Gunicorn default options (can be redefined for each wsgi app)
wsgi_gunicorn_command: gunicorn
wsgi_gunicorn_worker: sync
wsgi_gunicorn_workers: 4

Expand All @@ -66,12 +65,12 @@ wsgi_nginx_redirect_www: no # Redirect www.servername
wsgi_nginx_options: []
wsgi_nginx_servernames: "{{inventory_hostname}}" # Listen servernames (separated by space)
wsgi_nginx_ssl: no # Set yes to enable SSL
wsgi_nginx_ssl_protocols: TLSv1 TLSv1.1 TLSv1.2 # Enabled SSL protocols
wsgi_nginx_ssl_crt: # Path to certificate bundle
wsgi_nginx_ssl_key: # Path to certificate key
wsgi_nginx_ssl_port: 443
wsgi_nginx_ssl_redirect: no # Redirect to SSL port
wsgi_nginx_static_locations: [/static/, /media/]
wsgi_nginx_static_root: "{{wsgi_app_dir}}"

# Logging options
wsgi_log_rotate: yes # Rotate wsgi logs.
Expand Down
22 changes: 7 additions & 15 deletions defaults/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,30 +3,23 @@

wsgi_enabled: yes # Enable the role

wsgi_user: "{{deploy_user}}" # An user to run WSGI applications
wsgi_group: "{{deploy_group}}" # A group to run WSGI applications
wsgi_user: "{{deploy_user|default(ansible_user)}}" # An user to run WSGI applications
wsgi_group: "{{deploy_group|default(wsgi_user)}}" # A group to run WSGI applications
wsgi_server: uwsgi # A server which provide wsgi integration (uwsgi/gunicorn)
wsgi_proxy: nginx # A proxy http server (nginx)
wsgi_proxy_params: |
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header Host $http_host;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header Host $http_host;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
wsgi_reload: no # Reload servers when code changes

wsgi_pip_packages: [] # Pip packages to install

wsgi_applications: # Setup your wsgi application here
- name: "{{deploy_app_name}}"
pip_requirements: "{{wsgi_app_dir}}/requirements.txt"

# Default directories
wsgi_run_dir: "{{deploy_run_dir}}"
wsgi_etc_dir: "{{deploy_etc_dir}}"
wsgi_log_dir: "{{deploy_log_dir}}"
wsgi_app_dir: "{{deploy_src_dir}}"
- name: "{{deploy_app_name|default('wsgi')}}"

# Default virtualenv options
wsgi_virtualenv: "{{deploy_dir}}/env"
wsgi_virtualenv: "{{deploy_dir|default('/opt/wsgi')}}/env"
wsgi_virtualenv_python: python

wsgi_service: upstart
Expand Down Expand Up @@ -56,7 +49,6 @@ wsgi_nginx_ssl_key: # Path to certificate key
wsgi_nginx_ssl_port: 443
wsgi_nginx_ssl_redirect: no # Redirect to SSL port
wsgi_nginx_static_locations: [/static/, /media/]
wsgi_nginx_static_root: "{{wsgi_app_dir}}"

# Logging options
wsgi_log_rotate: yes # Rotate wsgi logs.
Expand Down
4 changes: 2 additions & 2 deletions handlers/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@

- name: wsgi restart
service: name={{item.name}}-wsgi state=restarted
with_items: wsgi_applications
with_items: "{{wsgi_applications}}"

- name: wsgi reload
service: name={{item.name}}-wsgi state=reloaded
with_items: wsgi_applications
with_items: "{{wsgi_applications}}"
1 change: 0 additions & 1 deletion meta/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
# vim:sw=2:ft=ansible

dependencies:
- Stouts.deploy
- Stouts.python
- Stouts.nginx

Expand Down
16 changes: 8 additions & 8 deletions tasks/configure.yml
Original file line number Diff line number Diff line change
@@ -1,36 +1,36 @@
---

- name: Setup py configuration file
copy: content="{{ item.cfg.content|default(item.cfg) }}" dest={{ item.cfg.path|default(wsgi_etc_dir + '/config.py') }}
copy: content="{{ item.cfg.content|default(item.cfg) }}" dest={{ item.cfg.path|default("/opt/%s/etc/config.py" % item.name) }}
when: item.cfg|default(None)
with_items: wsgi_applications
with_items: "{{wsgi_applications}}"
notify: wsgi restart

- name: Setup uWSGI applications
template: src=uwsgi.ini.j2 dest={{item.etc_dir|default(wsgi_etc_dir)}}/{{item.name}}-uwsgi.ini owner={{wsgi_user}} group={{wsgi_group}}
template: src=uwsgi.ini.j2 dest={{item.etc_dir|default("/opt/%s/etc" % item.name)}}/{{item.name}}-uwsgi.ini owner={{wsgi_user}} group={{wsgi_group}}
when: item.server|default(wsgi_server) == 'uwsgi'
with_items: wsgi_applications
with_items: "{{wsgi_applications}}"
notify: wsgi restart

- name: Setup Upstart
template: src=upstart-{{item.server|default(wsgi_server)}}.conf.j2 dest=/etc/init/{{item.name}}-wsgi.conf owner=root group=root mode=0644
with_items: wsgi_applications
with_items: "{{wsgi_applications}}"
when: wsgi_service == 'upstart'
notify: wsgi restart

- name: Setup Init
template: src=init-{{item.server|default(wsgi_server)}}.j2 dest=/etc/init.d/{{item.name}}-wsgi owner=root group=root mode=0755
with_items: wsgi_applications
with_items: "{{wsgi_applications}}"
when: wsgi_service == 'init'
notify: wsgi restart

- name: Setup SystemD
template: src=systemd-{{item.server|default(wsgi_server)}}.j2 dest=/etc/systemd/system/{{item.name}}-wsgi.service owner=root group=root mode=0644
with_items: wsgi_applications
with_items: "{{wsgi_applications}}"
when: wsgi_service == 'systemd'
notify: wsgi restart

- name: Ensure that the services are started
service: name={{item.name}}-wsgi state=started enabled=yes
ignore_errors: yes
with_items: wsgi_applications
with_items: "{{wsgi_applications}}"
12 changes: 6 additions & 6 deletions tasks/dirs.yml
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@

- name: Prepare configuration directories
file: state=directory path={{item.etc_dir|default(wsgi_etc_dir)}} owner={{wsgi_user}} group={{wsgi_group}}
with_items: wsgi_applications
file: state=directory path={{item.etc_dir|default("/opt/%s/etc" % item.name)}} owner={{wsgi_user}} group={{wsgi_group}}
with_items: "{{wsgi_applications}}"

- name: Prepare run directories
file: state=directory path={{item.run_dir|default(wsgi_run_dir)}} owner={{wsgi_user}} group={{wsgi_group}}
with_items: wsgi_applications
file: state=directory path={{item.run_dir|default("/opt/%s/run" % item.name)}} owner={{wsgi_user}} group={{wsgi_group}}
with_items: "{{wsgi_applications}}"

- name: Prepare log directories
file: state=directory path={{item.log_dir|default(wsgi_log_dir)}} owner={{wsgi_user}} group={{wsgi_group}}
with_items: wsgi_applications
file: state=directory path={{item.log_dir|default("/opt/%s/log" % item.name)}} owner={{wsgi_user}} group={{wsgi_group}}
with_items: "{{wsgi_applications}}"
16 changes: 10 additions & 6 deletions tasks/virtualenv.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,16 @@
# vim:sw=2:ft=ansible

- name: Setup virtualenv for applications pt. 1
template: src=virtualenv.sh dest={{item.etc_dir|default(wsgi_etc_dir)}}/{{item.name}}.sh mode=0755 owner={{wsgi_user}} group={{wsgi_group}}
with_items: wsgi_applications
template:
dest: "{{item.etc_dir|default('/opt/%s/etc' % item.name)}}/virtualenv.sh"
group: "{{wsgi_group}}"
mode: 0755
owner: "{{wsgi_user}}"
src: virtualenv.sh
with_items: "{{wsgi_applications}}"

- name: Setup virtualenv for applications pt. 2
shell: "{{item.etc_dir|default(wsgi_etc_dir)}}/{{item.name}}.sh"
with_items: wsgi_applications
shell: "{{item.etc_dir|default('/opt/%s/etc' % item.name)}}/virtualenv.sh"
with_items: "{{wsgi_applications}}"
changed_when: False
sudo: yes
sudo_user: "{{wsgi_user}}"
become_user: "{{wsgi_user}}"
10 changes: 5 additions & 5 deletions tasks/wsgi.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,21 +12,21 @@
tags: [wsgi, wsgi-configure]

- name: Setup Nginx configurations
template: src=nginx.conf.j2 dest={{item.etc_dir|default(wsgi_etc_dir)}}/{{item.name}}-nginx.conf owner={{wsgi_user}} group={{wsgi_group}}
template: src=nginx.conf.j2 dest={{item.etc_dir|default("/opt/%s/etc" % item.name)}}/{{item.name}}-nginx.conf owner={{wsgi_user}} group={{wsgi_group}}
when: item.proxy|default(wsgi_proxy) == 'nginx'
with_items: wsgi_applications
with_items: "{{wsgi_applications}}"
notify: nginx reload
tags: [wsgi, wsgi-nginx]

- name: Enable nginx sites
file: state=link dest={{nginx_sites_dir}}/{{item.name}}.conf src={{item.etc_dir|default(wsgi_etc_dir)}}/{{item.name}}-nginx.conf
file: state=link dest={{nginx_sites_dir}}/{{item.name}}.conf src={{item.etc_dir|default("/opt/%s/etc" % item.name)}}/{{item.name}}-nginx.conf
when: item.proxy|default(wsgi_proxy) == 'nginx'
with_items: wsgi_applications
with_items: "{{wsgi_applications}}"
tags: [wsgi, wsgi-nginx]

- name: Configure log rotation
template: src=logrotate.conf.j2 dest=/etc/logrotate.d/{{item.name}}-wsgi.conf
with_items: wsgi_applications
with_items: "{{wsgi_applications}}"
when: wsgi_log_rotate
tags: [wsgi, wsgi-logrotate]

Expand Down
15 changes: 9 additions & 6 deletions templates/init-gunicorn.j2
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,16 @@
# Do NOT modify this file by hand!

{% set app = item -%}
{% set run_dir = item.run_dir|default("/opt/%s/run" % item.name) -%}
{% set log_dir = item.log_dir|default("/opt/%s/log" % item.name) -%}
{% set env_dir = item.virtualenv|default("/opt/%s/env" % item.name) -%}

# gunicorn - Startup script for gunicorn
#
# chkconfig: - 85 15
# processname: {{app.gunicorn_command|default(wsgi_gunicorn_command)}}
# config: /etc/sysconfig/$prog
# pidfile: {{app.run_dir|default(wsgi_run_dir)}}/{{app.name}}.pid
# pidfile: {{run_dir}}/{{app.name}}.pid
# description: Gunicorn is a program to run applications adhering to the Web Server Gateway Interface.

# Setting `prog` here allows you to symlink this init script, making it easy
Expand All @@ -24,14 +27,14 @@ PROG="$(basename $0)"
# on RHEL systems.
[ -f "/etc/sysconfig/$PROG" ] && . /etc/sysconfig/$PROG

GUNICORN={{app.virtualenv|default(wsgi_virtualenv)}}/bin/{{app.gunicorn_command|default(wsgi_gunicorn_command)}}
PIDFILE="{{app.run_dir|default(wsgi_run_dir)}}/{{app.name}}.pid"
GUNICORN={{env_dir}}/bin/{{app.gunicorn_command|default(wsgi_gunicorn_command)}}
PIDFILE="{{run_dir}}/{{app.name}}.pid"
LOCKFILE="/var/lock/subsys/{{app.name}}"
OPTS="\
--bind={{app.socket|default('unix:' + app.get('run_dir', wsgi_run_dir) + '/' + app.name)}} \
--log-file={{app.log_dir|default(wsgi_log_dir)}}/{{app.name}}-wsgi.log \
--bind={{app.socket|default('unix:' + run_dir) + '/' + app.name)}} \
--log-file={{log_dir}}/{{app.name}}-wsgi.log \
--name={{app.name}} \
--pid={{app.run_dir|default(wsgi_run_dir)}}/{{app.name}}.pid \
--pid={{run_dir}}/{{app.name}}.pid \
--worker-class={{app.gunicorn_worker|default(wsgi_gunicorn_worker)}} \
--workers={{app.gunicorn_workers|default(wsgi_gunicorn_workers)}} \
{% if wsgi_reload %}--reload {% endif %}"
Expand Down
12 changes: 8 additions & 4 deletions templates/init-uwsgi.j2
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,18 @@ PROG="$(basename $0)"
[ -f "/etc/sysconfig/$PROG" ] && . /etc/sysconfig/$PROG

{% set app = item -%}
{% set run_dir = item.run_dir|default("/opt/%s/run" % item.name) -%}
{% set log_dir = item.log_dir|default("/opt/%s/log" % item.name) -%}
{% set etc_dir = item.etc_dir|default("/opt/%s/etc" % item.name) -%}
{% set env_dir = item.virtualenv|default("/opt/%s/env" % item.name) -%}

UWSGI={{app.virtualenv|default(wsgi_virtualenv)}}/bin/uwsgi
PIDFILE="{{app.run_dir|default(wsgi_run_dir)}}/pid"
UWSGI={{env_dir}}/bin/uwsgi
PIDFILE="{{run_dir}}/pid"
LOCKFILE="/var/lock/subsys/{{app.name}}"
OPTS="\
--daemonize {{app.log_dir|default(wsgi_log_dir)}}/{{app.name}}-wsgi.log \
--daemonize {{log_dir}}/{{app.name}}-wsgi.log \
--pidfile $PIDFILE \
--ini {{app.etc_dir|default(wsgi_etc_dir)}}/{{app.name}}-uwsgi.ini"
--ini {{etc_dir}}/{{app.name}}-uwsgi.ini"
RETVAL=0

start() {
Expand Down
7 changes: 4 additions & 3 deletions templates/logrotate.conf.j2
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,22 @@
# Do NOT modify this file by hand!

{% set app = item %}
{% set log_dir = item.log_dir|default("/opt/%s/log" % item.name) -%}

# Rotate wsgi log
{{app.log_dir|default(wsgi_log_dir)}}/{{app.name}}-wsgi.log {
{{log_dir}}/{{app.name}}-wsgi.log {
{% for option in wsgi_log_rotate_options %}
{{ option }}
{% endfor %}
}
# Rotate access log
{{app.log_dir|default(wsgi_log_dir)}}/{{app.name}}-nginx-access.log {
{{log_dir}}/{{app.name}}-nginx-access.log {
{% for option in wsgi_log_rotate_options %}
{{ option }}
{% endfor %}
}
# Rotate errors log
{{app.log_dir|default(wsgi_log_dir)}}/{{app.name}}-nginx-error.log {
{{log_dir}}/{{app.name}}-nginx-error.log {
{% for option in wsgi_log_rotate_options %}
{{ option }}
{% endfor %}
Expand Down
10 changes: 6 additions & 4 deletions templates/nginx.conf.j2
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@
# Do NOT modify this file by hand!

{% set app = item %}
{% set run_dir = app.run_dir|default(wsgi_run_dir) %}
{% set app_dir = app.app_dir|default("/opt/%s/src" % item.name) %}
{% set run_dir = app.run_dir|default("/opt/%s/run" % item.name) %}
{% set log_dir = app.log_dir|default("/opt/%s/log" % item.name) %}
{% set socket = app.socket|default(run_dir + '/' + app.name) %}
{% set servernames = app.nginx_servernames|default(wsgi_nginx_servernames) %}
{% set port = app.nginx_port|default(wsgi_nginx_port) %}
Expand All @@ -25,8 +27,8 @@ server {
ssl_certificate_key {{wsgi_nginx_ssl_key}};
{% endif %}

access_log {{app.log_dir|default(wsgi_log_dir) + '/' + app.name + '-nginx-access.log'}};
error_log {{app.log_dir|default(wsgi_log_dir) + '/' + app.name + '-nginx-error.log'}};
access_log {{log_dir + '/' + app.name + '-nginx-access.log'}};
error_log {{log_dir + '/' + app.name + '-nginx-error.log'}};

charset utf-8;
client_max_body_size 15M;
Expand All @@ -50,7 +52,7 @@ server {
{% for location in app.nginx_static_locations|default(wsgi_nginx_static_locations) %}
location {{location}} {
access_log off;
root {{app.nginx_static_root|default(wsgi_nginx_static_root)}};
root {{app.nginx_static_root|default(app_dir)}};
expires max;
}
{% endfor -%}
Expand Down
9 changes: 7 additions & 2 deletions templates/systemd-gunicorn.j2
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,11 @@
# Do NOT modify this file by hand!

{% set app = item -%}
{% set run_dir = item.run_dir|default("/opt/%s/run" % item.name) -%}
{% set log_dir = item.log_dir|default("/opt/%s/log" % item.name) -%}
{% set etc_dir = item.etc_dir|default("/opt/%s/etc" % item.name) -%}
{% set env_dir = item.virtualenv|default("/opt/%s/env" % item.name) -%}
{% set app_dir = item.app_dir|default("/opt/%s/src" % item.name) -%}

[Unit]
Description=Gunicoron Instance to serve {{ app.name }}
Expand All @@ -10,9 +15,9 @@ After=syslog.target
[Service]
Environment={% for key in app.env|default({}) %}'{{key}}={{app.env[key]}}' {% endfor %}

WorkingDirectory={{app.app_dir|default(wsgi_app_dir)}}
WorkingDirectory={{app_dir}}
User={{wsgi_user}}
ExecStart={{app.virtualenv|default(wsgi_virtualenv)}}/bin/{{app.gunicorn_command|default(wsgi_gunicorn_command)}} {{app.module|default('wsgi:app')}} {{app.gunicorn_addopts|default('')}} --bind={{app.socket|default('unix:' + app.get('run_dir', wsgi_run_dir) + '/' + app.name)}} --log-file={{app.log_dir|default(wsgi_log_dir)}}/{{app.name}}-wsgi.log --name={{app.name}} --pid={{app.run_dir|default(wsgi_run_dir)}}/{{app.name}}.pid --worker-class={{app.gunicorn_worker|default(wsgi_gunicorn_worker)}} --workers={{app.gunicorn_workers|default(wsgi_gunicorn_workers)}} {% if wsgi_reload %}--reload {% endif %}
ExecStart={{env_dir}}/bin/{{app.gunicorn_command|default(wsgi_gunicorn_command)}} {{app.module|default('wsgi:app')}} {{app.gunicorn_addopts|default('')}} --bind={{app.socket|default('unix:' + run_dir + '/' + app.name)}} --log-file={{log_dir}}/{{app.name}}-wsgi.log --name={{app.name}} --pid={{run_dir}}/{{app.name}}.pid --worker-class={{app.gunicorn_worker|default(wsgi_gunicorn_worker)}} --workers={{app.gunicorn_workers|default(wsgi_gunicorn_workers)}} {% if wsgi_reload %}--reload {% endif %}
Restart=always
KillSignal=SIGQUIT
ExecReload?=/bin/kill -s HUP $MAINPID
Expand Down
Loading

0 comments on commit 65280c2

Please sign in to comment.