forked from arachnys/cabot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
fabfile.py
173 lines (138 loc) · 5.45 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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
from fabric.api import *
from fabric.contrib.files import *
from fabric.contrib.project import rsync_project
from subprocess import check_output
env.use_ssh_config = True
env.user = 'ubuntu'
ROOT_DIR = os.path.dirname(os.path.abspath(__file__))
HOME_DIR = '/home/ubuntu'
DEPLOY_PATH = '%s/cabot' % HOME_DIR
LOG_DIR = '/var/log/cabot/'
VENV_DIR = '%s/venv' % HOME_DIR
BACKUP_DIR = '/tmp/'
PG_DATABASE = 'index'
PG_USERNAME = 'cabot'
PG_PASSWORD = 'cabot' # You should probably change this
def _ensure_dirs():
dirs = [LOG_DIR]
for d in dirs:
sudo('mkdir -p {d}'.format(d=d))
sudo('chmod -R 777 {d}'.format(d=d))
def _setup_venv():
with settings(warn_only=True):
if sudo('test -d %s' % VENV_DIR).failed:
sudo('virtualenv %s' % VENV_DIR)
def install_requirements(deploy_path=DEPLOY_PATH):
sudo("{venv}/bin/pip install -e {path} --exists-action=w".format(
venv=VENV_DIR, path=deploy_path))
def run_migrations(deploy_path=DEPLOY_PATH):
with cd(deploy_path):
with prefix("source {venv}/bin/activate".format(venv=VENV_DIR)):
sudo(
"foreman run python manage.py syncdb -e conf/{env}.env".format(env=env.deploy_version))
sudo(
"foreman run python manage.py migrate cabotapp --noinput -e conf/{env}.env".format(env=env.deploy_version))
# Wrap in failure for legacy reasons
# https://github.com/celery/django-celery/issues/149
print "You can ignore an error message regarding 'relation \"celery_taskmeta\" already exists'"
with settings(warn_only=True):
sudo(
"foreman run python manage.py migrate djcelery --noinput -e conf/{env}.env".format(env=env.deploy_version))
def collect_static(deploy_path=DEPLOY_PATH):
with cd(deploy_path):
with prefix("source {venv}/bin/activate".format(venv=VENV_DIR)):
sudo(
"foreman run python manage.py collectstatic --noinput -e conf/{env}.env".format(env=env.deploy_version))
sudo(
"foreman run python manage.py compress -e conf/{env}.env".format(env=env.deploy_version))
def setup_upstart(deploy_path=DEPLOY_PATH):
with cd(deploy_path):
# Point at master (i.e. symlinked) path
procfile = os.path.join(DEPLOY_PATH, 'Procfile')
env_file = os.path.join(DEPLOY_PATH, 'conf', '%s.env' %
env.deploy_version)
template_file = os.path.join(DEPLOY_PATH, 'upstart')
sudo('foreman export upstart /etc/init -f {conf} -e {env} -u ubuntu -a cabot -t {tmplt}'.format(
conf=procfile, env=env_file, tmplt=template_file))
def production():
"""
Select production instance(s)
"""
env.hosts = ['cabot.arachnys.com']
def restart():
with settings(warn_only=True):
if sudo('restart cabot').failed:
sudo('start cabot')
def stop():
with settings(warn_only=True):
sudo('stop cabot')
def provision():
"""
Provision a clean Ubuntu 12.04 instance with dependencies
"""
with open(os.path.expanduser('~/.ssh/id_rsa.pub')) as f:
local_ssh_key = f.read().strip('\n')
put('bin/setup_dependencies.sh', '/tmp/setup_dependencies.sh')
sudo('LOCAL_SSH_KEY="%s" bash /tmp/setup_dependencies.sh' % local_ssh_key)
# Clean up
run('rm /tmp/setup_dependencies.sh')
def deploy(deploy_version=None):
"""
Deploy a new version of code to production or test server.
Push code to remote server, install requirements, apply migrations,
collect and compress static assets, export foreman to upstart,
restart service
"""
# TODO: replace this with
# - zip up working directory
# - upload and unzip into DEPLOY_PATH
env.deploy_version = deploy_version or 'production'
dirname = check_output(
["echo \"$(date +'%Y-%m-%d')-$(git log --pretty=format:'%h' -n 1)\""], shell=True).strip('\n ')
deploy_path = os.path.join(HOME_DIR, dirname)
run('mkdir -p {}'.format(deploy_path))
print 'Uploading project to %s' % deploy_path
rsync_project(
remote_dir=deploy_path,
local_dir='./',
exclude=['.git', 'backups', 'venv',
'static/CACHE', '.vagrant', '*.pyc', 'dev.db'],
)
with cd(deploy_path):
_ensure_dirs()
_setup_venv()
create_database()
install_requirements(deploy_path)
run_migrations(deploy_path)
collect_static(deploy_path)
# This may cause a bit of downtime
run('ln -sfn {new} {current}'.format(
new=deploy_path,
current=DEPLOY_PATH
))
setup_upstart(deploy_path)
restart()
print "Done!"
def backup():
"""
Back up database locally
TODO: send backups to s3
"""
backup_file = 'outfile.sql.gz'
with cd(BACKUP_DIR):
run('PGPASSWORD=cabot pg_dump -U cabot index | gzip > {}'.format(backup_file))
get(backup_file, 'backups/%(basename)s')
def create_database():
"""Creates role and database"""
with settings(warn_only=True):
sudo(
'psql -c "CREATE USER %s WITH NOCREATEDB NOCREATEUSER ENCRYPTED PASSWORD E\'%s\'"' %
(PG_USERNAME, PG_PASSWORD), user='postgres')
sudo('psql -c "CREATE DATABASE %s WITH OWNER %s"' %
(PG_DATABASE, PG_USERNAME), user='postgres')
@parallel
def logs():
"""
Tail logfiles
"""
sudo('tail -f {logdir}* /var/log/nginx/*.log'.format(logdir=LOG_DIR))