#NEWLINE# def detach_destroy_volumes( api ) : #NEWLINE# #INDENT# '\n    Detach and destroy all volumes known to this API.\n    If we failed to detach a volume for any reason,\n    sleep for 1 second and retry until we hit CLEANUP_RETRY_LIMIT.\n    This is to facilitate best effort cleanup of volume\n    environment after each test run, so that future runs\n    are not impacted.\n    ' #NEWLINE# volumes = api . list_volumes ( ) #NEWLINE# retry = 0 #NEWLINE# action_type = 'agent:blockdevice:cleanup:details' #NEWLINE# with start_action ( action_type = action_type ) : #NEWLINE# #INDENT# while ( ( retry < CLEANUP_RETRY_LIMIT ) and ( len ( volumes ) > 0 ) ) : #NEWLINE# #INDENT# for volume in volumes : #NEWLINE# #INDENT# try : #NEWLINE# #INDENT# if ( volume . attached_to is not None ) : #NEWLINE# #INDENT# api . detach_volume ( volume . blockdevice_id ) #NEWLINE# #UNINDENT# api . destroy_volume ( volume . blockdevice_id ) #NEWLINE# #UNINDENT# except : #NEWLINE# #INDENT# write_traceback ( _logger ) #NEWLINE# #UNINDENT# #UNINDENT# time . sleep ( 1.0 ) #NEWLINE# volumes = api . list_volumes ( ) #NEWLINE# retry += 1 #NEWLINE# #UNINDENT# if ( len ( volumes ) > 0 ) : #NEWLINE# #INDENT# Message . new ( 'agent:blockdevice:failedcleanup:volumes' , volumes = volumes ) . write ( )