Coder Social home page Coder Social logo

devops-course-starter's Introduction

DevOps Apprenticeship: Running Jason B's ToDo App

Azure

The app is live hosted on Azure:

https://jbuck-todo-app.azurewebsites.net/

In order to run within Azure, ensure that all the correct env variables are configured in the Azure Portal. If using GitHub for OAuth, you need to set up a different app in GitHub to accomodate the Azure endpoints and add those tokens in Azure configuration.

Terraform

The app can be created in Azure with Terraform. You will need to update the values in main.tf to reflect any unique environment settings.

Terraformed app URL: https://jb-terraform-todo-app.azurewebsites.net/

Checklist:

 * Create azruerm state back-end 
 * Create storage account
 * Create blob container
 * Create keyvault
 * Ensure unique keys are created for GitHub OAuth app integration
 * Integrate terrform config into Travis plus any env variables required

Follow this Tutorial

Documentation

Can be found in the 'Documentation' folder and uses the C4 methodology (https://c4model.com/)

 * Context Diagram
 * Container Diagram
 * Component Diagram
 * Code level diagrams

Use the plantUML plugin for VSCode (or other IDEs) to generate code level diagrams from simple markdown.

An example class_diagram.md for this application is included (and png of the diagram)

System Requirements

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):

Poetry installation (Bash)

curl -sSL https://raw.githubusercontent.com/python-poetry/poetry/master/get-poetry.py | python

Poetry installation (PowerShell)

(Invoke-WebRequest -Uri https://raw.githubusercontent.com/python-poetry/poetry/master/get-poetry.py -UseBasicParsing).Content | python

Dependencies

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.

Check for additional dependencies

Packages required:

  * Requests
  * Pytest
  * Dotenv
  * Selenium
  * Gunicorn
  * Flask-login
  * Oauthlib
  * PyMongo
  * Loggly

To update any missing or new dependencies:

  $ poetry add <package_name>

GitLab Oauth

You will need a valid GitHub account to run the application. This will give read only access and the ability to add or update items will be disabled.

To allow write access you will need to put your GitHub ID in the todo_user.py class before running the app. Additioanlly you will need to update your .env file to add your GitHub client ID and secret (see .env template file) Future updates will add valid users to the MongoDB.

Mongo DB

The project uses a Mongo database provisioned via Azure

Configure the following environment variables for your db instance:

 * MONGO_URL="your-url"
 * MONGO_DB="your-database"

The application will create the following collection when the first todo item is added, with the following schema:

 * todo_items: {"_id": "ITEM_ID", "name": "TODO_TITLE", "desc": "TODO_DESC", "due": "DD/MM/YYYY", "status_id": "STATUS_ID", "dateLastActivity": "ISO_DATETIME"}

Running the App

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.

Running Tests

Ensure the dependencies are correct as per previous section.

To run Selenium you will need an appropriate driver for your browser of choice:

 * Firefox - Gecko Driver
 * Chrome - ChromeDriver from the Chromium project

Tests:

 * test_todos: Unit tests for each class method
 * test_app: End2end system test with mocked MongoDB responses
 * test_browser: Selenium tests for launching app in different browsers (chrome test included)

Running Tests:

Unit and System tests:
 * $ poetry run pytest todo_app/tests
      OR individually run as:
      * $ poetry run pytest todo_app/tests/test_todos.py
      * $ poetry run pytest todo_app/tests/test_app.py

Browser tests:
 * $ poetry run pytest todo_app/browser_tests/chrome_test.py

Run all tests:
* $ sh test_suite.sh

Logging

Set the log level in your .env file, for example:
 * LOG_LEVEL=DEBUG

Logs can be sent to Loggly by providing a key and adding the entry to your .env:
 * LOGGLY_TOKEN=your-token

For more information on Loggly, read their documentation: LINK

Using a Virtual Machine

This application can also be run within a virtual machine by using Vagrant.

Vagrant requires a hypervisor installed. We recommend VirtualBox

Download and install Vagrant from the official website. You can check it's installed correctly by running the vagrant command in your terminal.

The Vagranfile within this repo will:

 * Pull down a Ubuntu Linux image to run the app
 * Prep your VM for Python installation
 * Install Python 3.8.5
 * Install poetry and load any dependencies
 * Launch the ToDo app

The VM can be managed using vagrant's CLI commands. Some useful ones are:

 * vagrant up - Starts your VM, creating and provisioning it automatically if required.
 * vagrant provision - Runs any VM provisioning steps specified in the Vagrantfile. Provisioning steps are one-off operations that adjust the system provided by the box.
 * vagrant suspend - Suspends any running VM. The VM will be restarted on the next vagrant up command.
 * vagrant destroy - Destroys the VM. It will be fully recreated the next time you run vagrant up.

Browse to the application from: http://0.0.0.0:5000/

Using Docker

This application can also be run within a Docker container, both as a production ready image, a development image and an image to just run tests.

Run the docker commands from the main application folder.

IMPORTANT:
You will need different .env files for production versus development. Use the production .env for running the tests (as you need to open a link to your database for the e2e system tests to work)

For production place the .env in your root and set the flask server config as:

# Flask server configuration.
FLASK_APP=todo_app.app
FLASK_ENV=production

For development place the .env in the todo_app directory and set the flask server config as:

# Flask server configuration.
FLASK_APP=app
FLASK_ENV=development

For a production ready container run:

$ docker-compose up --build

Image tag: 'todo-app:prod' Container will run using gunicorn

For a development ready container run:

$ docker-compose --file docker-compose-dev.yml up --build

Image tag: 'todo-app:dev' Container will run using the flask server and code can be updated and changes will reflect in the container

Browse to the application from: http://0.0.0.0:5000/

For a container to run all tests only:

$ docker-compose --file docker-compose-test.yml up --build

Image tag: 'todo-app:test' Container will run all the tests specified in 'entrypoint-test.sh' then stop

Using Travis CI

This application can be built using Travis CI.

The following files are included for Travis:

 * .travis.yml
 * docker-compose-travis.yml

The included travis.yml will do the following:

 * only run CI new PRs are targetted to main
 * only run CD when there are merges to main
 * run tests against the test docker image
 * if tests pass (and this is a merge to main), build a new production image
 * push new image to docker hub
 * push new image to Azure

Before using Travis for your build, change the following in the .travis.yml file:

 * Add any additional branches you want to auto build here: "(type = push AND branch IN (main)) OR (type = pull_request)"
 * Update any environment variables to meet your app needs (and remove any not needed)
 * Add any environment variables needed for Docker, Azure, MongoDb, etc 
 * Secure your secure tokens with your own Travis private key 
 * Update the notifications section to use your email address(es)

Goto https://travis-ci.com/ for more information on Travis

devops-course-starter's People

Contributors

jaybee1971 avatar tom-bell-softwire avatar jonesey13 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.