OLD | NEW |
1 # Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 # Copyright (c) 2012 The Chromium Authors. All rights reserved. |
2 # Use of this source code is governed by a BSD-style license that can be | 2 # Use of this source code is governed by a BSD-style license that can be |
3 # found in the LICENSE file. | 3 # found in the LICENSE file. |
4 | 4 |
5 """Utilities to enable slaves to determine their master without importing any | 5 """Utilities to enable slaves to determine their master without importing any |
6 buildbot or twisted code. | 6 buildbot or twisted code. |
7 """ | 7 """ |
8 | 8 |
9 import inspect | 9 import inspect |
10 import os | 10 import os |
11 import socket | |
12 import sys | 11 import sys |
13 | 12 |
14 from common import chromium_utils | 13 from common import chromium_utils |
15 import config_bootstrap | 14 import config_bootstrap |
16 | 15 |
17 | 16 |
18 def ImportMasterConfigs(master_name=None): | 17 def ImportMasterConfigs(master_name=None): |
19 """Imports master configs. | 18 """Imports master configs. |
20 | 19 |
21 Normally a slave can use GetActiveMaster() to find itself and | 20 Normally a slave can use chromium_utils.GetActiveMaster() to find |
22 determine which ActiveMaster to use. In that case, the active | 21 itself and determine which ActiveMaster to use. In that case, the |
23 master name is passed in as an arg, and we only load the | 22 active master name is passed in as an arg, and we only load the |
24 site_config.py that defines it. When testing, the current "slave" | 23 site_config.py that defines it. When testing, the current "slave" |
25 won't be found. In that case, we don't know which config to use, so | 24 won't be found. In that case, we don't know which config to use, so |
26 load them all. In either case, masters are assigned as attributes | 25 load them all. In either case, masters are assigned as attributes |
27 to the config.Master object.""" | 26 to the config.Master object. |
| 27 """ |
28 for master in chromium_utils.ListMasters(): | 28 for master in chromium_utils.ListMasters(): |
29 path = os.path.join(master, 'master_site_config.py') | 29 path = os.path.join(master, 'master_site_config.py') |
30 if os.path.exists(path): | 30 if os.path.exists(path): |
31 local_vars = {} | 31 local_vars = {} |
32 try: | 32 try: |
33 execfile(path, local_vars) | 33 execfile(path, local_vars) |
34 # pylint: disable=W0703 | 34 # pylint: disable=W0703 |
35 except Exception, e: | 35 except Exception, e: |
36 # Naked exceptions are banned by the style guide but we are | 36 # Naked exceptions are banned by the style guide but we are |
37 # trying to be resilient here. | 37 # trying to be resilient here. |
38 print >> sys.stderr, 'WARNING: cannot exec ' + path | 38 print >> sys.stderr, 'WARNING: cannot exec ' + path |
39 print >> sys.stderr, e | 39 print >> sys.stderr, e |
40 for (symbol_name, symbol) in local_vars.iteritems(): | 40 for (symbol_name, symbol) in local_vars.iteritems(): |
41 if inspect.isclass(local_vars[symbol_name]): | 41 if inspect.isclass(local_vars[symbol_name]): |
42 setattr(config_bootstrap.Master, symbol_name, symbol) | 42 setattr(config_bootstrap.Master, symbol_name, symbol) |
43 # If we have a master_name and it matches, set | 43 # If we have a master_name and it matches, set |
44 # config_bootstrap.Master.active_master. | 44 # config_bootstrap.Master.active_master. |
45 if master_name and master_name == symbol_name: | 45 if master_name and master_name == symbol_name: |
46 setattr(config_bootstrap.Master, 'active_master', symbol) | 46 setattr(config_bootstrap.Master, 'active_master', symbol) |
47 | |
48 | |
49 def GetActiveMaster(slavename=None): | |
50 """Parses all the slaves.cfg and returns the name of the active master | |
51 determined by the host name. Returns None otherwise. | |
52 | |
53 If a slavename is given, it will be matched against *both* the 'slavename' | |
54 and 'hostname' fields in slave.cfg. Otherwise, the machine's hostname will be | |
55 matched against only the 'hostname' field. | |
56 """ | |
57 if slavename is None: | |
58 config_keys = ['hostname'] | |
59 config_val = socket.getfqdn().split('.', 1)[0].lower() | |
60 else: | |
61 config_keys = ['slavename', 'hostname'] | |
62 config_val = slavename | |
63 for master in chromium_utils.ListMasters(): | |
64 path = os.path.join(master, 'slaves.cfg') | |
65 if os.path.exists(path): | |
66 for slave in chromium_utils.RunSlavesCfg(path): | |
67 for key in config_keys: | |
68 if slave.get(key, None) == config_val: | |
69 return slave['master'] | |
OLD | NEW |