Ejemplo n.º 1
0
    def _update_dependencies(self):
        """Create dependencies that rely on regexp matching."""

        for task in six.itervalues(self.node):
            # tasks and groups should be used for declaring dependencies
            # between tasks and roles (which are simply group of tasks)
            available_groups = self.get_groups_subgraph().nodes()
            for group in task.get('groups', ()):
                pattern = NameMatchingPolicy.create(group)
                not_matched = []
                for available_group in available_groups:
                    if pattern.match(available_group):
                        self.add_edge(task['id'], available_group)
                    else:
                        not_matched.append(available_group)
                # Add dependency for non-existing group which will be
                # resolved in GraphSolverValidator
                if len(available_groups) == len(not_matched):
                    self.add_edge(task['id'], group)
                    logger.warning('Group "%s" is an invalid dependency',
                                   group)

                available_groups = not_matched

            for req in task.get('tasks', ()):
                self.add_edge(req, task['id'])
Ejemplo n.º 2
0
    def _update_dependencies(self):
        """Create dependencies that rely on regexp matching."""

        for task in six.itervalues(self.node):
            # tasks and groups should be used for declaring dependencies
            # between tasks and roles (which are simply group of tasks)
            available_groups = self.get_groups_subgraph().nodes()
            for group in task.get('groups', ()):
                pattern = NameMatchingPolicy.create(group)
                not_matched = []
                for available_group in available_groups:
                    if pattern.match(available_group):
                        self.add_edge(task['id'], available_group)
                    else:
                        not_matched.append(available_group)
                # Add dependency for non-existing group which will be
                # resolved in DeploymentGraphValidator
                if len(available_groups) == len(not_matched):
                    self.add_edge(task['id'], group)
                    logger.warning(
                        'Group "%s" is an invalid dependency', group)

                available_groups = not_matched

            for req in task.get('tasks', ()):
                self.add_edge(req, task['id'])
Ejemplo n.º 3
0
    def get_all_roles(self, pattern=None):
        if pattern is None or pattern == consts.TASK_ROLES.all:
            return set(self.__mapping)

        if isinstance(pattern, six.string_types):
            pattern = [pattern]

        result = set()
        if isinstance(pattern, (list, tuple, set)):
            for p in pattern:
                p = NameMatchingPolicy.create(p)
                result.update(r for r in self.__mapping if p.match(r))
        return result
Ejemplo n.º 4
0
    def get_all_roles(self, pattern=None):
        if pattern is None or pattern == consts.TASK_ROLES.all:
            return set(self.__mapping)

        if isinstance(pattern, six.string_types):
            pattern = [pattern]

        result = set()
        if isinstance(pattern, (list, tuple, set)):
            for p in pattern:
                p = NameMatchingPolicy.create(p)
                result.update(r for r in self.__mapping if p.match(r))
        return result
Ejemplo n.º 5
0
    def resolve(self, roles, policy=None):
        result = set()
        if roles == consts.TASK_ROLES.all:
            # optimization
            result = {
                uid for nodes in six.itervalues(self.__mapping)
                for uid in nodes
            }
        else:
            if isinstance(roles, six.string_types):
                roles = [roles]

            if not isinstance(roles, (list, tuple, set)):
                # TODO(bgaifullin) fix wrong format for roles in tasks.yaml
                # After it will be allowed to raise exception here
                logger.warn(
                    'Wrong roles format, `roles` should be a list or "*": %s',
                    roles
                )
                return result

            for role in roles:
                if role in self.SPECIAL_ROLES:
                    result.update(self.SPECIAL_ROLES[role])
                else:
                    pattern = NameMatchingPolicy.create(role)
                    for node_role, nodes_ids in six.iteritems(self.__mapping):
                        if pattern.match(node_role):
                            result.update(nodes_ids)

        # in some cases need only one any node from pool
        # for example if need only one any controller.
        # to distribute load select first node from pool
        if result and policy == consts.NODE_RESOLVE_POLICY.any:
            result = {next(iter(result))}

        logger.debug(
            "Role '%s' and policy '%s' was resolved to: %s",
            roles, policy, result
        )
        return result
Ejemplo n.º 6
0
    def resolve(self, roles, policy=None):
        result = set()
        if roles == consts.TASK_ROLES.all:
            # optimization
            result = {
                uid
                for nodes in six.itervalues(self.__mapping) for uid in nodes
            }
        else:
            if isinstance(roles, six.string_types):
                roles = [roles]

            if not isinstance(roles, (list, tuple, set)):
                # TODO(bgaifullin) fix wrong format for roles in tasks.yaml
                # After it will be allowed to raise exception here
                logger.warn(
                    'Wrong roles format, `roles` should be a list or "*": %s',
                    roles)
                return result

            for role in roles:
                if role in self.SPECIAL_ROLES:
                    result.update(self.SPECIAL_ROLES[role])
                else:
                    pattern = NameMatchingPolicy.create(role)
                    for node_role, nodes_ids in six.iteritems(self.__mapping):
                        if pattern.match(node_role):
                            result.update(nodes_ids)

        # in some cases need only one any node from pool
        # for example if need only one any controller.
        # to distribute load select first node from pool
        if result and policy == consts.NODE_RESOLVE_POLICY.any:
            result = {next(iter(result))}

        logger.debug("Role '%s' and policy '%s' was resolved to: %s", roles,
                     policy, result)
        return result
Ejemplo n.º 7
0
 def test_exact_match(self):
     match_policy = NameMatchingPolicy.create("controller")
     self.assertIsInstance(match_policy, ExactMatchingPolicy)
     self.assertTrue(match_policy.match("controller"))
     self.assertFalse(match_policy.match("controller1"))
Ejemplo n.º 8
0
 def test_pattern_match(self):
     match_policy = NameMatchingPolicy.create("/controller/")
     self.assertIsInstance(match_policy, PatternMatchingPolicy)
     self.assertTrue(match_policy.match("controller"))
     self.assertTrue(match_policy.match("controller1"))
Ejemplo n.º 9
0
 def test_pattern_match(self):
     match_policy = NameMatchingPolicy.create("/controller/")
     self.assertIsInstance(match_policy, PatternMatchingPolicy)
     self.assertTrue(match_policy.match("controller"))
     self.assertTrue(match_policy.match("controller1"))
Ejemplo n.º 10
0
 def test_exact_match(self):
     match_policy = NameMatchingPolicy.create("controller")
     self.assertIsInstance(match_policy, ExactMatchingPolicy)
     self.assertTrue(match_policy.match("controller"))
     self.assertFalse(match_policy.match("controller1"))