| OLD | NEW |
| 1 # Copyright 2013 The LUCI Authors. All rights reserved. | 1 # Copyright 2013 The LUCI Authors. All rights reserved. |
| 2 # Use of this source code is governed under the Apache License, Version 2.0 | 2 # Use of this source code is governed under the Apache License, Version 2.0 |
| 3 # that can be found in the LICENSE file. | 3 # that can be found in the LICENSE file. |
| 4 | 4 |
| 5 """Swarming bot main process. | 5 """Swarming bot main process. |
| 6 | 6 |
| 7 This is the program that communicates with the Swarming server, ensures the code | 7 This is the program that communicates with the Swarming server, ensures the code |
| 8 is always up to date and executes a child process to run tasks and upload | 8 is always up to date and executes a child process to run tasks and upload |
| 9 results back. | 9 results back. |
| 10 | 10 |
| (...skipping 234 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 245 | 245 |
| 246 | 246 |
| 247 def is_base_dir_ok(botobj): | 247 def is_base_dir_ok(botobj): |
| 248 """Returns False if the bot must be quarantined at all cost.""" | 248 """Returns False if the bot must be quarantined at all cost.""" |
| 249 if not botobj: | 249 if not botobj: |
| 250 # This can happen very early in the process lifetime. | 250 # This can happen very early in the process lifetime. |
| 251 return os.path.dirname(THIS_FILE) != os.path.expanduser('~') | 251 return os.path.dirname(THIS_FILE) != os.path.expanduser('~') |
| 252 return botobj.base_dir != os.path.expanduser('~') | 252 return botobj.base_dir != os.path.expanduser('~') |
| 253 | 253 |
| 254 | 254 |
| 255 def get_min_free_space(botobj): | 255 def get_desired_free_space(botobj): |
| 256 """Returns free disk space needed. | 256 """Returns free disk space needed (in bytes).""" |
| 257 | 257 return int(os_utilities.get_desired_free_space(botobj.base_dir) * 1024 * 1024) |
| 258 Add a "250 MiB slack space" for logs, temporary files and whatever other leak. | |
| 259 """ | |
| 260 return int( | |
| 261 (os_utilities.get_min_free_space(botobj.base_dir) + 250.) * 1024 * 1024) | |
| 262 | 258 |
| 263 | 259 |
| 264 def generate_version(): | 260 def generate_version(): |
| 265 """Returns the bot's code version.""" | 261 """Returns the bot's code version.""" |
| 266 try: | 262 try: |
| 267 return zip_package.generate_version() | 263 return zip_package.generate_version() |
| 268 except Exception as e: | 264 except Exception as e: |
| 269 return 'Error: %s' % e | 265 return 'Error: %s' % e |
| 270 | 266 |
| 271 | 267 |
| (...skipping 121 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 393 | 389 |
| 394 It will remove unexpected files, remove corrupted files, trim the cache size | 390 It will remove unexpected files, remove corrupted files, trim the cache size |
| 395 based on the policies and update state.json. | 391 based on the policies and update state.json. |
| 396 """ | 392 """ |
| 397 cmd = [ | 393 cmd = [ |
| 398 sys.executable, THIS_FILE, 'run_isolated', | 394 sys.executable, THIS_FILE, 'run_isolated', |
| 399 '--clean', | 395 '--clean', |
| 400 '--log-file', os.path.join(botobj.base_dir, 'logs', 'run_isolated.log'), | 396 '--log-file', os.path.join(botobj.base_dir, 'logs', 'run_isolated.log'), |
| 401 '--cache', os.path.join(botobj.base_dir, 'isolated_cache'), | 397 '--cache', os.path.join(botobj.base_dir, 'isolated_cache'), |
| 402 '--named-cache-root', os.path.join(botobj.base_dir, 'c'), | 398 '--named-cache-root', os.path.join(botobj.base_dir, 'c'), |
| 403 '--min-free-space', str(get_min_free_space(botobj)), | 399 '--min-free-space', str(get_desired_free_space(botobj)), |
| 404 ] | 400 ] |
| 405 logging.info('Running: %s', cmd) | 401 logging.info('Running: %s', cmd) |
| 406 try: | 402 try: |
| 407 # Intentionally do not use a timeout, it can take a while to hash 50gb but | 403 # Intentionally do not use a timeout, it can take a while to hash 50gb but |
| 408 # better be safe than sorry. | 404 # better be safe than sorry. |
| 409 proc = subprocess42.Popen( | 405 proc = subprocess42.Popen( |
| 410 cmd, | 406 cmd, |
| 411 stdin=subprocess42.PIPE, | 407 stdin=subprocess42.PIPE, |
| 412 stdout=subprocess42.PIPE, stderr=subprocess42.STDOUT, | 408 stdout=subprocess42.PIPE, stderr=subprocess42.STDOUT, |
| 413 cwd=botobj.base_dir, | 409 cwd=botobj.base_dir, |
| (...skipping 316 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 730 auth_params_dumper.start() | 726 auth_params_dumper.start() |
| 731 | 727 |
| 732 command = [ | 728 command = [ |
| 733 sys.executable, THIS_FILE, 'task_runner', | 729 sys.executable, THIS_FILE, 'task_runner', |
| 734 '--swarming-server', url, | 730 '--swarming-server', url, |
| 735 '--in-file', task_in_file, | 731 '--in-file', task_in_file, |
| 736 '--out-file', task_result_file, | 732 '--out-file', task_result_file, |
| 737 '--cost-usd-hour', str(botobj.state.get('cost_usd_hour') or 0.), | 733 '--cost-usd-hour', str(botobj.state.get('cost_usd_hour') or 0.), |
| 738 # Include the time taken to poll the task in the cost. | 734 # Include the time taken to poll the task in the cost. |
| 739 '--start', str(start), | 735 '--start', str(start), |
| 740 '--min-free-space', str(get_min_free_space(botobj)), | 736 '--min-free-space', str(get_desired_free_space(botobj)), |
| 741 '--bot-file', bot_file, | 737 '--bot-file', bot_file, |
| 742 ] | 738 ] |
| 743 if botobj.remote.uses_auth: | 739 if botobj.remote.uses_auth: |
| 744 command.extend(['--auth-params-file', auth_params_file]) | 740 command.extend(['--auth-params-file', auth_params_file]) |
| 745 if is_grpc: | 741 if is_grpc: |
| 746 command.append('--is-grpc') | 742 command.append('--is-grpc') |
| 747 logging.debug('Running command: %s', command) | 743 logging.debug('Running command: %s', command) |
| 748 | 744 |
| 749 # Put the output file into the current working directory, which should be | 745 # Put the output file into the current working directory, which should be |
| 750 # the one containing swarming_bot.zip. | 746 # the one containing swarming_bot.zip. |
| (...skipping 201 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 952 | 948 |
| 953 error = None | 949 error = None |
| 954 if len(args) != 0: | 950 if len(args) != 0: |
| 955 error = 'Unexpected arguments: %s' % args | 951 error = 'Unexpected arguments: %s' % args |
| 956 try: | 952 try: |
| 957 return run_bot(error) | 953 return run_bot(error) |
| 958 finally: | 954 finally: |
| 959 call_hook( | 955 call_hook( |
| 960 bot.Bot(None, None, None, None, base_dir, None), 'on_bot_shutdown') | 956 bot.Bot(None, None, None, None, base_dir, None), 'on_bot_shutdown') |
| 961 logging.info('main() returning') | 957 logging.info('main() returning') |
| OLD | NEW |