コード例 #1
0
    def _queue_task(self, host, task, task_vars, play_context):
        self.curr_host = host
        self.curr_task = task
        self.curr_task_vars = task_vars
        self.curr_play_context = play_context

        StrategyBase._queue_task(self, host, task, task_vars, play_context)
コード例 #2
0
    def _process_pending_results(self, iterator, one_pass=False):
        if not hasattr(self, "curr_host"):
            return StrategyBase._process_pending_results(self, iterator, one_pass)

        prev_host_state = iterator.get_host_state(self.curr_host)
        results = StrategyBase._process_pending_results(self, iterator, one_pass)

        while self._need_debug(results):
            next_action = NextAction()
            dbg = Debugger(self, results, next_action)
            dbg.cmdloop()

            if next_action.result == NextAction.REDO:
                # rollback host state
                self.curr_tqm.clear_failed_hosts()
                iterator._host_states[self.curr_host.name] = prev_host_state
                if reduce(lambda total, res : res.is_failed() or total, results, False):
                    self._tqm._stats.failures[self.curr_host.name] -= 1
                elif reduce(lambda total, res : res.is_unreachable() or total, results, False):
                    self._tqm._stats.dark[self.curr_host.name] -= 1

                # redo
                StrategyBase._queue_task(self, self.curr_host, self.curr_task, self.curr_task_vars, self.curr_play_context)
                results = StrategyBase._process_pending_results(self, iterator, one_pass)
            elif next_action.result == NextAction.CONTINUE:
                break
            elif next_action.result == NextAction.EXIT:
                exit(1)

        return results
コード例 #3
0
    def _queue_task(self, host, task, task_vars, play_context):
        self.curr_host = host
        self.curr_task = task
        self.curr_task_vars = task_vars
        self.curr_play_context = play_context

        StrategyBase._queue_task(self, host, task, task_vars, play_context)
コード例 #4
0
    def test_strategy_base_queue_task(self):
        fake_loader = DictDataLoader()

        workers = []
        for i in range(0, 3):
            worker_main_q = MagicMock()
            worker_main_q.put.return_value = None
            worker_result_q = MagicMock()
            workers.append([i, worker_main_q, worker_result_q])

        mock_tqm = MagicMock()
        mock_tqm._final_q = MagicMock()
        mock_tqm.get_workers.return_value = workers
        mock_tqm.get_loader.return_value = fake_loader

        strategy_base = StrategyBase(tqm=mock_tqm)
        strategy_base._cur_worker = 0
        strategy_base._pending_results = 0
        strategy_base._queue_task(host=MagicMock(), task=MagicMock(), task_vars=dict(), play_context=MagicMock())
        self.assertEqual(strategy_base._cur_worker, 1)
        self.assertEqual(strategy_base._pending_results, 1)
        strategy_base._queue_task(host=MagicMock(), task=MagicMock(), task_vars=dict(), play_context=MagicMock())
        self.assertEqual(strategy_base._cur_worker, 2)
        self.assertEqual(strategy_base._pending_results, 2)
        strategy_base._queue_task(host=MagicMock(), task=MagicMock(), task_vars=dict(), play_context=MagicMock())
        self.assertEqual(strategy_base._cur_worker, 0)
        self.assertEqual(strategy_base._pending_results, 3)
        workers[0][1].put.side_effect = EOFError
        strategy_base._queue_task(host=MagicMock(), task=MagicMock(), task_vars=dict(), play_context=MagicMock())
        self.assertEqual(strategy_base._cur_worker, 1)
        self.assertEqual(strategy_base._pending_results, 3)
コード例 #5
0
    def _process_pending_results(self, iterator, one_pass=False):
        if not hasattr(self, "curr_host"):
            return StrategyBase._process_pending_results(
                self, iterator, one_pass)

        prev_host_state = iterator.get_host_state(self.curr_host)
        results = StrategyBase._process_pending_results(
            self, iterator, one_pass)

        while self._need_debug(results):
            next_action = NextAction()
            dbg = Debugger(self, results, next_action)
            dbg.cmdloop()

            if next_action.result == NextAction.REDO:
                # rollback host state
                self.curr_tqm.clear_failed_hosts()
                iterator._host_states[self.curr_host.name] = prev_host_state
                if reduce(lambda total, res: res.is_failed() or total, results,
                          False):
                    self._tqm._stats.failures[self.curr_host.name] -= 1
                elif reduce(lambda total, res: res.is_unreachable() or total,
                            results, False):
                    self._tqm._stats.dark[self.curr_host.name] -= 1

                # redo
                StrategyBase._queue_task(self, self.curr_host, self.curr_task,
                                         self.curr_task_vars,
                                         self.curr_play_context)
                results = StrategyBase._process_pending_results(
                    self, iterator, one_pass)
            elif next_action.result == NextAction.CONTINUE:
                break
            elif next_action.result == NextAction.EXIT:
                exit(1)

        return results
コード例 #6
0
    def test_strategy_base_queue_task(self, mock_worker):
        def fake_run(self):
            return

        mock_worker.run.side_effect = fake_run

        fake_loader = DictDataLoader()
        mock_var_manager = MagicMock()
        mock_host = MagicMock()
        mock_host.get_vars.return_value = dict()
        mock_host.has_hostkey = True
        mock_inventory = MagicMock()
        mock_inventory.get.return_value = mock_host
        mock_options = MagicMock()
        mock_options.module_path = None

        tqm = TaskQueueManager(
            inventory=mock_inventory,
            variable_manager=mock_var_manager,
            loader=fake_loader,
            options=mock_options,
            passwords=None,
        )
        tqm._initialize_processes(3)
        tqm.hostvars = dict()

        mock_task = MagicMock()
        mock_task._uuid = 'abcd'

        try:
            strategy_base = StrategyBase(tqm=tqm)
            strategy_base._queue_task(host=mock_host,
                                      task=mock_task,
                                      task_vars=dict(),
                                      play_context=MagicMock())
            self.assertEqual(strategy_base._cur_worker, 1)
            self.assertEqual(strategy_base._pending_results, 1)
            strategy_base._queue_task(host=mock_host,
                                      task=mock_task,
                                      task_vars=dict(),
                                      play_context=MagicMock())
            self.assertEqual(strategy_base._cur_worker, 2)
            self.assertEqual(strategy_base._pending_results, 2)
            strategy_base._queue_task(host=mock_host,
                                      task=mock_task,
                                      task_vars=dict(),
                                      play_context=MagicMock())
            self.assertEqual(strategy_base._cur_worker, 0)
            self.assertEqual(strategy_base._pending_results, 3)
        finally:
            tqm.cleanup()
コード例 #7
0
    def test_strategy_base_queue_task(self, mock_worker):
        def fake_run(self):
            return

        mock_worker.run.side_effect = fake_run

        fake_loader = DictDataLoader()
        mock_var_manager = MagicMock()
        mock_host = MagicMock()
        mock_host.get_vars.return_value = dict()
        mock_host.has_hostkey = True
        mock_inventory = MagicMock()
        mock_inventory.get.return_value = mock_host
        mock_options = MagicMock()
        mock_options.module_path = None

        tqm = TaskQueueManager(
            inventory=mock_inventory,
            variable_manager=mock_var_manager,
            loader=fake_loader,
            options=mock_options,
            passwords=None,
        )
        tqm._initialize_processes(3)
        tqm.hostvars = dict()

        mock_task = MagicMock()
        mock_task._uuid = 'abcd'

        try:
            strategy_base = StrategyBase(tqm=tqm)
            strategy_base._queue_task(host=mock_host, task=mock_task, task_vars=dict(), play_context=MagicMock())
            self.assertEqual(strategy_base._cur_worker, 1)
            self.assertEqual(strategy_base._pending_results, 1)
            strategy_base._queue_task(host=mock_host, task=mock_task, task_vars=dict(), play_context=MagicMock())
            self.assertEqual(strategy_base._cur_worker, 2)
            self.assertEqual(strategy_base._pending_results, 2)
            strategy_base._queue_task(host=mock_host, task=mock_task, task_vars=dict(), play_context=MagicMock())
            self.assertEqual(strategy_base._cur_worker, 0)
            self.assertEqual(strategy_base._pending_results, 3)
        finally:
            tqm.cleanup()