| OLD | NEW |
| 1 # Copyright 2016 The LUCI Authors. All rights reserved. | 1 # Copyright 2016 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 """Utilities for operating on instances.""" | 5 """Utilities for operating on instances.""" |
| 6 | 6 |
| 7 import json | 7 import json |
| 8 import logging | 8 import logging |
| 9 | 9 |
| 10 from google.appengine.ext import ndb | 10 from google.appengine.ext import ndb |
| 11 | 11 |
| 12 from components import gce | 12 from components import gce |
| 13 from components import net | 13 from components import net |
| 14 from components import pubsub | 14 from components import pubsub |
| 15 from components import utils | |
| 16 | 15 |
| 17 import instance_group_managers | 16 import instance_group_managers |
| 18 import metrics | 17 import metrics |
| 19 import models | 18 import models |
| 20 import utilities | 19 import utilities |
| 21 | 20 |
| 22 | 21 |
| 23 def get_instance_key(base_name, revision, zone, instance_name): | 22 def get_instance_key(base_name, revision, zone, instance_name): |
| 24 """Returns a key for an Instance. | 23 """Returns a key for an Instance. |
| 25 | 24 |
| (...skipping 198 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 224 max_concurrent=max_concurrent, | 223 max_concurrent=max_concurrent, |
| 225 ) | 224 ) |
| 226 | 225 |
| 227 instance_group_managers.set_instances(key, keys.values()) | 226 instance_group_managers.set_instances(key, keys.values()) |
| 228 | 227 |
| 229 | 228 |
| 230 def schedule_fetch(): | 229 def schedule_fetch(): |
| 231 """Enqueues tasks to fetch instances.""" | 230 """Enqueues tasks to fetch instances.""" |
| 232 for instance_group_manager in models.InstanceGroupManager.query(): | 231 for instance_group_manager in models.InstanceGroupManager.query(): |
| 233 if instance_group_manager.url: | 232 if instance_group_manager.url: |
| 234 if not utils.enqueue_task( | 233 utilities.enqueue_task('fetch-instances', instance_group_manager.key) |
| 235 '/internal/queues/fetch-instances', | |
| 236 'fetch-instances', | |
| 237 params={ | |
| 238 'key': instance_group_manager.key.urlsafe(), | |
| 239 }, | |
| 240 ): | |
| 241 logging.warning( | |
| 242 'Failed to enqueue task for InstanceGroupManager: %s', | |
| 243 instance_group_manager.key, | |
| 244 ) | |
| 245 | 234 |
| 246 | 235 |
| 247 def _delete(instance_template_revision, instance_group_manager, instance): | 236 def _delete(instance_template_revision, instance_group_manager, instance): |
| 248 """Deletes the given instance. | 237 """Deletes the given instance. |
| 249 | 238 |
| 250 Args: | 239 Args: |
| 251 instance_template_revision: models.InstanceTemplateRevision. | 240 instance_template_revision: models.InstanceTemplateRevision. |
| 252 instance_group_manager: models.InstanceGroupManager. | 241 instance_group_manager: models.InstanceGroupManager. |
| 253 instance: models.Instance | 242 instance: models.Instance |
| 254 """ | 243 """ |
| (...skipping 60 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 315 ) | 304 ) |
| 316 return | 305 return |
| 317 | 306 |
| 318 _delete(instance_template_revision, instance_group_manager, instance) | 307 _delete(instance_template_revision, instance_group_manager, instance) |
| 319 | 308 |
| 320 | 309 |
| 321 def schedule_pending_deletion(): | 310 def schedule_pending_deletion(): |
| 322 """Enqueues tasks to delete instances.""" | 311 """Enqueues tasks to delete instances.""" |
| 323 for instance in models.Instance.query(): | 312 for instance in models.Instance.query(): |
| 324 if instance.pending_deletion and not instance.deleted: | 313 if instance.pending_deletion and not instance.deleted: |
| 325 if not utils.enqueue_task( | 314 utilities.enqueue_task('delete-instance-pending-deletion', instance.key) |
| 326 '/internal/queues/delete-instance-pending-deletion', | |
| 327 'delete-instance-pending-deletion', | |
| 328 params={ | |
| 329 'key': instance.key.urlsafe(), | |
| 330 }, | |
| 331 ): | |
| 332 logging.warning('Failed to enqueue task for Instance: %s', instance.key) | |
| 333 | 315 |
| 334 | 316 |
| 335 def delete_drained(key): | 317 def delete_drained(key): |
| 336 """Deletes the given drained instance. | 318 """Deletes the given drained instance. |
| 337 | 319 |
| 338 Args: | 320 Args: |
| 339 key: ndb.Key for a models.Instance entity. | 321 key: ndb.Key for a models.Instance entity. |
| 340 """ | 322 """ |
| 341 instance = key.get() | 323 instance = key.get() |
| 342 if not instance: | 324 if not instance: |
| (...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 392 | 374 |
| 393 def schedule_drained_deletion(): | 375 def schedule_drained_deletion(): |
| 394 """Enqueues tasks to delete drained instances.""" | 376 """Enqueues tasks to delete drained instances.""" |
| 395 for instance_group_manager_key in ( | 377 for instance_group_manager_key in ( |
| 396 instance_group_managers.get_drained_instance_group_managers()): | 378 instance_group_managers.get_drained_instance_group_managers()): |
| 397 instance_group_manager = instance_group_manager_key.get() | 379 instance_group_manager = instance_group_manager_key.get() |
| 398 if instance_group_manager: | 380 if instance_group_manager: |
| 399 for instance_key in instance_group_manager.instances: | 381 for instance_key in instance_group_manager.instances: |
| 400 instance = instance_key.get() | 382 instance = instance_key.get() |
| 401 if instance and not instance.cataloged: | 383 if instance and not instance.cataloged: |
| 402 if not utils.enqueue_task( | 384 utilities.enqueue_task('delete-drained-instance', instance.key) |
| 403 '/internal/queues/delete-drained-instance', | |
| 404 'delete-drained-instance', | |
| 405 params={ | |
| 406 'key': instance.key.urlsafe(), | |
| 407 }, | |
| 408 ): | |
| 409 logging.warning( | |
| 410 'Failed to enqueue task for Instance: %s', instance.key) | |
| OLD | NEW |