-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathfabfile.py
executable file
·117 lines (89 loc) · 3.96 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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import os
import getpass
from StringIO import StringIO
from fabric.api import *
from fabric.contrib.files import exists
from jinja2 import Environment, FileSystemLoader
PROJECT_ROOT = os.path.dirname(__file__)
env.user = 'deploy'
@task
def staging():
env.environment = 'staging'
env.hosts = ['portland.pynorth.org']
env.site_hostname = 'staging.cfp.pycon.ca'
env.root = '/srv/www/pycon.ca/staging.cfp/django'
env.branch = 'master'
env.db_name = 'pycon2016_staging'
env.db_user = 'symposion'
env.workers = 1
env.db_pass = getpass.getpass(prompt="Please enter database (%(db_name)s) password for user %(db_user)s: " % env)
setup_path()
@task
def production():
env.environment = 'production'
env.hosts = ['portland.pynorth.org']
env.site_hostname = 'cfp.pycon.ca'
env.root = '/srv/www/pycon.ca/cfp/django'
env.branch = 'master'
env.db_name = 'pycon2016'
env.db_user = 'symposion'
env.workers = 2
env.db_pass = getpass.getpass(prompt="Please enter database (%(db_name)s) password for user %(db_user)s: " % env)
setup_path()
def setup_path():
env.slackbot_token = getpass.getpass(prompt="Please enter slackbot token: ")
env.code_root = os.path.join(env.root, 'symposion2016')
env.virtualenv_root = os.path.join(env.root, 'env')
env.logs_root = os.path.join(env.root, 'logs')
env.run_root = os.path.join(env.root, 'run')
@task
def deploy():
require('environment')
# Create a directory on a remote server, if it doesn't already exists
if not exists(env.code_root):
sudo('mkdir -p %(code_root)s' % env)
if not exists(env.logs_root):
sudo('mkdir -p %(logs_root)s' % env)
if not exists(env.run_root):
sudo('mkdir -p %(run_root)s' % env)
# Create a virtualenv, if it doesn't already exists
if not exists(env.virtualenv_root):
with cd(env.root):
sudo('mkdir env')
sudo('virtualenv env')
local('git archive --format=tar %(branch)s | gzip > release.tar.gz' % env)
put('release.tar.gz', env.code_root, use_sudo=True)
with cd(env.code_root):
sudo('tar zxf release.tar.gz', pty=True)
local('rm release.tar.gz')
# Activate the environment and install requirements
# run('source %(remote_env_path)s/bin/activate' % env)
sudo('source %(virtualenv_root)s/bin/activate && pip install --upgrade -r requirements.txt' % env)
with shell_env(DJANGO_SETTINGS_MODULE='symposion2016.settings.prod',
DATABASE_URL='postgres://%(db_user)s:%(db_pass)s@localhost:5432/%(db_name)s' % env,
PYTHONPATH='.'):
# Collect all the static files
sudo('%(virtualenv_root)s/bin/python manage.py collectstatic --noinput' % env)
# Give deploy access to logs and run directories
sudo('chown -R deploy:deploy %(logs_root)s' % env)
sudo('chown -R deploy:deploy %(run_root)s' % env)
# Migrate and Update the database
run('%(virtualenv_root)s/bin/python manage.py migrate --noinput' % env)
run('%(virtualenv_root)s/bin/python manage.py create_review_permissions' % env)
# gunicorn entry script
put(get_and_render_template('gunicorn_run.sh', env),
os.path.join(env.run_root, 'gunicorn_run.sh'), use_sudo=True)
sudo('chmod u+x %(run_root)s/gunicorn_run.sh' % env)
# put supervisor conf
put(get_and_render_template('symposion.conf', env),
'/etc/supervisor/conf.d/symposion_%(environment)s.conf' % env,
use_sudo=True)
# restart supervisor
sudo('supervisorctl reread && supervisorctl update')
sudo('supervisorctl restart symposion_%(environment)s' % env)
def get_and_render_template(filename, context):
jinja_env = Environment(loader=FileSystemLoader('scripts'))
tmpl = jinja_env.get_template(filename)
return StringIO(tmpl.render(context))