Changeset - b757d690af42
[Not reviewed]
0 7 1
Branko Majic (branko) - 7 years ago 2016-11-26 19:49:53
branko@majic.rs
MAR-75: Implemented support for specifying additional environment variables for the wsgi_website role. Environment is set-up for both the systemd service and for application admin user. Updated testsite implementation to use it for sample WSGI website.
8 files changed with 28 insertions and 2 deletions:
0 comments (0 inline, 0 general)
docs/rolereference.rst
Show inline comments
 
@@ -1460,6 +1460,11 @@ Parameters
 
  be served with ``Strict-Transport-Security`` header with value of
 
  ``max-age=31536000; includeSubDomains``.
 

	
 
**environment_variables** (dict, optional, ``{}``)
 
  Specify additional environment variables that should be set for running the
 
  service. Environment variables will be set in both the systemd service and for
 
  the application's administrator user (when logged in as one).
 

	
 
**fqdn** (string, mandatory)
 
  Fully-qualified domain name where the website is reachable. This value is used
 
  for calculating the user/group name for dedicated website user, as well as
 
@@ -1536,6 +1541,8 @@ running a bare Django project):
 
      virtualenv_packages:
 
        - django
 
      wsgi_application: django_example_com.wsgi:application
 
      environment_variables:
 
        DJANGO_SETTINGS_MODULE: "django_example_com.settings.production"
 
      https_tls_key: "{{ lookup('file', inventory_dir + '/tls/wsgi.example.com_https.key') }}"
 
      https_tls_certificate: "{{ lookup('file', inventory_dir + '/tls/wsgi.example.com_https.pem') }}"
 
      futures_version: 3.0.5
docs/usage.rst
Show inline comments
 
@@ -1472,6 +1472,9 @@ on the safe side:
 
  will be automatically activated for you.
 
* WSGI applications are executed via *Gunicorn*. The WSGI server listens on a
 
  Unix socket, making the socket accessible by *Nginx*.
 
* If you ever need to set some environment variables, this can easily be done
 
  via the ``environment_variables`` role parameter. This particular example does
 
  not set any, though.
 
* Static content is served directly by *Nginx*.
 
* Each web application gets distinct sub-directory under ``/var/www``, named
 
  after the FQDN. All sub-directories created under there are created with
roles/wsgi_website/defaults/main.yml
Show inline comments
 
@@ -7,6 +7,7 @@ rewrites: []
 
static_locations: []
 
use_paste: False
 
virtualenv_packages: []
 
environment_variables: {}
 
admin: "web-{{ fqdn | replace('.', '_') }}"
 
https_tls_certificate: "{{ lookup('file', tls_certificate_dir + '/' + fqdn + '_https.pem') }}"
 
https_tls_key: "{{ lookup('file', tls_private_key_dir + '/' + fqdn + '_https.key') }}"
roles/wsgi_website/tasks/main.yml
Show inline comments
 
@@ -20,6 +20,10 @@
 
  copy: src="profile_virtualenv.sh" dest="{{ home }}/.profile.d/virtualenv.sh"
 
        owner="root" group="{{ user }}" mode="640"
 

	
 
- name: Deploy profile configuration file for setting environment variables
 
  template: src="environment.sh.j2" dest="{{ home }}/.profile.d/environment.sh"
 
            owner="root" group="{{ user }}" mode=640
 

	
 
- name: Create WSGI website user
 
  user: name="{{ user }}" uid="{{ uid | default(omit) }}" group="{{ user }}" comment="umask=0007"
 
        system=yes createhome=no state=present
roles/wsgi_website/templates/environment.sh.j2
Show inline comments
 
new file 100644
 
{% for var, val in environment_variables.iteritems() %}
 
export {{ var }}='{{ val }}'
 
{% endfor %}
roles/wsgi_website/templates/systemd_wsgi_website.service.j2
Show inline comments
 
@@ -9,10 +9,14 @@ Group={{ user }}
 
WorkingDirectory={{ home }}/code
 
ExecStart={{ home }}/virtualenv/bin/gunicorn --bind unix:/run/wsgi/{{ fqdn }}.sock {% if use_paste %}--paste {{home}}/code/{{ wsgi_application }}{% else %}{{ wsgi_application }}{% endif %}
 

	
 
{% for var, val in environment_variables.iteritems() %}
 
Environment="{{ var }}={{ val }}"
 
{% endfor %}
 

	
 
ExecReload=/bin/kill -s HUP $MAINPID
 
ExecStop=/bin/kill -s TERM $MAINPID
 
PrivateTmp=true
 
UMask=0007
 

	
 
[Install]
 
WantedBy=multi-user.target
 
\ No newline at end of file
 
WantedBy=multi-user.target
testsite/playbooks/roles/wsgihello/files/hello.wsgi
Show inline comments
 
#!/usr/bin/env python
 

	
 
import os
 

	
 
def application(environ, start_response):
 
    status = '200 OK'
 
    output = 'Hello, world one!'
 
    output = 'Hello, world one! I am website %s' % os.environ.get("WEBSITE_NAME", "that nobody set a name for :(")
 

	
 
    response_headers = [('Content-type', 'text/plain'),
 
                        ('Content-Length', str(len(output)))]
testsite/playbooks/roles/wsgihello/meta/main.yml
Show inline comments
 
@@ -10,6 +10,8 @@ dependencies:
 
      - /static/
 
    https_tls_key: "{{ lookup('file', inventory_dir + '/tls/wsgi.' + testsite_domain + '_https.key') }}"
 
    https_tls_certificate: "{{ lookup('file', inventory_dir + '/tls/wsgi.' + testsite_domain + '_https.pem') }}"
 
    environment_variables:
 
      WEBSITE_NAME: "Majic Ansible Roles Test Site"
 
  - role: database
 
    db_name: wsgi_{{ testsite_domain_underscores }}
 
    db_password: wsgi_{{ testsite_domain_underscores }}
 
\ No newline at end of file
0 comments (0 inline, 0 general)