Changeset - 4b5ca3dcf102
[Not reviewed]
0 7 0
Branko Majic (branko) - 8 months ago 2024-02-15 01:29:37
branko@majic.rs
MAR-197: Implement environment indicator for the web_server role:

- Now the environment indicator will also cover the default landing
page for the web server.
- Tweaked the landing page to look a bit better in terms of
margins (particularly important for the environment indicator).
7 files changed with 71 insertions and 4 deletions:
0 comments (0 inline, 0 general)
docs/releasenotes.rst
Show inline comments
 
@@ -49,6 +49,16 @@ Dropped support for Debian 10 (Buster).
 
    ``/etc/pip_check_requirements_upgrades`` path, in accordance to
 
    changes made in this release to the ``common`` role.
 

	
 
**New features/improvements**
 

	
 
* ``web_server`` role
 

	
 
  * Added parameter ``environment_indicator`` which is used on the
 
    default page to indicate what environment the web server belongs
 
    to. Useful for distinguishing between test, staging, and
 
    production environments (similar to what is already available in
 
    the ``php_website`` and ``wsgi_website`` roles).
 

	
 
**Bug fixes:**
 

	
 
* ``common`` role
docs/rolereference.rst
Show inline comments
 
@@ -1448,6 +1448,28 @@ Parameters
 
  Private key used for TLS for HTTPS service. The file will be stored in
 
  directory ``/etc/ssl/private/`` under name ``{{ ansible_fqdn }}_https.key``.
 

	
 
**environment_indicator** (dictionary, optional, ``null``)
 
  Specify configuration for including environment indicator on all HTML
 
  pages. Indicator is a simple strip at bottom of a page with custom background
 
  colour, text colour, and text.
 

	
 
  Specifying environment indicator is useful for avoiding mistakes when testing
 
  by having better visibility what environment you are in
 
  (production/staging/test).
 

	
 
  The following keys need to be specified:
 

	
 
  **background_colour** (string, mandatory)
 
    Background colour to use for the strip at bottom. This should be value
 
    compatible with CSS ``background-color`` attribute.
 

	
 
  **text_colour** (string, mandatory)
 
    Text colour to use for the strip at bottom. This should be value compatible
 
    with CSS ``color`` attribute.
 

	
 
  **text** (string, mandatory)
 
    Text to show in show in the strip at bottom.
 

	
 
**web_default_title** (string, optional, ``Welcome``)
 
  Title for the default web page shown to users (if no other vhosts were matched).
 

	
 
@@ -1605,7 +1627,7 @@ Parameters
 
    Background colour to use for the strip at bottom. This should be value
 
    compatible with CSS ``background-color`` attribute.
 

	
 
  **text_colour** (string, mandatory
 
  **text_colour** (string, mandatory)
 
    Text colour to use for the strip at bottom. This should be value compatible
 
    with CSS ``color`` attribute.
 

	
 
@@ -1842,7 +1864,7 @@ Parameters
 
    Background colour to use for the strip at bottom. This should be value
 
    compatible with CSS ``background-color`` attribute.
 

	
 
  **text_colour** (string, mandatory
 
  **text_colour** (string, mandatory)
 
    Text colour to use for the strip at bottom. This should be value compatible
 
    with CSS ``color`` attribute.
 

	
roles/web_server/defaults/main.yml
Show inline comments
 
---
 

	
 
environment_indicator: null
 

	
 
web_default_title: "Welcome"
 
web_default_message: "You are attempting to access the web server using a wrong name or an IP address. Please check your URL."
 
web_server_tls_protocols:
roles/web_server/molecule/default/group_vars/parameters-optional.yml
Show inline comments
 
@@ -2,6 +2,10 @@
 

	
 
default_https_tls_certificate: "{{ lookup('file', 'tests/data/x509/server/{{ inventory_hostname }}_https.cert.pem') }}"
 
default_https_tls_key: "{{ lookup('file', 'tests/data/x509/server/{{ inventory_hostname }}_https.key.pem') }}"
 
environment_indicator:
 
  background_colour: "#ff0000"
 
  text_colour: "#00ff00"
 
  text: "parameters-optional"
 
web_default_title: "Optional Welcome"
 
web_default_message: "Welcome to default virtual host."
 
web_server_tls_protocols:
roles/web_server/molecule/default/tests/test_optional.py
Show inline comments
 
@@ -63,3 +63,17 @@ def test_default_vhost_index_page(host):
 
    assert "<title>Optional Welcome</title>" in page.stdout
 
    assert "<h1>Optional Welcome</h1>" in page.stdout
 
    assert "<p>Welcome to default virtual host.</p>" in page.stdout
 

	
 

	
 
def test_environment_indicator(host):
 
    """
 
    Tests if environment indicator is applied correctly.
 
    """
 

	
 
    hostname = host.ansible.get_variables()['inventory_hostname']
 

	
 
    page = host.run('curl https://%s/' % hostname)
 

	
 
    assert page.rc == 0
 
    assert "<div id='website-environment' style='background-color: #ff0000; width: 100%; text-align: center; position: fixed; bottom: 5px; color: #00ff00; " \
 
        "font-weight: bold; z-index: 999999;'>parameters-optional</div></body>" in page.stdout
roles/web_server/templates/index.html.j2
Show inline comments
 
@@ -3,15 +3,23 @@
 
<head>
 
<title>{{ web_default_title}}</title>
 
<style>
 

	
 
    body {
 
        margin: 0;
 
    }
 

	
 
    #welcome {
 
        width: 35em;
 
        margin: 0 auto;
 
        font-family: Tahoma, Verdana, Arial, sans-serif;
 
    }
 

	
 
</style>
 
</head>
 
<body>
 
<h1>{{ web_default_title}}</h1>
 
<p>{{ web_default_message }}</p>
 
<div id="welcome">
 
  <h1>{{ web_default_title}}</h1>
 
  <p>{{ web_default_message }}</p>
 
</div>
 
</body>
 
</html>
roles/web_server/templates/nginx-default.j2
Show inline comments
 
@@ -35,4 +35,11 @@ server {
 
        # Always point user to the same index page.
 
        try_files $uri /index.html;
 
    }
 

	
 
    {% if environment_indicator -%}
 
    # Show environment indicator on HTML pages.
 
    sub_filter_types text/html;
 
    sub_filter_once on;
 
    sub_filter "</body>" "<div id='website-environment' style='background-color: {{ environment_indicator.background_colour }}; width: 100%; text-align: center; position: fixed; bottom: 5px; color: {{ environment_indicator.text_colour }}; font-weight: bold; z-index: 999999;'>{{ environment_indicator.text }}</div></body>";
 
    {% endif -%}
 
}
0 comments (0 inline, 0 general)