| OLD | NEW |
| 1 # Copyright 2014 The LUCI Authors. All rights reserved. | 1 # Copyright 2014 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 code. Includes bootstrap and swarming_bot.zip. | 5 """Swarming bot code. Includes bootstrap and swarming_bot.zip. |
| 6 | 6 |
| 7 It includes everything that is AppEngine specific. The non-GAE code is in | 7 It includes everything that is AppEngine specific. The non-GAE code is in |
| 8 bot_archive.py. | 8 bot_archive.py. |
| 9 """ | 9 """ |
| 10 | 10 |
| (...skipping 198 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 209 signature = _get_signature(host) | 209 signature = _get_signature(host) |
| 210 version = memcache.get('version-' + signature, namespace='bot_code') | 210 version = memcache.get('version-' + signature, namespace='bot_code') |
| 211 if version: | 211 if version: |
| 212 return version, None | 212 return version, None |
| 213 | 213 |
| 214 # Need to calculate it. | 214 # Need to calculate it. |
| 215 additionals = {'config/bot_config.py': get_bot_config().content} | 215 additionals = {'config/bot_config.py': get_bot_config().content} |
| 216 bot_dir = os.path.join(ROOT_DIR, 'swarming_bot') | 216 bot_dir = os.path.join(ROOT_DIR, 'swarming_bot') |
| 217 version = bot_archive.get_swarming_bot_version( | 217 version = bot_archive.get_swarming_bot_version( |
| 218 bot_dir, host, utils.get_app_version(), additionals, | 218 bot_dir, host, utils.get_app_version(), additionals, |
| 219 local_config.settings().enable_ts_monitoring) | 219 local_config.settings()) |
| 220 memcache.set('version-' + signature, version, namespace='bot_code', time=60) | 220 memcache.set('version-' + signature, version, namespace='bot_code', time=60) |
| 221 return version, additionals | 221 return version, additionals |
| 222 | 222 |
| 223 | 223 |
| 224 def get_swarming_bot_zip(host): | 224 def get_swarming_bot_zip(host): |
| 225 """Returns a zipped file of all the files a bot needs to run. | 225 """Returns a zipped file of all the files a bot needs to run. |
| 226 | 226 |
| 227 Returns: | 227 Returns: |
| 228 A string representing the zipped file's contents. | 228 A string representing the zipped file's contents. |
| 229 """ | 229 """ |
| 230 version, additionals = get_bot_version(host) | 230 version, additionals = get_bot_version(host) |
| 231 content = get_cached_swarming_bot_zip(version) | 231 content = get_cached_swarming_bot_zip(version) |
| 232 if content: | 232 if content: |
| 233 logging.debug('memcached bot code %s; %d bytes', version, len(content)) | 233 logging.debug('memcached bot code %s; %d bytes', version, len(content)) |
| 234 return content | 234 return content |
| 235 | 235 |
| 236 # Get the start bot script from the database, if present. Pass an empty | 236 # Get the start bot script from the database, if present. Pass an empty |
| 237 # file if the files isn't present. | 237 # file if the files isn't present. |
| 238 additionals = additionals or { | 238 additionals = additionals or { |
| 239 'config/bot_config.py': get_bot_config().content, | 239 'config/bot_config.py': get_bot_config().content, |
| 240 } | 240 } |
| 241 bot_dir = os.path.join(ROOT_DIR, 'swarming_bot') | 241 bot_dir = os.path.join(ROOT_DIR, 'swarming_bot') |
| 242 content, version = bot_archive.get_swarming_bot_zip( | 242 content, version = bot_archive.get_swarming_bot_zip( |
| 243 bot_dir, host, utils.get_app_version(), additionals, | 243 bot_dir, host, utils.get_app_version(), additionals, |
| 244 local_config.settings().enable_ts_monitoring) | 244 local_config.settings()) |
| 245 logging.info('generated bot code %s; %d bytes', version, len(content)) | 245 logging.info('generated bot code %s; %d bytes', version, len(content)) |
| 246 cache_swarming_bot_zip(version, content) | 246 cache_swarming_bot_zip(version, content) |
| 247 return content | 247 return content |
| 248 | 248 |
| 249 | 249 |
| 250 def get_cached_swarming_bot_zip(version): | 250 def get_cached_swarming_bot_zip(version): |
| 251 """Returns the bot contents if its been cached, or None if missing.""" | 251 """Returns the bot contents if its been cached, or None if missing.""" |
| 252 # see cache_swarming_bot_zip for how the "meta" entry is set | 252 # see cache_swarming_bot_zip for how the "meta" entry is set |
| 253 meta = bot_memcache_get(version, 'meta').get_result() | 253 meta = bot_memcache_get(version, 'meta').get_result() |
| 254 if meta is None: | 254 if meta is None: |
| (...skipping 132 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 387 | 387 |
| 388 ## Config validators | 388 ## Config validators |
| 389 | 389 |
| 390 | 390 |
| 391 @config.validation.self_rule('regex:scripts/.+\\.py') | 391 @config.validation.self_rule('regex:scripts/.+\\.py') |
| 392 def _validate_scripts(content, ctx): | 392 def _validate_scripts(content, ctx): |
| 393 try: | 393 try: |
| 394 ast.parse(content) | 394 ast.parse(content) |
| 395 except (SyntaxError, TypeError) as e: | 395 except (SyntaxError, TypeError) as e: |
| 396 ctx.error('invalid %s: %s' % (ctx.path, e)) | 396 ctx.error('invalid %s: %s' % (ctx.path, e)) |
| OLD | NEW |