<s> """<STR_LIT>""" <EOL> import os <EOL> from ironic_lib import utils as ironic_utils <EOL> from oslo_config import cfg <EOL> from oslo_log import log as logging <EOL> from oslo_utils import excutils <EOL> from oslo_utils import importutils <EOL> from oslo_utils import strutils <EOL> from ironic . common import boot_devices <EOL> from ironic . common import exception <EOL> from ironic . common . i18n import _ <EOL> from ironic . common . i18n import _LE <EOL> from ironic . common . i18n import _LW <EOL> from ironic . common import states <EOL> from ironic . common import utils <EOL> from ironic . conductor import task_manager <EOL> from ironic . drivers import base <EOL> from ironic . drivers . modules import console_utils <EOL> from ironic . drivers import utils as driver_utils <EOL> pyghmi = importutils . try_import ( '<STR_LIT>' ) <EOL> if pyghmi : <EOL> from pyghmi import exceptions as pyghmi_exception <EOL> from pyghmi . ipmi import command as ipmi_command <EOL> opts = [ <EOL> cfg . IntOpt ( '<STR_LIT>' , <EOL> default = <NUM_LIT> , <EOL> help = _ ( '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' ) ) , <EOL> cfg . IntOpt ( '<STR_LIT>' , <EOL> default = <NUM_LIT:5> , <EOL> help = _ ( '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' ) ) , <EOL> ] <EOL> CONF = cfg . CONF <EOL> CONF . register_opts ( opts , group = '<STR_LIT>' ) <EOL> LOG = logging . getLogger ( __name__ ) <EOL> REQUIRED_PROPERTIES = { '<STR_LIT>' : _ ( "<STR_LIT>" ) , <EOL> '<STR_LIT>' : _ ( "<STR_LIT>" ) , <EOL> '<STR_LIT>' : _ ( "<STR_LIT>" ) } <EOL> OPTIONAL_PROPERTIES = { <EOL> '<STR_LIT>' : _ ( "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" ) <EOL> } <EOL> COMMON_PROPERTIES = REQUIRED_PROPERTIES . copy ( ) <EOL> COMMON_PROPERTIES . update ( OPTIONAL_PROPERTIES ) <EOL> CONSOLE_PROPERTIES = { <EOL> '<STR_LIT>' : _ ( "<STR_LIT>" <EOL> "<STR_LIT>" ) <EOL> } <EOL> _BOOT_DEVICES_MAP = { <EOL> boot_devices . DISK : '<STR_LIT>' , <EOL> boot_devices . PXE : '<STR_LIT>' , <EOL> boot_devices . CDROM : '<STR_LIT>' , <EOL> boot_devices . BIOS : '<STR_LIT>' , <EOL> } <EOL> def _parse_driver_info ( node ) : <EOL> """<STR_LIT>""" <EOL> info = node . driver_info or { } <EOL> missing_info = [ key for key in REQUIRED_PROPERTIES if not info . get ( key ) ] <EOL> if missing_info : <EOL> raise exception . MissingParameterValue ( _ ( <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" ) % missing_info ) <EOL> bmc_info = { } <EOL> bmc_info [ '<STR_LIT:address>' ] = info . get ( '<STR_LIT>' ) <EOL> bmc_info [ '<STR_LIT:username>' ] = info . get ( '<STR_LIT>' ) <EOL> bmc_info [ '<STR_LIT:password>' ] = info . get ( '<STR_LIT>' ) <EOL> bmc_info [ '<STR_LIT>' ] = info . get ( '<STR_LIT>' , False ) <EOL> bmc_info [ '<STR_LIT>' ] = node . uuid <EOL> port = info . get ( '<STR_LIT>' ) <EOL> if port is not None : <EOL> port = utils . validate_network_port ( port , '<STR_LIT>' ) <EOL> bmc_info [ '<STR_LIT:port>' ] = port <EOL> return bmc_info <EOL> def _console_pwfile_path ( uuid ) : <EOL> """<STR_LIT>""" <EOL> file_name = "<STR_LIT>" % { '<STR_LIT>' : uuid } <EOL> return os . path . join ( CONF . tempdir , file_name ) <EOL> def _power_on ( driver_info ) : <EOL> """<STR_LIT>""" <EOL> msg = _ ( "<STR_LIT>" <EOL> "<STR_LIT>" ) <EOL> try : <EOL> ipmicmd = ipmi_command . Command ( bmc = driver_info [ '<STR_LIT:address>' ] , <EOL> userid = driver_info [ '<STR_LIT:username>' ] , <EOL> password = driver_info [ '<STR_LIT:password>' ] ) <EOL> wait = CONF . ipmi . retry_timeout <EOL> ret = ipmicmd . set_power ( '<STR_LIT>' , wait ) <EOL> except pyghmi_exception . IpmiException as e : <EOL> error = msg % { '<STR_LIT>' : driver_info [ '<STR_LIT>' ] , '<STR_LIT:error>' : e } <EOL> LOG . error ( error ) <EOL> raise exception . IPMIFailure ( error ) <EOL> state = ret . get ( '<STR_LIT>' ) <EOL> if state == '<STR_LIT>' : <EOL> return states . POWER_ON <EOL> else : <EOL> error = _ ( "<STR_LIT>" ) % ret <EOL> LOG . error ( msg , { '<STR_LIT>' : driver_info [ '<STR_LIT>' ] , '<STR_LIT:error>' : error } ) <EOL> raise exception . PowerStateFailure ( pstate = states . POWER_ON ) <EOL> def _power_off ( driver_info ) : <EOL> """<STR_LIT>""" <EOL> msg = _ ( "<STR_LIT>" <EOL> "<STR_LIT>" ) <EOL> try : <EOL> ipmicmd = ipmi_command . Command ( bmc = driver_info [ '<STR_LIT:address>' ] , <EOL> userid = driver_info [ '<STR_LIT:username>' ] , <EOL> password = driver_info [ '<STR_LIT:password>' ] ) <EOL> wait = CONF . ipmi . retry_timeout <EOL> ret = ipmicmd . set_power ( '<STR_LIT>' , wait ) <EOL> except pyghmi_exception . IpmiException as e : <EOL> error = msg % { '<STR_LIT>' : driver_info [ '<STR_LIT>' ] , '<STR_LIT:error>' : e } <EOL> LOG . error ( error ) <EOL> raise exception . IPMIFailure ( error ) <EOL> state = ret . get ( '<STR_LIT>' ) <EOL> if state == '<STR_LIT>' : <EOL> return states . POWER_OFF <EOL> else : <EOL> error = _ ( "<STR_LIT>" ) % ret <EOL> LOG . error ( msg , { '<STR_LIT>' : driver_info [ '<STR_LIT>' ] , '<STR_LIT:error>' : error } ) <EOL> raise exception . PowerStateFailure ( pstate = states . POWER_OFF ) <EOL> def _reboot ( driver_info ) : <EOL> """<STR_LIT>""" <EOL> msg = _ ( "<STR_LIT>" <EOL> "<STR_LIT>" ) <EOL> try : <EOL> ipmicmd = ipmi_command . Command ( bmc = driver_info [ '<STR_LIT:address>' ] , <EOL> userid = driver_info [ '<STR_LIT:username>' ] , <EOL> password = driver_info [ '<STR_LIT:password>' ] ) <EOL> wait = CONF . ipmi . retry_timeout <EOL> ret = ipmicmd . set_power ( '<STR_LIT>' , wait ) <EOL> except pyghmi_exception . IpmiException as e : <EOL> error = msg % { '<STR_LIT>' : driver_info [ '<STR_LIT>' ] , '<STR_LIT:error>' : e } <EOL> LOG . error ( error ) <EOL> raise exception . IPMIFailure ( error ) <EOL> if '<STR_LIT:error>' in ret : <EOL> error = _ ( "<STR_LIT>" ) % ret <EOL> LOG . error ( msg , { '<STR_LIT>' : driver_info [ '<STR_LIT>' ] , '<STR_LIT:error>' : error } ) <EOL> raise exception . PowerStateFailure ( pstate = states . REBOOT ) <EOL> return states . POWER_ON <EOL> def _power_status ( driver_info ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> ipmicmd = ipmi_command . Command ( bmc = driver_info [ '<STR_LIT:address>' ] , <EOL> userid = driver_info [ '<STR_LIT:username>' ] , <EOL> password = driver_info [ '<STR_LIT:password>' ] ) <EOL> ret = ipmicmd . get_power ( ) <EOL> except pyghmi_exception . IpmiException as e : <EOL> msg = ( _ ( "<STR_LIT>" <EOL> "<STR_LIT>" ) % <EOL> { '<STR_LIT>' : driver_info [ '<STR_LIT>' ] , '<STR_LIT:error>' : e } ) <EOL> LOG . error ( msg ) <EOL> raise exception . IPMIFailure ( msg ) <EOL> state = ret . get ( '<STR_LIT>' ) <EOL> if state == '<STR_LIT>' : <EOL> return states . POWER_ON <EOL> elif state == '<STR_LIT>' : <EOL> return states . POWER_OFF <EOL> else : <EOL> LOG . warning ( _LW ( "<STR_LIT>" <EOL> "<STR_LIT>" ) , <EOL> { '<STR_LIT>' : driver_info [ '<STR_LIT>' ] , '<STR_LIT>' : ret } ) <EOL> return states . ERROR <EOL> def _get_sensors_data ( driver_info ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> ipmicmd = ipmi_command . Command ( bmc = driver_info [ '<STR_LIT:address>' ] , <EOL> userid = driver_info [ '<STR_LIT:username>' ] , <EOL> password = driver_info [ '<STR_LIT:password>' ] ) <EOL> ret = ipmicmd . get_sensor_data ( ) <EOL> except Exception as e : <EOL> LOG . error ( _LE ( "<STR_LIT>" <EOL> "<STR_LIT>" ) , <EOL> { '<STR_LIT>' : driver_info [ '<STR_LIT>' ] , '<STR_LIT:error>' : e } ) <EOL> raise exception . FailedToGetSensorData ( <EOL> node = driver_info [ '<STR_LIT>' ] , error = e ) <EOL> if not ret : <EOL> return { } <EOL> sensors_data = { } <EOL> for reading in ret : <EOL> if not reading . value : <EOL> continue <EOL> sensors_data . setdefault ( <EOL> reading . type , <EOL> { } ) [ reading . name ] = { <EOL> '<STR_LIT>' : '<STR_LIT>' % ( reading . value , reading . units ) , <EOL> '<STR_LIT>' : reading . name , <EOL> '<STR_LIT>' : str ( reading . states ) , <EOL> '<STR_LIT>' : reading . units , <EOL> '<STR_LIT>' : str ( reading . health ) } <EOL> return sensors_data <EOL> def _parse_raw_bytes ( raw_bytes ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> bytes_list = [ int ( x , base = <NUM_LIT:16> ) for x in raw_bytes . split ( ) ] <EOL> return bytes_list [ <NUM_LIT:0> ] , bytes_list [ <NUM_LIT:1> ] , bytes_list [ <NUM_LIT:2> : ] <EOL> except ValueError : <EOL> raise exception . InvalidParameterValue ( _ ( <EOL> "<STR_LIT>" ) % raw_bytes ) <EOL> except IndexError : <EOL> raise exception . InvalidParameterValue ( _ ( <EOL> "<STR_LIT>" ) ) <EOL> def _send_raw ( driver_info , raw_bytes ) : <EOL> """<STR_LIT>""" <EOL> netfn , command , data = _parse_raw_bytes ( raw_bytes ) <EOL> LOG . debug ( "<STR_LIT>" , <EOL> { '<STR_LIT>' : raw_bytes , '<STR_LIT>' : driver_info [ '<STR_LIT>' ] } ) <EOL> try : <EOL> ipmicmd = ipmi_command . Command ( bmc = driver_info [ '<STR_LIT:address>' ] , <EOL> userid = driver_info [ '<STR_LIT:username>' ] , <EOL> password = driver_info [ '<STR_LIT:password>' ] ) <EOL> ipmicmd . xraw_command ( netfn , command , data = data ) <EOL> except pyghmi_exception . IpmiException as e : <EOL> msg = ( _ ( "<STR_LIT>" <EOL> "<STR_LIT>" ) % <EOL> { '<STR_LIT>' : raw_bytes , '<STR_LIT>' : driver_info [ '<STR_LIT>' ] , <EOL> '<STR_LIT:error>' : e } ) <EOL> LOG . error ( msg ) <EOL> raise exception . IPMIFailure ( msg ) <EOL> class NativeIPMIPower ( base . PowerInterface ) : <EOL> """<STR_LIT>""" <EOL> def get_properties ( self ) : <EOL> return COMMON_PROPERTIES <EOL> def validate ( self , task ) : <EOL> """<STR_LIT>""" <EOL> _parse_driver_info ( task . node ) <EOL> def get_power_state ( self , task ) : <EOL> """<STR_LIT>""" <EOL> driver_info = _parse_driver_info ( task . node ) <EOL> return _power_status ( driver_info ) <EOL> @ task_manager . require_exclusive_lock <EOL> def set_power_state ( self , task , pstate ) : <EOL> """<STR_LIT>""" <EOL> driver_info = _parse_driver_info ( task . node ) <EOL> if pstate == states . POWER_ON : <EOL> driver_utils . ensure_next_boot_device ( task , driver_info ) <EOL> _power_on ( driver_info ) <EOL> elif pstate == states . POWER_OFF : <EOL> _power_off ( driver_info ) <EOL> else : <EOL> raise exception . InvalidParameterValue ( <EOL> _ ( "<STR_LIT>" <EOL> ) % pstate ) <EOL> @ task_manager . require_exclusive_lock <EOL> def reboot ( self , task ) : <EOL> """<STR_LIT>""" <EOL> driver_info = _parse_driver_info ( task . node ) <EOL> driver_utils . ensure_next_boot_device ( task , driver_info ) <EOL> _reboot ( driver_info ) <EOL> class NativeIPMIManagement ( base . ManagementInterface ) : <EOL> def get_properties ( self ) : <EOL> return COMMON_PROPERTIES <EOL> def validate ( self , task ) : <EOL> """<STR_LIT>""" <EOL> _parse_driver_info ( task . node ) <EOL> def get_supported_boot_devices ( self , task ) : <EOL> """<STR_LIT>""" <EOL> return list ( _BOOT_DEVICES_MAP . keys ( ) ) <EOL> @ task_manager . require_exclusive_lock <EOL> def set_boot_device ( self , task , device , persistent = False ) : <EOL> """<STR_LIT>""" <EOL> if device not in self . get_supported_boot_devices ( task ) : <EOL> raise exception . InvalidParameterValue ( _ ( <EOL> "<STR_LIT>" ) % device ) <EOL> if task . node . driver_info . get ( '<STR_LIT>' , False ) : <EOL> driver_utils . force_persistent_boot ( task , <EOL> device , <EOL> persistent ) <EOL> persistent = False <EOL> driver_info = _parse_driver_info ( task . node ) <EOL> try : <EOL> ipmicmd = ipmi_command . Command ( bmc = driver_info [ '<STR_LIT:address>' ] , <EOL> userid = driver_info [ '<STR_LIT:username>' ] , <EOL> password = driver_info [ '<STR_LIT:password>' ] ) <EOL> bootdev = _BOOT_DEVICES_MAP [ device ] <EOL> ipmicmd . set_bootdev ( bootdev , persist = persistent ) <EOL> except pyghmi_exception . IpmiException as e : <EOL> LOG . error ( _LE ( "<STR_LIT>" <EOL> "<STR_LIT>" ) , <EOL> { '<STR_LIT>' : driver_info [ '<STR_LIT>' ] , '<STR_LIT:error>' : e } ) <EOL> raise exception . IPMIFailure ( cmd = e ) <EOL> def get_boot_device ( self , task ) : <EOL> """<STR_LIT>""" <EOL> driver_info = task . node . driver_info <EOL> driver_internal_info = task . node . driver_internal_info <EOL> if ( driver_info . get ( '<STR_LIT>' , False ) and <EOL> driver_internal_info . get ( '<STR_LIT>' ) and <EOL> driver_internal_info . get ( '<STR_LIT>' , True ) ) : <EOL> return { <EOL> '<STR_LIT>' : driver_internal_info [ '<STR_LIT>' ] , <EOL> '<STR_LIT>' : True <EOL> } <EOL> driver_info = _parse_driver_info ( task . node ) <EOL> response = { '<STR_LIT>' : None } <EOL> try : <EOL> ipmicmd = ipmi_command . Command ( bmc = driver_info [ '<STR_LIT:address>' ] , <EOL> userid = driver_info [ '<STR_LIT:username>' ] , <EOL> password = driver_info [ '<STR_LIT:password>' ] ) <EOL> ret = ipmicmd . get_bootdev ( ) <EOL> if '<STR_LIT:error>' in ret : <EOL> raise pyghmi_exception . IpmiException ( ret [ '<STR_LIT:error>' ] ) <EOL> except pyghmi_exception . IpmiException as e : <EOL> LOG . error ( _LE ( "<STR_LIT>" <EOL> "<STR_LIT>" ) , <EOL> { '<STR_LIT>' : driver_info [ '<STR_LIT>' ] , '<STR_LIT:error>' : e } ) <EOL> raise exception . IPMIFailure ( cmd = e ) <EOL> response [ '<STR_LIT>' ] = ret . get ( '<STR_LIT>' ) <EOL> bootdev = ret . get ( '<STR_LIT>' ) <EOL> if bootdev : <EOL> response [ '<STR_LIT>' ] = next ( ( dev for dev , hdev in <EOL> _BOOT_DEVICES_MAP . items ( ) <EOL> if hdev == bootdev ) , None ) <EOL> return response <EOL> def get_sensors_data ( self , task ) : <EOL> """<STR_LIT>""" <EOL> driver_info = _parse_driver_info ( task . node ) <EOL> return _get_sensors_data ( driver_info ) <EOL> class NativeIPMIShellinaboxConsole ( base . ConsoleInterface ) : <EOL> """<STR_LIT>""" <EOL> def get_properties ( self ) : <EOL> d = COMMON_PROPERTIES . copy ( ) <EOL> d . update ( CONSOLE_PROPERTIES ) <EOL> return d <EOL> def validate ( self , task ) : <EOL> """<STR_LIT>""" <EOL> driver_info = _parse_driver_info ( task . node ) <EOL> if not driver_info [ '<STR_LIT:port>' ] : <EOL> raise exception . MissingParameterValue ( _ ( <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" ) ) <EOL> def start_console ( self , task ) : <EOL> """<STR_LIT>""" <EOL> driver_info = _parse_driver_info ( task . node ) <EOL> path = _console_pwfile_path ( driver_info [ '<STR_LIT>' ] ) <EOL> pw_file = console_utils . make_persistent_password_file ( <EOL> path , driver_info [ '<STR_LIT:password>' ] ) <EOL> console_cmd = ( "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> % { '<STR_LIT>' : os . getuid ( ) , <EOL> '<STR_LIT>' : os . getgid ( ) , <EOL> '<STR_LIT>' : driver_info [ '<STR_LIT:address>' ] , <EOL> '<STR_LIT:user>' : driver_info [ '<STR_LIT:username>' ] , <EOL> '<STR_LIT>' : pw_file } ) <EOL> try : <EOL> console_utils . start_shellinabox_console ( driver_info [ '<STR_LIT>' ] , <EOL> driver_info [ '<STR_LIT:port>' ] , <EOL> console_cmd ) <EOL> except exception . ConsoleError : <EOL> with excutils . save_and_reraise_exception ( ) : <EOL> ironic_utils . unlink_without_raise ( path ) <EOL> def stop_console ( self , task ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> console_utils . stop_shellinabox_console ( task . node . uuid ) <EOL> finally : <EOL> password_file = _console_pwfile_path ( task . node . uuid ) <EOL> ironic_utils . unlink_without_raise ( password_file ) <EOL> def get_console ( self , task ) : <EOL> """<STR_LIT>""" <EOL> driver_info = _parse_driver_info ( task . node ) <EOL> url = console_utils . get_shellinabox_console_url ( driver_info [ '<STR_LIT:port>' ] ) <EOL> return { '<STR_LIT:type>' : '<STR_LIT>' , '<STR_LIT:url>' : url } <EOL> class VendorPassthru ( base . VendorInterface ) : <EOL> def get_properties ( self ) : <EOL> return COMMON_PROPERTIES <EOL> def validate ( self , task , method , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> if method == '<STR_LIT>' : <EOL> raw_bytes = kwargs . get ( '<STR_LIT>' ) <EOL> if not raw_bytes : <EOL> raise exception . MissingParameterValue ( _ ( <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' ) ) <EOL> _parse_raw_bytes ( raw_bytes ) <EOL> _parse_driver_info ( task . node ) <EOL> @ base . passthru ( [ '<STR_LIT:POST>' ] ) <EOL> @ task_manager . require_exclusive_lock <EOL> def send_raw ( self , task , http_method , raw_bytes ) : <EOL> """<STR_LIT>""" <EOL> driver_info = _parse_driver_info ( task . node ) <EOL> _send_raw ( driver_info , raw_bytes ) <EOL> @ base . passthru ( [ '<STR_LIT:POST>' ] ) <EOL> @ task_manager . require_exclusive_lock <EOL> def bmc_reset ( self , task , http_method , warm = True ) : <EOL> """<STR_LIT>""" <EOL> driver_info = _parse_driver_info ( task . node ) <EOL> warm = strutils . bool_from_string ( warm ) <EOL> command = '<STR_LIT>' if warm else '<STR_LIT>' <EOL> raw_command = '<STR_LIT>' + command <EOL> _send_raw ( driver_info , raw_command ) </s>
