-
Notifications
You must be signed in to change notification settings - Fork 0
/
fabfile.py
executable file
·143 lines (114 loc) · 4 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
from fabric.api import *
from fabric.operations import _prefix_commands, _prefix_env_vars
from fabric.contrib import project
# Host
env.disable_known_hosts = True # always fails for me without this
env.user = 'root'
env.hosts = ['146.01.167.57']
env.proj_repo = '[email protected]:lincolnloop/seedbox.git'
env.roledefs['static'] = ['[email protected]', ]
# Where the static files get collected locally. Your STATIC_ROOT setting.
env.local_static_root = '/Users/mark/Developer/seedbox_env/var/static'
# Where the static files should go remotely
env.remote_static_root = '/srv/seedbox_env/var/'
# Paths
env.root = '/srv/seedbox_env'
env.proj_root = env.root + '/src/seedbox'
env.pip_file = env.proj_root + '/requirements.txt'
# ============================================================================
# Git
# ============================================================================
def push(remote=None, branch=None, reqs='no'):
"""Pushes the local git repo to the given remote and branch. Then pulls it
on 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))
if reqs == 'yes':
update_reqs()
collect()
def switch(branch):
"""Switch the repo branch which the server is using"""
with cd(env.proj_root):
ve_run('git checkout %s' % branch)
collect()
restart_server()
def version():
"""Show last commit to repo on server"""
with cd(env.proj_root):
sshagent_run('git log -1')
def quick():
push()
restart_server()
def update(remote=None, branch=None):
push(remote, branch, reqs='yes')
migrate()
# flush()
deploy_static()
restart_server()
# ============================================================================
# Server
# ============================================================================
def stop_server():
ve_run('service seedbox stop')
def start_server():
ve_run('service seedbox start')
def restart_server():
ve_run('service seedbox restart')
# def flush():
# """Flush memcache"""
# sshagent_run('/etc/init.d/memcached restart')
def update_reqs():
"""Update pip requirements"""
ve_run('yes w | pip install -r %s' % env.pip_file)
# ============================================================================
# Django
# ============================================================================
@roles('static')
def deploy_static():
local('npm run postinstall')
local('./manage.py collectstatic --noinput')
project.rsync_project(
remote_dir=env.remote_static_root,
local_dir=env.local_static_root,
delete=True,
)
require('root')
sshagent_run('chown -R www-data:www-data %s/var/static/' % (env.root))
def collect():
manage('collectstatic --noinput')
def migrate():
# manage('syncdb --noinput')
manage('migrate --noinput')
# ============================================================================
# SSH funcs
# ============================================================================
def manage(cmd):
return ve_run('python %s/manage.py %s' % (env.proj_root, 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)
)