From c0415b955caa7374a6df24fdc4ef8fb90be5797a Mon Sep 17 00:00:00 2001 From: Nikolai R Kristiansen Date: Mon, 12 Jan 2015 00:07:29 +0100 Subject: [PATCH] Adds fabfile for deployment --- README.md | 17 ++++++++++++- fabfile.py | 63 ++++++++++++++++++++++++++++++++++++++++++++++++ requirements.txt | 1 + 3 files changed, 80 insertions(+), 1 deletion(-) create mode 100644 fabfile.py diff --git a/README.md b/README.md index ea83d9f..99e0e5b 100644 --- a/README.md +++ b/README.md @@ -1,10 +1,25 @@ # Install + apt install python-dev libmysqlclient-dev python-virtualenv virtualenv venv . venv/bin/activate pip install -r requirements +# Deploy +First you need to have your public key in /home/gitdeploy/.ssh/authorized_keys on the server. + +Then, to deploy via login.neuf.no, add the following to your personal ~/.ssh/config + + Host lynx.neuf.no + User gitdeploy + ForwardAgent yes + ProxyCommand ssh -W %h:%p login.neuf.no -l USERNAME + +Then you can do + + fab deploy + + # TODO -- Fabfile with deploy task - Alias should only allow unique (source,destination) tuples # TODO later diff --git a/fabfile.py b/fabfile.py new file mode 100644 index 0000000..138c457 --- /dev/null +++ b/fabfile.py @@ -0,0 +1,63 @@ +import logging +import sys + +from contextlib import contextmanager as _contextmanager +from fabric.api import run, sudo, env, cd, prefix, lcd +from fabric.api import local as lrun + +env.use_ssh_config = True +env.hosts = ['lynx.neuf.no'] +env.project_path = '/var/www/neuf.no/django-postfix-dovecot-api' +env.user = 'gitdeploy' +env.activate = 'source {}/venv/bin/activate'.format(env.project_path) + + +@_contextmanager +def virtualenv(): + with cd(env.project_path): + with prefix(env.activate): + yield + + +def __git_validate_clean(): + with lcd(env.project_path): + if lrun('git diff-files', capture=True) != '': + logging.error("Your git-tree isn't clean! Commit your changes before deploying.") + sys.exit(1) + + +def test(): + lrun("python manage.py test dpdapi") + + +def commit(): + lrun("git add -p && git commit") + + +def push(): + lrun("git push") + + +def prepare_deploy(): + test() + commit() + push() + + +def gitstatus(): + # __git_validate_clean() + with virtualenv(): + # Get source + run('git branch -v') + + +def deploy(): + # __git_validate_clean() + with virtualenv(): + run('git pull') # Get source + run('pip install -r requirements.txt') # install deps in virtualenv + run('python manage.py collectstatic --noinput') # Collect static + run('python manage.py migrate') # Run DB migrations + + # Reload gunicorn + sudo('/etc/init.d/gunicorn reload', shell=False) diff --git a/requirements.txt b/requirements.txt index 1aef608..123793d 100644 --- a/requirements.txt +++ b/requirements.txt @@ -4,3 +4,4 @@ django-filter==0.9.1 djangorestframework>=3.0,<3.1 passlib==1.6.2 mysqlclient==1.3.4 +Fabric==1.10.1