Code Monkey home page Code Monkey logo

webapp's Introduction

This is an example Flask app, ready to be deployed with a Vagrant and Ansible.

## Flask Web App - Simple RESTful API
simple RESTful API using the Flask web framework (example: app.py)

Supported methods:
  - POST - Create a new event. Event will be added to DB with timestamp(UTC timezone)
          curl --header "Content-Type: application/json" --request POST --data '{"event":"some text"}' http://webapp/add
  - GET - Obtain information about all events stored in DB.
          curl http://webapp/get_all_events      

## How it works

Vagrant will deploy 4 hosts:
  - db   - database
  - app1 - flask application
  - app2 - flask application
  - lb   - nginx loadbalancer

after build process Vagrant will run Ansible provisioner once for all VMs at the end, as we have predifined list of machines and their roles. 
(can be extend to run for each box based on role) or (run based on dynamic inventory)

### Ansible
basic playbook will utilise 3 roles:
 - mongodb
    - add official mongodb repository
    - install mongodb-org
    - configure mongod to listen on 0.0.0.0 (all network interfaces)
 - webapp
    - install required python packages
    - create local user to run flask app 
    - deploy application from git 
    - create python virtual env and populate it with required modules (Flask, Gunicorn, PyMongo)
    - install systemd service to run application
    - install and configure nginx to run as reverse proxy for Gunicorn
 - lb
    - Install NGinx
    - Configure NGinx upstream servers based on Ansible inventory group

### Ansible Roles defaults that can be overwrited in playbook (see configuration.yml)
  - mongodb role
    - repo - repository to install MongoDB from
    - repo_keyserver - repository apt key
    - repo_id - repository apt key id
  - webapp role
    - webapp_name - web application name
    - webapp_user - user under which appliation will be started
    - git_repo - git repository from which application can be deployed
    - dbserver - database server to which application will be connected
    - dbport - database port to which application will be connected
  - lb role
    - app_name - the same as 'webapp_name' for webapp role, base for URL, example http://flask_app
    - webservers - ansible inventory group from which upstream servers will be populated

## Prerequisites
  - Vagrant installed
  - VirtualBox as provider for Vagrant
  - Ansible
  How to install them (out of scope this document) - you can find in official documentation.

## Deploying the app
Be sure that you have your ssh id_rsa key configured and installed in "~/.ssh/id_rsa" as Vagrant will use it to deploy it to a new build machines.
So you can ssh to machines from anywhere.

Run Vagrant
```
vagrant up
```

## Test
To test if you can POST data:
```
LB call
curl --header "Content-Type: application/json" --request POST --data '{"event":"some text"}' http://192.168.32.4/add

Directly APP call
curl --header "Content-Type: application/json" --request POST --data '{"event":"some text"}' http://192.168.32.3/add
```

To retrieve data from DB
```
curl http://192.168.32.4/get_all_events

curl http://192.168.32.3/get_all_events
```
Both results should be the same.

## Backup Strategy
Backup strategy depends from business requirements and DB size. 

Smaller DBs can be backuped by(in our case) by mongodump and restored by mongorestore. Automation - cron. 

for Bigger DBs - it will be good to use host "offload" - where we can "freeze" DB or temporary deny any write operation for time to take 
FS snapshot. After this snapshot can be presented to "backup" server to backup data files.
This also can be scheduled by cron, but better option is - to utilise centraly management solution like an example - Jenkins, etc.










webapp's People

Contributors

timota avatar

Watchers

 avatar  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.