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