<s> """<STR_LIT>""" <EOL> import re <EOL> import string <EOL> import uuid <EOL> from eventlet import greenthread <EOL> from oslo_config import cfg <EOL> from oslo_log import log as logging <EOL> from oslo_utils import strutils <EOL> from oslo_utils import versionutils <EOL> import six <EOL> from nova import exception <EOL> from nova . i18n import _ , _LE , _LW <EOL> xenapi_volume_utils_opts = [ <EOL> cfg . IntOpt ( '<STR_LIT>' , <EOL> default = <NUM_LIT:20> , <EOL> help = '<STR_LIT>' <EOL> '<STR_LIT>' ) , <EOL> ] <EOL> CONF = cfg . CONF <EOL> CONF . register_opts ( xenapi_volume_utils_opts , '<STR_LIT>' ) <EOL> LOG = logging . getLogger ( __name__ ) <EOL> SR_NAMESPACE = uuid . UUID ( "<STR_LIT>" ) <EOL> def parse_sr_info ( connection_data , description = '<STR_LIT>' ) : <EOL> params = { } <EOL> if '<STR_LIT>' not in connection_data : <EOL> params = _parse_volume_info ( connection_data ) <EOL> sr_identity = "<STR_LIT>" % ( params [ '<STR_LIT:target>' ] , params [ '<STR_LIT:port>' ] , <EOL> params [ '<STR_LIT>' ] ) <EOL> if six . PY2 and isinstance ( sr_identity , unicode ) : <EOL> sr_identity = sr_identity . encode ( '<STR_LIT:utf-8>' ) <EOL> sr_uuid = str ( uuid . uuid5 ( SR_NAMESPACE , sr_identity ) ) <EOL> else : <EOL> sr_uuid = connection_data [ '<STR_LIT>' ] <EOL> for k in connection_data . get ( '<STR_LIT>' , { } ) : <EOL> params [ k ] = connection_data [ k ] <EOL> label = connection_data . pop ( '<STR_LIT>' , <EOL> '<STR_LIT>' % sr_uuid ) <EOL> params [ '<STR_LIT>' ] = connection_data . get ( '<STR_LIT>' , <EOL> description ) <EOL> return ( sr_uuid , label , params ) <EOL> def _parse_volume_info ( connection_data ) : <EOL> """<STR_LIT>""" <EOL> volume_id = connection_data [ '<STR_LIT>' ] <EOL> target_portal = connection_data [ '<STR_LIT>' ] <EOL> target_host = _get_target_host ( target_portal ) <EOL> target_port = _get_target_port ( target_portal ) <EOL> target_iqn = connection_data [ '<STR_LIT>' ] <EOL> log_params = { <EOL> "<STR_LIT>" : volume_id , <EOL> "<STR_LIT:host>" : target_host , <EOL> "<STR_LIT:port>" : target_port , <EOL> "<STR_LIT>" : target_iqn <EOL> } <EOL> LOG . debug ( '<STR_LIT>' <EOL> '<STR_LIT>' , log_params ) <EOL> if ( volume_id is None or <EOL> target_host is None or <EOL> target_iqn is None ) : <EOL> raise exception . StorageError ( <EOL> reason = _ ( '<STR_LIT>' ) % <EOL> strutils . mask_password ( connection_data ) ) <EOL> volume_info = { } <EOL> volume_info [ '<STR_LIT:id>' ] = volume_id <EOL> volume_info [ '<STR_LIT:target>' ] = target_host <EOL> volume_info [ '<STR_LIT:port>' ] = target_port <EOL> volume_info [ '<STR_LIT>' ] = target_iqn <EOL> if ( '<STR_LIT>' in connection_data and <EOL> connection_data [ '<STR_LIT>' ] == '<STR_LIT>' ) : <EOL> volume_info [ '<STR_LIT>' ] = connection_data [ '<STR_LIT>' ] <EOL> volume_info [ '<STR_LIT>' ] = connection_data [ '<STR_LIT>' ] <EOL> return volume_info <EOL> def _get_target_host ( iscsi_string ) : <EOL> """<STR_LIT>""" <EOL> if iscsi_string : <EOL> host = iscsi_string . split ( '<STR_LIT::>' ) [ <NUM_LIT:0> ] <EOL> if len ( host ) > <NUM_LIT:0> : <EOL> return host <EOL> return CONF . xenserver . target_host <EOL> def _get_target_port ( iscsi_string ) : <EOL> """<STR_LIT>""" <EOL> if iscsi_string and '<STR_LIT::>' in iscsi_string : <EOL> return iscsi_string . split ( '<STR_LIT::>' ) [ <NUM_LIT:1> ] <EOL> return CONF . xenserver . target_port <EOL> def introduce_sr ( session , sr_uuid , label , params ) : <EOL> LOG . debug ( '<STR_LIT>' , label ) <EOL> sr_type , sr_desc = _handle_sr_params ( params ) <EOL> if _requires_backend_kind ( session . product_version ) and sr_type == '<STR_LIT>' : <EOL> params [ '<STR_LIT>' ] = '<STR_LIT>' <EOL> sr_ref = session . call_xenapi ( '<STR_LIT>' , sr_uuid , label , sr_desc , <EOL> sr_type , '<STR_LIT>' , False , params ) <EOL> LOG . debug ( '<STR_LIT>' ) <EOL> pbd_ref = _create_pbd ( session , sr_ref , params ) <EOL> LOG . debug ( '<STR_LIT>' ) <EOL> session . call_xenapi ( "<STR_LIT>" , pbd_ref ) <EOL> session . call_xenapi ( "<STR_LIT>" , sr_ref ) <EOL> return sr_ref <EOL> def _requires_backend_kind ( version ) : <EOL> version_as_string = '<STR_LIT:.>' . join ( str ( v ) for v in version ) <EOL> return ( versionutils . is_compatible ( '<STR_LIT>' , version_as_string ) ) <EOL> def _handle_sr_params ( params ) : <EOL> if '<STR_LIT:id>' in params : <EOL> del params [ '<STR_LIT:id>' ] <EOL> sr_type = params . pop ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> sr_desc = params . pop ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> return sr_type , sr_desc <EOL> def _create_pbd ( session , sr_ref , params ) : <EOL> pbd_rec = { } <EOL> pbd_rec [ '<STR_LIT:host>' ] = session . host_ref <EOL> pbd_rec [ '<STR_LIT>' ] = sr_ref <EOL> pbd_rec [ '<STR_LIT>' ] = params <EOL> pbd_ref = session . call_xenapi ( "<STR_LIT>" , pbd_rec ) <EOL> return pbd_ref <EOL> def introduce_vdi ( session , sr_ref , vdi_uuid = None , target_lun = None ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> vdi_ref = _get_vdi_ref ( session , sr_ref , vdi_uuid , target_lun ) <EOL> if vdi_ref is None : <EOL> greenthread . sleep ( CONF . xenserver . introduce_vdi_retry_wait ) <EOL> session . call_xenapi ( "<STR_LIT>" , sr_ref ) <EOL> vdi_ref = _get_vdi_ref ( session , sr_ref , vdi_uuid , target_lun ) <EOL> except session . XenAPI . Failure : <EOL> LOG . exception ( _LE ( '<STR_LIT>' ) ) <EOL> raise exception . StorageError ( <EOL> reason = _ ( '<STR_LIT>' ) % sr_ref ) <EOL> if not vdi_ref : <EOL> raise exception . StorageError ( <EOL> reason = _ ( '<STR_LIT>' <EOL> '<STR_LIT>' ) % <EOL> { '<STR_LIT>' : sr_ref , '<STR_LIT>' : vdi_uuid , <EOL> '<STR_LIT>' : target_lun } ) <EOL> try : <EOL> vdi_rec = session . call_xenapi ( "<STR_LIT>" , vdi_ref ) <EOL> LOG . debug ( vdi_rec ) <EOL> except session . XenAPI . Failure : <EOL> LOG . exception ( _LE ( '<STR_LIT>' ) ) <EOL> raise exception . StorageError ( <EOL> reason = _ ( '<STR_LIT>' ) % vdi_ref ) <EOL> if vdi_rec [ '<STR_LIT>' ] : <EOL> return vdi_ref <EOL> try : <EOL> return session . call_xenapi ( "<STR_LIT>" , <EOL> vdi_rec [ '<STR_LIT>' ] , <EOL> vdi_rec [ '<STR_LIT>' ] , <EOL> vdi_rec [ '<STR_LIT>' ] , <EOL> vdi_rec [ '<STR_LIT>' ] , <EOL> vdi_rec [ '<STR_LIT:type>' ] , <EOL> vdi_rec [ '<STR_LIT>' ] , <EOL> vdi_rec [ '<STR_LIT>' ] , <EOL> vdi_rec [ '<STR_LIT>' ] , <EOL> vdi_rec [ '<STR_LIT:location>' ] , <EOL> vdi_rec [ '<STR_LIT>' ] , <EOL> vdi_rec [ '<STR_LIT>' ] ) <EOL> except session . XenAPI . Failure : <EOL> LOG . exception ( _LE ( '<STR_LIT>' ) ) <EOL> raise exception . StorageError ( <EOL> reason = _ ( '<STR_LIT>' ) % sr_ref ) <EOL> def _get_vdi_ref ( session , sr_ref , vdi_uuid , target_lun ) : <EOL> if vdi_uuid : <EOL> LOG . debug ( "<STR_LIT>" , vdi_uuid ) <EOL> return session . call_xenapi ( "<STR_LIT>" , vdi_uuid ) <EOL> elif target_lun : <EOL> vdi_refs = session . call_xenapi ( "<STR_LIT>" , sr_ref ) <EOL> for curr_ref in vdi_refs : <EOL> curr_rec = session . call_xenapi ( "<STR_LIT>" , curr_ref ) <EOL> if ( '<STR_LIT>' in curr_rec and <EOL> '<STR_LIT>' in curr_rec [ '<STR_LIT>' ] and <EOL> curr_rec [ '<STR_LIT>' ] [ '<STR_LIT>' ] == str ( target_lun ) ) : <EOL> return curr_ref <EOL> else : <EOL> return ( session . call_xenapi ( "<STR_LIT>" , sr_ref ) ) [ <NUM_LIT:0> ] <EOL> return None <EOL> def purge_sr ( session , sr_ref ) : <EOL> vdi_refs = session . call_xenapi ( "<STR_LIT>" , sr_ref ) <EOL> for vdi_ref in vdi_refs : <EOL> vbd_refs = session . call_xenapi ( "<STR_LIT>" , vdi_ref ) <EOL> if vbd_refs : <EOL> LOG . warning ( _LW ( '<STR_LIT>' ) ) <EOL> return <EOL> forget_sr ( session , sr_ref ) <EOL> def forget_sr ( session , sr_ref ) : <EOL> """<STR_LIT>""" <EOL> LOG . debug ( '<STR_LIT>' ) <EOL> _unplug_pbds ( session , sr_ref ) <EOL> session . call_xenapi ( "<STR_LIT>" , sr_ref ) <EOL> def _unplug_pbds ( session , sr_ref ) : <EOL> try : <EOL> pbds = session . call_xenapi ( "<STR_LIT>" , sr_ref ) <EOL> except session . XenAPI . Failure as exc : <EOL> LOG . warning ( _LW ( '<STR_LIT>' <EOL> '<STR_LIT>' ) , { '<STR_LIT>' : exc , '<STR_LIT>' : sr_ref } ) <EOL> return <EOL> for pbd in pbds : <EOL> try : <EOL> session . call_xenapi ( "<STR_LIT>" , pbd ) <EOL> except session . XenAPI . Failure as exc : <EOL> LOG . warning ( _LW ( '<STR_LIT>' <EOL> '<STR_LIT>' ) , { '<STR_LIT>' : exc , '<STR_LIT>' : pbd } ) <EOL> def get_device_number ( mountpoint ) : <EOL> device_number = _mountpoint_to_number ( mountpoint ) <EOL> if device_number < <NUM_LIT:0> : <EOL> raise exception . StorageError ( <EOL> reason = _ ( '<STR_LIT>' ) % <EOL> mountpoint ) <EOL> return device_number <EOL> def _mountpoint_to_number ( mountpoint ) : <EOL> """<STR_LIT>""" <EOL> if mountpoint . startswith ( '<STR_LIT>' ) : <EOL> mountpoint = mountpoint [ <NUM_LIT:5> : ] <EOL> if re . match ( '<STR_LIT>' , mountpoint ) : <EOL> return ( ord ( mountpoint [ <NUM_LIT:2> : <NUM_LIT:3> ] ) - ord ( '<STR_LIT:a>' ) ) <EOL> elif re . match ( '<STR_LIT>' , mountpoint ) : <EOL> return ( ord ( mountpoint [ - <NUM_LIT:1> ] ) - ord ( '<STR_LIT:a>' ) ) <EOL> elif re . match ( '<STR_LIT>' , mountpoint ) : <EOL> return string . atoi ( mountpoint , <NUM_LIT:10> ) <EOL> else : <EOL> LOG . warning ( _LW ( '<STR_LIT>' ) , mountpoint ) <EOL> return - <NUM_LIT:1> <EOL> def find_sr_by_uuid ( session , sr_uuid ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> return session . call_xenapi ( "<STR_LIT>" , sr_uuid ) <EOL> except session . XenAPI . Failure as exc : <EOL> if exc . details [ <NUM_LIT:0> ] == '<STR_LIT>' : <EOL> return None <EOL> raise <EOL> def find_sr_from_vbd ( session , vbd_ref ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> vdi_ref = session . call_xenapi ( "<STR_LIT>" , vbd_ref ) <EOL> sr_ref = session . call_xenapi ( "<STR_LIT>" , vdi_ref ) <EOL> except session . XenAPI . Failure : <EOL> LOG . exception ( _LE ( '<STR_LIT>' ) ) <EOL> raise exception . StorageError ( <EOL> reason = _ ( '<STR_LIT>' ) % vbd_ref ) <EOL> return sr_ref <EOL> def find_sr_from_vdi ( session , vdi_ref ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> sr_ref = session . call_xenapi ( "<STR_LIT>" , vdi_ref ) <EOL> except session . XenAPI . Failure : <EOL> LOG . exception ( _LE ( '<STR_LIT>' ) ) <EOL> raise exception . StorageError ( <EOL> reason = _ ( '<STR_LIT>' ) % vdi_ref ) <EOL> return sr_ref <EOL> def find_vbd_by_number ( session , vm_ref , dev_number ) : <EOL> """<STR_LIT>""" <EOL> vbd_refs = session . VM . get_VBDs ( vm_ref ) <EOL> requested_device = str ( dev_number ) <EOL> if vbd_refs : <EOL> for vbd_ref in vbd_refs : <EOL> try : <EOL> user_device = session . VBD . get_userdevice ( vbd_ref ) <EOL> if user_device == requested_device : <EOL> return vbd_ref <EOL> except session . XenAPI . Failure : <EOL> msg = "<STR_LIT>" % ( vbd_ref , vm_ref ) <EOL> LOG . debug ( msg , exc_info = True ) <EOL> def is_booted_from_volume ( session , vm_ref ) : <EOL> """<STR_LIT>""" <EOL> vbd_ref = find_vbd_by_number ( session , vm_ref , <NUM_LIT:0> ) <EOL> vbd_other_config = session . VBD . get_other_config ( vbd_ref ) <EOL> if vbd_other_config . get ( '<STR_LIT>' , False ) : <EOL> return True <EOL> return False </s>
