Skip to content

gmateesc/WebAppDeploymentAutomation

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

14 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

WebAppDeploymentAutomation

Table of Contents

Overview

This page discusses a solution to automating the deployment of a Flask web application.

Simple usage of the Ansible code

The simplest usage of the ansible code is to clone this repository and then run the playbook deploy_web_service.yml.

Get the code from Git

$ git clone https://github.com/gmateesc/WebAppDeploymentAutomation

$ cd WebAppDeploymentAutomation

Run ansible-playbook

Then set GIT_USER and GIT_PASSWORD for accessing the Git repository for the Web Service and run the playbook deploy_web_service.yml

  $ ansible-playbook -e gituser=$GIT_USER -e gitpassword=$GIT_PASSWORD deploy_web_service.yml 2>&1 \
                     | tee deploy_web_service.log

This will deploy the web app to the local host.

An example output from running the playbook is shown here.

To deploy to other hosts, edit the inventory file and add the desired hosts.

Start the service

Run the start/stop script created by Ansible. For the above run the script is:

  $ egrep "start\|stop" deploy_web_service.log 
  /tmp/Scheduler_app/app/WebService/schedule_service.sh {start|stop|status}

So one can start the service with

  $ /tmp/Scheduler_app/app/WebService/schedule_service.sh start

The inventory file

The default inventory file includes the local host, and shows how to add remote hosts, and, depending on the site settings, use a different remote user or use a jump-host to access the remote host:

$ more inventory#
# If the default version of python is not 2.6 or 2.7, 
# set ansible_python_interpreter to point to the correct 
# version of Python, e.g., 
#
# localhost ansible_connection=local ansible_python_interpreter=/usr/bin/python
#

#
# Add remote machines here using one of these formats
#
#  HOST_NAME_OR_IP_ADDR
#  HOST_NAME_OR_IP_ADDR ansible_user=USER
#  HOST_NAME_OR_IP_ADDR ansible_user=USER  ssh_args="-o ProxyCommand ssh -W %h:%p JUMP_HOST"
[webservers]
localhost ansible_connection=local

[all:children]
webservers

Description of the playbook

The playbook invokes several roles:

$ more deploy_web_service.yml
#
# Run with
#
#  ansible-playbook  -e gituser=$GIT_USER -e gitpassword=$GIT_PASSWORD deploy_web_service.yml
#
# If http_proxy or https_proxy is required, then define them in group_vars/all
#
---
- hosts: all
  gather_facts: no
  become: no

  roles:
  - { role: "http_proxy" }
  - { role: "python" }
  - { role: "pip" }
  - { role: "git" }
  - { role: "venv" }
  - { role: "app" }

where

  • role "http_proxy" enables deploying the web app to hosts that must use an http or http proxy to access Git and python repositories;

  • the roles "python" and "pip" check the presense of the required versions of python and pip

  • the role: "git" clones the web app from Git

  • the role: "venv" creates a python virtual environment containing all the modules required by the web app;

  • the role: "app" configures the web app as shown below.

Configuration of the web app

The app role includes the file roles/app/defaults/main.yml that defines application configuration. Additionally, group-level variables are defined in group_vars/all

File group_vars/all

The file group_vars/all defines application configuration, including:

  • the location where to deploy the web app: variable project_dir

  • the minimum required Python version;

  • the directory where to store the JSON documents POSTed by client: varible service_document_root;

  • the proxy to use for accesing http and https resources: variables http_proxy and https_proxy;

The full file group_vars/all is:

$ more group_vars/all 
---
#
# Settings defined here:
#
# 1. Override role defaults, e.g., ../roles/*/defaults
#
# 2. Are overriden by role vars, e.g., ../roles/*/vars
#

project_dir: /tmp/Scheduler_app

python_version: 2.6
python_virtualenv_path: "{{ project_dir}}/venv"

app_dir:  "{{ project_dir}}/app"


#
# If http/https proxy is needed, set the environment 
# variable http_proxy and https_proxy or set the 
# variables below. Setting the variables here is 
# needed when different host groups use different proxies.
#
#http_proxy: YOUR_HTTP_PROXY
#https_proxy: YOUR_HTTPS_PROXY

File roles/app/defaults/main.yml

The file roles/app/defaults/main.yml defines application configuration, including:

  • the location of the SSL certificates used by the web app: variable ssl_cert_root;

  • the directory where to store the JSON documents POSTed by client: varible service_document_root;

The file roles/app/defaults/main.yml is shown here:

$ more roles/app/defaults/main.yml 
---

#
# IP address and port to which the web service listens
#
webserver_host: "0.0.0.0"
webserver_port: 8888

#
# Service name and name of program providing the service
#
service_name: WebService
service_code: schedule_service.py
service_script: "{{ service_code | replace('.py', '.sh') }}"


#
# Directory where to deploy the SSL keys and cert
#
ssl_cert_root: "{{ app_dir }}/ssl"

#
# Directory where to store the JSON documents POSTed by client
#
service_document_root: "{{ app_dir}}/status"

#
# Log directory of the web service
#
service_log_dir: "{{ app_dir}}/log"




About

Automation of deploying a Flask web application

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages