<s> from ironic_lib import disk_utils <EOL> from oslo_concurrency import processutils <EOL> from oslo_log import log <EOL> from oslo_utils import uuidutils <EOL> try : <EOL> import rtslib_fb <EOL> except ImportError : <EOL> import rtslib as rtslib_fb <EOL> from ironic_python_agent import errors <EOL> from ironic_python_agent . extensions import base <EOL> from ironic_python_agent import hardware <EOL> from ironic_python_agent import utils <EOL> LOG = log . getLogger ( __name__ ) <EOL> DEFAULT_ISCSI_PORTAL_PORT = <NUM_LIT> <EOL> def _execute ( cmd , error_msg , ** kwargs ) : <EOL> try : <EOL> stdout , stderr = utils . execute ( * cmd , ** kwargs ) <EOL> except ( processutils . ProcessExecutionError , OSError ) as e : <EOL> LOG . error ( error_msg ) <EOL> raise errors . ISCSICommandError ( error_msg , e . exit_code , <EOL> e . stdout , e . stderr ) <EOL> def _wait_for_tgtd ( attempts = <NUM_LIT:10> ) : <EOL> """<STR_LIT>""" <EOL> cmd = [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT:target>' , '<STR_LIT>' , '<STR_LIT>' ] <EOL> _execute ( cmd , "<STR_LIT>" , attempts = attempts ) <EOL> def _start_tgtd ( iqn , portal_port , device ) : <EOL> """<STR_LIT>""" <EOL> _execute ( [ '<STR_LIT>' ] , "<STR_LIT>" ) <EOL> _wait_for_tgtd ( ) <EOL> if portal_port != DEFAULT_ISCSI_PORTAL_PORT : <EOL> cmd = [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' + str ( portal_port ) ] <EOL> _execute ( cmd , "<STR_LIT>" <EOL> % portal_port ) <EOL> cmd = [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT:target>' , '<STR_LIT>' , <EOL> '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT:1>' , '<STR_LIT>' , iqn ] <EOL> _execute ( cmd , "<STR_LIT>" % iqn ) <EOL> cmd = [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT:1>' , '<STR_LIT>' , '<STR_LIT:1>' , '<STR_LIT>' , device ] <EOL> _execute ( cmd , "<STR_LIT>" % iqn ) <EOL> cmd = [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT:target>' , '<STR_LIT>' , <EOL> '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT:1>' , '<STR_LIT>' , '<STR_LIT>' ] <EOL> _execute ( cmd , "<STR_LIT>" <EOL> "<STR_LIT>" % iqn ) <EOL> def _start_lio ( iqn , portal_port , device ) : <EOL> try : <EOL> storage = rtslib_fb . BlockStorageObject ( name = iqn , dev = device ) <EOL> target = rtslib_fb . Target ( rtslib_fb . FabricModule ( '<STR_LIT>' ) , iqn , <EOL> mode = '<STR_LIT>' ) <EOL> tpg = rtslib_fb . TPG ( target , mode = '<STR_LIT>' ) <EOL> tpg . set_attribute ( '<STR_LIT>' , '<STR_LIT:0>' ) <EOL> tpg . set_attribute ( '<STR_LIT>' , '<STR_LIT:0>' ) <EOL> tpg . set_attribute ( '<STR_LIT>' , '<STR_LIT:1>' ) <EOL> rtslib_fb . LUN ( tpg , storage_object = storage , lun = <NUM_LIT:1> ) <EOL> tpg . enable = <NUM_LIT:1> <EOL> except rtslib_fb . utils . RTSLibError as exc : <EOL> msg = '<STR_LIT>' . format ( exc ) <EOL> raise errors . ISCSIError ( msg ) <EOL> try : <EOL> rtslib_fb . NetworkPortal ( tpg , '<STR_LIT>' , portal_port ) <EOL> except rtslib_fb . utils . RTSLibError as exc : <EOL> msg = '<STR_LIT>' . format ( exc ) <EOL> raise errors . ISCSIError ( msg ) <EOL> def clean_up ( device ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> rts_root = rtslib_fb . RTSRoot ( ) <EOL> except ( EnvironmentError , rtslib_fb . RTSLibError ) as exc : <EOL> LOG . info ( '<STR_LIT>' , exc ) <EOL> return <EOL> storage = None <EOL> for x in rts_root . storage_objects : <EOL> if x . udev_path == device : <EOL> storage = x <EOL> break <EOL> if storage is None : <EOL> LOG . info ( '<STR_LIT>' <EOL> '<STR_LIT>' , <EOL> { '<STR_LIT>' : device , <EOL> '<STR_LIT>' : [ x . udev_path for x in rts_root . storage_objects ] } ) <EOL> return <EOL> else : <EOL> LOG . info ( '<STR_LIT>' , <EOL> { '<STR_LIT:target>' : storage . name , '<STR_LIT>' : device } ) <EOL> try : <EOL> for x in rts_root . targets : <EOL> if x . wwn == storage . name : <EOL> x . delete ( ) <EOL> break <EOL> storage . delete ( ) <EOL> except rtslib_fb . utils . RTSLibError as exc : <EOL> msg = ( '<STR_LIT>' <EOL> '<STR_LIT>' ) % { '<STR_LIT:target>' : storage . name , <EOL> '<STR_LIT>' : device , <EOL> '<STR_LIT:error>' : exc } <EOL> raise errors . ISCSIError ( msg ) <EOL> class ISCSIExtension ( base . BaseAgentExtension ) : <EOL> @ base . sync_command ( '<STR_LIT>' ) <EOL> def start_iscsi_target ( self , iqn = None , wipe_disk_metadata = False , <EOL> portal_port = None ) : <EOL> """<STR_LIT>""" <EOL> if iqn is None : <EOL> iqn = '<STR_LIT>' % uuidutils . generate_uuid ( ) <EOL> device = hardware . dispatch_to_managers ( '<STR_LIT>' ) <EOL> if wipe_disk_metadata : <EOL> disk_utils . destroy_disk_metadata ( <EOL> device , <EOL> self . agent . get_node_uuid ( ) ) <EOL> LOG . debug ( "<STR_LIT>" <EOL> "<STR_LIT>" , { '<STR_LIT>' : iqn , '<STR_LIT>' : device } ) <EOL> try : <EOL> rts_root = rtslib_fb . RTSRoot ( ) <EOL> except ( EnvironmentError , rtslib_fb . RTSLibError ) as exc : <EOL> LOG . warning ( '<STR_LIT>' <EOL> '<STR_LIT>' , exc ) <EOL> rts_root = None <EOL> if portal_port is None : <EOL> portal_port = DEFAULT_ISCSI_PORTAL_PORT <EOL> if rts_root is None : <EOL> _start_tgtd ( iqn , portal_port , device ) <EOL> else : <EOL> _start_lio ( iqn , portal_port , device ) <EOL> LOG . debug ( '<STR_LIT>' , rts_root . dump ( ) ) <EOL> LOG . info ( '<STR_LIT>' <EOL> '<STR_LIT>' , <EOL> { '<STR_LIT>' : iqn , '<STR_LIT:port>' : portal_port , '<STR_LIT>' : device , <EOL> '<STR_LIT>' : '<STR_LIT>' if rts_root is None else '<STR_LIT>' } ) <EOL> return { "<STR_LIT>" : iqn } </s>
