Roll tools/swarming_client/ to bdad1183d6047cc6a1459a5be167ba0a7325146e.
[chromium-blink-merge.git] / testing / legion / host_controller.py
blobdadcba45b4bc4c157729c21102ea747becb3c399
1 # Copyright 2015 The Chromium Authors. All rights reserved.
2 # Use of this source code is governed by a BSD-style license that can be
3 # found in the LICENSE file.
5 """Defines the host controller base library.
7 This module is the basis on which host controllers are built and executed.
8 """
10 import logging
11 import sys
13 #pylint: disable=relative-import
14 import client_lib
15 import common_lib
16 import discovery_server
19 class HostController(object):
20 """The base host controller class."""
22 def __init__(self):
23 self._discovery_server = discovery_server.DiscoveryServer()
25 def SetUp(self):
26 """Setup method used by the subclass."""
27 pass
29 def Task(self):
30 """Main task method used by the subclass."""
31 pass
33 def TearDown(self):
34 """Teardown method used by the subclass."""
35 pass
37 def NewClient(self, *args, **kwargs):
38 controller = client_lib.ClientController(*args, **kwargs)
39 self._discovery_server.RegisterClientCallback(
40 controller.otp, controller.OnConnect)
41 return controller
43 def RunController(self):
44 """Main entry point for the controller."""
45 print ' '.join(sys.argv)
46 common_lib.InitLogging()
47 self._discovery_server.Start()
49 error = None
50 tb = None
51 try:
52 self.SetUp()
53 self.Task()
54 except Exception as e:
55 # Defer raising exceptions until after TearDown and _TearDown are called.
56 error = e
57 tb = sys.exc_info()[-1]
58 try:
59 self.TearDown()
60 except Exception as e:
61 # Defer raising exceptions until after _TearDown is called.
62 # Note that an error raised here will obscure any errors raised
63 # previously.
64 error = e
65 tb = sys.exc_info()[-1]
67 self._discovery_server.Shutdown()
68 client_lib.ClientController.ReleaseAllControllers()
69 if error:
70 raise error, None, tb #pylint: disable=raising-bad-type