The project uses poetry for Python to create an isolated environment and manage package dependencies. To prepare your system, ensure you have an official distribution of Python version 3.7+ and install poetry using one of the following commands (as instructed by the poetry documentation):
curl -sSL https://raw.githubusercontent.com/python-poetry/poetry/master/get-poetry.py | python
(Invoke-WebRequest -Uri https://raw.githubusercontent.com/python-poetry/poetry/master/get-poetry.py -UseBasicParsing).Content | python
The project uses a virtual environment to isolate package dependencies. To create the virtual environment and install required packages, run the following from your preferred shell:
$ poetry install
You'll also need to clone a new .env
file from the .env.tempalate
to store local configuration options. This is a one-time operation on first setup:
$ cp .env.template .env # (first time only)
The .env
file is used by flask to set environment variables when running flask run
. This enables things like development mode (which also enables features like hot reloading when you make a file change). There's also a SECRET_KEY variable which is used to encrypt the flask session cookie.
Once the all dependencies have been installed, start the Flask app in development mode within the poetry environment by running:
$ poetry run flask run
You should see output similar to the following:
* Serving Flask app "app" (lazy loading)
* Environment: development
* Debug mode: on
* Running on http://127.0.0.1:5000/ (Press CTRL+C to quit)
* Restarting with fsevents reloader
* Debugger is active!
* Debugger PIN: 226-556-590
Now visit http://localhost:5000/
in your web browser to view the app.
This enhancement connects the app with Trello to fetch and update to-do items. The application can -
- Fetch all cards from one or more lists (To Do, Doing, Done, etc.). Default pull is from all lists, can be changed by passing list names to
fetch_all_items
method. - Create a new card, by letting the user choose list name and title of the card.
- Update status of card to Done. The update method can be changed to move card to other list by changing second parm of method
change_card_status
The .env
file has four new variables. Two of these are for Trello API key and token.
The other two variables store User Name (to fetch boards for given user) and Board Name (to fetch cards from the given board).
Visit http://localhost:5000/
in your web browser to view the app.
The project uses pytest as testing framework. For end to end tests, Mozilla Firefox and Gecko Driver executable (which should be placed in the root of the project).
Unit and Integration tests are present in tests folder and can be executed through the following command
$ poetry run pytest tests
The end to end tests are located in tests_e2e folder and can be executed using the following comand
$ poetry run pytest tests_e2e
During VM provisioning, all the dependencies are installed. Provisioning steps are documented in vagrantfile.
Once VM is brought up (using the command $ vagrant up), visit http://localhost:5000/
in your web browser to view the app.
For Part-1 of exercise, execute following commands to build docker image and create container:
$ docker build --tag todo-app .
$ docker run -p 5000:5000 --env-file .env todo-app gunicorn --bind 0.0.0.0:5000 todo_app.app:app
For Part-2 (multi-stage build file), following are the commands -
Development Image:
$ docker build --target development --tag todo-app:dev .
$ docker run --env-file .env -p 5100:5000 --mount type=bind,source="$(pwd)"/todo_app,target=/app/todo_app todo-app:dev
Production Image: (application is copied to image as well, hence no need to use bind mount)
Please Note - As an update to Module 8 exercise, we need to now provide value for PORT if running on local. The run command has been updated.
$ docker build --target production --tag todo-app:prod .
$ docker run --env-file .env -e "PORT=5000" -p 5000:5000 todo-app:prod
The architecture diagrams can be found in documentation folder. This folder contains 3 .drawio files (one each for context, component, and container diagrams), and 4 html files which correspond to the drawio files.
The .drawio files can be edited through online tool available at https://app.diagrams.net/
Use following commands to create test image.
$ docker build --target test --tag todo-app:test .
Subsequently, the individual tests can be executed by providing appropriate env file through --env-file
flag, or by providing individual variables through --env
flag. Following commands use --env-file
flag.
$ docker run --env-file .env todo-app:test tests/test_trello_api_calls.py
$ docker run --env-file .env.test todo-app:test tests/test_app.py
$ docker run --env-file .env todo-app:test tests_e2e/test_e2e.py
Login to Travis through Github credentials, and push commit to execute build on your branch.
- The test environment variables can either be passed through
--env-file
flag or--env
flag. - The production secrets should be encrypted before passing through
--env
flag. - Travis CLI (used for encrypting production variables) would need a github token for authentication, it does not accept Github userid / password.
- Updating the build frequency to ensure only pull requests are built - enable the 'Build pushed pull requests' flag under General settings.
- To enable auto cancelling builds - enable 'auto cancel branch builds' and 'Auto cancel pull request builds' under Auto Cancellation settings.
Following three environment variables are accessed through Travis build settings -
- DOCKER_PASSWORD
- HEROKU_API_KEY
- HEROKU_AUTH_TOKEN
To update HEROKU_API_KEY and HEROKU_AUTH_TOKEN, generate the auth token by running heroku login, which will prompt you to open browser and login
$ heroku login
Once done, execute the following command to get authorization token, which can be updated in Travis settings for both the variables (HEROKU_AUTH_TOKEN and HEROKU_API_KEY)
$ heroku auth:token
Ensure configuration variables are setup in Heroku Settings before deploying the application.
Login to Travis through Github credentials, and push a commit on current branch to start the build & deployment.
Once build and deployment completes successfully, validate the changes by visiting the application at https://todo-app-devops.herokuapp.com/
Package loggly-python-handler is used to send logs to Loggly. This is part of poetry.
- The loglevel can be set to DEBUG, INFO, WARNING, ERROR OR CRITICAL through the environment variable LOG_LEVEL in .env file
- Each module refers to the same logger object through inclusion of the following line
log=logging.getLogger('todo_app.app')
- To avoid capture of secrets passed through API calls, increase the loglevel for urllib3 by including the following line logging.
getLogger("urllib3").setLevel(logging.WARNING)
Authentication with Loggly is done through the use of customer token, which is stored in environment variable LOGGLY_TOKEN in .env file.
All the references to Trello have been replaced with Mongo database. The .env file should now include the MongoDB authentication variables, MongoDB Production Database/Collection names, and MongoDB Test Database/Collection names (for E2E tests).These are all included in .env.template file.
Nothing else changes, the application should be built and deployed as per previous instructions.
To support authentication, two roles (reader and writer) have been added. Authentication is enabled through GitHub Oath through the following steps -
- Create OAuth app with Github (https://docs.github.com/en/developers/apps/building-oauth-apps/creating-an-oauth-app)
- Use the URL (http://localhost:5000/) during registration process.
For the time-being, the accesses have been hard-coded in Authenticated_User module.