| 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 interacting with the Machine Provider catalog.""" | 5 """Utilities for interacting with the Machine Provider catalog.""" |
| 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 from protorpc.remote import protojson | 11 from protorpc.remote import protojson |
| 12 | 12 |
| 13 from components import gce | 13 from components import gce |
| 14 from components import machine_provider | 14 from components import machine_provider |
| 15 from components import net | 15 from components import net |
| 16 from components import utils | |
| 17 | 16 |
| 18 import instances | 17 import instances |
| 19 import instance_group_managers | 18 import instance_group_managers |
| 20 import metrics | 19 import metrics |
| 21 import models | 20 import models |
| 21 import utilities |
| 22 | 22 |
| 23 | 23 |
| 24 def get_policies(key, service_account): | 24 def get_policies(key, service_account): |
| 25 """Returns Machine Provider policies governing the given instance. | 25 """Returns Machine Provider policies governing the given instance. |
| 26 | 26 |
| 27 Args: | 27 Args: |
| 28 key: ndb.Key for a models.Instance entity. | 28 key: ndb.Key for a models.Instance entity. |
| 29 service_account: Name of the service account the instance will use to | 29 service_account: Name of the service account the instance will use to |
| 30 talk to Machine Provider. | 30 talk to Machine Provider. |
| 31 """ | 31 """ |
| (...skipping 126 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 158 if instance_template.active: | 158 if instance_template.active: |
| 159 instance_template_revision = instance_template.active.get() | 159 instance_template_revision = instance_template.active.get() |
| 160 if instance_template_revision: | 160 if instance_template_revision: |
| 161 for instance_group_manager_key in instance_template_revision.active: | 161 for instance_group_manager_key in instance_template_revision.active: |
| 162 instance_group_manager = instance_group_manager_key.get() | 162 instance_group_manager = instance_group_manager_key.get() |
| 163 if instance_group_manager: | 163 if instance_group_manager: |
| 164 for instance_key in instance_group_manager.instances: | 164 for instance_key in instance_group_manager.instances: |
| 165 instance = instance_key.get() | 165 instance = instance_key.get() |
| 166 if instance: | 166 if instance: |
| 167 if not instance.cataloged and not instance.pending_deletion: | 167 if not instance.cataloged and not instance.pending_deletion: |
| 168 if not utils.enqueue_task( | 168 utilities.enqueue_task('catalog-instance', instance.key) |
| 169 '/internal/queues/catalog-instance', | |
| 170 'catalog-instance', | |
| 171 params={ | |
| 172 'key': instance.key.urlsafe(), | |
| 173 }, | |
| 174 ): | |
| 175 logging.warning( | |
| 176 'Failed to enqueue task for Instance: %s', instance.key) | |
| 177 | 169 |
| 178 | 170 |
| 179 def remove(key): | 171 def remove(key): |
| 180 """Removes the given instance from the catalog. | 172 """Removes the given instance from the catalog. |
| 181 | 173 |
| 182 Args: | 174 Args: |
| 183 key: ndb.Key for a models.Instance entity. | 175 key: ndb.Key for a models.Instance entity. |
| 184 """ | 176 """ |
| 185 instance = key.get() | 177 instance = key.get() |
| 186 if not instance: | 178 if not instance: |
| (...skipping 18 matching lines...) Expand all Loading... |
| 205 | 197 |
| 206 def schedule_removal(): | 198 def schedule_removal(): |
| 207 """Enqueues tasks to remove drained instances from the catalog.""" | 199 """Enqueues tasks to remove drained instances from the catalog.""" |
| 208 for instance_group_manager_key in ( | 200 for instance_group_manager_key in ( |
| 209 instance_group_managers.get_drained_instance_group_managers()): | 201 instance_group_managers.get_drained_instance_group_managers()): |
| 210 instance_group_manager = instance_group_manager_key.get() | 202 instance_group_manager = instance_group_manager_key.get() |
| 211 if instance_group_manager: | 203 if instance_group_manager: |
| 212 for instance_key in instance_group_manager.instances: | 204 for instance_key in instance_group_manager.instances: |
| 213 instance = instance_key.get() | 205 instance = instance_key.get() |
| 214 if instance and instance.cataloged: | 206 if instance and instance.cataloged: |
| 215 if not utils.enqueue_task( | 207 utilities.enqueue_task('remove-cataloged-instance', instance.key) |
| 216 '/internal/queues/remove-cataloged-instance', | |
| 217 'remove-cataloged-instance', | |
| 218 params={ | |
| 219 'key': instance.key.urlsafe(), | |
| 220 }, | |
| 221 ): | |
| 222 logging.warning( | |
| 223 'Failed to enqueue task for Instance: %s', instance.key) | |
| 224 | 208 |
| 225 | 209 |
| 226 def update_cataloged_instance(key): | 210 def update_cataloged_instance(key): |
| 227 """Updates an Instance based on its state in Machine Provider's catalog. | 211 """Updates an Instance based on its state in Machine Provider's catalog. |
| 228 | 212 |
| 229 Args: | 213 Args: |
| 230 key: ndb.Key for a models.Instance entity. | 214 key: ndb.Key for a models.Instance entity. |
| 231 """ | 215 """ |
| 232 instance = key.get() | 216 instance = key.get() |
| 233 if not instance: | 217 if not instance: |
| (...skipping 15 matching lines...) Expand all Loading... |
| 249 ) | 233 ) |
| 250 metrics.send_machine_event('METADATA_UPDATE_PROPOSED', instance.hostname) | 234 metrics.send_machine_event('METADATA_UPDATE_PROPOSED', instance.hostname) |
| 251 except net.NotFoundError: | 235 except net.NotFoundError: |
| 252 instances.mark_for_deletion(key) | 236 instances.mark_for_deletion(key) |
| 253 | 237 |
| 254 | 238 |
| 255 def schedule_cataloged_instance_update(): | 239 def schedule_cataloged_instance_update(): |
| 256 """Enqueues tasks to update information about cataloged instances.""" | 240 """Enqueues tasks to update information about cataloged instances.""" |
| 257 for instance in models.Instance.query(): | 241 for instance in models.Instance.query(): |
| 258 if instance.cataloged: | 242 if instance.cataloged: |
| 259 if not utils.enqueue_task( | 243 utilities.enqueue_task('update-cataloged-instance', instance.key) |
| 260 '/internal/queues/update-cataloged-instance', | |
| 261 'update-cataloged-instance', | |
| 262 params={ | |
| 263 'key': instance.key.urlsafe(), | |
| 264 }, | |
| 265 ): | |
| 266 logging.warning('Failed to enqueue task for Instance: %s', instance.key) | |
| OLD | NEW |