Skip to content

A repository for devops course at Innopolis University

Notifications You must be signed in to change notification settings

WinnerOK/devops

Repository files navigation

Introduction

The app environment is built referencing TezRomacH/python-package-template with some modifications

App description

The app shows datetime in Moscow timezone (GMT+3)

  • App runs on port 8000
  • Swagger is available at /docs
  • Application metrics are available at /metrics
  • Root endpoint access time is recorded and can be retrieved at /visits

Running docker image

Usage of Makefile to build docker container is specified at Makefile usage section of this readme. Subsection 8.

For more specific information refer README.md inside docker folder.

Running a container from pre-built image:

docker run -it  -p 8000:8000 -d winnerokay/app_python

Checkout available tags at dockerhub

Deployment

  1. Fill ~/.aws/credentials in the following format (in order for dynamic inventory to work properly):
    [default]
    aws_access_key_id=<your aws key id>
    aws_secret_access_key=<your aws access key>
    
  2. Fill in IaC/terraform/variables.tfvars as IaC/terraform/example.tfvars
  3. Go to IaC/terraform folder
  4. Run terraform:
    terraform plan
    
    If everything goes as expected, run
    terraform apply
    
  5. Go to ansible folder:
  6. Install ansible dependencies
    pip3 install -r requirements.txt
    ansible-galaxy install -r requirements.yml
    
  7. Verify the dynamic inventory
    ansible-inventory -i ./inventory --graph
    
  8. Run ansible:
    ansible-playbook -i ./inventory --private-key <path to your private key> provision_app.yml
    
    In case needed, you can overwrite ansible varibles from command line

Testing

The code is tested using pytest framework. Tests are located at /tests folder

Run tests executing either:

poetry run pytest

or

make test

Monitoring stack

The repository contains a Loki + Promtail + Grafana + Prometheus as monitoring stack.

Monitoring currently inspects itself and the application running at 0.0.0.0:8000. You can check out the stack in monitoring folder, where you can also find the demonstration

Start coding

Initialize your code

  1. Clone repo
  2. If you don't have Poetry installed run:
make poetry-download
  1. Initialize poetry and install pre-commit hooks:
make install
make pre-commit-install

Makefile usage

Makefile contains often used commands

1. Download and remove Poetry

Download and install Poetry:

make poetry-download

Uninstall Poetry

make poetry-remove

2. Install all dependencies and pre-commit hooks

Install requirements:

make install

Install pre-commit hooks:

make pre-commit-install

3. Codestyle

Automatic formatting uses pyupgrade, isort and black.

make format

Codestyle checks only, without rewriting files (uses isort, black and darglint):

make check-codestyle
4. Code security

Launch `Poetry` integrity checks, dependency vulnerability checks via `Safety` and code analysis using `Bandit`.

make check-safety

5. Type checks

Run mypy type checker

make mypy

6. Tests

Run pytest

make test

7. All linters

test, codestyle checks, mypy checks and safety checks: ```bash make lint ```

8. Docker

make docker-build

which is equivalent to:

make docker-build VERSION=latest

Remove docker image with

make docker-remove

Docker readme.

9. Cleanup

Delete pycache files

make pycache-remove

Or to remove pycache, build and docker image run:

make clean-all