#NEWLINE# @ contextlib . contextmanager #NEWLINE# def _open_cinder_volume( self , client , volume , mode ) : #NEWLINE# #INDENT# attach_mode = ( 'rw' if ( mode == 'wb' ) else 'ro' ) #NEWLINE# device = None #NEWLINE# root_helper = get_root_helper ( ) #NEWLINE# host = socket . gethostname ( ) #NEWLINE# properties = connector . get_connector_properties ( root_helper , host , False , False ) #NEWLINE# try : #NEWLINE# #INDENT# volume . reserve ( volume ) #NEWLINE# #UNINDENT# except cinder_exception . ClientException as e : #NEWLINE# #INDENT# msg = ( _ ( 'Failed to reserve volume %(volume_id)s: %(error)s' ) % { #NEWLINE# #INDENT# 'volume_id' : volume . id , #NEWLINE# 'error' : e , #NEWLINE# #UNINDENT# } ) #NEWLINE# LOG . error ( msg ) #NEWLINE# raise exceptions . BackendException ( mode ) #NEWLINE# #UNINDENT# try : #NEWLINE# #INDENT# connection_info = volume . initialize_connection ( volume , properties ) #NEWLINE# conn = connector . InitiatorConnector . factory ( connection_info [ 'driver_volume_type' ] , root_helper , conn = connection_info ) #NEWLINE# device = conn . connect_volume ( connection_info [ 'data' ] ) #NEWLINE# volume . attach ( None , None , attach_mode , host_name = host ) #NEWLINE# volume = self . _wait_volume_status ( volume , 'attaching' , 'in-use' ) #NEWLINE# LOG . debug ( 'Opening host device "%s"' , device [ 'path' ] ) #NEWLINE# with temporary_chown ( device [ 'path' ] ) , open ( device [ 'path' ] , mode ) as f : #NEWLINE# #INDENT# ( yield f ) #NEWLINE# #UNINDENT# #UNINDENT# except Exception : #NEWLINE# #INDENT# LOG . exception ( _LE ( 'Exception while accessing to cinder volume %(volume_id)s.' ) , { #NEWLINE# #INDENT# 'volume_id' : volume . id , #NEWLINE# #UNINDENT# } ) #NEWLINE# raise #NEWLINE# #UNINDENT# finally : #NEWLINE# #INDENT# if ( volume . status == 'in-use' ) : #NEWLINE# #INDENT# volume . begin_detaching ( volume ) #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# if ( volume . status == 'attaching' ) : #NEWLINE# #INDENT# volume . unreserve ( volume ) #NEWLINE# #UNINDENT# #UNINDENT# if device : #NEWLINE# #INDENT# try : #NEWLINE# #INDENT# conn . disconnect_volume ( connection_info [ 'data' ] , device ) #NEWLINE# #UNINDENT# except Exception : #NEWLINE# #INDENT# LOG . exception ( _LE ( 'Failed to disconnect volume %(volume_id)s.' ) , { #NEWLINE# #INDENT# 'volume_id' : volume . id , #NEWLINE# #UNINDENT# } ) #NEWLINE# #UNINDENT# #UNINDENT# try : #NEWLINE# #INDENT# volume . terminate_connection ( volume , properties ) #NEWLINE# #UNINDENT# except Exception : #NEWLINE# #INDENT# LOG . exception ( _LE ( 'Failed to terminate connection of volume %(volume_id)s.' ) , { #NEWLINE# #INDENT# 'volume_id' : volume . id , #NEWLINE# #UNINDENT# } ) #NEWLINE# #UNINDENT# try : #NEWLINE# #INDENT# client . volumes . detach ( volume ) #NEWLINE# #UNINDENT# except Exception : #NEWLINE# #INDENT# LOG . exception ( _LE ( 'Failed to detach volume %(volume_id)s.' ) , { #NEWLINE# #INDENT# 'volume_id' : volume . id , #NEWLINE# #UNINDENT# } )