File: pipelining.py

package info (click to toggle)
pyinfra 0.2.2%2Bgit20161227.ec708ef-1
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 11,804 kB
  • ctags: 677
  • sloc: python: 5,944; sh: 71; makefile: 11
file content (61 lines) | stat: -rw-r--r-- 2,035 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# pyinfra
# File: pyinfra/api/pipelining.py
# Desc: pipelining related functions and helpers

from __future__ import unicode_literals

from inspect import getargspec

import six

from pyinfra import logger


class PipelineFacts(object):
    def __init__(self, state):
        self.state = state
        self.state.ops_to_pipeline = []
        self.state.facts_to_pipeline = {}

    def __enter__(self):
        self.state.pipelining = True

    def __exit__(self, type_, value, traceback):
        self.state.pipelining = False

        # Get pipelined facts!
        # for name, args in six.iteritems(self.state.facts_to_pipeline):
        #     get_facts(self.state, name, pipeline_args=args)

        # Actually build our ops
        for (host_name, func, args, kwargs) in self.state.ops_to_pipeline:
            logger.debug(
                'Replaying op: {0}, args={1}, kwargs={2}'.format(func, args, kwargs)
            )

            func(self.state, self.state.inventory[host_name], *args, **kwargs)

    def process(self, func, decorated_func, args, kwargs):
        pipeline_facts = getattr(decorated_func, 'pipeline_facts', None)

        if pipeline_facts:
            func_args = list(getargspec(func).args)
            func_args = func_args[2:]

            for fact_name, arg_name in six.iteritems(pipeline_facts):
                index = func_args.index(arg_name)

                if len(args) >= index:
                    fact_arg = args[index]
                else:
                    fact_arg = kwargs.get(arg_name)

                if fact_arg:
                    # Get the sudo/sudo_user state, because facts are uniquely hashed
                    # using their name, command and sudo/sudo_user.
                    sudo = kwargs.get('sudo', self.state.config.SUDO)
                    sudo_user = kwargs.get('sudo_user', self.state.config.SUDO_USER)

                    self.state.facts_to_pipeline.setdefault(
                        (fact_name, sudo, sudo_user), set()
                    ).add(fact_arg)