Skip to content

Commit 3d617db

Browse files
committed
feat: adds a mock python flask based app
the application uses a set of environment variables from an array and renders them using a jinja template, this is not intended for production use, this is only meant to be used for our terraform labs to check out setup. note that flask is running in an asgi wrapper. environment variables at present have to be added manually via the configuration path
0 parents  commit 3d617db

File tree

4 files changed

+355
-0
lines changed

4 files changed

+355
-0
lines changed

src/lab_mock/__init__.py

+93
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
"""Mock Python Server.
2+
3+
This is a minimal Python web application that uses Flask to
4+
render a template that displays a nominated server of environment
5+
variables.
6+
7+
The purpose of this mock application is to validate that a desired
8+
Docker/Kubernetes environment is running as desired by Anomaly labs.
9+
10+
Refer to https://github.com/anomaly/lab-tf-linode for more information.
11+
12+
A production grade Python application is documented at
13+
https://github.com/anomaly/lab-python-server
14+
15+
"""
16+
__name__ = "lab_mock"
17+
18+
import os
19+
from asgiref.wsgi import WsgiToAsgi
20+
21+
from flask import Flask
22+
from jinja2 import Environment, PackageLoader, select_autoescape
23+
24+
wsgi_app = Flask(__name__)
25+
26+
_ENV_LIST = [
27+
{
28+
"name": "PostgreSQL",
29+
"description": "Postgres database credentials",
30+
"vars": [
31+
"POSTGRES_USER",
32+
"POSTGRES_PASSWORD",
33+
"POSTGRES_HOST",
34+
"POSTGRES_PORT",
35+
"POSTGRES_DB",
36+
]
37+
},
38+
{
39+
"name": "Redis",
40+
"description": "Redis database credentials",
41+
"vars": [
42+
"REDIS_HOST",
43+
"REDIS_PORT",
44+
]
45+
},
46+
{
47+
"name": "App",
48+
"description": "Application credentials",
49+
"vars": [
50+
"CSRF_SECRET"
51+
]
52+
}
53+
]
54+
55+
@wsgi_app.route('/')
56+
def lab_mock():
57+
"""
58+
The only endpoint made available by this application to
59+
use a Jinaj2 template to render a list of environment variables.
60+
"""
61+
jinja2_env = Environment(
62+
loader=PackageLoader(package_name="lab_mock"),
63+
autoescape=select_autoescape()
64+
)
65+
66+
# Process each environment variable in the list
67+
# fetch the value from the environment and pass it
68+
# onto the template to render
69+
70+
_TEMPLATE_VARS = []
71+
for env in _ENV_LIST:
72+
_TEMPLATE_VARS.append({
73+
"name": env["name"],
74+
"description": env["description"],
75+
"vars": [
76+
{
77+
"name": var,
78+
# If the environment var has the value
79+
# use that or use None
80+
"value": os.environ.get(var, None)
81+
} for var in env["vars"]
82+
]
83+
})
84+
85+
template = jinja2_env.get_template('index.jinja2')
86+
return template.render(ENVIRONMENT=_TEMPLATE_VARS)
87+
88+
89+
90+
91+
# WSGI to ASGI middleware
92+
# https://flask.palletsprojects.com/en/2.1.x/deploying/asgi/
93+
app = WsgiToAsgi(wsgi_app)

src/lab_mock/templates/index.jinja2

+48
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
<!DOCTYPE html>
2+
<html lang="en">
3+
<head>
4+
<title>Mock Python server</title>
5+
<meta charset="UTF-8">
6+
<meta name="viewport" content="width=device-width, initial-scale=1.0">
7+
<script src="https://cdn.tailwindcss.com"></script>
8+
</head>
9+
<body class="mx-8 my-4 md:mx-10 md:my-10">
10+
<h1 class="text-3xl font-bold text-blue-800">Lab Mock Server</h1>
11+
<p class="my-4 text-gray-500 lg:max-w-lg">
12+
This is a mock server that displays a set of environment variables.
13+
It's intended to be used with an Anomaly lab to test a Terraform
14+
environment. Anomaly uses Python on the server and this mimics what
15+
a Dockerised environment would look like.
16+
</p>
17+
<p class="my-4 font-black text-red-500 lg:max-w-lg text-upper">
18+
Do not use this in any production context
19+
</p>
20+
{% for section in ENVIRONMENT %}
21+
<h2 class="text-xl font-bold text-gray-800">{{ section.name }}</h2>
22+
<p>{{ section.description }}</p>
23+
<table class="mt-6 mb-10 text-gray-900 table-auto">
24+
<thead>
25+
<tr class="text-left">
26+
<th>Name</th>
27+
<th>Value</th>
28+
</tr>
29+
</thead>
30+
<tbody>
31+
{% for var in section.vars %}
32+
<tr class="mb-2 font-mono">
33+
<td>
34+
<span class="p-1 bg-gray-100 border-2 border-gray-800 rounded-md">
35+
{{ var.name }}
36+
</span>
37+
</td>
38+
<td>{{ var.value }}</td>
39+
</tr>
40+
{% endfor %}
41+
</tbody>
42+
</table>
43+
{% endfor %}
44+
<footer class="mt-4 text-sm text-gray-500">
45+
&copy; Anomaly Software. Licensed under the Apache 2.0 license
46+
</footer>
47+
</body>
48+
</html>

src/poetry.lock

+196
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/pyproject.toml

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
[tool.poetry]
2+
name = "lab_mock"
3+
version = "0.1.0"
4+
description = "A mock python web app for Anomaly labs"
5+
authors = ["Dev Mukherjee <[email protected]>"]
6+
license = "Apache 2.0"
7+
8+
[tool.poetry.dependencies]
9+
python = "^3.10"
10+
Flask = "^2.1.2"
11+
uvicorn = "^0.18.1"
12+
asgiref = "^3.5.2"
13+
14+
[tool.poetry.dev-dependencies]
15+
16+
[build-system]
17+
requires = ["poetry-core>=1.0.0"]
18+
build-backend = "poetry.core.masonry.api"

0 commit comments

Comments
 (0)