Source

fabulator / src / fabfile.py

Full commit
Benoit C 1a01758 































Benoit C 2b362d0 
Benoit C 1a01758 

Benoit C fee865f 
Benoit C 3135dbd 

Benoit C 1a01758 
Benoit C 1e1cbe1 


Benoit C 4c9f866 
Benoit C 9f6381b 
Benoit C efdf15d 
Benoit C 9f6381b 
Benoit C 1a01758 
Benoit C 2b362d0 
Benoit C 1a01758 
Benoit C 778543d 
Benoit C b3307ed 
Benoit C 778543d 
Benoit C b3307ed 
Benoit C 1e1cbe1 


Benoit C 0e75ab7 


Benoit C efdf15d 

Benoit C 0e75ab7 



Benoit C efdf15d 




Benoit C 1e1cbe1 
Benoit C 778543d 
Benoit C bb01180 
Benoit C 778543d 
Benoit C b3307ed 





Benoit C 1e1cbe1 
Benoit C b3307ed 
Benoit C 778543d 
Benoit C bb01180 
Benoit C 778543d 
Benoit C 8b39d80 
Benoit C b3307ed 

Benoit C 8b39d80 

Benoit C b3307ed 
Benoit C 1e1cbe1 
Benoit C b3307ed 

Benoit C 8b39d80 

Benoit C efdf15d 
Benoit C 8b39d80 

Benoit C 1a01758 

Benoit C efdf15d 
Benoit C 951280c 
Benoit C efdf15d 
Benoit C 9f6381b 

Benoit C 0e75ab7 
Benoit C efdf15d 



Benoit C 9f6381b 

Benoit C 1486cce 

Benoit C efdf15d 
Benoit C 9f6381b 

















# -*- coding: utf-8 -
"""
    Une documentation de ce fabfile s'impose.

    fabric est une solution de deploiment
    -> http://docs.fabfile.org/0.9.0/

    ce fichier définit des rules pour:

    -> restarter les services (nginx, gunicorn, memcached)
    -> mettre a jour le code (via mercurial)

    chaque path est configurable via le fichier conf.yaml.

    notez que:
        - c'est super dépendant du path et du repository qui sont 
        toujours organisés de la sorte chez moi:

        repos/
            src/
                <project_django>/   # le dossier du projet django
            sandbox/                # le virtualenv
            deploy/                 # le dossier ou se trouve le fabfile
            doc/                    # ce dossier est voué a etre vide

    généralement, il me suffit de faire:
    fab {pre,}prod install

    todo: 
        - ajouter south
"""
import os
import datetime
from fabric.api import *
from fabric.contrib.files import exists, sed
from fabulator.decorators import Permissions, ConfChecker
from fabulator.core import south, haystack
from fabulator.core.db import mysql

from fabulator.core.db import database_factory
from fabulator.core.vc import vc_factory
from fabulator.core.http import http_factory
from fabulator.core.system import os_factory
from fabulator.core.virtualenv import VirtualEnv
from fabulator.core.django import Django

import yaml
import time

def load_yaml(env_type, conf):
    # sure we can do more there
    env.yaml = yaml.load(file(conf).read()).get(env_type)


def build(conf):
    """instanciate the backend"""
    # we can be more magic here
    if "vc" in conf:
        env.vc = vc_factory(conf["vc"].get("type"), conf.get("vc"), env)
    if "db" in conf:
        env.db = database_factory(conf["db"].get("type"), conf.get("db"), env)
    if "httpd" in conf:
        env.httpd = http_factory(conf["httpd"].get("type"), conf.get("httpd"), env)
    if "virtualenv" in conf:
        env.virtualenv = VirtualEnv(conf.get("virtualenv"), env)
    if "distro" in conf:
        env.os = os_factory(conf["distro"].get("name"), conf.get("distro"), env)
    if "django" in conf:
        env.django = Django(conf.get("django"), env)


def preprod(conf="conf.yaml"):
    """Preprod tasks"""
    load_yaml('preprod', conf)
    env.project_name = env.yaml.get('project').get('name')
    env.type = 'preprod'
    env.hosts = env.yaml.get('hosts').get('web')
    env.user = env.yaml.get('project').get('sudo_user')
    env.path = env.yaml.get('project').get('root_dir')
    env.virtualhost_path = env.path
    build(env.yaml)

def prod(conf="conf.yaml"):
    """Prod tasks"""
    load_yaml('prod', conf)
    env.project_name = env.yaml.get('project').get('name')
    env.type = 'prod'
    env.hosts = env.yaml.get('hosts').get('web')
    env.user = env.yaml.get('project').get('sudo_user')
    env.path = env.yaml.get('project').get('root_dir')
    env.virtualhost_path = env.path
    build(env.yaml)


def update():
    """updates the code and everything else"""
    env.vc.update_repository()


def install():
    """for the first install onto the machines"""
    #env.os.install_packages()
    env.vc.clone_repository()
    env.vc.switch_branch(env.vc.conf.get("branch"))
    env.virtualenv.create_virtualenv()
    env.virtualenv.install_requirements(False)
    
    #env.db.create_database
    #env.django.configure()
    #env.django.syncdb()
    #env.django.runserver()


def reset():
    env.vc.remove_repository()
    #env.db.drop_database()


@Permissions("normal_user", env)
@ConfChecker("wsgi", env)
def wsgi_restart():
    """Restarts the wsgi process"""
    conf = env.yaml.get("wsgi")
    django = env.yaml.get("django")
    if isinstance(conf.get('port'), list):
        for port in conf.get('port'):
            pidfile = "%s.%d" % (conf.get('pidfile'), port)
            if exists(pidfile):
                run("kill -0 `cat %s`" % pidfile)
                run("kill -HUP `head -1 %s`" % pidfile)
            else:
                with cd(django.get('src_dir')):
                    run("%s/gunicorn_django --workers=%d --bind=127.0.0.1:%d --pid=%s --log-file=%s -D" % \
                            (conf.get('path'), conf.get('workers'),
                             port, pidfile, conf.get('logfile')))