Exemplo n.º 1
0
Arquivo: events.py Projeto: h3/up
from up.logger import get_logger

log = get_logger('up.events')


class EventMixin(object):
    """
    This object uses the Borg pattern to ensure a shared
    state to act as a bus to allow plugins to communicate
    using an event based model.
    """
    _state = {'callbacks': {}}

    def __init__(self):
        self.__dict__ = self._state

    def on(self, eventName, callback, context=None):
        """
        Binds an event to a callback, also accept a context
        which will be updated by the trigger's context
        """
        if eventName not in self._state['callbacks']:
            self._state['callbacks'][eventName] = []
        args = {
            'eventName': eventName,
            'callback': callback,
            'context': context,
        }
        self._state['callbacks'][eventName].append(args)
        log.info(u'%s (context: %s)' % (eventName, context))
Exemplo n.º 2
0
from fabric.api import env,  get, local, open_shell, prompt, put, sudo, run
from up.conf import Settings
from up.logger import get_logger

settings = Settings()
log = get_logger('up.fabric')
env.use_ssh_config = True

cmds = {
    'get': get,
    'open_shell': open_shell,
    'prompt': prompt,
    'put': put,
    'run': run,
    'sudo': sudo,
}


class FabricMixin(object):

    def cmd(self, cmd, *args, **kwargs):
        rs = []
        ctx = settings.get_context()

        if 'servers' in ctx:
            for host in ctx.get('servers'):
                env.host_string = host
                rs.append(cmds.get(cmd)(*args, **kwargs))
                if 'local_path' in kwargs:
                    print '%s' % kwargs.get('local_path').getvalue()
                log.info("[%s] %s: %s (%s)" % (host, cmd, args, kwargs))
Exemplo n.º 3
0
Arquivo: plugin.py Projeto: h3/up
import os
import logging

from yapsy.IPlugin import IPlugin
from fabric.api import env, sudo, run

from up.logger import get_logger
from up.utils import import_class
from up.conf import Settings
from up.events import EventMixin
from up.fabricwrapper import FabricMixin
from up.templates import template

settings = Settings()
log = get_logger('up.plugin')


class UpPlugin(IPlugin, EventMixin, FabricMixin):
    """
    Base plugin class
    """

    def copy_templates(self, context):
        rs = []
        tpl_context = settings.get_context()
        if 'up-templates' in tpl_context.get(self.name):
            for tpl in tpl_context.get(self.name).get('up-templates'):
                rs.append(self.copy_template(tpl[0], tpl[1], context=tpl_context))
        return rs