forked from PyCon/pycon2019
-
Notifications
You must be signed in to change notification settings - Fork 0
/
fabfile.py
146 lines (125 loc) · 4.67 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
import json
import os
import re
from fabric.api import cd, env, get, hide, local, put, require, run, settings, sudo, task
from fabric.colors import red
from fabric.contrib import files, project
from fabric.utils import abort, error
# Directory structure
PROJECT_ROOT = os.path.dirname(__file__)
env.project = 'pycon'
env.project_user = os.environ['LOGNAME']
env.shell = '/bin/bash -c'
env.settings = 'symposion.settings'
@task
def staging():
env.environment = 'staging'
env.hosts = ['virt-nsz0jn.psf.osuosl.org']
env.site_hostname = 'staging-pycon.python.org'
env.root = '/srv/staging-pycon.python.org'
env.branch = 'staging'
env.db = 'psf-pycon-2014-staging'
env.db_host = 'pg1.osuosl.org'
env.db_user = 'psf-pycon-2014-staging'
setup_path()
@task
def production():
env.environment = 'production'
env.hosts = ['virt-ak9lsk.psf.osuosl.org']
env.site_hostname = 'us.pycon.org'
env.root = '/srv/staging-pycon.python.org'
env.branch = 'production'
env.db = 'psf_pycon_2014'
env.db_host = 'pg1.osuosl.org'
env.db_user = 'psf_pycon_2014'
setup_path()
def setup_path():
env.home = '/home/%(project_user)s/' % env
env.code_root = os.path.join(env.root, 'current')
env.virtualenv_root = os.path.join(env.root, 'shared/env')
env.media_root = os.path.join(env.root, 'shared', 'media')
@task
def manage_run(command, sudo=False):
"""Run a Django management command on the remote server."""
require('environment')
manage_base = u"%(virtualenv_root)s/bin/python %(code_root)s/manage.py " % env
with cd(env.code_root):
if sudo:
sudo(u'%s %s' % (manage_base, command))
else:
run(u'%s %s' % (manage_base, command))
@task
def manage_shell():
"""Drop into the remote Django shell."""
manage_run("shell")
@task
def deploy():
"""Deploy to a given environment."""
# NOTE: chef will check every 30 minutes or so whether the
# repo has changed, and if so, redeploy. Or you can use this
# to make it run immediately.
require('environment')
sudo('chef-client')
@task
def ssh():
"""Ssh to a given server"""
require('environment')
local("ssh %s" % env.hosts[0])
@task
def get_db_dump(dbname, clean=True):
"""Overwrite your local `dbname` database with the data from the server.
The name of your local db is required as an argument, e.g.:
fab staging get_db_dump:dbname=mydbname
"""
require('environment')
if not files.exists("%(home)s/.pgpass" % env):
abort("Please get a copy of .pgpass and put it in your home dir on the server of interest (not your local system)")
dump_file = '%(project)s-%(environment)s.sql' % env
flags = '-Ox'
if clean:
flags += 'c'
pg_dump = 'pg_dump -h %s -U %s %s %s' % (env.db_host, env.db_user,
flags, env.db)
host = '%s@%s' % (env.user, env.hosts[0])
# save pg_dump output to file in local home directory
local('ssh -C %s %s > ~/%s' % (host, pg_dump, dump_file))
local('dropdb %s; createdb %s' % (dbname, dbname))
local('psql %s -f ~/%s' % (dbname, dump_file))
@task
def get_media(root='site_media/media'):
"""Syncs media files from server to a local dir.
Defaults to ./site_media/media; you can override by passing
a different relative path as root:
fab server get_media:root=my_dir/media/foo
Local dir ought to exist already.
"""
rsync = 'rsync -rvaz %(user)s@%(host)s:%(media_root)s/' % env
cmd = '%s ./%s' % (rsync, root)
local(cmd)
@task
def load_db_dump(dump_file):
"""Given a dump on your home dir on the server, load it to the server's
database, overwriting any existing data. BE CAREFUL!"""
require('environment')
if not files.exists("%(home)s/.pgpass" % env):
abort("Please get a copy of .pgpass and put it in your home dir")
temp_file = os.path.join(env.home, '%(project)s-%(environment)s.sql' % env)
put(dump_file, temp_file)
run('psql -h %s -U %s -d %s -f %s' % (env.db_host, env.db_user, env.db, temp_file))
@task
def make_messages():
"""Extract English text from code and templates, and update the
.po files for translators to translate"""
# Make sure gettext is installed
local("gettext --help >/dev/null 2>&1")
if os.path.exists("locale/fr/LC_MESSAGES/django.po"):
local("python manage.py makemessages -a")
else:
local("python manage.py makemessages -l fr")
@task
def compile_messages():
"""Compile the translated .po files into more efficient .mo
files for runtime use"""
# Make sure gettext is installed
local("gettext --help >/dev/null 2>&1")
local("python manage.py compilemessages")