| OLD | NEW |
| 1 # coding: utf-8 |
| 1 # Copyright 2014 The LUCI Authors. All rights reserved. | 2 # Copyright 2014 The LUCI Authors. All rights reserved. |
| 2 # Use of this source code is governed under the Apache License, Version 2.0 | 3 # Use of this source code is governed under the Apache License, Version 2.0 |
| 3 # that can be found in the LICENSE file. | 4 # that can be found in the LICENSE file. |
| 4 | 5 |
| 5 """OS specific utility functions. | 6 """OS specific utility functions. |
| 6 | 7 |
| 7 Includes code: | 8 Includes code: |
| 8 - to declare the current system this code is running under. | 9 - to declare the current system this code is running under. |
| 9 - to run a command on user login. | 10 - to run a command on user login. |
| 10 - to reboot the host. | 11 - to reboot the host. |
| (...skipping 501 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 512 if sys.platform == 'darwin': | 513 if sys.platform == 'darwin': |
| 513 return platforms.osx.get_uptime() | 514 return platforms.osx.get_uptime() |
| 514 if sys.platform == 'win32': | 515 if sys.platform == 'win32': |
| 515 return platforms.win.get_uptime() | 516 return platforms.win.get_uptime() |
| 516 if sys.platform == 'cygwin': | 517 if sys.platform == 'cygwin': |
| 517 # Not important. | 518 # Not important. |
| 518 return 0. | 519 return 0. |
| 519 return platforms.linux.get_uptime() | 520 return platforms.linux.get_uptime() |
| 520 | 521 |
| 521 | 522 |
| 523 def get_named_caches(): |
| 524 """Returns the list of named caches.""" |
| 525 # Strictly speaking, this is a layering violation. This data is managed by |
| 526 # run_isolated.py but this is valuable to expose this as a Swarming bot |
| 527 # dimensions so ¯\_(ツ)_/¯ |
| 528 # |
| 529 # Assumptions: |
| 530 # - ../__main__.py calls os.chdir(__file__) |
| 531 # - ../bot_code/bot_main.py specifies |
| 532 # --named-cache-root os.path.join(botobj.base_dir, 'c') to run_isolated. |
| 533 # - ../client/named_cache.py behavior |
| 534 # |
| 535 # A better implementation would require: |
| 536 # - Access to bot.Bot instance to query bot.base_dir |
| 537 # - Access to named_cache.py to load state.json |
| 538 # - Access to --named-cache-root hardcoded in bot_main.py |
| 539 # |
| 540 # but hey, the following code is 5 lines... |
| 541 try: |
| 542 with open(os.path.join(u'c', u'state.json'), 'rb') as f: |
| 543 return sorted(i[0] for i in json.load(f)['items']) |
| 544 except (IOError, KeyError, OSError): |
| 545 return [] |
| 546 |
| 547 |
| 522 class AuthenticatedHttpRequestFailure(Exception): | 548 class AuthenticatedHttpRequestFailure(Exception): |
| 523 pass | 549 pass |
| 524 | 550 |
| 525 | 551 |
| 526 def authenticated_http_request(service_account, *args, **kwargs): | 552 def authenticated_http_request(service_account, *args, **kwargs): |
| 527 """Sends an OAuth2-authenticated HTTP request. | 553 """Sends an OAuth2-authenticated HTTP request. |
| 528 | 554 |
| 529 Args: | 555 Args: |
| 530 service_account: Service account to use. For GCE, the name of the service | 556 service_account: Service account to use. For GCE, the name of the service |
| 531 account, otherwise the path to the service account JSON file. | 557 account, otherwise the path to the service account JSON file. |
| (...skipping 315 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 847 u'cpu': [ | 873 u'cpu': [ |
| 848 cpu_type, | 874 cpu_type, |
| 849 cpu_type + u'-' + cpu_bitness, | 875 cpu_type + u'-' + cpu_bitness, |
| 850 ], | 876 ], |
| 851 u'gpu': get_gpu()[0], | 877 u'gpu': get_gpu()[0], |
| 852 u'id': [get_hostname_short()], | 878 u'id': [get_hostname_short()], |
| 853 u'os': get_os_values(), | 879 u'os': get_os_values(), |
| 854 # This value is frequently overridden by bots.cfg via luci-config. | 880 # This value is frequently overridden by bots.cfg via luci-config. |
| 855 u'pool': [u'default'], | 881 u'pool': [u'default'], |
| 856 } | 882 } |
| 883 caches = get_named_caches() |
| 884 if caches: |
| 885 dimensions[u'caches'] = caches |
| 857 if u'avx2' in cpuinfo.get(u'flags', []): | 886 if u'avx2' in cpuinfo.get(u'flags', []): |
| 858 dimensions[u'cpu'].append(cpu_type + u'-' + cpu_bitness + u'-avx2') | 887 dimensions[u'cpu'].append(cpu_type + u'-' + cpu_bitness + u'-avx2') |
| 859 if any(u'avx512' in x for x in cpuinfo.get(u'flags', [])): | 888 if any(u'avx512' in x for x in cpuinfo.get(u'flags', [])): |
| 860 dimensions[u'cpu'].append(cpu_type + u'-' + cpu_bitness + u'-avx512') | 889 dimensions[u'cpu'].append(cpu_type + u'-' + cpu_bitness + u'-avx512') |
| 861 if u'none' not in dimensions[u'gpu']: | 890 if u'none' not in dimensions[u'gpu']: |
| 862 hidpi = get_monitor_hidpi() | 891 hidpi = get_monitor_hidpi() |
| 863 if hidpi: | 892 if hidpi: |
| 864 dimensions[u'hidpi'] = hidpi | 893 dimensions[u'hidpi'] = hidpi |
| 865 | 894 |
| 866 machine_type = get_machine_type() | 895 machine_type = get_machine_type() |
| (...skipping 334 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1201 | 1230 |
| 1202 | 1231 |
| 1203 def trim_rolled_log(name): | 1232 def trim_rolled_log(name): |
| 1204 try: | 1233 try: |
| 1205 for item in glob.iglob('%s.??' % name): | 1234 for item in glob.iglob('%s.??' % name): |
| 1206 os.remove(item) | 1235 os.remove(item) |
| 1207 for item in glob.iglob('%s.???' % name): | 1236 for item in glob.iglob('%s.???' % name): |
| 1208 os.remove(item) | 1237 os.remove(item) |
| 1209 except Exception as e: | 1238 except Exception as e: |
| 1210 logging.exception('trim_rolled_log(%s) failed: %s', name, e) | 1239 logging.exception('trim_rolled_log(%s) failed: %s', name, e) |
| OLD | NEW |