-
Notifications
You must be signed in to change notification settings - Fork 1
/
fabfile.py
executable file
·123 lines (102 loc) · 3.06 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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
#!/usr/bin/python
# encoding: utf-8
""" Deployment module """
from __future__ import with_statement
import time
import os
import sys
from fabric.api import cd, run, prefix, task, env, roles, settings
from fabric.colors import yellow, green
from contextlib import contextmanager as _contextmanager
BASEDIR = os.path.realpath(os.path.join(os.path.dirname(__file__),
os.path.pardir))
sys.path.append(BASEDIR)
os.environ['DJANGO_SETTINGS_MODULE'] = 'exway.settings'
# globals
env.project = 'exway'
env.roledefs = {
'production': ['[email protected]'],
}
env.path = '/home/ubuntu/ivan-carmo-da-rocha-neto'
env.activate = 'source /home/ubuntu/.virtualenvs/exway/bin/activate'
env.colors = True
env.format = True
@_contextmanager
def virtualenv():
""" Setup env """
with cd(env.path):
with prefix(env.activate):
yield
@task
@roles('production')
def test():
""" Test machine name """
run('uname -a')
@task
@roles('production')
def sync_migrate_db():
"Migrate database"
with virtualenv(), settings(user='ubuntu'):
print(yellow('Syncing and Migrating database'))
run('pip install -r requirements.txt')
run('python manage.py syncdb')
print(green('Done'))
@task
@roles('dev')
def pull(branch):
"Pull files from git to server"
with virtualenv(), settings(user='ubuntu'):
print(yellow('Reset Head'))
run("git checkout -f")
run("git checkout %s" % branch)
print(green('Done'))
print(yellow('Pull files from server'))
run("git pull origin master")
print(green('Done'))
run("git log -n 1")
print(green('Done'))
@task
@roles('dev')
def restart():
"Restart webserver"
print(yellow('Restart server'))
with virtualenv(), settings(user='ubuntu'):
run("./restart.sh")
print(green('Done'))
@task
@roles('dev')
def collectstatic():
"collect static files"
with virtualenv(), settings(user='ubuntu'):
print(yellow("Collecting Files"))
run("python manage.py collectstatic --noinput")
print(green("Collect static complete!"))
@task
@roles('dev')
def deploy_dev(branch='master'):
"Send files to server and restart webserver"
pull(branch)
sync_migrate_db()
collectstatic()
restart()
@task
@roles('production')
def deploy():
"""deploy to production server"""
WORKON = 'source /home/ubuntu/.virtualenvs/exway/bin/activate'
with prefix(WORKON), cd('/home/ubuntu/ivan-carmo-da-rocha-neto/'):
run('mkdir -p static')
run('mkdir -p exway/static')
run('mkdir -p public')
run('mkdir -p logs/nginx')
run('git checkout -f')
run('git pull')
# clean pyc
run('rm -rfv `find ./ | egrep -i "(pyc|.swp|˜)$"`')
# install requirements
run('pip install -r requirements.txt')
run('python manage.py syncdb')
run('python manage.py collectstatic --noinput')
time.sleep(3)
run('/home/ubuntu/ivan-carmo-da-rocha-neto/restart.sh', pty=False)
sys.exit()