Example #1
0
    def test_param_dupe(self):
        params1 = {
            # 'from_files': {},
            'tags': [],
            u'testvalue': False,
            u'testvalue2': True,
            # 'when': []
        }
        params2 = {
            # 'from_files': {},
            'tags': [],
            u'testvalue': True,
            u'testvalue2': False,
            # 'when': []
        }
        res1 = hash_params(params1)
        res2 = hash_params(params2)

        self.assertNotEqual(hash(res1), hash(res2))
        self.assertNotEqual(res1, res2)

        foo = {}
        foo[res1] = 'params1'
        foo[res2] = 'params2'

        self.assertEqual(len(foo), 2)

        del foo[res2]
        self.assertEqual(len(foo), 1)

        for key in foo:
            self.assertTrue(key in foo)
            self.assertIn(key, foo)
Example #2
0
    def test_param_dict_dupe_values(self):
        params1 = {'foo': False}
        params2 = {'bar': False}

        res1 = hash_params(params1)
        res2 = hash_params(params2)

        hash1 = hash(res1)
        hash2 = hash(res2)
        self.assertNotEqual(res1, res2)
        self.assertNotEqual(hash1, hash2)
Example #3
0
    def test_generator(self):
        def my_generator():
            for i in ['a', 1, None, {}]:
                yield i

        params = my_generator()
        res = hash_params(params)
        self._assert_hashable(res)
Example #4
0
    def test_generator(self):
        def my_generator():
            for i in ['a', 1, None, {}]:
                yield i

        params = my_generator()
        res = hash_params(params)
        self._assert_hashable(res)
Example #5
0
 def test_dict_tuple(self):
     params = {
         'foo': (
             1,
             'bar',
         )
     }
     res = hash_params(params)
     self._assert_set(res)
Example #6
0
 def test_dict_with_list_value(self):
     params = {'foo': [1, 4, 'bar']}
     res = hash_params(params)
     self._assert_set(res)
     self._assert_hashable(res)
Example #7
0
 def test_list(self):
     params = ['foo', 'bar', 1, 37, None]
     res = hash_params(params)
     self._assert_set(res)
     self._assert_hashable(res)
Example #8
0
 def test_tuple_dict(self):
     params = ({'foo': 'bar'}, 37)
     res = hash_params(params)
     self._assert_hashable(res)
Example #9
0
 def test_tuple(self):
     params = (1, None, 'foo')
     res = hash_params(params)
     self._assert_hashable(res)
Example #10
0
 def test(self):
     params = {'foo': 'bar'}
     res = hash_params(params)
     self._assert_set(res)
     self._assert_hashable(res)
Example #11
0
 def test(self):
     params = {'foo': 'bar'}
     res = hash_params(params)
     self._assert_set(res)
     self._assert_hashable(res)
    def _process_pending_results(self, iterator):
        '''
        Reads results off the final queue and takes appropriate action
        based on the result (executing callbacks, updating state, etc.).
        '''

        while not self._final_q.empty() and not self._tqm._terminated:
            try:
                result = self._final_q.get(block=False)
                debug("got result from result worker: %s" % (result, ))

                # all host status messages contain 2 entries: (msg, task_result)
                if result[0] in ('host_task_ok', 'host_task_failed',
                                 'host_task_skipped', 'host_unreachable'):
                    task_result = result[1]
                    host = task_result._host
                    task = task_result._task
                    if result[0] == 'host_task_failed':
                        if not task.ignore_errors:
                            debug("marking %s as failed" % host.get_name())
                            self._tqm._failed_hosts[host.get_name()] = True
                        self._callback.runner_on_failed(task, task_result)
                    elif result[0] == 'host_unreachable':
                        self._tqm._unreachable_hosts[host.get_name()] = True
                        self._callback.runner_on_unreachable(task, task_result)
                    elif result[0] == 'host_task_skipped':
                        self._callback.runner_on_skipped(task, task_result)
                    elif result[0] == 'host_task_ok':
                        self._callback.runner_on_ok(task, task_result)

                    self._pending_results -= 1
                    if host.name in self._blocked_hosts:
                        del self._blocked_hosts[host.name]

                    # If this is a role task, mark the parent role as being run (if
                    # the task was ok or failed, but not skipped or unreachable)
                    if task_result._task._role is not None and result[0] in (
                            'host_task_ok', 'host_task_failed'):
                        # lookup the role in the ROLE_CACHE to make sure we're dealing
                        # with the correct object and mark it as executed
                        for (entry,
                             role_obj) in ROLE_CACHE[task_result._task._role.
                                                     _role_name].iteritems():
                            #hashed_entry = frozenset(task_result._task._role._role_params.iteritems())
                            hashed_entry = hash_params(
                                task_result._task._role._role_params)
                            if entry == hashed_entry:
                                role_obj._had_task_run = True

                elif result[0] == 'include':
                    host = result[1]
                    task = result[2]
                    include_file = result[3]
                    include_vars = result[4]

                    if isinstance(task, Handler):
                        # FIXME: figure out how to make includes work for handlers
                        pass
                    else:
                        original_task = iterator.get_original_task(task)
                        if original_task._role:
                            include_file = self._loader.path_dwim_relative(
                                original_task._role._role_path, 'tasks',
                                include_file)
                        new_tasks = self._load_included_file(
                            original_task, include_file, include_vars)
                        iterator.add_tasks(host, new_tasks)

                elif result[0] == 'add_host':
                    task_result = result[1]
                    new_host_info = task_result.get('add_host', dict())

                    self._add_host(new_host_info)

                elif result[0] == 'add_group':
                    host = result[1]
                    task_result = result[2]
                    group_name = task_result.get('add_group')

                    self._add_group(host, group_name)

                elif result[0] == 'notify_handler':
                    host = result[1]
                    handler_name = result[2]

                    if handler_name not in self._notified_handlers:
                        self._notified_handlers[handler_name] = []

                    if host not in self._notified_handlers[handler_name]:
                        self._notified_handlers[handler_name].append(host)

                elif result[0] == 'set_host_var':
                    host = result[1]
                    var_name = result[2]
                    var_value = result[3]
                    self._variable_manager.set_host_variable(
                        host, var_name, var_value)

                elif result[0] == 'set_host_facts':
                    host = result[1]
                    facts = result[2]
                    self._variable_manager.set_host_facts(host, facts)

                else:
                    raise AnsibleError("unknown result message received: %s" %
                                       result[0])
            except Queue.Empty:
                pass
Example #13
0
    def _process_pending_results(self, iterator):
        '''
        Reads results off the final queue and takes appropriate action
        based on the result (executing callbacks, updating state, etc.).
        '''

        ret_results = []

        while not self._final_q.empty() and not self._tqm._terminated:
            try:
                result = self._final_q.get(block=False)
                debug("got result from result worker: %s" %
                      ([unicode(x) for x in result], ))

                # all host status messages contain 2 entries: (msg, task_result)
                if result[0] in ('host_task_ok', 'host_task_failed',
                                 'host_task_skipped', 'host_unreachable'):
                    task_result = result[1]
                    host = task_result._host
                    task = task_result._task
                    if result[0] == 'host_task_failed' or task_result.is_failed(
                    ):
                        if not task.ignore_errors:
                            debug("marking %s as failed" % host.name)
                            iterator.mark_host_failed(host)
                            self._tqm._failed_hosts[host.name] = True
                            self._tqm._stats.increment('failures', host.name)
                        else:
                            self._tqm._stats.increment('ok', host.name)
                        self._tqm.send_callback(
                            'v2_runner_on_failed',
                            task_result,
                            ignore_errors=task.ignore_errors)
                    elif result[0] == 'host_unreachable':
                        self._tqm._unreachable_hosts[host.name] = True
                        self._tqm._stats.increment('dark', host.name)
                        self._tqm.send_callback('v2_runner_on_unreachable',
                                                task_result)
                    elif result[0] == 'host_task_skipped':
                        self._tqm._stats.increment('skipped', host.name)
                        self._tqm.send_callback('v2_runner_on_skipped',
                                                task_result)
                    elif result[0] == 'host_task_ok':
                        self._tqm._stats.increment('ok', host.name)
                        if 'changed' in task_result._result and task_result._result[
                                'changed']:
                            self._tqm._stats.increment('changed', host.name)
                        self._tqm.send_callback('v2_runner_on_ok', task_result)

                    self._pending_results -= 1
                    if host.name in self._blocked_hosts:
                        del self._blocked_hosts[host.name]

                    # If this is a role task, mark the parent role as being run (if
                    # the task was ok or failed, but not skipped or unreachable)
                    if task_result._task._role is not None and result[0] in (
                            'host_task_ok', 'host_task_failed'):
                        # lookup the role in the ROLE_CACHE to make sure we're dealing
                        # with the correct object and mark it as executed
                        for (entry, role_obj) in iterator._play.ROLE_CACHE[
                                task_result._task._role._role_name].iteritems(
                                ):
                            params = task_result._task._role._role_params
                            if task_result._task._role.tags is not None:
                                params['tags'] = task_result._task._role.tags
                            if task_result._task._role.when is not None:
                                params['when'] = task_result._task._role.when
                            hashed_entry = hash_params(params)
                            if entry == hashed_entry:
                                role_obj._had_task_run = True

                    ret_results.append(task_result)

                elif result[0] == 'add_host':
                    task_result = result[1]
                    new_host_info = task_result.get('add_host', dict())

                    self._add_host(new_host_info)

                elif result[0] == 'add_group':
                    task = result[1]
                    self._add_group(task, iterator)

                elif result[0] == 'notify_handler':
                    task_result = result[1]
                    handler_name = result[2]

                    original_task = iterator.get_original_task(
                        task_result._host, task_result._task)
                    if handler_name not in self._notified_handlers:
                        self._notified_handlers[handler_name] = []

                    if task_result._host not in self._notified_handlers[
                            handler_name]:
                        self._notified_handlers[handler_name].append(
                            task_result._host)

                elif result[0] == 'register_host_var':
                    # essentially the same as 'set_host_var' below, however we
                    # never follow the delegate_to value for registered vars
                    host = result[1]
                    var_name = result[2]
                    var_value = result[3]
                    self._variable_manager.set_host_variable(
                        host, var_name, var_value)

                elif result[0] in ('set_host_var', 'set_host_facts'):
                    host = result[1]
                    task = result[2]
                    item = result[3]

                    if task.delegate_to is not None:
                        task_vars = self._variable_manager.get_vars(
                            loader=self._loader,
                            play=iterator._play,
                            host=host,
                            task=task)
                        task_vars = self.add_tqm_variables(task_vars,
                                                           play=iterator._play)
                        if item is not None:
                            task_vars['item'] = item
                        templar = Templar(loader=self._loader,
                                          variables=task_vars)
                        host_name = templar.template(task.delegate_to)
                        target_host = self._inventory.get_host(host_name)
                        if target_host is None:
                            target_host = Host(name=host_name)
                    else:
                        target_host = host

                    if result[0] == 'set_host_var':
                        var_name = result[4]
                        var_value = result[5]
                        self._variable_manager.set_host_variable(
                            target_host, var_name, var_value)
                    elif result[0] == 'set_host_facts':
                        facts = result[4]
                        self._variable_manager.set_host_facts(
                            target_host, facts)

                else:
                    raise AnsibleError("unknown result message received: %s" %
                                       result[0])
            except Queue.Empty:
                pass

        return ret_results
Example #14
0
 def test_empty_set(self):
     params = set([])
     res = hash_params(params)
     self._assert_hashable(res)
     self._assert_set(res)
Example #15
0
 def test_empty_set(self):
     params = set([])
     res = hash_params(params)
     self._assert_hashable(res)
     self._assert_set(res)
Example #16
0
 def test_dict_with_list_value(self):
     params = {'foo': [1, 4, 'bar']}
     res = hash_params(params)
     self._assert_set(res)
     self._assert_hashable(res)
Example #17
0
 def test_list(self):
     params = ['foo', 'bar', 1, 37, None]
     res = hash_params(params)
     self._assert_set(res)
     self._assert_hashable(res)
Example #18
0
 def test_tuple_dict(self):
     params = ({'foo': 'bar'}, 37)
     res = hash_params(params)
     self._assert_hashable(res)
Example #19
0
 def test_tuple(self):
     params = (1, None, 'foo')
     res = hash_params(params)
     self._assert_hashable(res)
Example #20
0
 def test_dict_tuple(self):
     params = {'foo': (1, 'bar',)}
     res = hash_params(params)
     self._assert_set(res)
Example #21
0
    def _process_pending_results(self, iterator):
        '''
        Reads results off the final queue and takes appropriate action
        based on the result (executing callbacks, updating state, etc.).
        '''

        ret_results = []

        while not self._final_q.empty() and not self._tqm._terminated:
            try:
                result = self._final_q.get(block=False)
                debug("got result from result worker: %s" % (result, ))

                # all host status messages contain 2 entries: (msg, task_result)
                if result[0] in ('host_task_ok', 'host_task_failed',
                                 'host_task_skipped', 'host_unreachable'):
                    task_result = result[1]
                    host = task_result._host
                    task = task_result._task
                    if result[0] == 'host_task_failed':
                        if not task.ignore_errors:
                            debug("marking %s as failed" % host.name)
                            iterator.mark_host_failed(host)
                            self._tqm._failed_hosts[host.name] = True
                        self._tqm._stats.increment('failures', host.name)
                        self._tqm.send_callback('v2_runner_on_failed',
                                                task_result)
                    elif result[0] == 'host_unreachable':
                        self._tqm._unreachable_hosts[host.name] = True
                        self._tqm._stats.increment('dark', host.name)
                        self._tqm.send_callback('v2_runner_on_unreachable',
                                                task_result)
                    elif result[0] == 'host_task_skipped':
                        self._tqm._stats.increment('skipped', host.name)
                        self._tqm.send_callback('v2_runner_on_skipped',
                                                task_result)
                    elif result[0] == 'host_task_ok':
                        self._tqm._stats.increment('ok', host.name)
                        if 'changed' in task_result._result and task_result._result[
                                'changed']:
                            self._tqm._stats.increment('changed', host.name)
                        self._tqm.send_callback('v2_runner_on_ok', task_result)

                    self._pending_results -= 1
                    if host.name in self._blocked_hosts:
                        del self._blocked_hosts[host.name]

                    # If this is a role task, mark the parent role as being run (if
                    # the task was ok or failed, but not skipped or unreachable)
                    if task_result._task._role is not None and result[0] in (
                            'host_task_ok', 'host_task_failed'):
                        # lookup the role in the ROLE_CACHE to make sure we're dealing
                        # with the correct object and mark it as executed
                        for (entry,
                             role_obj) in ROLE_CACHE[task_result._task._role.
                                                     _role_name].iteritems():
                            hashed_entry = hash_params(
                                task_result._task._role._role_params)
                            if entry == hashed_entry:
                                role_obj._had_task_run = True

                    ret_results.append(task_result)

                elif result[0] == 'add_host':
                    task_result = result[1]
                    new_host_info = task_result.get('add_host', dict())

                    self._add_host(new_host_info)

                elif result[0] == 'add_group':
                    host = result[1]
                    task_result = result[2]
                    group_name = task_result.get('add_group')

                    self._add_group(host, group_name)

                elif result[0] == 'notify_handler':
                    host = result[1]
                    handler_name = result[2]

                    if handler_name not in self._notified_handlers:
                        self._notified_handlers[handler_name] = []

                    if host not in self._notified_handlers[handler_name]:
                        self._notified_handlers[handler_name].append(host)

                elif result[0] == 'set_host_var':
                    host = result[1]
                    var_name = result[2]
                    var_value = result[3]
                    self._variable_manager.set_host_variable(
                        host, var_name, var_value)

                elif result[0] == 'set_host_facts':
                    host = result[1]
                    facts = result[2]
                    self._variable_manager.set_host_facts(host, facts)

                else:
                    raise AnsibleError("unknown result message received: %s" %
                                       result[0])
            except Queue.Empty:
                pass

        return ret_results
Example #22
0
    def _process_pending_results(self, iterator):
        '''
        Reads results off the final queue and takes appropriate action
        based on the result (executing callbacks, updating state, etc.).
        '''

        ret_results = []

        while not self._final_q.empty() and not self._tqm._terminated:
            try:
                result = self._final_q.get(block=False)
                debug("got result from result worker: %s" % (result,))

                # all host status messages contain 2 entries: (msg, task_result)
                if result[0] in ('host_task_ok', 'host_task_failed', 'host_task_skipped', 'host_unreachable'):
                    task_result = result[1]
                    host = task_result._host
                    task = task_result._task
                    if result[0] == 'host_task_failed':
                        if not task.ignore_errors:
                            debug("marking %s as failed" % host.name)
                            iterator.mark_host_failed(host)
                            self._tqm._failed_hosts[host.name] = True
                        self._tqm._stats.increment('failures', host.name)
                        self._tqm.send_callback('v2_runner_on_failed', task_result)
                    elif result[0] == 'host_unreachable':
                        self._tqm._unreachable_hosts[host.name] = True
                        self._tqm._stats.increment('dark', host.name)
                        self._tqm.send_callback('v2_runner_on_unreachable', task_result)
                    elif result[0] == 'host_task_skipped':
                        self._tqm._stats.increment('skipped', host.name)
                        self._tqm.send_callback('v2_runner_on_skipped', task_result)
                    elif result[0] == 'host_task_ok':
                        self._tqm._stats.increment('ok', host.name)
                        if 'changed' in task_result._result and task_result._result['changed']:
                            self._tqm._stats.increment('changed', host.name)
                        self._tqm.send_callback('v2_runner_on_ok', task_result)

                    self._pending_results -= 1
                    if host.name in self._blocked_hosts:
                        del self._blocked_hosts[host.name]

                    # If this is a role task, mark the parent role as being run (if
                    # the task was ok or failed, but not skipped or unreachable)
                    if task_result._task._role is not None and result[0] in ('host_task_ok', 'host_task_failed'):
                        # lookup the role in the ROLE_CACHE to make sure we're dealing
                        # with the correct object and mark it as executed
                        for (entry, role_obj) in ROLE_CACHE[task_result._task._role._role_name].iteritems():
                            hashed_entry = hash_params(task_result._task._role._role_params)
                            if entry == hashed_entry :
                                role_obj._had_task_run = True

                    ret_results.append(task_result)

                elif result[0] == 'add_host':
                    task_result = result[1]
                    new_host_info = task_result.get('add_host', dict())
                    
                    self._add_host(new_host_info)

                elif result[0] == 'add_group':
                    host        = result[1]
                    task_result = result[2]
                    group_name  = task_result.get('add_group')

                    self._add_group(host, group_name)

                elif result[0] == 'notify_handler':
                    host         = result[1]
                    handler_name = result[2]

                    if handler_name not in self._notified_handlers:
                        self._notified_handlers[handler_name] = []

                    if host not in self._notified_handlers[handler_name]:
                        self._notified_handlers[handler_name].append(host)

                elif result[0] == 'set_host_var':
                    host      = result[1]
                    var_name  = result[2]
                    var_value = result[3]
                    self._variable_manager.set_host_variable(host, var_name, var_value)

                elif result[0] == 'set_host_facts':
                    host  = result[1]
                    facts = result[2]
                    self._variable_manager.set_host_facts(host, facts)

                else:
                    raise AnsibleError("unknown result message received: %s" % result[0])
            except Queue.Empty:
                pass

        return ret_results