This repository has been archived by the owner on Apr 11, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 6
/
fabfile.py
executable file
·91 lines (74 loc) · 2.2 KB
/
fabfile.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
from fabric.api import local, env, get, prefix
from fabric.operations import prompt
from runreport.settings import FABRIC_HOSTS, DATABASES
import os
env.hosts = FABRIC_HOSTS
def syncdb():
'''
For dev only, passwords will be reset !
'''
# Import dump from server
local_encrypted = 'current_prod.gpg.tar'
local_dump = 'current_prod.tar'
if os.path.exists(local_dump):
# Adk for reuse of local_dump
keep_dump = prompt('Found a local dump (%s). Use it [y/n] ?' % local_dump)
if keep_dump.lower() != 'y':
os.unlink(local_dump)
if not os.path.exists(local_dump):
# Download
prod_dump = '~/backups/current/db.tar.gpg'
get(prod_dump, local_encrypted)
# Decrypt
decrypt = 'gpg --decrypt %s > %s' % (local_encrypted, local_dump)
local(decrypt)
# Cleanup
os.remove(local_encrypted)
# Re create db
createdb()
# Restore db from dump
cmd = pg(command='pg_restore')
cmd += ' -n public --no-owner %s' % local_dump
local(cmd)
# Run migrations
local('./manage.py migrate')
# Reset passwords
local('./manage.py reset_passwords')
# Build stats cache
local('./manage.py build_stats')
# Cleanup
os.remove(local_dump)
def createdb():
'''
Create the Pgsql database
* delete old database if exists
* create new one through psql
'''
# Drop old database manually
db = DATABASES['default']
pg('drop database if exists %s' % db['NAME'], 'postgres')
# Create new database
pg('create database %(NAME)s with owner = %(USER)s' % db, 'postgres')
# Init Postgis on database
pg('create extension postgis')
def pg(sql=None, dbname=None, command='psql'):
'''
Run a pgsql command through cli
'''
db = DATABASES['default']
suffix = db['ENGINE'][db['ENGINE'].rindex('.') + 1:]
if suffix not in ('postgis',):
raise Exception('Only PostGis is supported')
db['COMMAND'] = command
print db
cmd = 'PGPASSWORD="%(PASSWORD)s" %(COMMAND)s --username=%(USER)s --host=%(HOST)s --port=%(PORT)s' % db
cmd += ' --dbname=%s' % (dbname or db['NAME'])
if sql:
cmd += ' --command="%s"' % sql
local(cmd)
return cmd
def virtualenv(name='django'):
'''
Source a virtualenv on prefix
'''
return prefix('source %s/bin/activate' % name)