fabdeploy / fabdeploy /

import re
import ast

from fabric.api import env, run, sudo, settings, hide
from fabric.utils import puts, abort
from fabric.contrib.files import append

from .containers import conf
from .task import Task

__all__ = [

class ExePython(Task):
    def escaped_code(self):
        return self.conf.code.replace('"', r'\\"')

    def exe(self):
        with settings(hide('everything')):
            output = run('python -c "%(escaped_code)s"' % self.conf)
        return ast.literal_eval(output)

    def do(self):
        r = self.execute()

exe_python = ExePython()

class CpuCount(ExePython):
    def code(self):
        return 'import multiprocessing; print(multiprocessing.cpu_count())'

    def cpu_count(self):
        return self.exe()

    def do(self):
        cpu_count = self.cpu_count()
        puts('Number of CPUs: %s' % cpu_count)

cpu_count = CpuCount()

class OSCodename(ExePython):
    def code(self):
        return 'import platform; print(platform.dist())'

    def codename(self):
        distname, version, id = self.exe()

        patterns = [
            ('squeeze', ('debian', '^6', '')),
            ('lenny', ('debian', '^5', '')),
            ('natty', ('Ubuntu', '^11.04', '')),
            ('maverick', ('Ubuntu', '^10.10', '')),
            ('lucid', ('Ubuntu', '^10.04', '')),
        for name, p in patterns:
            if (re.match(p[0], distname) and
                    re.match(p[1], version) and
                    re.match(p[2], id)):
                return name

    def do(self):
        codename = self.codename()
        if codename is None:
            abort('Your OS is unsupported')
        puts('OS codename: %s' % codename)

os_codename = OSCodename()

class AptitudeUpdate(Task):
    def force(self):
        return False

    def do(self):
        if self.conf.force or not hasattr(env, '_aptitude_updated'):
            sudo('aptitude update')
            env._aptitude_updated = True

aptitude_update = AptitudeUpdate()

class AptitudeInstall(Task):
    def options(self):
        return ''

    def do(self):
        sudo('aptitude install %(options)s -y %(packages)s' % self.conf)

aptitude_install = AptitudeInstall()

    'lenny': (' '
              'lenny-backports main contrib non-free'),
    'squeeze': (' '
                'squeeze-backports main contrib non-free'),
    'natty': (' '
              'natty-backports main universe multiverse restricted'),
    'maverick': (' '
                 'maverick-backports main universe multiverse restricted'),
    'lucid': (' '
              'lucid-backports main universe multiverse restricted'),

class SetupBackports(Task):
    def backports(self):
        if self.conf.os in BACKPORTS:
            return BACKPORTS[self.conf.os]

    def do(self):
        if not self.backports:
            puts('Backports are not available for %(os)s' % self.conf)

            'deb %(backports)s' % self.conf,

setup_backports = SetupBackports()