Skip to content

Instantly share code, notes, and snippets.

@kecs
Last active August 29, 2015 13:57
Show Gist options
  • Save kecs/9511584 to your computer and use it in GitHub Desktop.
Save kecs/9511584 to your computer and use it in GitHub Desktop.
not so good fabfile, we can learn from it
from fabric.api import env, local, run, require, cd, sudo
import os
from fabric.context_managers import cd
env.project_name = 'weight'
def prod():
"Use the actual webserver"
env.hosts = ['178.*.*.*']
env.user = '*'
env.path = '/opt/...'
def wrap():
wrapper = './wrap.sh'
try:
with open(wrapper):
local(wrapper)
except IOError:
pass
def _setup():
local("alias gti='git'")
def r():
"""
Function: r
Run devserver
"""
_setup()
rm_pyc()
wrap()
local('python manage.py runserver 0.0.0.0:8000')
def graph(app='api'):
local('python manage.py graph_models -o /home/kecs/dev/%s/docs/%s_%s.png %s' % (env.project_name, env.project_name, app, app))
def rm_pyc():
with cd('/home/kecs/dev/%s' % env.project_name):
local('find ./ -name \*.pyc | xargs rm')
def psh():
branch = local('git rev-parse --abbrev-ref HEAD', capture=True)
local('git push upstream %s' % branch)
def pll():
require('hosts', provided_by=[prod])
_setup()
rm_pyc()
branch = local('git rev-parse --abbrev-ref HEAD', capture=True)
with cd(env.path):
run('git checkout %s;git pull upstream %s' % (branch, branch), pty=True)
#~ def p():
#~ require('hosts', provided_by=[prod])
#~ _setup()
#~ wrap()
#~ rm_pyc()
#~ with cd('/home/kecs/dev/weight/weight'):
#~ local('scp ./weight/static/javascripts/app.js user@*.*.*.*:/opt/adamweb.com/weight/static/javascripts/app.js')
def rst():
require('hosts', provided_by=[prod])
with cd(env.path):
run('/etc/init.d/nginx stop', pty=True)
run('find ./ -type f -name \*.pyc -exec rm -f {} \;', pty=True)
run('touch /tmp/adamweb-uwsgi', pty=True)
#~ [*] NOTABENE: this re spawns 10 process, use this after cleanup:
#~ run('service uwsgi stop', pty=True)
#~ run('uwsgi --ini /opt/adamweb.com/conf/adamweb-uwsgi.ini', pty=True)
run('/etc/init.d/nginx start', pty=True)
def t(app='api'):
"""
Function: t
Run tests
"""
print '[*] Run BDD tests'
local("REUSE_DB=1")
local("NOSE_WITH_DJANGO=1")
local("python manage.py harvest -a %s" % app)
def go():
require('hosts', provided_by=[prod])
pll()
rst()
def trans(w='0'):
require('hosts', provided_by=[prod])
local('./manage.py trans -%s' % int(w) and 'w' or 'r')
def sh(): local('./manage.py shell_plus --ipython')
def mg1(app='api'):
local('./manage.py schemamigration %s --auto' % app)
def mg2(app='api'):
local('./manage.py migrate %s' % app)
def mg(): mg1(); mg2()
def mgrt(app='api'):
require('hosts', provided_by=[prod])
with cd(env.path):
run('./manage.py migrate %s' % app, pty=True)
def kill_uwsgi():
"""
uwsgi is not daemonized properly, it produces zombies upon restart,
so lets kill zombies.
"""
with cd(env.path):
for i, pid in enumerate(run('pidof uwsgi').split()):
sudo('kill -9 %s' % pid, pty=True)
print '[*] Killed %d zombies.' % i
def x():
"""for the ultra lazy"""
require('hosts', provided_by=[prod])
branch = local('git rev-parse --abbrev-ref HEAD', capture=True)
local('git commit -a -m "%s"' % raw_input('[*] Commit msg: '))
local('git checkout develop')
local('git merge %s' % branch)
local('git push upstream develop')
with cd(env.path):
run('git pull upstream develop')
rst()
local('git checkout %s' % branch)
def trans():
"""Use on any branch, it adds, commits and pushes
only .po changes, pulls to server, restarts wsgi"""
require('hosts', provided_by=[prod])
branch = local('git rev-parse --abbrev-ref HEAD', capture=True)
local('git add *.po')
local('git commit -m "%s"' %
(raw_input('[*] Commit msg (default is "TRANSLATION"): ') or 'TRANSLATION'))
if branch != 'develop':
local('git checkout develop')
local('git merge %s' % branch)
local('git push upstream develop')
with cd(env.path):
run('git pull upstream develop')
sudo('./manage.py compilemessages')
rst()
if branch != 'develop':
local('git checkout %s' % branch)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment