Coder Social home page Coder Social logo

to-do-app's Introduction

DevOps Apprenticeship: Project Exercise

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/install-poetry.py | python -

Poetry installation (PowerShell)

(Invoke-WebRequest -Uri https://raw.githubusercontent.com/python-poetry/poetry/master/install-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.template 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.

Set up MongoDB for tasks storage.

  1. Log into Azure and create MongoDB cluster either via the Portal UI or with the CLI.

With the Portal:

• New → CosmosDB Database • Select "Azure Cosmos DB API for MongoDB" • Choose "Serverless" for Capacity mode • Create new collection within your databse. • You can also configure secure firewall connections here, but for now you should permit access from "All Networks" to enable easier testing of the integration with the app.

With the CLI:

• Create new CosmosDB Account by typing in your terminal: az cosmosdb create --name <cosmos_account_name> --resource-group <resource_group_name> --kind MongoDB --capabilities EnableServerless -- server-version 3.6 • Create new MongoDB database under that account: az cosmosdb mongodb database create --account-name <cosmos_account_name> --name <database_name> --resource-group <resource_group_name> • Create new collection under that database account: az cosmosdb mongodb collection create --account-name <cosmos_account_name> --databasename <database_name> --name <collection_name> --resource-group <resource_group_name>

Obtain "Primary Connection String" to connect to your MongoDB instance. You can find it under "Settings" in the Azure Portal UI.

  1. Repeat Step 4 from 'Deploying the App on Azure' paragraph, but first populate your .envjson with the following:
{
  "DB_CONNECTION_STRING": "<Primary Connection String>",
  "TASKS_DB_NAME": "<database_name>",
  "COLLECTION_NAME": "<collection_name>"
}

Remember to rerun the azure cli command from Step 4.

  1. Task transfer from Trello to MongoDB

To transfer tasks from Trello to MongoDB instance, run python3 migrate.py in the root folder of this repo. By default it will delete the tasks from Trello during the migration. If you wish to change this behaviour set 'delete_after_transfer' value in 'migrate.py' file to False.

  1. Confirm your app is and running. Check if all your tasks from Trello have been migrated.

Go to "http://{Your web app name}.azurewebsites.net/"

Running the App locally

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.

Testing the App

To run tests which are kept in todo_app/tests directory use the following:

$ poetry run pytest or

If you wish to run only selected tests:

$ poetry run pytest todo_app/tests/<the file/test you wish to run>

Running the App on a VM

VM Provision:

Once on your Control Node, use this command to provision host VM: (You need to know the password to decrypt env_vars.yml)

$ ansible-playbook Playbook -i Inventory --ask-vault-pass

For this command to be successfull you need to make sure you can connect to your Host VM via SSH.

Running the App in a container (locally)

To spin up all three containers(dev, test & prod) use the following command:

$ docker-compose up

If you wish to spin up only one of the containers (either dev, test or prod) use respecitively either (Note: if you wish to run the prod container locally please change "$PORT" in gunicorn.sh to "80"):

$ docker-compose up webapp-prod
$ docker-compose up webapp-dev
$ docker-compose up webapp-test

Deploying the App on Azure

To deploy the app on Azure follow the below steps:

  1. Log in to Azure (You need to set up an account first - go to https://signup.azure.com/ to sign up )
$ az login
  1. Create an App Service Plan
$ az appservice plan create -g {Your resource group name eg. test-resource-group-01} -n {Name of the service app plan you wish to create e.g. test-asp-01 } --sku {e.g. B1} --is-Linux
  1. Create a Web App and deploy the production image container
$ az webapp create -g {Your resource group name} --plan {Your app service plan name} --name {Name of your app. Needs to be unique globally e.g. test-app-01} --deployment-container-image-name {docker hub username/name of the image and a tag e.g. username/your_apps_image:latest}
  1. Configure your app

First, create a file called "env.json" in which set all the environment variables following the format below:

{
  "CONFIG_KEY_01": "CONFIG_VALUE_01",
  "CONFIG_KEY_02": "CONFIG_VALUE_02"
}

Then run the following command:

$ az webapp config appsettings set -g {Your resource group name eg. test-resource-group-01} -n {Name of your app} --settings @env.json
  1. Confirm your app is up and running

Go to http://{Your web app name}.azurewebsites.net/

Provisioning Infrastructure

This app's infrastructure is managed via terraform.

Navigate to 'infrastructure' folder in your terminal and run:

terraform apply

Analyse the terraform plan and if you are happy with it enter 'Yes'.

Logging

Application logging is displayed in the console however there is an option to send the logs to Loggly. To send those logs follow the steps below.

  1. Sign up to Loggly using this link: https://www.loggly.com/
  2. Obtain a token from Loggly

Find the icon for "Logs" in the lefthand menu. Under this, select "Source Setup". Then, on the tabs along the top of the page, select "Customer Tokens". Add a new customer token.

  1. Add the token as an app setting to your app using 'LOGGLY_TOKEN' key.
  2. View logs in Loggly.

Go to Loggly and navigate to Logs > Log Explorer. Set the time range and click Search. You should see your logs.

to-do-app's People

Contributors

matjadach avatar robo-softwire avatar tom-bell-softwire avatar jonesey13 avatar hugh-emerson avatar

Watchers

 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.