This repository has been archived by the owner on Aug 11, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
fabfile.py
124 lines (103 loc) · 3.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
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
from fabric.api import env, local, run, require, cd
from fabric.operations import _prefix_commands, _prefix_env_vars
# Host
env.disable_known_hosts = True # always fails for me without this
env.user = 'root'
env.hosts = ['django-de.org']
env.proj_repo = 'git@github.com:bartTC/django_de.git'
# Paths
env.root = '/opt/webapps/django-de.org'
env.proj_root = env.root + '/src/django-de.org'
env.pid_file = env.root + '/var/gunicorn.pid'
env.proj_bin = env.proj_root + '/django_de/bin'
env.pip_file = env.proj_root + '/requirements.pip'
env.local_settings = env.proj_root + '/django_de/conf/local/settings.py'
# ============================================================================
# Git
# ============================================================================
def push(remote=None, branch=None, reload=True):
"""Pushes the local git repo to the given remote and branch. Then pulls it
o n the server."""
remote = remote or 'origin'
branch = branch or 'master'
local('git push %s %s' % (remote, branch))
with cd(env.proj_root):
ve_run('git pull %s %s' % (remote, branch))
rmpyc()
if reload:
restart()
def switch(branch):
"""Switch the repo branch which the server is using"""
with cd(env.proj_root):
ve_run('git checkout %s' % branch)
rmpyc()
restart()
def version():
"""Show last commit to repo on server"""
with cd(env.proj_root):
sshagent_run('git log -1')
# ============================================================================
# Server
# ============================================================================
def restart():
"""Kill the gunicorn process, Cherokee will start it upon request"""
ve_run('kill `cat %s`' % env.pid_file)
def flush():
"""Flush memcache"""
sshagent_run('/etc/init.d/memcached restart')
def rmpyc():
ve_run("find . -name '*.pyc' -exec rm {} \;")
# ============================================================================
# Django
# ============================================================================
def update_reqs():
"""Update pip requirements"""
ve_run('yes w | pip install -r %s' % env.pip_file)
def collect():
manage('collectstatic --noinput')
def update(extreme=False):
push(reload=False)
if extreme:
update_reqs()
collect()
if extreme:
flush()
restart()
def debugon():
"""Turn debug mode on for the production server."""
run("sed -i -e 's/^DEBUG = .*/DEBUG = True/' %s" % env.local_settings)
restart()
def debugoff():
"""Turn debug mode on for the production server."""
run("sed -i -e 's/^DEBUG = .*/DEBUG = False/' %s" % env.local_settings)
restart()
# ============================================================================
# SSH funcs
# ============================================================================
def manage(cmd):
return ve_run('python %s/manage.py %s' % (env.proj_bin, cmd))
def ve_run(cmd):
"""
Helper function.
Runs a command using the virtualenv environment
"""
require('root')
return sshagent_run('source %s/bin/activate; %s' % (env.root, cmd))
def sshagent_run(cmd):
"""
Helper function.
Runs a command with SSH agent forwarding enabled.
Note:: Fabric (and paramiko) can't forward your SSH agent.
This helper uses your system's ssh to do so.
"""
# Handle context manager modifications
wrapped_cmd = _prefix_commands(_prefix_env_vars(cmd), 'remote')
try:
host, port = env.host_string.split(':')
return local(
"ssh -p %s -A %s@%s '%s'" % (port, env.user, host, wrapped_cmd)
)
except ValueError:
return local(
"ssh -A %s@%s '%s'" % (env.user, env.host_string, wrapped_cmd)
)