/
fabfile.py
275 lines (235 loc) · 9.54 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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
import ConfigParser
import os
from argyle import rabbitmq, postgres, nginx, system
from argyle.base import sshagent_run, upload_template
from argyle.postgres import create_db_user, create_db
from argyle.supervisor import supervisor_command, upload_supervisor_app_conf
from argyle.system import service_command, start_service, stop_service, restart_service
from fabric.api import cd, env, get, hide, local, put, require, run, settings, sudo, task
from fabric.contrib import files, console
# Directory structure
PROJECT_ROOT = os.path.dirname(__file__)
CONF_ROOT = os.path.join(PROJECT_ROOT, 'conf')
env.project = 'copelco'
env.project_user = 'copelco'
env.repo = u'' # FIXME: Add repo URL
env.shell = '/bin/bash -c'
env.disable_known_hosts = True
env.ssh_port = 2222
# Additional settings for argyle
env.ARGYLE_TEMPLATE_DIRS = (
os.path.join(CONF_ROOT, 'templates')
)
@task
def vagrant():
env.environment = 'staging'
env.hosts = ['33.33.33.10', ]
env.branch = 'master'
env.server_name = 'dev.example.com'
setup_path()
@task
def staging():
env.environment = 'staging'
env.hosts = [] # FIXME: Add staging server hosts
env.branch = 'master'
env.server_name = '' # FIXME: Add staging server name
setup_path()
@task
def production():
env.environment = 'production'
env.hosts = [] # FIXME: Add production hosts
env.branch = 'master'
env.server_name = '' # FIXME: Add production server name
setup_path()
def setup_path():
env.home = '/home/%(project_user)s/' % env
env.root = os.path.join(env.home, 'www', env.environment)
env.code_root = os.path.join(env.root, env.project)
env.project_root = os.path.join(env.code_root, env.project)
env.virtualenv_root = os.path.join(env.root, 'env')
env.log_dir = os.path.join(env.root, 'log')
env.db = '%s_%s' % (env.project, env.environment)
env.vhost = '%s_%s' % (env.project, env.environment)
env.settings = '%(project)s.settings.%(environment)s' % env
@task
def create_users():
"""Create project user and developer users."""
ssh_dir = u"/home/%s/.ssh" % env.project_user
system.create_user(env.project_user, groups=['www-data', 'login', ])
sudo('mkdir -p %s' % ssh_dir)
user_dir = os.path.join(CONF_ROOT, "users")
for username in os.listdir(user_dir):
key_file = os.path.normpath(os.path.join(user_dir, username))
system.create_user(username, groups=['dev', 'login', ], key_file=key_file)
with open(key_file, 'rt') as f:
ssh_key = f.read()
# Add ssh key for project user
files.append('%s/authorized_keys' % ssh_dir, ssh_key, use_sudo=True)
files.append(u'/etc/sudoers', r'%dev ALL=(ALL) NOPASSWD:ALL', use_sudo=True)
sudo('chown -R %s:%s %s' % (env.project_user, env.project_user, ssh_dir))
@task
def configure_ssh():
"""
Change sshd_config defaults:
Change default port
Disable root login
Disable password login
Restrict to only login group
"""
ssh_config = u'/etc/ssh/sshd_config'
files.sed(ssh_config, u"Port 22$", u"Port %s" % env.ssh_port, use_sudo=True)
files.sed(ssh_config, u"PermitRootLogin yes", u"PermitRootLogin no", use_sudo=True)
files.append(ssh_config, u"AllowGroups login", use_sudo=True)
files.append(ssh_config, u"PasswordAuthentication no", use_sudo=True)
service_command(u'ssh', u'reload')
@task
def install_packages(*roles):
"""Install packages for the given roles."""
roles = list(roles)
if roles == ['all', ]:
roles = SERVER_ROLES
if 'base' not in roles:
roles.insert(0, 'base')
config_file = os.path.join(CONF_ROOT, u'packages.conf')
config = ConfigParser.SafeConfigParser()
config.read(config_file)
for role in roles:
if config.has_section(role):
# Get ppas
if config.has_option(role, 'ppas'):
for ppa in config.get(role, 'ppas').split(' '):
system.add_ppa(ppa, update=False)
# Get sources
if config.has_option(role, 'sources'):
for section in config.get(role, 'sources').split(' '):
source = config.get(section, 'source')
key = config.get(section, 'key')
system.add_apt_source(source=source, key=key, update=False)
sudo(u"apt-get update")
sudo(u"apt-get install -y %s" % config.get(role, 'packages'))
sudo(u"apt-get upgrade -y")
@task
def setup_server(*roles):
"""Install packages and add configurations for server given roles."""
require('environment')
# Set server locale
sudo('/usr/sbin/update-locale LANG=en_US.UTF-8')
install_packages(*roles)
if 'db' in roles:
if console.confirm(u"Do you want to reset the Postgres cluster?.", default=False):
# Ensure the cluster is using UTF-8
sudo('pg_dropcluster --stop 9.1 main', user='postgres')
sudo('pg_createcluster --start -e UTF-8 9.1 main', user='postgres')
postgres.create_db_user(username=env.project_user)
postgres.create_db(name=env.db, owner=env.project_user)
if 'app' in roles:
# Create project directories and install Python requirements
project_run('mkdir -p %(root)s' % env)
project_run('mkdir -p %(log_dir)s' % env)
with settings(user=env.project_user):
# TODO: Add known hosts prior to clone.
# i.e. ssh -o StrictHostKeyChecking=no git@github.com
sshagent_run('git clone %(repo)s %(code_root)s' % env)
project_run('git checkout %(branch)s' % env)
# Install and create virtualenv
with settings(hide('everything'), warn_only=True):
test_for_pip = run('which pip')
if not test_for_pip:
sudo("easy_install -U pip")
with settings(hide('everything'), warn_only=True):
test_for_virtualenv = run('which virtualenv')
if not test_for_virtualenv:
sudo("pip install -U virtualenv")
project_run('virtualenv -p python2.6 --clear --distribute %s' % env.virtualenv_root)
path_file = os.path.join(env.virtualenv_root, 'lib', 'python2.6', 'site-packages', 'project.pth')
files.append(path_file, env.code_root, use_sudo=True)
sudo('chown %s:%s %s' % (env.project_user, env.project_user, path_file))
sudo('npm install less -g')
update_requirements()
upload_supervisor_app_conf(app_name=u'gunicorn')
upload_supervisor_app_conf(app_name=u'group')
# Restart services to pickup changes
supervisor_command('reload')
supervisor_command('restart %(environment)s:*' % env)
if 'lb' in roles:
nginx.remove_default_site()
nginx.upload_nginx_site_conf(site_name=u'%(project)s-%(environment)s.conf' % env)
def project_run(cmd):
""" Uses sudo to allow developer to run commands as project user."""
sudo(cmd, user=env.project_user)
@task
def update_requirements():
"""Update required Python libraries."""
require('environment')
project_run(u'%(virtualenv)s/bin/pip install --use-mirrors -q -r %(requirements)s' % {
'virtualenv': env.virtualenv_root,
'requirements': os.path.join(env.code_root, 'requirements', 'production.txt')
})
@task
def manage_run(command):
"""Run a Django management command on the remote server."""
require('environment')
manage_base = u"%(virtualenv_root)s/bin/django-admin.py " % env
if '--settings' not in command:
command = u"%s --settings=%s" % (command, env.settings)
project_run(u'%s %s' % (manage_base, command))
@task
def manage_shell():
"""Drop into the remote Django shell."""
manage_run("shell")
@task
def syncdb():
"""Run syncdb and South migrations."""
manage_run('syncdb --noinput')
manage_run('migrate --noinput')
@task
def collectstatic():
"""Collect static files."""
manage_run('collectstatic --noinput')
def match_changes(branch, match):
changes = run("git diff {0} origin/{0} --stat | grep {1} | cat".format(branch, match))
return any(changes)
@task
def deploy(branch=None):
"""Deploy to a given environment."""
require('environment')
if branch is not None:
env.branch = branch
requirements = False
migrations = False
# Fetch latest changes
with cd(env.code_root):
with settings(user=env.project_user):
sshagent_run('git fetch origin')
# Look for new requirements or migrations
requirements = match_changes(env.branch, "'requirements\/'")
migrations = match_changes(env.branch, "'\/migration\/'")
if requirements or migrations:
supervisor_command('stop %(environment)s:*' % env)
run("git reset --hard origin/%(branch)s" % env)
if requirements:
update_requirements()
# New requirements might need new tables/migrations
syncdb()
elif migrations:
syncdb()
collectstatic()
supervisor_command('restart %(environment)s:*' % env)
@task
def get_db_dump(clean=True):
"""Get db dump of remote enviroment."""
require('environment')
dump_file = '%(environment)s.sql' % env
temp_file = os.path.join(env.home, dump_file)
flags = '-Ox'
if clean:
flags += 'c'
sudo('pg_dump %s %s > %s' % (flags, env.db, temp_file), user=env.project_user)
get(temp_file, dump_file)
@task
def load_db_dump(dump_file):
"""Load db dump on a remote environment."""
require('environment')
temp_file = os.path.join(env.home, '%(environment)s.sql' % env)
put(dump_file, temp_file, use_sudo=True)
sudo('psql -d %s -f %s' % (env.db, temp_file), user=env.project_user)