-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathfabfile.py
More file actions
157 lines (130 loc) · 4.85 KB
/
fabfile.py
File metadata and controls
157 lines (130 loc) · 4.85 KB
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
import os
from fabric.api import *
from fabric.contrib.project import rsync_project
from fabric.contrib import files, console
from fabric import utils
from fabric.decorators import hosts
import envs.local_settings as local_settings
import envs.pro_settings as pro_settings
RSYNC_EXCLUDE = (
'.DS_Store',
'.hg',
'.svn',
'*.pyc',
'*.example',
'media/admin',
'media/attachments',
'fabfile.py',
'bootstrap.py',
)
# Remote Home Directory
env.home = '/home/deploy'
# Target DNS name
env.domain = 'cfbreference.com'
# Remote Project Directory. Default: cfbreference.com
env.project = 'cfbreference_com'
# Remote User
env.user = 'deploy'
# Apache Root
env.apache_conf_root = "/etc/apache2/sites-available"
def _setup_path():
# Destination site for this push.
env.site = env.environment + "." + env.domain
# Root directory for project. Default: {env.home}/www/{env.site}
env.root = os.path.join(os.path.join(env.home, "www"), env.site)
# Root directory for source code. Default: {env.root}/{env.project}
env.code_root = os.path.join(env.root, env.project)
# Remote virtualenv directory. Default: {env.home}/.virtualenvs/{env.site}
env.virtualenv_root = os.path.join(os.path.join(env.home, '.virtualenvs'), env.site)
# Target project settings file. Default: {env.project}.settings
env.settings = '%s.settings' % env.project
def pro():
# Target Environment sub-domain ( i.e. beta.domain.com, www.domain.com)
env.environment = 'www'
# Host Tuple List for deployment.
env.hosts = pro_settings.HOSTS
_setup_path()
def bootstrap():
""" initialize remote host environment (virtualenv, deploy, update) """
# Require a valid env.root value
require('root', provided_by=('pro'))
# Create env.root directory
run('mkdir -p %(root)s' % env)
create_virtualenv()
deploy()
update_requirements()
def create_virtualenv():
""" setup virtualenv on remote host """
require('virtualenv_root', provided_by=('pro'))
args = '-v --clear'
run('mkdir -p %s' % env.virtualenv_root)
run('virtualenv %s %s' % (args, env.virtualenv_root))
def deploy():
""" rsync code to remote host """
require('root', provided_by=('pro'))
if env.environment == 'pro':
if not console.confirm('Are you sure you want to deploy production?', default=False):
utils.abort('Production deployment aborted.')
# defaults rsync options:
# -pthrvz
# -p preserve permissions
# -t preserve times
# -h output numbers in a human-readable format
# -r recurse into directories
# -v increase verbosity
# -z compress file data during the transfer
extra_opts = '--omit-dir-times'
rsync_project(
remote_dir=env.root,
exclude=RSYNC_EXCLUDE,
delete=True,
extra_opts=extra_opts,
)
touch()
update_apache_conf()
def update_requirements():
""" update external dependencies on remote host """
require('code_root', provided_by=('pro'))
requirements = os.path.join(env.code_root, 'requirements')
with cd(requirements):
cmd = ['pip install']
cmd += ['-E %(virtualenv_root)s' % env]
cmd += ['--requirement %s' % os.path.join(requirements, 'apps.txt')]
run(' '.join(cmd))
def touch():
""" touch wsgi file to trigger reload """
require('code_root', provided_by=('pro'))
apache_dir = os.path.join(env.code_root, 'apache')
with cd(apache_dir):
run('touch %s.wsgi' % env.environment)
def update_apache_conf():
""" upload apache configuration to remote host """
require('root', provided_by=('pro'))
source = os.path.join(os.path.join(env.code_root, 'apache'), '%(environment)s.conf' % env)
dest = os.path.join(env.apache_conf_root, '%(environment)s.%(domain)s' % env)
sudo('cp %s %s' % ( source, dest ))
apache_reload()
def configtest():
""" test Apache configuration """
require('root', provided_by=('pro'))
run('apache2ctl configtest')
def apache_reload():
""" reload Apache on remote host """
require('root', provided_by=('pro'))
sudo('/etc/init.d/apache2 reload')
def apache_restart():
""" restart Apache on remote host """
require('root', provided_by=('pro'))
run('sudo /etc/init.d/apache2 restart')
def symlink_django():
""" create symbolic link so Apache can serve django admin media """
require('root', provided_by=('pro'))
admin_media = os.path.join(env.virtualenv_root, 'src/django/django/contrib/admin/media/')
media = os.path.join(env.code_root, 'media/admin')
if not files.exists(media):
run('ln -s %s %s' % (admin_media, media))
def reset_local_media():
""" Reset local media from remote host """
require('root', provided_by=('pro'))
media = os.path.join(env.code_root, 'media', 'upload')
local('rsync -rvaz %s@%s:%s media/' % (env.user, env.hosts[0], media))