-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfabfile.py
103 lines (77 loc) · 2.61 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
92
93
94
95
96
97
98
99
100
101
102
103
import os
import sys
from fabric.colors import green, red, blue
from fabric.context_managers import settings
from fabric.decorators import task, runs_once
from fabric.operations import local
from fabric.state import env as fab_env
@runs_once
def _set_current_environment(env):
"""sets the current environment for this invocation of Fabric
Runs only once, and is intended to be called by the first task"""
print('setting Fabric environment to {}'.format(env))
fab_env['environment'] = env
print('setting xword config to {}'.format(fab_env['environment']))
os.environ['CONFIG_ENV'] = './config/{}.yaml'.format(fab_env['environment'])
# TODO: Make the path work generically
os.environ['PYTHONPATH'] = "/Users/rachelkogan/xword-app"
@task
def test():
"""Load test configuration"""
_set_current_environment('test')
@task
def dev():
"""Load test configuration"""
_set_current_environment('dev')
@task
def clean():
"""Remove all .pyc files."""
print green('Clean up .pyc files')
local("find . -name '*.py[co]' -exec rm -f '{}' ';'")
@task
def db():
"""Connect to the database."""
from xword.utils.configuration import config
local(
'psql -h {} -p {} --username {} {}'.format(
config.get('database.host'),
config.get('database.port'),
config.get('database.user'),
config.get('database.name'),
),
)
@task
def migrate(command='upgrade head'):
"""Database migration."""
print green('Running migrations'.format(fab_env['environment']))
# Migrate tables
res = local('python manage.py db {}'.format(command))
if not res.succeeded:
print red('Failed to migrate tables.')
return
print green('Successfully migrated the database.')
@task
def serve():
"""Start the server."""
local('python app.py')
@task()
def shell():
"""Run the shell."""
local("python manage.py shell")
@task()
def bootstrap_database(force=False):
"""Bootstrap the database."""
if fab_env['environment'] == 'prod' and not force:
raise ValueError('Bootstrapping the production database is not allowed.')
from xword.utils.configuration import config
with settings(warn_only=True):
# Create a new role
local(
'psql -h {} -p {} -c "CREATE ROLE {} WITH ENCRYPTED PASSWORD \'{}\' '
'SUPERUSER CREATEDB CREATEROLE LOGIN;"'.format(
config.get('database.host'),
config.get('database.port'),
config.get('database.user'),
config.get('database.password'),
)
)