From 771c6bac2d92eecdb236a9dcc1a25fef72b740c2 Mon Sep 17 00:00:00 2001 From: Dorian Pula Date: Tue, 13 Jan 2015 22:45:07 -0500 Subject: [PATCH] Turn configurations into templates with variables. Add multiple variables for all the necessary templated values. Make the role reusable beyond working with rookeries. --- files/nginx/rookeries-uwsgi_nginx.conf | 18 ------------ files/supervisor/rookeries_supervisor.conf | 9 ------ files/uwsgi/rookeries-uwsgi.ini | 8 ------ tasks/nginx.yaml | 20 +++++++------- tasks/python.yaml | 4 +-- tasks/supervisor.yaml | 5 ++-- tasks/uwsgi.yaml | 4 +-- tasks/web_data_folders.yaml | 9 +++--- templates/nginx/app_uwsgi_nginx.conf | 18 ++++++++++++ templates/supervisor/app_supervisor.conf | 9 ++++++ templates/uwsgi/app_uwsgi.ini | 8 ++++++ vars/main.yml | 32 +++++++++++++--------- 12 files changed, 75 insertions(+), 69 deletions(-) delete mode 100644 files/nginx/rookeries-uwsgi_nginx.conf delete mode 100644 files/supervisor/rookeries_supervisor.conf delete mode 100644 files/uwsgi/rookeries-uwsgi.ini create mode 100644 templates/nginx/app_uwsgi_nginx.conf create mode 100644 templates/supervisor/app_supervisor.conf create mode 100644 templates/uwsgi/app_uwsgi.ini diff --git a/files/nginx/rookeries-uwsgi_nginx.conf b/files/nginx/rookeries-uwsgi_nginx.conf deleted file mode 100644 index 84496ee..0000000 --- a/files/nginx/rookeries-uwsgi_nginx.conf +++ /dev/null @@ -1,18 +0,0 @@ -server { - - server_name localhost; - access_log /srv/www/logs/nginx/rookeries-access.log; - error_log /srv/www/logs/nginx/rookeries-error.log info; - - location /static/ { - alias /srv/www/rookeries_webapp/rookeries/static/; - } - - location / { - try_files $uri @yourapplication; - } - location @yourapplication { - include uwsgi_params; - uwsgi_pass 127.0.0.1:8001; - } -} \ No newline at end of file diff --git a/files/supervisor/rookeries_supervisor.conf b/files/supervisor/rookeries_supervisor.conf deleted file mode 100644 index 0fcfc2a..0000000 --- a/files/supervisor/rookeries_supervisor.conf +++ /dev/null @@ -1,9 +0,0 @@ -; UWSGI for Rookeries -[program:rookeries_uwsgi] -user=www-data -command=/srv/www/virtualenvs/uwsgi/bin/uwsgi --ini /srv/www/config/uwsgi/rookeries-uwsgi.ini -stopsignal=TERM -stdout_logfile = /srv/www/logs/supervisor/rookeries-application.log -stdout_logfile_backups = 5 -stderr_logfile = /srv/www/logs/supervisor/rookeries-error.log -stderr_logfile_backups = 5 diff --git a/files/uwsgi/rookeries-uwsgi.ini b/files/uwsgi/rookeries-uwsgi.ini deleted file mode 100644 index aa41ae5..0000000 --- a/files/uwsgi/rookeries-uwsgi.ini +++ /dev/null @@ -1,8 +0,0 @@ -[uwsgi] -socket = :8001 -master = true -processes = 8 - -chdir = /srv/www/rookeries_webapp -virtualenv = /srv/www/virtualenvs/rookeries -module = rookeries:make_rookeries_app() diff --git a/tasks/nginx.yaml b/tasks/nginx.yaml index a5eeeec..30805e0 100644 --- a/tasks/nginx.yaml +++ b/tasks/nginx.yaml @@ -3,17 +3,17 @@ apt: pkg=nginx-full state=present sudo: yes -# TODO Look at using templated configuration - name: configure nginx - copy: src=nginx/{{ app_nginx_conf }} dest=/etc/nginx/sites-available - -- name: enable the copied over nginx configuration - file: state=link - src=/etc/nginx/sites-available/{{ app_nginx_conf }} - path=/etc/nginx/sites-enabled/{{ app_nginx_conf }} + template: src=nginx/app_uwsgi_nginx.conf dest=/etc/nginx/sites-available/{{ nginx_app_conf }} sudo: yes -- name: disable the default page configuration +- name: link to enable nginx configuration + file: state=link + src=/etc/nginx/sites-available/{{ nginx_app_conf }} + path=/etc/nginx/sites-enabled/{{ nginx_app_conf }} + sudo: yes + +- name: disable default configuration file: state=absent path=/etc/nginx/sites-enabled/default sudo: yes @@ -21,7 +21,7 @@ service: name=nginx state=restarted sudo: yes -- name: set permissions on nginx config + log directories +- name: set nginx directories permissions file: path={{ item }} state=directory group={{ web_server_group }} mode=0755 with_items: @@ -31,7 +31,7 @@ - /etc/nginx/sites-enabled sudo: yes -- name: set permissions on nginx logs +- name: set nginx logs permissions file: path=/var/log/nginx/{{ item }} state=file owner={{ web_server_group }} group={{ web_server_group }} mode=0644 with_items: diff --git a/tasks/python.yaml b/tasks/python.yaml index d474e0f..14fb6f5 100644 --- a/tasks/python.yaml +++ b/tasks/python.yaml @@ -1,12 +1,12 @@ --- -- name: install python dev dependencies +- name: install python dev sudo: yes apt: pkg={{ item }} state=present with_items: - python-dev - python-setuptools -- name: bootstrap pip using setuptools +- name: install pip sudo: yes easy_install: name=pip diff --git a/tasks/supervisor.yaml b/tasks/supervisor.yaml index 5f6d1f8..eb15b95 100644 --- a/tasks/supervisor.yaml +++ b/tasks/supervisor.yaml @@ -19,7 +19,6 @@ chown: root:{{ web_server_group }} sudo: yes -- name: upload rookeries supervisor configuration to web server home - copy: src=supervisor/{{ item }} dest=/etc/supervisor/conf.d - with_items: supervisor_configs +- name: upload supervisor configuration to web server home + template: src=supervisor/app_supervisor.conf dest=/etc/supervisor/conf.d/{{ supervisor_app_config }} sudo: yes diff --git a/tasks/uwsgi.yaml b/tasks/uwsgi.yaml index d86e3f7..32e2a83 100644 --- a/tasks/uwsgi.yaml +++ b/tasks/uwsgi.yaml @@ -3,8 +3,8 @@ pip: name=uwsgi virtualenv={{ uwsgi_venv }} version=2.0 sudo: yes -- name: upload app's uwsgi configuration - copy: src=uwsgi/{{ uwsgi_ini }} dest={{ uwsgi_config }} +- name: configure app on uwsgi server + template: src=uwsgi/app_uwsgi.ini dest={{ uwsgi_config }}/{{ uwsgi_app_ini }} sudo: yes - name: restart uwsgi server diff --git a/tasks/web_data_folders.yaml b/tasks/web_data_folders.yaml index e55420e..94d5fec 100644 --- a/tasks/web_data_folders.yaml +++ b/tasks/web_data_folders.yaml @@ -1,10 +1,11 @@ --- - name: setup webapp deployment folder with the correct permissions - file: path={{ web_app_home }} state=directory owner={{ web_server_group }} group=www-data mode=0774 + file: path={{ web_root_path }} state=directory + owner={{ web_server_group }} group={{ web_server_group }} mode=0774 sudo: yes - name: setup webapp deployment configuration + log folders with the correct permissions - file: path={{ web_app_home }}/{{ item }} state=directory + file: path={{ web_root_path }}/{{ item }} state=directory owner={{ web_server_group }} group={{ web_server_group }} mode=0774 with_items: - config @@ -15,7 +16,7 @@ - name: link remote folders with webapp home folder file: state=link - path={{ web_app_home }}/{{ item.value }} + path={{ web_root_path }}/{{ item.value }} src={{ item.key }} owner={{ web_server_group }} group={{ web_server_group }} mode=0774 with_dict: @@ -26,5 +27,5 @@ sudo: yes - name: add user to webserver group - user: name={{ user }} append=yes groups={{ web_server_group }} + user: name={{ web_user }} append=yes groups={{ web_server_group }} sudo: yes diff --git a/templates/nginx/app_uwsgi_nginx.conf b/templates/nginx/app_uwsgi_nginx.conf new file mode 100644 index 0000000..39add26 --- /dev/null +++ b/templates/nginx/app_uwsgi_nginx.conf @@ -0,0 +1,18 @@ +server { + + server_name {{ nginx_hostname }}; + access_log {{ web_root_path }}/logs/nginx/{{ app_name }}-access.log; + error_log {{ web_root_path }}/logs/nginx/{{ app_name }}-error.log info; + + location /static/ { + alias {{ nginx_app_static }}; + } + + location / { + try_files $uri @yourapplication; + } + location @yourapplication { + include uwsgi_params; + uwsgi_pass 127.0.0.1:{{ uwsgi_port }}; + } +} \ No newline at end of file diff --git a/templates/supervisor/app_supervisor.conf b/templates/supervisor/app_supervisor.conf new file mode 100644 index 0000000..6e7afd3 --- /dev/null +++ b/templates/supervisor/app_supervisor.conf @@ -0,0 +1,9 @@ +; UWSGI for {{ app_name }} +[program:{{ uwsgi_service_name }}] +user={{ web_server_group }} +command={{ uwsgi_venv }}/bin/uwsgi --ini {{ uwsgi_config }}/{{ uwsgi_app_ini }} +stopsignal=TERM +stdout_logfile = {{ web_root_path }}/logs/supervisor/{{ app_name }}-application.log +stdout_logfile_backups = 5 +stderr_logfile = {{ web_root_path }}/logs/supervisor/{{ app_name }}-error.log +stderr_logfile_backups = 5 diff --git a/templates/uwsgi/app_uwsgi.ini b/templates/uwsgi/app_uwsgi.ini new file mode 100644 index 0000000..445b9cc --- /dev/null +++ b/templates/uwsgi/app_uwsgi.ini @@ -0,0 +1,8 @@ +[uwsgi] +socket = :{{ uwsgi_port }} +master = true +processes = 8 + +chdir = {{ app_home }} +virtualenv = {{ app_venv }} +module = {{ uwsgi_app_executable }} diff --git a/vars/main.yml b/vars/main.yml index f78d6de..d851a3d 100644 --- a/vars/main.yml +++ b/vars/main.yml @@ -1,23 +1,29 @@ --- # vars file for ansible-nginx-uwsgi-supervisor -# TODO Make more configurable and flexible - -# Web app home + group -web_app_home: /srv/www +# Web root paths + app name + home +app_name: rookeries +web_root_path: /srv/www web_server_group: www-data -user: "{{ ansible_env.SUDO_USER }}" +web_user: "{{ ansible_env.SUDO_USER }}" +app_home: "{{ web_root_path }}/{{ app_name }}_webapp" + +# Virtualenvs +virtualenv_root: "{{ web_root_path }}/virtualenvs" +uwsgi_venv: "{{ virtualenv_root }}/uwsgi" +app_venv: "{{ virtualenv_root }}/{{ app_name }}" # NGINX -app_nginx_conf: rookeries-uwsgi_nginx.conf +nginx_hostname: localhost +nginx_app_static: "{{ app_home }}/{{ app_name }}/static/" +nginx_app_conf: "{{ app_name }}_uwsgi_nginx.conf" # Supervisor -# TODO Allow for multiple supervisord configurations -supervisor_configs: -# - mailsink_supervisor.conf - - rookeries_supervisor.conf +supervisor_app_config: "{{ app_name }}_supervisor.conf" # UWSGI -uwsgi_venv: "{{ web_app_home }}/virtualenvs/uwsgi" -uwsgi_config: "{{ web_app_home }}/config/uwsgi" -uwsgi_ini: rookeries-uwsgi.ini +uwsgi_config: "{{ web_root_path }}/config/uwsgi" +uwsgi_app_ini: "{{ app_name }}_uwsgi.ini" +uwsgi_port: 8001 +uwsgi_service_name: "{{ app_name }}_uwsgi" +uwsgi_app_executable: rookeries:make_rookeries_app()