Backend Python Flask Quickstarter (be-python-flask)

The project supports generation of Python Flask project boilerplate and quick installation and integration of it with OpenShift CD pipelines.

Purpose of this quickstarter

The quickstarter comes with a simple API-server example written in Python and using Flask framework. The package allows easily build a Python project, using different Python modules and frameworks. It contains the basic setup for Docker, Jenkins, SonarQube and OpenShift.

What files / architecture is generated?

├── Jenkinsfile - This file contains Jenkins build configuration settings
├── .pre-commit-config.yaml
├── README.md
├── docker - This folder contains Docker configuration settings
│   ├── Dockerfile
│   └── run.sh - This bash script solves issue with permissions for a container user
├── src
│   └── main.py - This file is the main entry point in the project.
├── tests
│   ├── __init__.py
│   └── main_test.py - Unit testing src/main.py
├── .coveragerc - Coverage configuration file
├── metadata.yml - Component metadata
├── mypy.ini - mypy configuration file
├── release-manager.yml - Configuration file for the Release Manager
├── requirements.txt - This file contains a list of required Python modules to run your application
├── tests_requirements.txt - required Python modules for the Test Suite (includes requirements.txt) and IDE integration
└── sonar-project.properties - This file contains SonarQube configuration settings

Usage - how do you start after you provisioned this quickstarter

The project is production ready when deployed in OpenShift thanks to gunicorn.

It is strongly recommended when you are developing a Python project to use separated environments. For this purpose usually one can use Python’s venv core package (check how to use it in the next steps).

since the version of Python is 3.11, ensure your system’s python executable is also in version 3.11
# Create virtual environment 'mylocaldevelopment' (will be located in the folder mylocaldevelopment) (run only once)
python -m venv mylocaldevelopment

# Initiate virtual environment for the project (every time)
source mylocaldevelopment/bin/activate

# Runs installation of required modules in the virtual environment (run only once)
pip install -r requirements.txt

# Start your application
PYTHONPATH=src python src/main.py

Metadata

The following are typical metadata values that can be used for components based on this quickstarter: Note that the OpenShift resources will be labeled based on this metadata.

name: <the component id (this is the default, if omitted)>
description: "Some microservice implemented in Python over Flask"
supplier: https://example.com
version: 1.0.1
type: ods
role: backend
runtime: flask
runtimeVersion: 3.0.0

How this quickstarter is built through Jenkins

The Jenkinsfile is provisioned with this quick starter to ease CI/CD process. In Jenkinsfile, there are various stages:

  • Prepare Test Suite - Prepares the virtual environment for testing and linting:

      pip install -r tests_requirements.txt
  • Lint - Runs mypy and flake8 for increasing code quality:

      mypy src
      flake8 --max-line-length=120 src
  • Test - Runs pytest and generates xUnit and code coverage reports:

      PYTHONPATH=src python -m pytest --junitxml=tests.xml -o junit_family=xunit2 --cov-report term-missing --cov-report xml --cov=src -o testpaths=tests
  • Build - Builds the application: copies src folder into docker/dist folder.

include::partial$secret-scanning-with-gitleaks.adoc

Builder agent used

This quickstarter uses Python builder agent Jenkins builder agent.

NOTE: The ODS Jenkins Pytnon Agent supports Python versions 3.11, 3.9 and 3.8. See next chapter for further information.

Multiple Python versions support

Build and run environment defaults to python3.11, but older python3.8 and python3.6 versions are also supported.

If you need older versions support in your project, change:

  • in the provided Jenkinsfile, the mentions of python3.11 binary to, for example, python3.9 binary,

  • and switch the FROM statement in your Dockerfile to the python version required, for example, registry.access.redhat.com/ubi8/python-38.

Known limitations

Let us know if you find any, thanks!