66 :license: MIT, see LICENSE for more details.
77"""
88import datetime
9- import itertools
109import logging
11- import random
1210import socket
1311import time
1412import warnings
@@ -58,7 +56,7 @@ def __init__(self, client, ordering_manager=None):
5856 else :
5957 self .ordering_manager = ordering_manager
6058
61- @retry (exceptions . SoftLayerAPIError , logger = LOGGER )
59+ @retry (logger = LOGGER )
6260 def list_instances (self , hourly = True , monthly = True , tags = None , cpus = None ,
6361 memory = None , hostname = None , domain = None ,
6462 local_disk = None , datacenter = None , nic_speed = None ,
@@ -162,7 +160,7 @@ def list_instances(self, hourly=True, monthly=True, tags=None, cpus=None,
162160 func = getattr (self .account , call )
163161 return func (** kwargs )
164162
165- @retry (exceptions . SoftLayerAPIError , logger = LOGGER )
163+ @retry (logger = LOGGER )
166164 def get_instance (self , instance_id , ** kwargs ):
167165 """Get details about a virtual server instance.
168166
@@ -237,7 +235,7 @@ def get_instance(self, instance_id, **kwargs):
237235
238236 return self .guest .getObject (id = instance_id , ** kwargs )
239237
240- @retry (exceptions . SoftLayerAPIError , logger = LOGGER )
238+ @retry (logger = LOGGER )
241239 def get_create_options (self ):
242240 """Retrieves the available options for creating a VS.
243241
@@ -414,7 +412,7 @@ def _generate_create_dict(
414412
415413 return data
416414
417- @retry (exceptions . SoftLayerAPIError , logger = LOGGER )
415+ @retry (logger = LOGGER )
418416 def wait_for_transaction (self , instance_id , limit , delay = 10 ):
419417 """Waits on a VS transaction for the specified amount of time.
420418
@@ -428,7 +426,7 @@ def wait_for_transaction(self, instance_id, limit, delay=10):
428426
429427 return self .wait_for_ready (instance_id , limit , delay = delay , pending = True )
430428
431- def wait_for_ready (self , instance_id , limit , delay = 10 , pending = False ):
429+ def wait_for_ready (self , instance_id , limit = 3600 , delay = 10 , pending = False ):
432430 """Determine if a VS is ready and available.
433431
434432 In some cases though, that can mean that no transactions are running.
@@ -439,7 +437,7 @@ def wait_for_ready(self, instance_id, limit, delay=10, pending=False):
439437 cancellations.
440438
441439 :param int instance_id: The instance ID with the pending transaction
442- :param int limit: The maximum amount of time to wait.
440+ :param int limit: The maximum amount of seconds to wait.
443441 :param int delay: The number of seconds to sleep before checks. Defaults to 10.
444442 :param bool pending: Wait for pending transactions not related to
445443 provisioning or reloads such as monitoring.
@@ -449,43 +447,21 @@ def wait_for_ready(self, instance_id, limit, delay=10, pending=False):
449447 # Will return once vsi 12345 is ready, or after 10 checks
450448 ready = mgr.wait_for_ready(12345, 10)
451449 """
452- until = time .time () + limit
453- for new_instance in itertools .repeat (instance_id ):
454- mask = """id,
455- lastOperatingSystemReload.id,
456- activeTransaction.id,provisionDate"""
457- try :
458- instance = self .get_instance (new_instance , mask = mask )
459- last_reload = utils .lookup (instance , 'lastOperatingSystemReload' , 'id' )
460- active_transaction = utils .lookup (instance , 'activeTransaction' , 'id' )
461-
462- reloading = all ((
463- active_transaction ,
464- last_reload ,
465- last_reload == active_transaction ,
466- ))
467-
468- # only check for outstanding transactions if requested
469- outstanding = False
470- if pending :
471- outstanding = active_transaction
472-
473- # return True if the instance has finished provisioning
474- # and isn't currently reloading the OS.
475- if all ([instance .get ('provisionDate' ),
476- not reloading ,
477- not outstanding ]):
478- return True
479- LOGGER .info ("%s not ready." , str (instance_id ))
480- except exceptions .SoftLayerAPIError as exception :
481- delay = (delay * 2 ) + random .randint (0 , 9 )
482- LOGGER .info ('Exception: %s' , str (exception ))
483-
450+ now = time .time ()
451+ until = now + limit
452+ mask = "mask[id, lastOperatingSystemReload[id], activeTransaction, provisionDate]"
453+
454+ while now <= until :
455+ instance = self .get_instance (instance_id , mask = mask )
456+ if utils .is_ready (instance , pending ):
457+ return True
458+ transaction = utils .lookup (instance , 'activeTransaction' , 'transactionStatus' , 'friendlyName' )
459+ snooze = min (delay , until - now )
460+ LOGGER .info ("%s - %d not ready. Auto retry in %ds" , transaction , instance_id , snooze )
461+ time .sleep (snooze )
484462 now = time .time ()
485- if now >= until :
486- return False
487- LOGGER .info ('Auto retry in %s seconds' , str (min (delay , until - now )))
488- time .sleep (min (delay , until - now ))
463+
464+ LOGGER .info ("Waiting for %d expired." , instance_id )
489465 return False
490466
491467 def verify_create_instance (self , ** kwargs ):
@@ -581,7 +557,7 @@ def create_instance(self, **kwargs):
581557 self .set_tags (tags , guest_id = inst ['id' ])
582558 return inst
583559
584- @retry (exceptions . SoftLayerAPIError , logger = LOGGER )
560+ @retry (logger = LOGGER )
585561 def set_tags (self , tags , guest_id ):
586562 """Sets tags on a guest with a retry decorator
587563
0 commit comments