Skip to content

Instantly share code, notes, and snippets.

@averrin
Created September 21, 2012 10:45
Show Gist options
  • Select an option

  • Save averrin/3760858 to your computer and use it in GitHub Desktop.

Select an option

Save averrin/3760858 to your computer and use it in GitHub Desktop.
Some fabric magic
# -*- coding: utf-8 -*-
__author__ = 'Alexey "Averrin" Nabrodov'
__version__ = '0.8'
from fabric.api import run, env, local, cd, prompt, lcd, open_shell
from fabric.utils import puts
from fabric.colors import *
from fabric.decorators import roles, runs_once
from secret import user, password
env.passwords = {'%[email protected]' % user: password, '%[email protected]' % user: password}
env.roledefs['dev'] = ['%[email protected]' % user]
env.roledefs['note'] = ['%[email protected]' % user]
import imp
PROJECTS = {
'emercom': {
'title': "Emercom",
'local_path': '/home/averrin/projects/emercom',
'remote_path': '/home/dev/emercom',
'make_docs': True,
'pidfile': '/home/dev/django_em.pid',
'cmd_after': ["/usr/bin/python %(remote_path)s/manage.py runfcgi protocol=scgi host=127.0.0.1 port=37816 pidfile=%(pidfile)s"],
'cmd_before': ['python %(local_path)s/manage.py graph_models core -o %(local_path)s/docs/modules/core/core.png'],
'pep_exclude': []
},
'dev': {
'title': "Dev",
'local_path': '/home/averrin/projects/dev',
'remote_path': '/home/dev/dev',
'make_docs': False,
'pidfile': '/home/dev/django_em.pid',
'cmd_after': [],
'cmd_before': [],
'pep_exclude': []
},
'api': {
'title': "API",
'local_path': '/home/averrin/projects/api',
'remote_path': '/home/dev/api',
'make_docs': False,
'pidfile': '',
'cmd_after': [],
'cmd_before': [],
'pep_exclude': []
}
}
ignore_errors = ['E501', 'W', 'E128', 'E302', 'E122', 'E127']
exclude_dirs = ['static', 'registration', 'contrib', 'migrations', 'debug*']
def check_pep(project):
path = project['local_path']
with lcd(path):
puts(cyan('Check PEP: %s' % project['title']))
ex_dirs = exclude_dirs[:]
ex_dirs.extend(project['pep_exclude'])
ex_dirs = ','.join(ex_dirs)
local('pep8 --exclude=%s --ignore=%s --show-source --statistics .' % (ex_dirs, ','.join(ignore_errors)))
def commit(project, comment, ver=None):
path = project['local_path']
with lcd(path):
puts(cyan('Commiting: %s' % project['title']))
env.warn_only = True
rev = local('git log --pretty=format:"1"', capture=True)
env.warn_only = False
if ver is None:
puts('Calc new version')
VERSION = imp.load_source('version', path + '/version.py').VERSION
ver = '.'.join(VERSION.split('.')[:-1])
v = "VERSION = '%s.%s'" % (ver, len(rev.split('\n')) + 1)
puts('For %s new version is %s' % (cyan(project['title']), green(v)))
file('version.py', 'w').write(v)
for cmd in project['cmd_before']:
local(cmd % project)
local('git commit -am "%s"' % comment)
@runs_once
def push(comment=None, ver=None):
commited = 0
for p in PROJECTS:
project = PROJECTS[p]
path = project['local_path']
with lcd(path):
need_commit = False
status = local('git status -s', capture=True).split('\n')
for f in status:
if f.startswith('M'):
puts(green('%s have changes' % p))
need_commit = True
commited += 1
break
if need_commit:
if comment is None:
task = prompt('Task number: ')
comment = prompt('Commit description: ')
comment = '#%s %s' % (task, comment)
check_pep(project)
commit(project, comment, ver)
puts(cyan('Pushing: %s' % project['title']))
local('git push')
else:
puts(yellow('%s have no changes' % project['title']))
return commited
def deploy(docs=True):
for p in PROJECTS:
project = PROJECTS[p]
with cd(project['remote_path']):
pull(project['remote_path'])
if project['make_docs'] and docs:
make_docs(project['remote_path'])
if project['pidfile']:
reload_project(project)
for cmd in project['cmd_after']:
run(cmd % project)
def pull(path):
with cd(path):
puts(green('Pulling: %s' % path))
run('git pull')
@roles('dev')
def reload_project(project):
with cd(project['remote_path']):
run('while read a ; do sudo kill $a; done < %s' % project['pidfile'])
@roles('dev')
def run_server():
run('/usr/bin/python %s/manage.py runfcgi protocol=scgi host=127.0.0.1 port=37816 pidfile=/home/dev/django_em.pid' % (remote_path + 'emercom/'))
@roles('dev')
def make_docs(path, force=False):
with cd(path + '/docs'):
if force:
run('rm -rf ./_build')
run('make html')
def update(comment=None, ver=None, docs=True):
if push(comment, ver):
deploy(docs)
def shell():
open_shell()
def syncdb():
with lcd(project_path + 'emercom'):
local('./manage.py syncdb --migrate')
# local('./manage.py migrate')
local('python ./manage.py loaddata ./core/fixtures/auth_data.json')
env.warn_only = True
local('python ../dev/manage.py syncdb --database="emercom"', capture=True)
env.warn_only = False
local('python ../dev/manage.py loaddata ../dev/api/fixtures/initial_data.json --database=emercom')
def update_models(app='core'):
with lcd(project_path + 'emercom'):
local('./manage.py schemamigration %s --auto' % app)
local('./manage.py migrate %s' % app)
local('git add %s/migrations/*.py' % app)
def psql(base='emercom'):
local('sudo -u postgres psql -h s4.lcs.local -U postgres %s' % base)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment