-
Notifications
You must be signed in to change notification settings - Fork 0
/
fabfile.py
85 lines (70 loc) · 2.74 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
from fabric.api import *
from fabric.contrib.console import confirm
from contextlib import contextmanager as _contextmanager
production_hosts = ['[email protected]']
production_user = 'rday'
production_branch = 'production'
staging_hosts = ['[email protected]']
staging_user = 'rday'
staging_branch = 'staging'
repository = 'git://github.com/novapython/Website.git'
root_dir = '/var/www/novapython/Website'
code_dir = '%s/src' % root_dir
virtualenv_dir = '%s/env' % root_dir
env.activate = 'source %s/bin/activate' % virtualenv_dir
@_contextmanager
def virtualenv():
with cd(code_dir):
with prefix(env.activate):
yield
def production_deploy():
env.user = production_user
env.environment = 'production'
env.hosts = production_hosts
env.branch = production_branch
execute(_deploy)
def staging_deploy():
env.user = staging_user
env.environment = 'staging'
env.hosts = staging_hosts
env.branch = staging_branch
execute(_deploy)
def build_virtualenv():
sudo('mkdir -p %s' % virtualenv_dir)
sudo('chown -R %s.%s %s' % (env.user, env.user, virtualenv_dir))
sudo('easy_install pip')
sudo('yum -y install gcc mysql-devel python-devel mod_wsgi')
run('virtualenv --no-site-packages %s' % virtualenv_dir)
def build_src():
sudo('mkdir -p %s' % code_dir)
sudo('chown -R %s.%s %s' % (env.user, env.user, code_dir))
with cd(code_dir):
run("git clone %s %s" % (repository, code_dir))
run('mkdir %s/sessions' % code_dir)
run('chmod 777 %s/sessions' % code_dir)
def _deploy():
if local('test deploy_files/config-%s.py' % env.environment).failed:
abort('No config file was found!')
if local('test deploy_files/virtualhost-%s' % env.environment).failed:
abort('No virtualhost file was found!')
sudo('mkdir -p /var/run/wsgi')
with settings(warn_only=True):
if run('test -d %s' % virtualenv_dir).failed:
execute(build_virtualenv)
if run('test -d %s' % code_dir).failed:
execute(build_src)
with virtualenv():
run('git pull origin %s' % env.branch)
run('pip install -r %s/requirements.txt' % code_dir)
put('deploy_files/config-%s.py' % env.environment, 'novaconfig.py')
put('deploy_files/virtualhost-%s' % env.environment, 'virtualhost')
if run('test -d /etc/apache2/sites-enabled').failed:
sudo('mkdir -p /etc/apache2/sites-enabled')
sudo('mv virtualhost /etc/apache2/sites-enabled/014-novapy')
sudo('/usr/sbin/apache2ctl restart')
sudo('/usr/sbin/service varnish restart')
def deploy():
if confirm('Deploy to production?', default=False):
production_deploy()
else:
staging_deploy()