Beispiel #1
0
 def wrapped(*args, **kwargs):
     with settings(warn_only=True):
         succeeded = run(command).succeeded
     if succeeded:
         print colors.green('Command "{}" succeeded. Skipping the '
                            '"{}" task'
                            .format(command, task.__name__))
     else:
         task(*args, **kwargs)
Beispiel #2
0
    def task_with_setup(*args, **kwargs):
        # If `s:server` was run before the current command - then we should copy values to
        # `env`. Otherwise, hosts were passed through command line with `fab -H host1,host2
        # command` and we skip.
        if env.get("group", None):
            for key,val in env.group[env.host].items():
                setattr(env, key, val)
                if fabric.state.output['debug']:
                    puts("[env] %s : %s" % (key, val))

        task(*args, **kwargs)
Beispiel #3
0
    def task_with_setup(*args, **kwargs):
        # If `s:server` was run before the current command - then we should copy values to
        # `env`. Otherwise, hosts were passed through command line with `fab -H host1,host2
        # command` and we skip.
        if env.get("group", None):
            for key, val in env.group[env.host].items():
                setattr(env, key, val)
                if fabric.state.output['debug']:
                    puts("[env] %s : %s" % (key, val))

        task(*args, **kwargs)
Beispiel #4
0
def loadScriptsOfKind(kind, project, subdir):

    # upload scripts

    from fabfile.scripts import psql
    from fabfile.scripts import user

    if kind == 'psql':
        isscript = is_sql_script
        varname = sql_varname
        execfun = psql
    elif kind == 'user':
        isscript = is_shell_script
        varname = shell_varname
        execfun = user

    scriptpath = os.path.sep.join((os.curdir, project, subdir))
    scripts = filter(isscript, os.listdir(scriptpath))
    result = {}
    for script in scripts:
        scriptfile = scriptpath + os.path.sep + script
        func = partial(execfun, scriptfile)
        func.__doc__ = "%s(%s)" % (execfun.__doc__, green(os.path.sep.join((scriptpath,script))))
        result[varname(script)] = task(name=varname(script))(func)

    del user
    del psql

    return result
Beispiel #5
0
 def test_passes_all_regular_args_to_run(self):
     def foo(*args): return args
     random_args = tuple(
         [random.randint(1000, 2000) for i in range(random.randint(1, 5))]
     )
     task = WrappedCallableTask(foo)
     eq_(random_args, task(*random_args))
 def test_passes_all_regular_args_to_run(self):
     def foo(*args): return args
     random_args = tuple(
         [random.randint(1000, 2000) for i in range(random.randint(1, 5))]
     )
     task = WrappedCallableTask(foo)
     eq_(random_args, task(*random_args))
Beispiel #7
0
 def __new__(cls, *args, **kwargs):
     self = super(Tasks, cls).__new__(cls)
     for attr in dir(cls):
         attr_value = getattr(cls, attr)
         if is_task_object(attr_value):
             task_decorator = fab.task(
                 default=attr_value.is_default,
                 name=attr_value.name,
                 aliases=attr_value.aliases,
                 task_class=attr_value.__class__,
             )
             bounded_task = functools.partial(attr_value.wrapped, self)
             task = task_decorator(
                 functools.wraps(attr_value)(bounded_task))
             for wrapped_attr in [
                     'parallel',
                     'serial',
                     'pool_size',
                     'hosts',
                     'roles',
             ]:
                 if hasattr(attr_value.wrapped, wrapped_attr):
                     setattr(
                         task.wrapped,
                         wrapped_attr,
                         getattr(attr_value.wrapped, wrapped_attr),
                     )
             setattr(self, attr, task)
     return self
Beispiel #8
0
    def method(self, name, task=False, doc=None):
        """
        Define a method.

        :param name: name of the method to proxy.
        :param task: apply fabric @task decorator to method.
        :param doc: doc string to use when proxying method.
        """
        self._methods.add(name)

        # Method wrapper that does redirection
        def wrapper(proxy, name):
            def _proxy(*args, **kwargs):
                method = proxy.get_env_method(name)
                return method(*args, **kwargs)
            return _proxy

        proxy = wrapper(self, name)
        proxy.__doc__ = doc
        proxy.__name__ = name

        if task:
            return api.task(proxy)
        else:
            return proxy
Beispiel #9
0
def define_stage_tasks(module, config):
    ''' Define tasks for the stages dynamically. '''
    for (stage_name, _) in config['stages'].iteritems():
        task_func = task(name=stage_name)(configure_env)
        task_func.__doc__ = 'Configures the {} server environment.'.format(
            stage_name)
        setattr(module, stage_name, task_func)
Beispiel #10
0
 def decorator(*args, **kwargs):
     evaluated_args = [evaluate(arg) for arg in args]
     evaluated_kwargs = {
         arg: evaluate(value)
         for arg, value in kwargs.iteritems()
     }
     return task(*evaluated_args, **evaluated_kwargs)
Beispiel #11
0
def rtask(requires=None, *args, **kwargs):
    def wrapper(t_fn, *t_args, **t_kwargs):
        if requires:
            table = globals()
            for req in requires:
                req() if callable(req) else table[req]()
        return t_fn(*t_args, **t_kwargs)
    return task(*args, **kwargs)(wrapper)
Beispiel #12
0
 def test_passes_all_keyword_args_to_run(self):
     def foo(**kwargs): return kwargs
     random_kwargs = {}
     for i in range(random.randint(1, 5)):
         random_key = ("foo", "bar", "baz", "foobar", "barfoo")[i]
         random_kwargs[random_key] = random.randint(1000, 2000)
     task = WrappedCallableTask(foo)
     eq_(random_kwargs, task(**random_kwargs))
Beispiel #13
0
    def test_dispatches_to_wrapped_callable_on_run(self):
        random_value = "some random value %d" % random.randint(1000, 2000)

        def foo():
            return random_value

        task = WrappedCallableTask(foo)
        eq_(random_value, task())
Beispiel #14
0
    def test_calling_the_object_is_the_same_as_run(self):
        random_return = random.randint(1000, 2000)

        def foo():
            return random_return

        task = WrappedCallableTask(foo)
        eq_(task(), task.run())
Beispiel #15
0
    def test_dispatches_to_wrapped_callable_on_run(self):
        random_value = "some random value %d" % random.randint(1000, 2000)

        def foo():
            return random_value

        task = WrappedCallableTask(foo)
        eq_(random_value, task())
Beispiel #16
0
 def test_passes_all_keyword_args_to_run(self):
     def foo(**kwargs): return kwargs
     random_kwargs = {}
     for i in range(random.randint(1, 5)):
         random_key = ("foo", "bar", "baz", "foobar", "barfoo")[i]
         random_kwargs[random_key] = random.randint(1000, 2000)
     task = WrappedCallableTask(foo)
     eq_(random_kwargs, task(**random_kwargs))
Beispiel #17
0
    def test_calling_the_object_is_the_same_as_run(self):
        random_return = random.randint(1000, 2000)

        def foo():
            return random_return

        task = WrappedCallableTask(foo)
        eq_(task(), task.run())
Beispiel #18
0
def make_tasks_for_envs(available_envs):
    tasks = {}
    for env_name in available_envs:
        environment = get_environment(env_name)
        if not environment.meta_config.bare_non_cchq_environment:
            tasks[env_name] = task(alias=env_name)(functools.partial(_setup_env, env_name))
            tasks[env_name].__doc__ = environment.proxy_config['SITE_HOST']
    return tasks
Beispiel #19
0
def make_tasks_for_envs(available_envs):
    tasks = {}
    for env_name in available_envs:
        tasks[env_name] = task(alias=env_name)(functools.partial(
            _setup_env, env_name))
        tasks[env_name].__doc__ = get_environment(
            env_name).proxy_config['SITE_HOST']
    return tasks
Beispiel #20
0
def test_decorator_closure_hiding():
    """
    @task should not accidentally destroy decorated attributes from @hosts/etc
    """
    from fabric.decorators import task, hosts
    def foo():
        print((env.host_string))
    foo = task(hosts("me@localhost")(foo))
    eq_(["me@localhost"], foo.hosts)
Beispiel #21
0
def set_node_envs(node_envs=NODE_ENVS):
    for node_env in node_envs:
        def _env_nodes(k):
            nodes(node_envs.get(k))

        ptask = partial(_env_nodes, node_env)
        ptask.__doc__ = "{0} nodes".format(node_env.capitalize())

        setattr(module, node_env, task(name=node_env)(ptask))
Beispiel #22
0
def test_decorator_incompatibility_on_task():
    from fabric.decorators import task, hosts, runs_once, roles
    def foo(): return "foo"
    foo = task(foo)

    # since we aren't setting foo to be the newly decorated thing, its cool
    hosts('me@localhost')(foo)
    runs_once(foo)
    roles('www')(foo)
Beispiel #23
0
def test_decorator_incompatibility_on_task():
    from fabric.decorators import task, hosts, runs_once, roles
    def foo(): return "foo"
    foo = task(foo)

    # since we aren't setting foo to be the newly decorated thing, its cool
    hosts('me@localhost')(foo)
    runs_once(foo)
    roles('www')(foo)
Beispiel #24
0
def test_decorator_closure_hiding():
    """
    @task should not accidentally destroy decorated attributes from @hosts/etc
    """
    from fabric.decorators import task, hosts
    def foo():
        print(env.host_string)
    foo = task(hosts("me@localhost")(foo))
    eq_(["me@localhost"], foo.hosts)
Beispiel #25
0
def create_env_task(env_name, env_dict, namespace):
    def env_task():
        env.stage = env_name
        construct_env(env_dict)

    env_task.__doc__ = u'''Set environment for {0}'''.format(env_name)

    wrapper = task(name=env_name)
    rand = '%d' % (time.time() * 100000)
    namespace['task_%s_%s' % (env_name, rand)] = wrapper(env_task)
Beispiel #26
0
def _define_instance_task(name, stacklevel=1):
    """Define an instance task

    This task will set env.instance to the name of the task.
    """
    def fn():
        env.instance = name
    fn.__doc__ = """Select instance '%s' for subsequent tasks.""" % name
    instance_task = task(name=name)(fn)
    sys._getframe(stacklevel).f_globals[name.replace('-', '_')] = instance_task
Beispiel #27
0
def create_tasks(name, namespace):
    service = Service(name)
    for f in service.get_methods():
        fname = f.__name__
        # task description
        f.__func__.__doc__ = 'service %s %s' % (name, fname)
        # task name
        task_name = '%s_%s' % (name, fname)
        wrapper = task(name=task_name)
        rand = '%d' % (time.time() * 100000)
        namespace['task_%s_%s' % (task_name, rand)] = wrapper(f)
Beispiel #28
0
def _run_task_core(task, args, kwargs):
    if task:
        if args:
            task(* args)
        elif kwargs:
            task(** kwargs)
        else:
            task()
Beispiel #29
0
    def getTasks(self):
        """
        Get all tasks of this L{Service} object.

        Intended to be used like::

            globals().update(Service('name').getTasks())

        at the module level of a fabfile.

        @returns: L{dict} of L{fabric.tasks.Task}
        """
        tasks = [(t, _stripPrefix(t))
                 for t in prefixedMethods(self, TASK_PREFIX)]
        return {name: task(name=name)(t) for t, name in tasks}
Beispiel #30
0
    def getTasks(self):
        """
        Get all tasks of this L{Service} object.

        Intended to be used like::

            globals().update(Service('name').getTasks())

        at the module level of a fabfile.

        @returns: L{dict} of L{fabric.tasks.Task}
        """
        tasks = [(t, _stripPrefix(t))
                 for t in prefixedMethods(self, TASK_PREFIX)]
        return {name: task(name=name)(t) for t, name in tasks}
Beispiel #31
0
def generate_server_names_as_tasks(servers, global_vars):
    """Generates tasks for all the servers specified in servers.yml
    """
    def set_server(name, server):
        def fn():
            hostname = server['host']
            env.hosts.append(hostname)
            if 'password' in server:
                env.passwords[hostname] = server['password']
            env.servers[hostname] = server
        fn.__name__ = name
        return fn

    for name, server in servers.items():
        global_vars[name] = task(set_server(name, server))
Beispiel #32
0
 def _decorator(task):
     @functools.wraps(task)
     def _task(*args, **kwargs):
         if confirm:
             confirmed = utils.yes(os.environ.get(autoconfirm_env_var, 0))
             if not confirmed and not console.confirm(
                 'Are you sure you want to select {infrastructure} '
                 'infrastructure to run task(s) on?'.format(
                     infrastructure=color(task.__name__),
                 ),
                 default=False,
             ):
                 fab.abort('Aborted')
         fab.env.infrastructure = task.__name__
         return task(*args, **kwargs)
     return fab.task(_task)
Beispiel #33
0
    def __call__(self, func):
        if not self.roles:
            error("Define a role for %s.%s" % (func.__module__, func.__name__))

        def decorated(*args, **kwargs):
            self.check_roles()
            if self.pre_task_execution:
                self.pre_task_execution(func, *args, **kwargs)
            func(*args, **kwargs)

        d = decorated
        d.__name__ = func.__name__
        d.__doc__ = func.__doc__
        d.__module__ = func.__module__

        return task(roles_decorator(self.roles)(d))
Beispiel #34
0
def server_task(name):
    """Generate a dynamic Fabric task function to set a server as the host

    if you call:
        server_task("production")
    a Fabric task named "production" will be generated and when specified
    on the command line, env.hosts will have the production server
    (as identified in the server configuration file) added
    """
    def task_func():
        env.server = read()[name]
        env.server["label"] = name
        env.hosts.append(env.server["hostname"])
    task_func.__name__ = str(name)
    task_func.__doc__ = "Use target server {}".format(name)
    return task(task_func)
Beispiel #35
0
def setup_task(wrapped_function):
    """
	Setup task function decorator
	"""

    wrapped_function_name = wrapped_function.__name__

    def _pre_setup():
        """
		Creates required structure and adds hosts to known hosts
		"""
        with settings(hide('output')):
            create_required_structure()
            add_repo_to_known_hosts()
            add_host_to_known_hosts()

    def setup_wrapper(*args):
        """
		Runs setup process on remote server
			1) check required settings (user, host, deploy_to)
			2) creates required structure (releases, shared, tmp)   
			3) adds repo and host to known hosts if possible
			4) runs wrpapped function
		"""

        check_setup_config()

        start_time = timeit.default_timer()

        echo_task('Running "%s" task' % wrapped_function_name)

        with settings(show('debug'), colorize_errors=True):
            try:
                _pre_setup()
                wrapped_function(*args)

                print_task_stats(wrapped_function_name, start_time)
            except Exception as e:
                print_task_stats(wrapped_function_name, start_time, e)

    # Copy __name__ and __doc__ from decorated function to decorator function
    setup_wrapper.__name__ = wrapped_function_name or 'setup'
    if wrapped_function.__doc__:
        setup_wrapper.__doc__ = wrapped_function.__doc__

    # Decorate with `fabric3` task decorator
    return task(setup_wrapper)
Beispiel #36
0
 def __new__(cls, **kwargs):
     self = object.__new__(cls)
     _self = weakref.proxy(self)
     for attr in dir(cls):
         attr_value = getattr(cls, attr)
         if is_task_object(attr_value):
             task_decorator = fab.task(
                 default=attr_value.is_default,
                 name=attr_value.name,
                 aliases=attr_value.aliases,
                 task_class=attr_value.__class__,
             )
             task = task_decorator(functools.wraps(attr_value.wrapped)(
                 # TODO fix Fabric's --display option
                 functools.partial(attr_value.wrapped, _self),
             ))
             setattr(self, attr, task)
     return self
def _define_instance_task(name, instance_name=None, stacklevel=1):
    """Define an instance task

    This task will set env.instance to the name of the task.
    """
    if not _valid_task_name(name):
        abort("'{name}' is not a valid task name.".format(name=name))
    if instance_name is None:
        instance_name = name
    def fn():
        env.instance = instance_name
    fn.__doc__ = """Select instance '%s' for subsequent tasks.""" % instance_name
    instance_task = task(name=name)(fn)
    fn_name = _pythonify_name(name)
    module_globals = sys._getframe(stacklevel).f_globals
    while fn_name in module_globals:
        fn_name += '_'
    module_globals[fn_name] = instance_task
Beispiel #38
0
    def getTasks(self, role=None):
        """
        Get all tasks of this L{Service} object.

        Intended to be used like::

            globals().update(Service('name').getTasks())

        at the module level of a fabfile.

        @returns: L{dict} of L{fabric.tasks.Task}
        """
        tasks = prefixedMethods(self, TASK_PREFIX)
        tasks = ((_stripPrefix(t), t) for t in tasks)
        tasks = ((name, task(name=name)(t)) for name, t in tasks)

        if role:
            tasks = ((name, roles(role)(t)) for name, t in tasks)

        return dict(tasks)
Beispiel #39
0
    def getTasks(self, role=None):
        """
        Get all tasks of this L{Service} object.

        Intended to be used like::

            globals().update(Service('name').getTasks())

        at the module level of a fabfile.

        @returns: L{dict} of L{fabric.tasks.Task}
        """
        tasks = prefixedMethods(self, TASK_PREFIX)
        tasks = ((_stripPrefix(t), t) for t in tasks)
        tasks = ((name, task(name=name)(t)) for name, t in tasks)

        if role:
            tasks = ((name, roles(role)(t)) for name, t in tasks)

        return dict(tasks)
Beispiel #40
0
def add_class_methods_as_module_level_functions_for_fabric(instance, module_name):
    '''
    Utility to take the methods of the instance of a class, instance,
    and add them as functions to a module, module_name, so that Fabric
    can find and call them. Call this at the bottom of a module after
    the class definition.
    '''
    # get the module as an object
    module_obj = sys.modules[module_name]
    
    # Iterate over the methods of the class and dynamically create a function
    # for each method that calls the method and add it to the current module
    for method in inspect.getmembers(instance, predicate=inspect.ismethod):
        method_name, method_obj = method

        if not method_name.startswith('_'):
            # get the bound method
            func = task(getattr(instance, method_name))

            # add the function to the current module
            setattr(module_obj, method_name, func)
Beispiel #41
0
def configure(module_name):
    """
    bound the dev,stage and prod tasks
    to the current module.
    """
    from types import MethodType
    steps = ['dev','test','live']

    # get the module as an object
    module_obj = sys.modules[module_name]

    # bound the tasks
    for step in steps:
        funcs = task(env_setter(step))
        funcs.name = step
        method = MethodType(funcs,module_obj,module_obj.__class__)
        setattr(module_obj,step,funcs)

    # if no env, use default
    if not set(steps).intersection(set(sys.argv)):
        env_setter(steps[0])();
Beispiel #42
0
def execute_flat(task, *args, **kwargs):
    """ Replacement function for fabric.api.execute() that allows to avoid the squared (or cubed or more)
        execution sequence of decorated tasks.
        For example, if you have these definitions:
        @task
        @hosts('host1', 'host2')
        def task1()
            ...task steps...

        @task
        @hosts('host1', 'host2')
        def task2()
            ...task steps...
            execute(task1)

        @task
        @hosts('host1', 'host2')
        def task3()
            ...task steps...
            execute_flat(task1)

        If you run 'fab task2', you will have the execution sequence (squared):
          task2 on host1
            task1 on host1
            task1 on host2
          task2 on host2
            task1 on host1
            task1 on host2
        If you run 'fab task3', you will have the execution sequence (flattened):
          task3 on host1
            task1 on host1
          task3 on host2
            task1 on host2

        Parameters and return value are identical to execute()
    """
    if env.get('host_string', None):
        return task(*args, **kwargs)
    else:
        return execute(task, *args, **kwargs)
Beispiel #43
0
def execute_flat(task, *args, **kwargs):
    """ Replacement function for fabric.api.execute() that allows to avoid the squared (or cubed or more)
        execution sequence of decorated tasks.
        For example, if you have these definitions:
        @task
        @hosts('host1', 'host2')
        def task1()
            ...task steps...

        @task
        @hosts('host1', 'host2')
        def task2()
            ...task steps...
            execute(task1)

        @task
        @hosts('host1', 'host2')
        def task3()
            ...task steps...
            execute_flat(task1)

        If you run 'fab task2', you will have the execution sequence (squared):
          task2 on host1
            task1 on host1
            task1 on host2
          task2 on host2
            task1 on host1
            task1 on host2
        If you run 'fab task3', you will have the execution sequence (flattened):
          task3 on host1
            task1 on host1
          task3 on host2
            task1 on host2

        Parameters and return value are identical to execute()
    """
    if env.get('host_string', None):
        return task(*args, **kwargs)
    else:
        return execute(task, *args, **kwargs)
Beispiel #44
0
def define_stage_tasks(module, config):
    ''' Define tasks for the stages dynamically. '''
    for (stage_name, value) in config['stages'].iteritems():
        task_func = task(name=stage_name)(configure_env)
        setattr(module, stage_name, task_func)
def run(task):
    """
    Run task to execute the actions.
    Here's where it's all starting...
    """
    utils.init_env_settings('webserver')
    
    utils.print_double_line()
    
    if task not in env.tasks:
        abort(red("Task `%s` does not exist!" % task))
        
    if 'description' in env.tasks[task]:
        print(green(env.tasks[task]['description']))
    
    if 'input_params' in env.tasks[task]:
        for param_key, param_value in env.tasks[task]['input_params'].items():
            env.params[param_value['param']] = prompt(param_value['prompt'] + ' : ')
    
    ordered_tasks = sorted(env.tasks[task]['actions'].items(), key=lambda (k,v): v['sequence'])
    
    utils.print_single_line()
    
    for key_task, current_task in ordered_tasks:
        
        if 'description' in current_task:
            print(current_task['description'])
        else:
            print("Starting `%s`" % key_task)
        
        if 'execute' in current_task:
            print(red("The `execute` key is deprecated, use `command` instead!"))
        
        if not 'command' in current_task:
            abort(red("No valid command value in config"))
        
        print("Command : %s" % current_task['command'])
        print("")
        
        if 'enabled' in current_task and (current_task['enabled'] == False or current_task['enabled'] == "False"):
            print("Skipped : %s" % current_task['command'])
            continue
        
        
        
        if 'confirm' in current_task:
            if not confirm(current_task['confirm']):
                continue
            
        script = 'command.%s' % current_task['command']
        p, m = script.rsplit('.', 1)
        
        mod = import_module(p)
        task = getattr(mod, m)
         
        if 'params' in current_task:
            params = current_task['params']
        else:
            params = {}
        
        task(params = params)
        
        utils.print_single_line()
Beispiel #46
0
def load_environments(environments):
    for (key, values) in environments.items():
        globals()[key] = task(get_environment_func(key, values))
Beispiel #47
0
def execute_or_not(task, *args, **kwargs):
    """ Run Fabric's execute(), but only if there are hosts/roles to run it on.
        Else, just discard the task, and print a warning message.

        This allows to have empty roles/hosts lists for some tasks, in
        architectures where all roles are not needed.

        .. note:: if you would like to have many services on the same host
            (eg. a worker_high and a worker_low, with 2 different
            configurations), you should call execute_or_not() once at a
            time for each role, not one time with all roles grouped in a
            list parameter. See `sparks.django.fabfile.restart_services()`
            for an example. **This is a limitation of the sparks model**.

        .. versionadded: 2.x.
    """

    # execute kwargs: host, hosts, role, roles and exclude_hosts

    # LOGGER.debug(u'ROLEDEFS AT execute_or_not(): %s', env.roledefs)

    roles = kwargs.pop('sparks_roles', ['__all__'])
    non_empty = non_empty_roles(roles)

    # If execute_or_not() is called without `sparks_roles`, there is a
    # chance the user picked some roles / hosts manually with “R:” / “H:”
    # fabric pseudo-tasks. In this case, pick them on-the-fly, else we
    # won't have any host to run on, while in fact the user has selected
    # some.
    if not non_empty and roles == ['__all__'] \
        and getattr(env, 'roles_picked', False) \
            or getattr(env, 'hosts_picked', False):
        non_empty = non_empty_roles(env.roledefs.keys())

    LOGGER.debug(
        u'Running task %s on roles: %s, current_context: %s, '
        u'matching: %s',
        task.func_name if hasattr(task, 'func_name') else task, roles,
        non_empty, env.roledefs.get(roles[0], []))

    # Reset in case. The role should be found preferably in
    # env.host_string.role, but in ONE case (when running sparks
    # tasks on single hosts with -H), Fabric will set it to None
    # and will reset all other attributes (thus we can't use
    # env.host_string.sparks_role for example) and we still
    # need our tasks to figure out the machine's role.
    env.sparks_current_role = None

    if env.host_string:
        if non_empty:

            should_run = False

            for role in non_empty:
                if env.host_string in env.roledefs[role]:
                    should_run = True

                    if not hasattr(env.host_string, 'role') \
                            or env.host_string.role is None:
                        # Supposing we are running via -H, populate the role
                        # manually in a dedicated attribute. Fabric's execute
                        # will reset env.host_string, and it's legitimate if
                        # only -H is given on CLI.
                        env.sparks_current_role = role

                    # No need to look further.
                    break

            if should_run:
                # If the user manually specified a host string / list,
                # we must not add superfluous roles / machines.

                LOGGER.debug(
                    u'Multi-run mode: execute(%s, *%s, **%s) '
                    u'with %s, %s and %s.', task, args, kwargs,
                    env.host_string, env.hosts, env.roles)

                # NOTE: don't use Fabric's execute(), it duplicates tasks.
                return task(*args, **kwargs)

            else:
                LOGGER.debug(
                    'Not executing %s(%s, %s): host %s not '
                    'in current role(s) “%s”.',
                    getattr(task, 'name', str(task)), args, kwargs,
                    env.host_string, ', '.join(roles))
        else:
            LOGGER.debug(
                'Not executing %s(%s, %s): no role(s) “%s” in '
                'current context.', getattr(task, 'name', str(task)), args,
                kwargs, ', '.join(roles))

    else:
        if non_empty:
            kwargs['roles'] = non_empty

            LOGGER.debug(u'One-shot mode: execute(%s, *%s, **%s)', task, args,
                         kwargs)

            return execute(task, *args, **kwargs)

        else:
            LOGGER.debug(
                'Not executing %s(%s, %s): no role(s) “%s” in '
                'current context.', getattr(task, 'name', str(task)), args,
                kwargs, ', '.join(roles))
Beispiel #48
0
from fabric.api import task, run

from fab_deploy2.operating_systems.redhat.utils import *
from fab_deploy2.amazon import utils as amazon_utils

get_ip = task(amazon_utils.get_ip)
Beispiel #49
0
def init(fabfile, sentinel=None, min_version=None, systemd=None):
    if sentinel is not None:
        abort(red(
            'Pass min_version and systemd as keyword arguments to'
            ' fh_fablib.init() please'
        ))

    if min_version is not None:
        if VERSION < min_version:
            abort(red(
                'fh-fablib update required. Have: %s. Want: %s.' % (
                    '.'.join(map(str, VERSION)),
                    '.'.join(map(str, min_version)),
                ),
            ))

    if systemd is None:
        abort(red(
            'fh_fablib.init() requires either systemd=True or systemd=False,'
            ' depending on whether you want to use systemd for process'
            ' supervision or not.'
        ))

    fabfile['__all__'] = (
        'check',
        'deploy',
        'dev',
        'git',
        'local',
        'server',
    )

    if pwd.getpwuid(getuid())[0] == 'www-data':
        abort(red('Stop fab-ing on the server.', bold=True))

    # Set defaults -----------------------------------------------------------

    if systemd:
        for key, value in DEFAULTS_SYSTEMD.items():
            env.setdefault(key, value)

    for key, value in DEFAULTS.items():
        env.setdefault(key, value)

    # Multi-env support ------------------------------------------------------

    def _create_setup_task_for_env(environment):
        def _setup():
            env['box_environment'] = environment
            for key, value in env.box_environments[environment].items():
                env['box_%s' % key] = value
            env.hosts = env.box_servers
        _setup.__name__ = str(environment)
        _setup.__doc__ = 'Set environment to %s' % environment
        return _setup

    if env.get('box_hardwired_environment'):
        _create_setup_task_for_env(env.box_hardwired_environment)()

    else:
        # Create a task per environment
        for environment in env.box_environments:
            t = _create_setup_task_for_env(environment)
            shortcut = env.box_environments[environment].get('shortcut')
            aliases = (shortcut,) if shortcut else ()
            fabfile[environment] = task(aliases=aliases)(t)
            fabfile['__all__'] += (environment,)

    # Fabric commands with environment interpolation -------------------------

    def interpolate_with_env(fn):
        """Wrapper which extends a few Fabric API commands to fill in values from
        Fabric's environment dictionary"""
        @wraps(fn)
        def _dec(string, *args, **kwargs):
            return fn(string % env, *args, **kwargs)
        return _dec

    g = globals()
    g['cd'] = interpolate_with_env(cd)
    g['run'] = interpolate_with_env(run)
    g['run_local'] = interpolate_with_env(run_local)
    g['confirm'] = interpolate_with_env(confirm)

    # Git pre-commit hook which always runs "fab check" ----------------------

    def ensure_pre_commit_hook_installed():
        """
        Ensures that ``git commit`` fails if ``fab check`` returns any errors.
        """
        p = Popen('git rev-parse --git-dir'.split(), stdout=PIPE)
        git_dir = p.stdout.read().strip()
        project_dir = dirname(git_dir)

        if not any(exists(join(project_dir, name)) for name in (
                'fabfile.py', 'fabfile')):
            # Does not look like a Django project.
            # Additionally, "fab check" wouldn't work anyway.
            return

        pre_commit_hook_path = join(git_dir, 'hooks', 'pre-commit')
        if not exists(pre_commit_hook_path):
            with open(pre_commit_hook_path, 'w') as hook:
                hook.write('#!/bin/sh\nfab check\n')
            chmod(pre_commit_hook_path, 0o755)

    # Run this each time the fabfile is loaded
    ensure_pre_commit_hook_installed()

    if not exists('tmp'):
        mkdir('tmp')

    from fh_fablib import check, deploy, dev, git, local, server
    fabfile.update({
        'check': check,
        'deploy': deploy,
        'dev': dev,
        'git': git,
        'local': local,
        'server': server,
    })
Beispiel #50
0
    _setup.__name__ = str(environment)
    _setup.__doc__ = 'Set environment to %s' % environment
    return _setup


if env.get('box_hardwired_environment'):
    _create_setup_task_for_env(env.box_hardwired_environment)()

else:
    # Create a task for all environments, and use the first character as alias
    g = globals()
    for environment in env.box_environments:
        t = _create_setup_task_for_env(environment)
        shortcut = env.box_environments[environment].get('shortcut')
        aliases = (shortcut,) if shortcut else ()
        g[environment] = task(aliases=aliases)(t)
        __all__ += (environment,)


def require_env(fn):
    @wraps(fn)
    def _dec(*args, **kwargs):
        # box_remote is as good as any value being set from the
        # environment dictionary
        if not env.get('box_remote'):
            abort(red(
                'Environment (one of %s) missing. "fab <env> <command>"'
                % ', '.join(env.box_environments.keys()), bold=True))
        return fn(*args, **kwargs)
    return _dec
Beispiel #51
0
 def test_simple(self, fn):
     t = monthly(task(fn))
     data = visitor.visit_task(t, ())
     assert data['cron'] == '0 0 0 * *'
Beispiel #52
0
    print_msg_box("Defaults: ")
    pp.pprint(config[name])
    print_msg_box("User overrides: ", )
    pp.pprint(user_config[name])

    for plugin_name in plugins_config:
        try:
            print_msg_box("%s plugin overrides: " % (plugin_name))
            pp.pprint(plugins_config[plugin_name][name])
        except KeyError:
            pass


# Metaprogram the machine wrappers
for machine_name in set(config.keys()) - set(['default']):
    globals()[machine_name] = task(alias=machine_name)(partial(
        machine, machine_name))


def add_plugin_environment_variable(plugin_name, plugin_path, machine_name):
    # machines_<plugin>.yml
    # machines_<plugin>_user.yml
    plugin_machines_user = os.path.join(plugin_path,
                                        "machines_%s_user.yml" % (plugin_name))

    if not os.path.exists(plugin_machines_user):
        return

    plugin_config = yaml.load(open(plugin_machines_user),
                              Loader=yaml.SafeLoader)

    # only update environment variable based on plugin_machines_user yaml file
Beispiel #53
0
from fabric.contrib import django
from fabric.api import env, task

from prefab.environ import fabenv

__all__ = [
    'settings',
    'environ',
    'shell',
]

# turn fabric helper method into an actual task.
settings = task(name='settings')(django.settings_module)


@task
def environ(environ):
    """
    Setup the environment that is being worked on.  [prod, stag, test, default]
    """
    env.environ = environ
    fabenv(environ)


@task
def shell():
    """
    Runs a Python interactive interpreter.

    This code is modified from django's ``shell`` manangement command.
Beispiel #54
0
import itertools
import os
import pkg_resources
import datetime

from utils import ingest_yaml, expand_tree
from clean import cleaner
import generate
import process
import docs_meta

conf = docs_meta.get_conf()
paths = conf.build.paths

from intersphinx import intersphinx, intersphinx_jobs
intersphinx = task(intersphinx)

env.EDITION = None
@task
def edition(val=None):
    if 'editions' in conf.project and val in conf.project.editions:
        env.EDITION = val
        conf.project.edition = val

    if conf.project.name == 'mms':
        conf.build.paths.public_site_output = conf.build.paths.mms[val]

        if val == 'saas':
            conf.build.paths.branch_output = os.path.join(conf.build.paths.output, val)
        elif val == 'hosted':
            conf.build.paths.branch_output = os.path.join(conf.build.paths.output, val,
Beispiel #55
0
    have_ovirt_sdk = False

if have_ovirt_sdk:
    from query import query
    import vm  # noqa
    import template  # noqa
    import vmpool  # noqa

    from imp import new_module
    from functools import partial
    from fabric_ovirt.lib.ovirt import oVirtObjectType

    for ootypename, ootype in oVirtObjectType.all_types.iteritems():
        mod = globals().setdefault(
            ootypename, new_module('.'.join((__name__, ootypename))))
        mod.query = task(partial(query, ootypename))
        mod.query.__doc__ = """
        Query oVirt for {ootypename} objects

        :param str oquery: The oVirt engine query to run. Engine wildards can
                           be used. Make sure the escape equel signs (=) with
                           backslash (\) when passing query from the command
                           line.
        :param str show: A colon (:) separated list of fields to show, the same
                         field could be shown multiple times and the ordering
                         is significant.
                         Supported fields are: {ootype_fields}
                         Default value is: {ootype_default_fields}
        :param str headers: 'yes' to show column headers (The default),
                            anything else to hide them.
        """.format(
Beispiel #56
0
def generate_spectask(taskname):
    def spectask(files="", options="", translated=True):
        runner = Rubyspecs(files, options, translated=(translated != "False"))
        getattr(runner, taskname)()
    spectask.__name__ = taskname
    return task(spectask)
Beispiel #57
0
            local(self.restart_cmd)
        elif oper == "stop":
            local(self.stop_cmd)
        else:
            print "wrong command"

class SupervisorServer(Server):

    def __init__(self, name):
        self.name = name
        self.start_cmd = "supervisorctl start %s" % name
        self.stop_cmd = "supervisorctl stop %s" % name
        self.restart_cmd = "supervisorctl restart %s" % name


server = task(SupervisorServer('ztcjl'))
nginx = task(Server('nginx', {'start': 'nginx', 'stop': 'nginx -s stop', 'restart':'nginx -s reload'}))

@task
def grunt(debug = 0):
    if int(debug):
        local("grunt develop")
    else:
        local("grunt")

@task
def update(branch = "master", debug = 0):
    local("git pull origin %s" % branch)
    # grunt(debug)
    server("restart")
Beispiel #58
0
def init(fabfile, sentinel=None, min_version=None, systemd=None):
    from fabric import api
    from fabric.contrib.console import confirm

    if sentinel is not None:
        abort("Pass min_version and systemd as keyword arguments to"
              " fh_fablib.init() please")

    if min_version is not None:
        if VERSION < min_version:
            abort(
                "fh-fablib update required. Have: %s. Want: %s." %
                (".".join(map(str, VERSION)), ".".join(map(str, min_version))))

    if systemd is None:
        abort("fh_fablib.init() requires either systemd=True or systemd=False,"
              " depending on whether you want to use systemd for process"
              " supervision or not.")

    fabfile["__all__"] = ("check", "deploy", "dev", "git", "local", "server")

    if pwd.getpwuid(getuid())[0] == "www-data":
        abort("Stop fab-ing on the server.")

    # Set defaults -----------------------------------------------------------

    if systemd:
        for key, value in DEFAULTS_SYSTEMD.items():
            api.env.setdefault(key, value)

    for key, value in DEFAULTS.items():
        api.env.setdefault(key, value)

    # Multi-env support ------------------------------------------------------

    def _create_setup_task_for_env(environment):
        def _setup():
            api.env["box_environment"] = environment
            for key, value in api.env.box_environments[environment].items():
                api.env["box_%s" % key] = value
            api.env.hosts = api.env.box_servers

        _setup.__name__ = str(environment)
        _setup.__doc__ = "Set environment to %s" % environment
        return _setup

    if api.env.get("box_hardwired_environment"):
        _create_setup_task_for_env(api.env.box_hardwired_environment)()

    else:
        # Create a task per environment
        for environment in api.env.box_environments:
            t = _create_setup_task_for_env(environment)
            shortcut = api.env.box_environments[environment].get("shortcut")
            aliases = (shortcut, ) if shortcut else ()
            fabfile[environment] = api.task(aliases=aliases)(t)
            fabfile["__all__"] += (environment, )

    # Fabric commands with environment interpolation -------------------------

    def interpolate_with_env(fn):
        """Wrapper which extends a few Fabric API commands to fill in values from
        Fabric's environment dictionary"""
        @wraps(fn)
        def _dec(string, *args, **kwargs):
            return fn(string % api.env, *args, **kwargs)

        return _dec

    g = globals()
    g["cd"] = interpolate_with_env(api.cd)
    g["run"] = interpolate_with_env(api.run)
    g["run_local"] = interpolate_with_env(api.local)
    g["confirm"] = interpolate_with_env(confirm)

    # Git pre-commit hook which always runs "fab check" ----------------------

    def ensure_pre_commit_hook_installed():
        """
        Ensures that ``git commit`` fails if ``fab check`` returns any errors.
        """
        p = Popen("git rev-parse --git-dir".split(), stdout=PIPE)
        git_dir = p.stdout.read().strip()
        project_dir = dirname(git_dir)

        if not any(
                exists(join(project_dir, name))
                for name in ("fabfile.py", "fabfile")):
            # Does not look like a Django project.
            # Additionally, "fab check" wouldn't work anyway.
            return

        pre_commit_hook_path = join(git_dir, "hooks", "pre-commit")
        if not exists(pre_commit_hook_path):
            with open(pre_commit_hook_path, "w") as hook:
                hook.write("#!/bin/sh\nfab check\n")
            chmod(pre_commit_hook_path, 0o755)

    # Run this each time the fabfile is loaded
    ensure_pre_commit_hook_installed()

    if not exists("tmp"):
        mkdir("tmp")

    from fh_fablib import check, deploy, dev, git, local, server

    fabfile.update({
        "check": check,
        "deploy": deploy,
        "dev": dev,
        "git": git,
        "local": local,
        "server": server,
    })
Beispiel #59
0
    def deploy_task_wrapper(wrapped_function):
        """
		Deploy task decorator
		"""

        wrapped_function_name = wrapped_function.__name__

        def _pre_deploy():
            try:
                check_lock()
                lock()
                create_build_path()
                discover_latest_release()

                set_state('pre_deploy', True)
            except Exception as error:
                set_state('pre_deploy', error)

                raise error  # escalate exception to 'deploy_wrapper' function

        def _deploy(*args):
            if state.get('pre_deploy') == True:
                try:
                    with cd(fetch('build_to')):
                        wrapped_function(*args)

                    set_state('deploy', True)
                except Exception as error:
                    set_state('deploy', error)

                    raise error  # escalate exception to 'deploy_wrapper' function

        def _post_deploy():
            if state.get('deploy') == True:
                try:
                    move_build_to_releases()
                    link_release_to_current()

                    set_state('post_deploy', True)
                except Exception as error:
                    set_state('post_deploy', error)

                    raise error  # escalate exception to 'deploy_wrapper' function

        def _finalize_deploy():
            if not state.get('success') == True:
                echo_task('Cleaning up failed deploy')

            try:
                cleanup_releases()
                remove_build_path()
                force_unlock()

                set_state('finalize', True)
            except Exception as error:
                set_state('finalize', error)

        def _on_success_deploy():
            if state.get('success') == True and callable(on_success):
                try:
                    on_success()

                    set_state('on_success', True)
                except Exception as error:
                    set_state('on_success', error)

        def deploy_wrapper(*args):
            """
			Runs deploy process on remote server
				1) Pre deploy (lock, build path, latest_release)
				2) Deploy
				3) Post deploy (move to releases, link release to current)
				4) Finalize deploy (cleanup, remove build path, unlock)
				5) Runs `on_success` callback function if deploy successfully finished
				6) Shows deploy stats
			"""

            start_time = timeit.default_timer()

            check_deploy_config()

            echo_task('Running "%s" task' % wrapped_function_name)

            with settings(colorize_errors=True):
                try:
                    _pre_deploy()
                    _deploy(*args)
                    _post_deploy()

                    set_state('success', True)
                except Exception as error:
                    set_state('success', error)

                    echo_comment(('\n[ERROR]\n%s\n' % error), error=True)

                _finalize_deploy()
                _on_success_deploy()

                print_deploy_stats(wrapped_function_name,
                                   start_time=start_time)

        # Copy __name__ and __doc__ from decorated function to decorator function
        deploy_wrapper.__name__ = wrapped_function_name or 'deploy'
        if wrapped_function.__doc__:
            deploy_wrapper.__doc__ = wrapped_function.__doc__

        # Decorate with `fabric3` task decorator
        return task(deploy_wrapper)
Beispiel #60
0
 def wrapper(func):
     role_env = os.getenv('BLDR_ROLE')
     if role_env in roles:
         # a role has been set
         return task(func)
     return func