| 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 instance templates.""" | 5 """Utilities for operating on instance templates.""" |
| 6 | 6 |
| 7 import logging | 7 import logging |
| 8 | 8 |
| 9 from google.appengine.ext import ndb | 9 from google.appengine.ext import ndb |
| 10 | 10 |
| 11 from components import gce | 11 from components import gce |
| 12 from components import net | 12 from components import net |
| 13 from components import utils | |
| 14 | 13 |
| 15 import models | 14 import models |
| 15 import utilities |
| 16 | 16 |
| 17 | 17 |
| 18 def get_instance_template_key(base_name): | 18 def get_instance_template_key(base_name): |
| 19 """Returns a key for an InstanceTemplate. | 19 """Returns a key for an InstanceTemplate. |
| 20 | 20 |
| 21 Args: | 21 Args: |
| 22 base_name: Base name for the models.InstanceTemplate. | 22 base_name: Base name for the models.InstanceTemplate. |
| 23 | 23 |
| 24 Returns: | 24 Returns: |
| 25 ndb.Key for a models.InstanceTemplate entity. | 25 ndb.Key for a models.InstanceTemplate entity. |
| (...skipping 130 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 156 """Enqueues tasks to create missing instance templates.""" | 156 """Enqueues tasks to create missing instance templates.""" |
| 157 # For each active InstanceTemplateRevision without a URL, schedule | 157 # For each active InstanceTemplateRevision without a URL, schedule |
| 158 # creation of its instance template. Since we are outside a transaction | 158 # creation of its instance template. Since we are outside a transaction |
| 159 # the InstanceTemplateRevision could be out of date and may already have | 159 # the InstanceTemplateRevision could be out of date and may already have |
| 160 # a task scheduled/completed. In either case it doesn't matter since | 160 # a task scheduled/completed. In either case it doesn't matter since |
| 161 # we make creating an instance template and updating the URL idempotent. | 161 # we make creating an instance template and updating the URL idempotent. |
| 162 for instance_template in models.InstanceTemplate.query(): | 162 for instance_template in models.InstanceTemplate.query(): |
| 163 if instance_template.active: | 163 if instance_template.active: |
| 164 instance_template_revision = instance_template.active.get() | 164 instance_template_revision = instance_template.active.get() |
| 165 if instance_template_revision and not instance_template_revision.url: | 165 if instance_template_revision and not instance_template_revision.url: |
| 166 if not utils.enqueue_task( | 166 utilities.enqueue_task( |
| 167 '/internal/queues/create-instance-template', | 167 'create-instance-template', instance_template.active) |
| 168 'create-instance-template', | |
| 169 params={ | |
| 170 'key': instance_template.active.urlsafe(), | |
| 171 }, | |
| 172 ): | |
| 173 logging.warning( | |
| 174 'Failed to enqueue task for InstanceTemplateRevision: %s', | |
| 175 instance_template.active, | |
| 176 ) | |
| 177 | 168 |
| 178 | 169 |
| 179 def get_instance_template_to_delete(key): | 170 def get_instance_template_to_delete(key): |
| 180 """Returns the URL of the instance template to delete. | 171 """Returns the URL of the instance template to delete. |
| 181 | 172 |
| 182 Args: | 173 Args: |
| 183 key: ndb.Key for a models.InstanceTemplateRevision entity. | 174 key: ndb.Key for a models.InstanceTemplateRevision entity. |
| 184 | 175 |
| 185 Returns: | 176 Returns: |
| 186 The URL of the instance template to delete, or None if there isn't one. | 177 The URL of the instance template to delete, or None if there isn't one. |
| (...skipping 63 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 250 keys.append(key) | 241 keys.append(key) |
| 251 return keys | 242 return keys |
| 252 | 243 |
| 253 | 244 |
| 254 def schedule_deletion(): | 245 def schedule_deletion(): |
| 255 """Enqueues tasks to delete drained instance templates.""" | 246 """Enqueues tasks to delete drained instance templates.""" |
| 256 for key in get_drained_instance_template_revisions(): | 247 for key in get_drained_instance_template_revisions(): |
| 257 instance_template = key.get() | 248 instance_template = key.get() |
| 258 if instance_template and instance_template.url: | 249 if instance_template and instance_template.url: |
| 259 if not instance_template.active and not instance_template.drained: | 250 if not instance_template.active and not instance_template.drained: |
| 260 if not utils.enqueue_task( | 251 utilities.enqueue_task('delete-instance-template', key) |
| 261 '/internal/queues/delete-instance-template', | |
| 262 'delete-instance-template', | |
| 263 params={ | |
| 264 'key': key.urlsafe(), | |
| 265 }, | |
| 266 ): | |
| 267 logging.warning( | |
| 268 'Failed to enqueue task for InstanceTemplateRevision: %s', key) | |
| OLD | NEW |