Commit 65f2cab9952dbcfe99e438318ac4c532409d3dfa

Authored by hitier
1 parent bb40eaa6

Auto-Deploy files

INSTALL.md
... ... @@ -9,3 +9,12 @@ update your server installation
9 9 ress/flaskenv > .flaskenv
10 10 run tests
11 11 PYTHONPATH=. pytest --cov=app --cov-report=xml:"coverage.xml" --cov-report=term --junitxml "tests-report.xml"
  12 +install apache
  13 +--------------
  14 +pdc_web.py
  15 +pdc_web.wsgi
  16 +.flaskenv
  17 +post-deploy.sh
  18 +git-hook
  19 +virtual-apage.conf
  20 +git bare repo
... ...
pdc_web.wsgi 0 → 100644
... ... @@ -0,0 +1,43 @@
  1 +#
  2 +# Simple wsgi wrapper to run flask app through apache.
  3 +#
  4 +# Relies on an apache2 virtualhost configuration.
  5 +# See ./resources/apache2-virtual-host.conf
  6 +#
  7 +# It merely calls the app factory and exports it as
  8 +# the application variable.
  9 +
  10 +# The apache WSGIDaemonProcess allows us to set
  11 +# a virtualenv
  12 +# a pythonpath
  13 +
  14 +# Instead we could:
  15 +#
  16 +## 1- activate venv
  17 +# python_home = '/var/www/html/app-web/'
  18 +# activate_this = python_home + 'venv/bin/activate_this.py'
  19 +# with open(activate_this) as file_:
  20 +# exec(file_.read(), dict(__file__=activate_this))
  21 +#
  22 +## 2- set a python path
  23 +# sys.path.insert(0, '/var/www/html/app-web/')
  24 +#
  25 +## 3- set some logging facilities
  26 +# import logging, sys
  27 +# logging.basicConfig(stream=sys.stderr)
  28 +
  29 +# # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
  30 +
  31 +#
  32 +# Main Part
  33 +#
  34 +
  35 +# simply use application already set in main app file
  36 +#
  37 +from app import create_app
  38 +
  39 +
  40 +application = create_app()
  41 +
  42 +
  43 +# vim: ft=python
... ...
resources/apache2-virtual-host.conf 0 → 100644
... ... @@ -0,0 +1,66 @@
  1 +# Apache virtual host conf file for Climso-Web web-app
  2 +#
  3 +# Will map a server name and an url to the wsgi script.
  4 +#
  5 +# Allows to use a python virtualenvironment thanks to two WSGIDaemonProcess
  6 +# attributes:
  7 +# python-home
  8 +# python-path
  9 +
  10 +
  11 +# Edit Configuration
  12 +#
  13 +Define flaskapp_path /var/www/html/pdc-web
  14 +Define flaskapp_wsgiscript pdc_web.wsgi
  15 +Define flaskapp_server pdc-it1.irap.omp.eu
  16 +Define flaskapp_user apache
  17 +Define flaskapp_group apache
  18 +
  19 +# Warning:
  20 +# --------
  21 +#
  22 +# Stdout/Stderr wont be logged in per virtualhost log files.
  23 +#
  24 +# 1- Unsuccessfull tries to fix it:
  25 +#
  26 +# WSGIRestrictEmbedded On
  27 +# WSGIRestrictStdout Off
  28 +#
  29 +# 2- what works is to comment out ErrorLog, TransferLog and CustomLog
  30 +# and let apache log everything in global log files
  31 +
  32 +<VirtualHost ${flaskapp_server}:80>
  33 + # Add machine's IP address (use ifconfig command)
  34 + ServerName ${flaskapp_server}
  35 + # Give an alias to to start your website url with
  36 + DocumentRoot ${flaskapp_path}
  37 + LogLevel warn
  38 + LogFormat "%a %l %u %t \"%r\" %>s %b"
  39 + #
  40 + # Virtualhost log config doesnt show stdout/err see before
  41 + # ErrorLog ${flaskapp_path}/flaskapp-error.log
  42 + # TransferLog ${flaskapp_path}/aroma-access.log
  43 + # CustomLog ${flaskapp_path}/aroma-custom.log combined
  44 +
  45 + # python-home is the virtual env path
  46 + # python-path sets the PYTHON_PATH for modules import
  47 + WSGIDaemonProcess ${flaskapp_server} \
  48 + user=${flaskapp_user} group=${flaskapp_user} \
  49 + processes=2 threads=5 \
  50 + python-home=${flaskapp_path}/venv \
  51 + python-path=${flaskapp_path} \
  52 + display-name=%{GROUP}
  53 + WSGIScriptAlias / ${flaskapp_path}/${flaskapp_wsgiscript}
  54 + WSGIProcessGroup ${flaskapp_server}
  55 + WSGIApplicationGroup %{GLOBAL}
  56 + <Directory ${flaskapp_path}>
  57 + # set permissions as per apache2.conf file
  58 + Options -Indexes -MultiViews +FollowSymLinks
  59 + AllowOverride All
  60 + Require all granted
  61 + </Directory>
  62 +</VirtualHost>
  63 +
  64 +
  65 +
  66 +# vim: tabstop=4 sw=4 et tw=0
... ...
resources/post-receive.git-hook 0 → 100644
... ... @@ -0,0 +1,69 @@
  1 +#!/bin/bash
  2 +#
  3 +# Continuous Deployment
  4 +# ---------------------
  5 +#
  6 +# This file is a post-receive hook to be installed in the
  7 +# ./hooks/ dir of a bare git repository.
  8 +#
  9 +# When pushing to that repo from outside (a dev machine,
  10 +# or a jenkins/gitlab ci_cd rule), it updates a working dir previously
  11 +# set by cd to ${TARGET} and get the latest code .
  12 +
  13 +# How to set it up:
  14 +# -----------------
  15 +#
  16 +# On the server to deploy:
  17 +#
  18 +# 1- create a bare repository and set hook
  19 +# git clone url-to-central-repo deploy-repo.git
  20 +# cp post-receive.git-hook deploy-repo.git/hooks/post-receive
  21 +#
  22 +# 2- set a working dir and configure
  23 +# cd /var/www/
  24 +# git clone deploy-repo.git your-app-dir
  25 +# do_any_config_tasks
  26 +#
  27 +#
  28 +# On the desktop computer:
  29 +#
  30 +# 1- add the server as a remote source
  31 +# git remote add deploy url-to-deploy-repo.git
  32 +#
  33 +# 2- try it out
  34 +# git push deploy HEAD:BRANCH_NAME
  35 +
  36 +#
  37 +# $TARGET: is the working dir you want to update at last
  38 +# $BRANCH: is the branch you want to update from
  39 +#
  40 +TARGET="/var/www/aroma-db"
  41 +BRANCH="DEV"
  42 +
  43 +while read oldrev newrev ref
  44 +do
  45 + # only checking out the branch you would like to deploy
  46 + if [ "$ref" = "refs/heads/$BRANCH" ];
  47 + then
  48 + echo "Ref $ref received. Deploying ${BRANCH} branch to production..."
  49 + cd $TARGET || exit
  50 + unset GIT_DIR
  51 + git stash # remove any modification, sorry guy.
  52 + git checkout ${BRANCH} # set branch if not done yet, sorry guy
  53 + git pull origin ${BRANCH} # get latest modifications, assumes remote origin previously set
  54 + touch *wsgi # now, trigger wsgidaemons restarting
  55 + /bin/sh scripts/post-deploy.sh
  56 + exec git update-server-info
  57 + else
  58 + echo "Ref $ref received. Doing nothing: only the ${BRANCH} branch may be deployed on this server."
  59 + fi
  60 +done
  61 +
  62 +# Alternately, it is possible to just update a
  63 +# working tree without any git meta data in it with the
  64 +# following instruction:
  65 +# $GIT_DIR: is the directory of current bare repo
  66 +# GIT_DIR="/home/richard/aroma-db.git/"
  67 +# git --work-tree=$TARGET --git-dir=$GIT_DIR checkout -f $BRANCH
  68 +
  69 +# vim: ft=sh
... ...
scripts/post-deploy.sh 0 → 100644
... ... @@ -0,0 +1,15 @@
  1 +#!/bin/sh
  2 +
  3 +# SCript to run after application source was updated.
  4 +#
  5 +# Should be execute within root directory of project
  6 +
  7 +
  8 +cd ..
  9 +
  10 +source ./venv/bin/activate
  11 +pip install -r requirements.txt
  12 +
  13 +# Now run some cli post update commands
  14 +# flask db upgrade
  15 +# flask translation
... ...