<s> import json <EOL> from txredis . client import RedisClientFactory <EOL> from twisted . internet import defer <EOL> from twisted . internet import reactor <EOL> from twisted . web import server <EOL> from twisted . python import log <EOL> from protocols . http import HBReverseProxyRequest <EOL> from protocols . http import HBReverseProxyResource <EOL> from models import SessionModel <EOL> class OperationResponse ( object ) : <EOL> def __init__ ( self , code , message , key ) : <EOL> self . deferred = defer . Deferred ( ) <EOL> self . response = { '<STR_LIT:code>' : code , <EOL> '<STR_LIT:message>' : [ message ] , <EOL> '<STR_LIT:key>' : key <EOL> } <EOL> def get_code ( self ) : <EOL> return self . response [ '<STR_LIT:code>' ] <EOL> def get_message ( self ) : <EOL> return str ( self . response [ '<STR_LIT:message>' ] ) <EOL> def set_code ( self , code ) : <EOL> self . response [ '<STR_LIT:code>' ] = code <EOL> def set_message ( self , message ) : <EOL> self . response [ '<STR_LIT:message>' ] = [ ] <EOL> self . response [ '<STR_LIT:message>' ] . append ( message ) <EOL> def add_message ( self , message ) : <EOL> self . response [ '<STR_LIT:message>' ] . append ( message ) <EOL> def send ( self ) : <EOL> pass <EOL> def getDeferred ( self ) : <EOL> return self . deferred <EOL> class RedisOperationResponse ( OperationResponse ) : <EOL> def __init__ ( self , code , message , key , redis_endpoint = None , <EOL> response_channel = None ) : <EOL> OperationResponse . __init__ ( self , code , message , key ) <EOL> self . response_channel = response_channel <EOL> self . redis_conn = redis_endpoint . connect ( RedisClientFactory ( ) ) <EOL> self . redis_conn . addCallback ( self . set_redis_client ) <EOL> def set_redis_client ( self , redis_client ) : <EOL> self . redis_client = redis_client <EOL> def _send ( self , result ) : <EOL> self . redis_client . publish ( self . response_channel , <EOL> json . dumps ( self . response ) ) <EOL> self . redis_client . set ( self . response [ '<STR_LIT:key>' ] , <EOL> json . dumps ( self . response ) ) <EOL> log . msg ( "<STR_LIT>" + self . response [ '<STR_LIT:key>' ] ) <EOL> def send ( self ) : <EOL> self . redis_conn . addCallback ( self . _send ) <EOL> class OperationResponseFactory ( object ) : <EOL> def get_response ( self , code , message , key ) : <EOL> response = OperationResponse ( code , <EOL> message , <EOL> key ) <EOL> return response <EOL> class RedisOperationResponseFactory ( OperationResponseFactory ) : <EOL> def __init__ ( self , redis_endpoint , response_channel ) : <EOL> self . reactor = reactor <EOL> self . response_channel = response_channel <EOL> self . redis_endpoint = redis_endpoint <EOL> def get_response ( self , code , message , key ) : <EOL> response = RedisOperationResponse ( code , message , key , <EOL> self . redis_endpoint , <EOL> self . response_channel ) <EOL> return response <EOL> class TcpOperationResponseFactory ( OperationResponseFactory ) : <EOL> def get_response ( self , code , message , key ) : <EOL> pass <EOL> class OperationFactory ( object ) : <EOL> def __init__ ( self , controller ) : <EOL> self . controller = controller <EOL> self . response_factory = OperationResponseFactory ( ) <EOL> def get_operation ( self , message ) : <EOL> op_string = json . loads ( message ) <EOL> operation = None <EOL> if "<STR_LIT>" == op_string [ '<STR_LIT>' ] : <EOL> operation = StopProxy ( self . controller , <EOL> self . response_factory , <EOL> op_string [ '<STR_LIT:key>' ] ) <EOL> if "<STR_LIT:start>" == op_string [ '<STR_LIT>' ] : <EOL> operation = StartProxy ( self . controller , <EOL> self . response_factory , <EOL> op_string [ '<STR_LIT:key>' ] ) <EOL> if "<STR_LIT>" == op_string [ '<STR_LIT>' ] : <EOL> operation = StopRecording ( self . controller , <EOL> self . response_factory , <EOL> op_string [ '<STR_LIT:key>' ] , <EOL> recording_id = op_string [ '<STR_LIT>' ] ) <EOL> if "<STR_LIT>" == op_string [ '<STR_LIT>' ] : <EOL> operation = StartRecording ( self . controller , <EOL> self . response_factory , <EOL> op_string [ '<STR_LIT:key>' ] , <EOL> recording_id = op_string [ '<STR_LIT>' ] ) <EOL> if "<STR_LIT>" == op_string [ '<STR_LIT>' ] : <EOL> operation = StopSession ( self . controller , <EOL> self . response_factory , <EOL> op_string [ '<STR_LIT:key>' ] , <EOL> session_id = op_string [ '<STR_LIT>' ] ) <EOL> if "<STR_LIT>" == op_string [ '<STR_LIT>' ] : <EOL> operation = StartSession ( self . controller , <EOL> self . response_factory , <EOL> op_string [ '<STR_LIT:key>' ] , <EOL> session_id = op_string [ '<STR_LIT>' ] ) <EOL> if "<STR_LIT>" == op_string [ '<STR_LIT>' ] : <EOL> operation = ReloadPlugins ( self . controller , <EOL> self . response_factory , <EOL> op_string [ '<STR_LIT:key>' ] ) <EOL> if "<STR_LIT>" == op_string [ '<STR_LIT>' ] : <EOL> operation = ResetPlugins ( self . controller , <EOL> self . response_factory , <EOL> op_string [ '<STR_LIT:key>' ] ) <EOL> if "<STR_LIT>" == op_string [ '<STR_LIT>' ] : <EOL> operation = ChangeUpstreamPort ( self . controller , <EOL> self . response_factory , <EOL> op_string [ '<STR_LIT:key>' ] , <EOL> new_port = op_string [ '<STR_LIT>' ] ) <EOL> if "<STR_LIT>" == op_string [ '<STR_LIT>' ] : <EOL> operation = ChangeUpstreamHost ( self . controller , <EOL> self . response_factory , <EOL> op_string [ '<STR_LIT:key>' ] , <EOL> new_host = op_string [ '<STR_LIT>' ] ) <EOL> if "<STR_LIT>" == op_string [ '<STR_LIT>' ] : <EOL> self . controller . proxy_address = op_string [ '<STR_LIT>' ] <EOL> operation = ChangeBindAddress ( self . controller , <EOL> self . response_factory , <EOL> op_string [ '<STR_LIT:key>' ] ) <EOL> if "<STR_LIT>" == op_string [ '<STR_LIT>' ] : <EOL> self . controller . proxy_port = op_string [ '<STR_LIT>' ] <EOL> operation = ChangeBindPort ( self . controller , <EOL> self . response_factory , <EOL> op_string [ '<STR_LIT:key>' ] ) <EOL> if "<STR_LIT:test>" == op_string [ '<STR_LIT>' ] : <EOL> operation = RunTest ( self . controller , <EOL> self . response_factory , <EOL> op_string [ '<STR_LIT:key>' ] , <EOL> module_name = op_string [ '<STR_LIT>' ] [ '<STR_LIT>' ] , <EOL> response_file = op_string [ '<STR_LIT>' ] [ '<STR_LIT>' ] ) <EOL> if "<STR_LIT:status>" == op_string [ '<STR_LIT>' ] : <EOL> operation = Status ( self . controller , <EOL> self . response_factory , <EOL> op_string [ '<STR_LIT:key>' ] , <EOL> module_name = op_string [ '<STR_LIT>' ] ) <EOL> return operation <EOL> class RedisOperationFactory ( OperationFactory ) : <EOL> def __init__ ( self , proxy , redis_endpoint , response_channel ) : <EOL> OperationFactory . __init__ ( self , proxy ) <EOL> self . response_factory = RedisOperationResponseFactory ( redis_endpoint , <EOL> response_channel ) <EOL> class TcpOperationFactory ( OperationFactory ) : <EOL> def __init__ ( self , hb_proxy , response_channel ) : <EOL> OperationFactory . __init__ ( hb_proxy ) <EOL> self . repsponse_factory = TcpOperationResponseFactory ( ) <EOL> class ServerOperation ( object ) : <EOL> def __init__ ( self , controller , response_factory , key ) : <EOL> self . controller = controller <EOL> self . operation = defer . Deferred ( ) <EOL> self . response = response_factory . get_response ( <NUM_LIT:200> , <EOL> "<STR_LIT>" , <EOL> key ) <EOL> self . key = key <EOL> def execute ( self ) : <EOL> return self . operation . callback ( self . response ) <EOL> def respond ( self , result ) : <EOL> self . response . send ( ) <EOL> def addCallback ( self , callback ) : <EOL> return self . operation . addCallback ( callback ) <EOL> class StopProxy ( ServerOperation ) : <EOL> def __init__ ( self , controller , response_factory , key ) : <EOL> ServerOperation . __init__ ( self , controller , response_factory , key ) <EOL> self . addCallback ( self . stop ) <EOL> self . addCallback ( self . respond ) <EOL> def stop ( self , result ) : <EOL> deferred = self . controller . proxy . stopListening ( ) <EOL> self . response . set_message ( "<STR_LIT>" + self . response . get_message ( ) ) <EOL> return deferred <EOL> class StartProxy ( ServerOperation ) : <EOL> def __init__ ( self , controller , response_factory , key ) : <EOL> ServerOperation . __init__ ( self , controller , response_factory , key ) <EOL> self . addCallback ( self . start ) <EOL> self . addCallback ( self . respond ) <EOL> def start ( self , result ) : <EOL> resource = HBReverseProxyResource ( self . controller . upstream_host , <EOL> self . controller . upstream_port , '<STR_LIT>' , <EOL> self . controller . module_registry ) <EOL> f = server . Site ( resource ) <EOL> f . requestFactory = HBReverseProxyRequest <EOL> proxy_port = self . controller . proxy_port <EOL> proxy_address = self . controller . proxy_address <EOL> self . controller . proxy = reactor . listenTCP ( proxy_port , f , <EOL> interface = proxy_address ) <EOL> self . response . set_message ( "<STR_LIT>" + self . response . get_message ( ) ) <EOL> return self . controller . proxy <EOL> class ChangeUpstreamPort ( ServerOperation ) : <EOL> def __init__ ( self , controller , response_factory , key , new_port ) : <EOL> ServerOperation . __init__ ( self , controller , response_factory , key ) <EOL> stop_op = StopProxy ( controller , response_factory , key ) <EOL> start_op = StartProxy ( controller , response_factory , key ) <EOL> self . new_port = new_port <EOL> controller . upstream_port = new_port <EOL> d = self . addCallback ( stop_op . stop ) . addCallback ( start_op . start ) <EOL> d . addCallback ( self . update ) <EOL> d . addCallback ( self . respond ) <EOL> def update ( self , result ) : <EOL> self . response . set_message ( { '<STR_LIT>' : <EOL> [ self . response . get_message ( ) ] <EOL> } ) <EOL> log . msg ( "<STR_LIT>" + str ( self . new_port ) ) <EOL> class ChangeUpstreamHost ( ServerOperation ) : <EOL> def __init__ ( self , controller , response_factory , key , new_host ) : <EOL> ServerOperation . __init__ ( self , controller , response_factory , key ) <EOL> self . new_host = new_host <EOL> controller . upstream_host = new_host <EOL> stop_op = StopProxy ( controller , response_factory , key ) <EOL> start_op = StartProxy ( controller , response_factory , key ) <EOL> d = self . addCallback ( stop_op . stop ) . addCallback ( start_op . start ) <EOL> d . addCallback ( self . update ) <EOL> d . addCallback ( self . respond ) <EOL> def update ( self , result ) : <EOL> self . response . set_message ( { '<STR_LIT>' : <EOL> [ self . response . get_message ( ) ] <EOL> } ) <EOL> log . msg ( "<STR_LIT>" + str ( self . new_host ) ) <EOL> class ChangeBindAddress ( ServerOperation ) : <EOL> def __init__ ( self , controller , response_factory , key ) : <EOL> ServerOperation . __init__ ( self , controller , response_factory , key ) <EOL> stop_op = StopProxy ( controller , response_factory , key ) <EOL> start_op = StartProxy ( controller , response_factory , key ) <EOL> d = self . addCallback ( stop_op . stop ) . addCallback ( start_op . start ) <EOL> d . addCallback ( self . update_message ) <EOL> d . addCallback ( self . respond ) <EOL> def update_message ( self , result ) : <EOL> self . response . set_message ( { '<STR_LIT>' : <EOL> [ self . response . get_message ( ) ] <EOL> } ) <EOL> class ChangeBindPort ( ServerOperation ) : <EOL> def __init__ ( self , controller , response_factory , key ) : <EOL> ServerOperation . __init__ ( self , controller , response_factory , key ) <EOL> stop_op = StopProxy ( controller , response_factory , key ) <EOL> start_op = StartProxy ( controller , response_factory , key ) <EOL> d = self . addCallback ( stop_op . stop ) . addCallback ( start_op . start ) <EOL> d . addCallback ( self . update_message ) <EOL> d . addCallback ( self . respond ) <EOL> def update_message ( self , result ) : <EOL> self . response . set_message ( { '<STR_LIT>' : <EOL> [ self . response . get_message ( ) ] <EOL> } ) <EOL> class StopRecording ( ServerOperation ) : <EOL> def __init__ ( self , controller , response_factory , key , ** params ) : <EOL> ServerOperation . __init__ ( self , controller , response_factory , key ) <EOL> self . params = params <EOL> d = self . addCallback ( self . stop_recording ) <EOL> d . addCallback ( self . respond ) <EOL> def stop_recording ( self , result ) : <EOL> self . controller . module_registry . stop ( module_name = '<STR_LIT>' , <EOL> ** self . params ) <EOL> self . response . set_message ( "<STR_LIT>" + <EOL> self . response . get_message ( ) + "<STR_LIT:U+002CU+0020>" + "<STR_LIT:}>" ) <EOL> class StartRecording ( ServerOperation ) : <EOL> def __init__ ( self , controller , response_factory , key , ** params ) : <EOL> ServerOperation . __init__ ( self , controller , response_factory , key ) <EOL> self . params = params <EOL> d = self . addCallback ( self . start_recording ) <EOL> d . addCallback ( self . respond ) <EOL> def start_recording ( self , result ) : <EOL> status = self . controller . module_registry . status ( <EOL> module_name = '<STR_LIT>' , <EOL> ** self . params ) <EOL> if status [ '<STR_LIT:state>' ] != "<STR_LIT>" : <EOL> self . controller . module_registry . start ( <EOL> module_name = '<STR_LIT>' , <EOL> ** self . params ) <EOL> self . response . set_message ( { '<STR_LIT>' : <EOL> [ self . response . get_message ( ) ] } ) <EOL> else : <EOL> self . response . set_code ( <NUM_LIT> ) <EOL> self . response . set_message ( { '<STR_LIT>' : status } ) <EOL> class StartSession ( ServerOperation ) : <EOL> def __init__ ( self , controller , response_factory , key , ** params ) : <EOL> ServerOperation . __init__ ( self , controller , response_factory , key ) <EOL> self . params = params <EOL> session_id = params [ '<STR_LIT>' ] <EOL> self . session = SessionModel ( { "<STR_LIT>" : session_id } ) <EOL> controller . upstream_host = self . session [ "<STR_LIT>" ] <EOL> self . response . add_message ( "<STR_LIT>" + <EOL> str ( controller . upstream_host ) ) <EOL> controller . upstream_port = self . session [ "<STR_LIT>" ] <EOL> self . response . add_message ( "<STR_LIT>" + <EOL> str ( controller . upstream_port ) ) <EOL> stop_op = StopProxy ( controller , response_factory , key ) <EOL> start_op = StartProxy ( controller , response_factory , key ) <EOL> d = self . addCallback ( stop_op . stop ) . addCallback ( start_op . start ) <EOL> d . addCallback ( self . start_session ) <EOL> d . addCallback ( self . respond ) <EOL> def start_session ( self , result ) : <EOL> try : <EOL> self . params [ '<STR_LIT>' ] = self . session [ '<STR_LIT>' ] [ '<STR_LIT:id>' ] <EOL> status = self . controller . module_registry . status ( <EOL> module_name = '<STR_LIT>' , <EOL> ** self . params ) <EOL> if status [ '<STR_LIT:state>' ] != "<STR_LIT>" : <EOL> self . controller . module_registry . start ( <EOL> module_name = '<STR_LIT>' , <EOL> ** self . params ) <EOL> else : <EOL> self . response . set_code ( <NUM_LIT> ) <EOL> self . response . set_message ( { '<STR_LIT>' : status } ) <EOL> except KeyError : <EOL> pass <EOL> try : <EOL> profile_id = self . session [ '<STR_LIT>' ] <EOL> self . params [ '<STR_LIT>' ] = profile_id <EOL> status = self . controller . module_registry . status ( <EOL> module_name = '<STR_LIT>' , <EOL> ** self . params ) <EOL> if status [ '<STR_LIT:state>' ] != "<STR_LIT>" : <EOL> self . controller . module_registry . start ( <EOL> module_name = '<STR_LIT>' , <EOL> ** self . params ) <EOL> else : <EOL> self . response . set_code ( <NUM_LIT> ) <EOL> self . response . set_message ( { '<STR_LIT>' : status } ) <EOL> except KeyError : <EOL> pass <EOL> try : <EOL> profile_id = self . session [ '<STR_LIT>' ] <EOL> self . params [ '<STR_LIT>' ] = profile_id <EOL> status = self . controller . module_registry . status ( <EOL> module_name = '<STR_LIT>' , <EOL> ** self . params ) <EOL> if status [ '<STR_LIT:state>' ] != "<STR_LIT>" : <EOL> self . controller . module_registry . start ( <EOL> module_name = '<STR_LIT>' , <EOL> ** self . params ) <EOL> else : <EOL> self . response . set_code ( <NUM_LIT> ) <EOL> self . response . set_message ( { '<STR_LIT>' : status } ) <EOL> except KeyError : <EOL> pass <EOL> self . response . set_message ( { '<STR_LIT>' : <EOL> [ self . response . get_message ( ) ] } ) <EOL> class StopSession ( ServerOperation ) : <EOL> def __init__ ( self , controller , response_factory , key , ** params ) : <EOL> ServerOperation . __init__ ( self , controller , response_factory , key ) <EOL> self . params = params <EOL> d = self . addCallback ( self . stop_session ) <EOL> d . addCallback ( self . respond ) <EOL> def stop_session ( self , result ) : <EOL> self . controller . module_registry . stop ( module_name = '<STR_LIT>' , <EOL> ** self . params ) <EOL> self . controller . module_registry . stop ( module_name = '<STR_LIT>' , <EOL> ** self . params ) <EOL> self . controller . module_registry . stop ( module_name = '<STR_LIT>' , <EOL> ** self . params ) <EOL> self . response . set_message ( "<STR_LIT>" + <EOL> self . response . get_message ( ) + "<STR_LIT:U+002CU+0020>" + "<STR_LIT:}>" ) <EOL> class ResetPlugins ( ServerOperation ) : <EOL> def __init__ ( self , controller , response_factory , key ) : <EOL> ServerOperation . __init__ ( self , controller , response_factory , key ) <EOL> stop_op = StopProxy ( controller , response_factory , key ) <EOL> start_op = StartProxy ( controller , response_factory , key ) <EOL> d = self . addCallback ( stop_op . stop ) <EOL> d = d . addCallback ( start_op . start ) <EOL> d . addCallback ( self . reset_plugins ) <EOL> d . addCallback ( self . update_message ) <EOL> d . addCallback ( self . respond ) <EOL> def reset_plugins ( self , result ) : <EOL> self . controller . module_registry . reset ( ) <EOL> def update_message ( self , result ) : <EOL> self . response . set_message ( { '<STR_LIT>' : <EOL> [ self . response . get_message ( ) ] <EOL> } ) <EOL> class ReloadPlugins ( ServerOperation ) : <EOL> def __init__ ( self , controller , response_factory , key ) : <EOL> ServerOperation . __init__ ( self , controller , response_factory , key ) <EOL> stop_op = StopProxy ( controller , response_factory , key ) <EOL> start_op = StartProxy ( controller , response_factory , key ) <EOL> d = self . addCallback ( stop_op . stop ) <EOL> d = d . addCallback ( start_op . start ) <EOL> d . addCallback ( self . reload_plugins ) <EOL> d . addCallback ( self . update_message ) <EOL> d . addCallback ( self . respond ) <EOL> def reload_plugins ( self , result ) : <EOL> self . controller . module_registry . reload ( ) <EOL> def update_message ( self , result ) : <EOL> self . response . set_message ( { '<STR_LIT>' : <EOL> [ self . response . get_message ( ) ] <EOL> } ) <EOL> class RunTest ( ServerOperation ) : <EOL> def __init__ ( self , <EOL> server , <EOL> response_factory , <EOL> key , <EOL> module_name , <EOL> response_file ) : <EOL> ServerOperation . __init__ ( self , server , response_factory , key ) <EOL> self . module_name = module_name <EOL> self . response_file = response_file <EOL> self . server = server <EOL> self . response_factory = response_factory <EOL> self . key = key <EOL> self . orig_upstream_host = server . upstream_host <EOL> self . orig_upstream_port = server . upstream_port <EOL> d = self . _connect_echo_server ( ) <EOL> d . addCallback ( self . run_test ) <EOL> d . addCallback ( self . respond ) <EOL> d . addCallback ( self . _disconnect_echo_server ) <EOL> def run_test ( self , result ) : <EOL> tests = self . controller . module_registry . test ( <EOL> module_name = self . module_name , response_file = self . response_file ) <EOL> self . response . set_message ( { '<STR_LIT>' : <EOL> [ self . response . get_message ( ) ] <EOL> } ) <EOL> tests . callback ( self . response ) <EOL> def _connect_echo_server ( self ) : <EOL> self . server . upstream_host = '<STR_LIT:127.0.0.1>' <EOL> self . server . upstream_port = <NUM_LIT> <EOL> stop_op = StopProxy ( self . server , self . response_factory , self . key ) <EOL> start_op = StartProxy ( self . server , self . response_factory , self . key ) <EOL> log . msg ( "<STR_LIT>" + <EOL> str ( self . server . upstream_host ) + <EOL> "<STR_LIT::>" + str ( self . server . upstream_port ) ) <EOL> return self . addCallback ( stop_op . stop ) . addCallback ( start_op . start ) <EOL> def _disconnect_echo_server ( self , unused ) : <EOL> self . server . upstream_host = self . orig_upstream_host <EOL> self . server . upstream_port = self . orig_upstream_port <EOL> stop_op = StopProxy ( self . server , self . response_factory , self . key ) <EOL> start_op = StartProxy ( self . server , self . response_factory , self . key ) <EOL> log . msg ( "<STR_LIT>" + <EOL> str ( self . server . upstream_host ) + <EOL> "<STR_LIT::>" + str ( self . server . upstream_port ) ) <EOL> return self . addCallback ( stop_op . stop ) . addCallback ( start_op . start ) <EOL> class Status ( ServerOperation ) : <EOL> def __init__ ( self , controller , response_factory , key , module_name ) : <EOL> ServerOperation . __init__ ( self , controller , response_factory , key ) <EOL> self . module_name = module_name <EOL> d = self . addCallback ( self . get_status ) <EOL> d . addCallback ( self . respond ) <EOL> def get_status ( self , result ) : <EOL> status = self . controller . module_registry . status ( <EOL> module_name = self . module_name ) <EOL> self . response . set_message ( { '<STR_LIT:status>' : status } ) </s>
