Skip to content

Instantly share code, notes, and snippets.

@D3f0
Created June 13, 2012 15:14
Show Gist options
  • Save D3f0/2924689 to your computer and use it in GitHub Desktop.
Save D3f0/2924689 to your computer and use it in GitHub Desktop.
Update django
# encoding: utf-8
from fabric.api import *
from fabric.contrib.files import *
import sys
import os
from os import path
from base64 import decodestring as _decodestring
from fabutils import Package
author_email = 'bmFodWVsLmRlZm9zc2VAZ21haWwuY29t\n'
try:
from fabsettings import AMAZON_EC2_HOST, PROJECT_NAME, REPOSITORY, USER, PASSWORD, VIRTUALENVS, SETTINGS_SUBDIR, KEY_FILENAME
except ImportError:
print "ImportError: Couldn't find fabsettings.py, it either does not exist or giving import problems (missing settings)"
print "Pedirlo por correo a %s con asunto mdfmaps fabsettings" % _decodestring(author_email)
sys.exit(1)
env.hosts = [AMAZON_EC2_HOST, ]
env.user = USER
env.password = PASSWORD
env.home = "/home/%s" % USER
env.project = PROJECT_NAME
env.repo = REPOSITORY
env.project_dir = '/home/{USER}/webapps/{PROJECT_NAME}/'.format(**locals())
env.settings_dir = env.project_dir + SETTINGS_SUBDIR
env.supervisor_dir = env.home + '/webapps/supervisor'
env.virtualenv_dir = VIRTUALENVS
env.supervisor_ve_dir = env.virtualenv_dir + '/supervisor'
env.key_filename = KEY_FILENAME
def _ve_run(cmd, ve = env.project):
"""virtualenv wrapper for fabric commands
"""
run("""source %s/%s/bin/activate && %s""" % (env.virtualenv_dir, ve, cmd))
def ec2_stop():
'''Detiene la instancia de supervisord en EC2'''
run('sudo supervisorctl stop mde')
def ec2_start():
'''Arranca la instancia de supervisord en EC2'''
run('sudo supervisorctl start mde')
def ec2_git_update():
'''Actualiza el proyecto en Amazon EC2'''
ec2_stop()
local('git push origin master')
with cd(env.project_dir):
run('git pull origin master')
_ve_run('pip install -r requirements/production.txt')
_ve_run('python manage.py collectstatic --noinput')
ec2_start()
# MySQL old update
#def ec2_putdata():
# dump = 'dump.json.gz'
# local('manage.py dumporder licencias auth contenttypes > dump.json')
# local('gzip dump.json')
# if not exists(os.path.join(env.project_dir, dump)):
# put(dump, env.project_dir)
# with cd(env.project_dir):
# run('gunzip %s' % dump)
# _ve_run('python manage.py loaddata dump.json')
def graph_models():
''' Graficar los modelos '''
# -g agrupar -a todo -d deshabilitar campos
local('python manage.py graph_models -a -g -o doc/modelos/todo.pdf')
local('python manage.py graph_models -a -g -d -o doc/modelos/todo_simple.pdf')
def ec2_restart():
''' Rearrancar el servidor '''
run('sudo supervisorctl restart mde')
DEV_ONLY_PKGS = map(lambda s: re.compile(s, re.IGNORECASE),(
'graph'
'yolk',
'stallion',
'docutils',
'jinja2',
'simplejson',
'selenium',
# sst
'PyVirtualDisplay',
'junitxml',
'unittest2',
'entrypoint2',
'EasyProcess',
'argparse',
)
)
#===============================================================================
# Requirements filtering
#===============================================================================
def _production_filter(s, regexes = DEV_ONLY_PKGS):
''' @return: True if package is for production'''
for regex in regexes:
if regex.search(s):
return False
return True
def freeze_reqs():
''' Generate devel / deploy reqsuirements.txt file'''
packages = local('pip freeze', capture = True)
with open('requirements/develop.txt', 'w') as f:
f.write(packages)
with open('requirements/production.txt', 'w') as f:
for req in filter(_production_filter, packages.split('\n')):
f.write('%s\n' % req)
local('wc -l requirements/*')
#local('pip freeze | grep -v graphviz > reqsuirements/production.txt')
def adminextras_local_update():
''' Actualiza localmente adminextras'''
local('pip install -U -e "git+https://github.com/D3f0/django-admin-extras.git#egg=adminextras"')
def adminextras_remote_update():
''' Fix transitorio para actualizar a HEAD lav ersion de adminextras en remote'''
_ve_run('pip install -U -e "git+https://github.com/D3f0/django-admin-extras.git#egg=adminextras"')
ec2_restart()
_get_intalled_pkgs = lambda: local('pip freeze', True).split('\n')
def freeze_diff_dev():
'''Checks difference between installed packages and dev resquirements'''
packages = set(map(Package.from_pip_freeze, _get_intalled_pkgs()))
with open('requirements/develop.txt') as f:
requirements = set(map(Package.from_pip_freeze, f.readlines()))
new = packages.difference(requirements)
for pkg in new:
print pkg
def remote_manage(cmd):
'''Run remote command'''
with cd(env.project_dir):
_ve_run("python manage.py %s" % cmd)
import re
class Package(object):
def __init__(self, name, version):
self.name = name
self.version = version
@classmethod
def from_pip_freeze(cls, pip_line):
pip_line = pip_line.strip()
if '==' in pip_line:
name, version = pip_line.split('==')
version = tuple(map(int, version.split('.')))
return cls(name = name, version = version)
elif '#egg' in pip_line:
match = re.search('\@(?P<version>[\d\w]+)\#egg\=(?P<name>[\w\_\d]+)', pip_line, re.IGNORECASE)
if match:
return cls(name = match.group('name'),
version = match.group('version'))
raise Exception("Not understood package sig: %s" % pip_line)
def __str__(self):
return "<%s %s>" % (self.name, self.version)
__repr__ = __str__
def __eq__(self, other):
return self.name == other.name and self.version == other.version
def __hash__(self):
return hash((self.name, self.version))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment