Skip to content

TOSIT-IO/tdp-server

Repository files navigation

TDP Server

tdp-server provides a server to interact with tdp-lib.

A full beginers guide to launch a testing server is provided at docs/quick-start.md.

Requirements

The server is made with Python. The following is required:

  • Python ^3.6
  • Poetry 1.1.15
  • A RDBMS, to store deployment history (e.g. PostgresQL)
  • An identity management system, for authentication purposes (e.g. Keycloak)

Dev environment

A dev/testing environment using Keycloak and PostgreSQL is provided through docker-compose:

docker-compose -f dev/docker-compose.yml up -d

Installation

  1. Use Poetry to download and install the required Python dependencies:

    poetry install
  2. Define the required environment variables in an .env file. An example file is provided in dev/.env-dev:

    cp dev/.env-dev .env

    In particular:

    • DATABASE_DSN: the data source name of the RDBMS.
    • TDP_COLLECTION_PATH: the path to one or more TDP collection, separated by : (as tdp-collection and tdp-collection-extras).
    • TDP_VARS: the path to an empty directory where the tdp_vars will be stored and versioned.
    • TDP_RUN_DIRECTORY: the path to the directory where the Ansible command will be launched (as tdp-getting-started for example). Note: the ansible.cfg file of the working directory must contain the path of the tdp_vars directory defined previously.
  3. Initialize the database and the tdp_vars directory:

    python tdp_server/initialize_database.py
    python tdp_server/initialize_tdp_vars.py

Usage

Start the server using:

uvicorn tdp_server.main:app --reload

Build Docker Container

docker build -t tdp_server -f docker/Dockerfile .

Run Docker Container

Executing the container with the minimal configuration variables:

docker run \
  -e TDP_COLLECTION_PATH="/tdp/ops/tdp/ansible/ansible_collections/tosit/tdp" \
  -e TDP_RUN_DIRECTORY="/tdp/ops" \
  -e TDP_VARS="/tdp/ops/inventory/tdp_vars" \
  -e DATABASE_DSN=sqlite:////tdp/sqlite.db \
  -e OPENID_CONNECT_DISCOVERY_URL="http://host.docker.internal:8080/auth/realms/tdp_server/.well-known/openid-configuration" \
  -e OPENID_CLIENT_ID=tdp_server \
  -e OPENID_CLIENT_SECRET=secret \
  -v "..../sqlite.db:/tdp/sqlite.db" \
  -v"..../tdp-ops:/tdp/ops" \
  -p 8000:8000 \
  tdp_server

N.B.: Mounting a sqlite database is not the recommended way to persist the server's data.

Accessing the REST API

A token must be provided to access the API. Tokens can be obtained using the get_token.py script.

For example, using curl:

token=$(python get_token.py)
curl -H "Authorization: Bearer $token" http://localhost:8000/api/v1/service/

Accessing the API documentation pages

Documentation pages of the API are available at:

Contributing

tdp-server uses Git Hooks to enforce consistency in the code and commit messages.

Use Poetry to install the hooks:

poetry run pre-commit install --hook-type pre-commit
poetry run pre-commit install --hook-type commit-msg

The following environment variables can be used to ease development:

  • DO_NOT_USE_IN_PRODUCTION_DISABLE_TOKEN_CHECK
  • MOCK_DEPLOY