• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# Lint as: python2, python3
2# Copyright (c) 2012 The Chromium Authors. All rights reserved.
3# Use of this source code is governed by a BSD-style license that can be
4# found in the LICENSE file.
5
6import logging
7try:
8    import queue
9except ImportError:
10    import Queue as queue
11import time
12import traceback
13
14from autotest_lib.client.common_lib.cros.network import ap_constants
15from autotest_lib.server.cros.ap_configurators import ap_configurator
16from threading import Thread
17
18# Maximum configurators to run at once
19THREAD_MAX = 15
20
21
22class APCartridge(object):
23    """Class to run multiple configurators in parallel."""
24
25
26    def __init__(self):
27        self.cartridge = queue.Queue()
28
29
30    def push_configurators(self, configurators):
31        """Adds multiple configurators to the cartridge.
32
33        @param configurators: a list of configurator objects.
34        """
35        for configurator in configurators:
36            self.cartridge.put(configurator)
37
38
39    def push_configurator(self, configurator):
40        """Adds a configurator to the cartridge.
41
42        @param configurator: a configurator object.
43        """
44        self.cartridge.put(configurator)
45
46
47    def _apply_settings(self, broken_pdus):
48        while True:
49            configurator = self.cartridge.get()
50            try:
51                # Don't run this thread if the PDU in question was found to be
52                # down by any previous thread.
53                if configurator.pdu in broken_pdus:
54                   configurator.configuration_success = ap_constants.PDU_FAIL
55                   raise ap_configurator.PduNotResponding(configurator.pdu)
56                configurator.apply_settings()
57            except ap_configurator.PduNotResponding as e:
58                if configurator.pdu not in broken_pdus:
59                    broken_pdus.append(configurator.pdu)
60            except Exception:
61                configurator.configuration_success = ap_constants.CONFIG_FAIL
62                trace = ''.join(traceback.format_exc())
63                configurator.store_config_failure(trace)
64                logging.error('Configuration failed for AP: %s\n%s',
65                               configurator.name, trace)
66            finally:
67                configurator.reset_command_list()
68                logging.info('Configuration of AP %s complete.',
69                              configurator.name)
70                self.cartridge.task_done()
71
72
73    def run_configurators(self, broken_pdus):
74        """Runs apply_settings for all configurators in the cartridge.
75
76        @param broken_pdus: List of all the PDUs that are down.
77        """
78        for i in range(THREAD_MAX):
79            t = Thread(target=self._apply_settings,args=(broken_pdus,))
80            t.daemon = True
81            t.start()
82        self.cartridge.join()
83