From aaf846aceef8ed6cf9e4d30233ddcee970764fd7 Mon Sep 17 00:00:00 2001 From: Claude Paroz Date: Thu, 11 Jul 2013 10:53:18 +0200 Subject: [PATCH] Add a fab script to locally clone remote data --- .gitignore | 1 + scripts/fabfile.py | 36 ++++++++++++++++++++++++++++++++++++ 2 files changed, 37 insertions(+) create mode 100644 scripts/fabfile.py diff --git a/.gitignore b/.gitignore index 353d9fb..cdd541d 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,4 @@ *.pyc database.db common/local_settings.py +scripts/epcstages.json diff --git a/scripts/fabfile.py b/scripts/fabfile.py new file mode 100644 index 0000000..b340077 --- /dev/null +++ b/scripts/fabfile.py @@ -0,0 +1,36 @@ +# -*- coding: utf-8 -*- +import os +import sys + +from fabric.api import cd, env, get, local, prefix, prompt, run +from fabric.utils import abort + +env.hosts = ['stages.pierre-coullery.ch'] + +def clone_remote_db(dbtype='sqlite'): + """ + Copy remote data (JSON dump), download it locally and recreate a local + SQLite database with those data. + """ + sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__)))) + from common import settings + + db_path = settings.DATABASES['default']['NAME'] + if os.path.exists(db_path): + rep = prompt('A local database (%s) already exists. Overwrite? (y/n)' % db_path) + if rep == 'y': + os.remove(db_path) + else: + abort("Database not copied") + + # Dump remote data and download the file + with cd('/var/www/epcstages'): + with prefix('source /var/venvs/stages/bin/activate'): + run('python manage.py dumpdata --indent 1 contenttypes auth stages > epcstages.json') + get('epcstages.json', '.') + + # Recreate a fresh DB with downloaded data + local("python ../manage.py syncdb --noinput") + local("python ../manage.py migrate") + local("python ../manage.py flush --noinput") + local("python ../manage.py loaddata epcstages.json")