<s> """<STR_LIT>""" <EOL> import os <EOL> import signal <EOL> import jinja2 <EOL> from oslo_concurrency import processutils <EOL> from oslo_config import cfg <EOL> from oslo_log import log as logging <EOL> from oslo_utils import excutils <EOL> from nova import context <EOL> from nova import db <EOL> from nova . i18n import _ , _LE <EOL> from nova import paths <EOL> from nova import utils <EOL> xvp_opts = [ <EOL> cfg . StrOpt ( '<STR_LIT>' , <EOL> default = paths . basedir_def ( '<STR_LIT>' ) , <EOL> help = '<STR_LIT>' ) , <EOL> cfg . StrOpt ( '<STR_LIT>' , <EOL> default = '<STR_LIT>' , <EOL> help = '<STR_LIT>' ) , <EOL> cfg . StrOpt ( '<STR_LIT>' , <EOL> default = '<STR_LIT>' , <EOL> help = '<STR_LIT>' ) , <EOL> cfg . StrOpt ( '<STR_LIT>' , <EOL> default = '<STR_LIT>' , <EOL> help = '<STR_LIT>' ) , <EOL> cfg . IntOpt ( '<STR_LIT>' , <EOL> default = <NUM_LIT> , <EOL> min = <NUM_LIT:1> , <EOL> max = <NUM_LIT> , <EOL> help = '<STR_LIT>' ) , <EOL> ] <EOL> CONF = cfg . CONF <EOL> CONF . register_opts ( xvp_opts ) <EOL> CONF . import_opt ( '<STR_LIT:host>' , '<STR_LIT>' ) <EOL> LOG = logging . getLogger ( __name__ ) <EOL> class XVPConsoleProxy ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self ) : <EOL> self . xvpconf_template = open ( CONF . console_xvp_conf_template ) . read ( ) <EOL> self . host = CONF . host <EOL> super ( XVPConsoleProxy , self ) . __init__ ( ) <EOL> @ property <EOL> def console_type ( self ) : <EOL> return '<STR_LIT>' <EOL> def get_port ( self , context ) : <EOL> """<STR_LIT>""" <EOL> return CONF . console_xvp_multiplex_port <EOL> def setup_console ( self , context , console ) : <EOL> """<STR_LIT>""" <EOL> self . _rebuild_xvp_conf ( context . elevated ( ) ) <EOL> def teardown_console ( self , context , console ) : <EOL> """<STR_LIT>""" <EOL> self . _rebuild_xvp_conf ( context . elevated ( ) ) <EOL> def init_host ( self ) : <EOL> """<STR_LIT>""" <EOL> ctxt = context . get_admin_context ( ) <EOL> self . _rebuild_xvp_conf ( ctxt ) <EOL> def fix_pool_password ( self , password ) : <EOL> """<STR_LIT>""" <EOL> return self . _xvp_encrypt ( password , is_pool_password = True ) <EOL> def fix_console_password ( self , password ) : <EOL> """<STR_LIT>""" <EOL> return self . _xvp_encrypt ( password ) <EOL> def _rebuild_xvp_conf ( self , context ) : <EOL> LOG . debug ( '<STR_LIT>' ) <EOL> pools = [ pool for pool in <EOL> db . console_pool_get_all_by_host_type ( context , self . host , <EOL> self . console_type ) <EOL> if pool [ '<STR_LIT>' ] ] <EOL> if not pools : <EOL> LOG . debug ( '<STR_LIT>' ) <EOL> self . _xvp_stop ( ) <EOL> return <EOL> conf_data = { '<STR_LIT>' : CONF . console_xvp_multiplex_port , <EOL> '<STR_LIT>' : pools } <EOL> tmpl_path , tmpl_file = os . path . split ( CONF . injected_network_template ) <EOL> env = jinja2 . Environment ( loader = jinja2 . FileSystemLoader ( tmpl_path ) ) <EOL> env . filters [ '<STR_LIT>' ] = self . fix_console_password <EOL> template = env . get_template ( tmpl_file ) <EOL> self . _write_conf ( template . render ( conf_data ) ) <EOL> self . _xvp_restart ( ) <EOL> def _write_conf ( self , config ) : <EOL> try : <EOL> LOG . debug ( '<STR_LIT>' , CONF . console_xvp_conf ) <EOL> with open ( CONF . console_xvp_conf , '<STR_LIT:w>' ) as cfile : <EOL> cfile . write ( config ) <EOL> except IOError : <EOL> with excutils . save_and_reraise_exception ( ) : <EOL> LOG . exception ( _LE ( "<STR_LIT>" ) ) <EOL> def _xvp_stop ( self ) : <EOL> LOG . debug ( '<STR_LIT>' ) <EOL> pid = self . _xvp_pid ( ) <EOL> if not pid : <EOL> return <EOL> try : <EOL> os . kill ( pid , signal . SIGTERM ) <EOL> except OSError : <EOL> pass <EOL> def _xvp_start ( self ) : <EOL> if self . _xvp_check_running ( ) : <EOL> return <EOL> LOG . debug ( '<STR_LIT>' ) <EOL> try : <EOL> utils . execute ( '<STR_LIT>' , <EOL> '<STR_LIT>' , CONF . console_xvp_pid , <EOL> '<STR_LIT:-c>' , CONF . console_xvp_conf , <EOL> '<STR_LIT>' , CONF . console_xvp_log ) <EOL> except processutils . ProcessExecutionError as err : <EOL> LOG . error ( _LE ( '<STR_LIT>' ) , err ) <EOL> def _xvp_restart ( self ) : <EOL> LOG . debug ( '<STR_LIT>' ) <EOL> if not self . _xvp_check_running ( ) : <EOL> LOG . debug ( '<STR_LIT>' ) <EOL> self . _xvp_start ( ) <EOL> else : <EOL> pid = self . _xvp_pid ( ) <EOL> os . kill ( pid , signal . SIGUSR1 ) <EOL> def _xvp_pid ( self ) : <EOL> try : <EOL> with open ( CONF . console_xvp_pid , '<STR_LIT:r>' ) as pidfile : <EOL> pid = int ( pidfile . read ( ) ) <EOL> except IOError : <EOL> return None <EOL> except ValueError : <EOL> return None <EOL> return pid <EOL> def _xvp_check_running ( self ) : <EOL> pid = self . _xvp_pid ( ) <EOL> if not pid : <EOL> return False <EOL> try : <EOL> os . kill ( pid , <NUM_LIT:0> ) <EOL> except OSError : <EOL> return False <EOL> return True <EOL> def _xvp_encrypt ( self , password , is_pool_password = False ) : <EOL> """<STR_LIT>""" <EOL> maxlen = <NUM_LIT:8> <EOL> flag = '<STR_LIT>' <EOL> if is_pool_password : <EOL> maxlen = <NUM_LIT:16> <EOL> flag = '<STR_LIT>' <EOL> password = password [ : maxlen ] <EOL> out , err = utils . execute ( '<STR_LIT>' , flag , process_input = password ) <EOL> if err : <EOL> raise processutils . ProcessExecutionError ( _ ( "<STR_LIT>" ) ) <EOL> return out . strip ( ) </s>
