<s> from SimpleXMLRPCServer import SimpleXMLRPCServer <EOL> from SimpleXMLRPCServer import SimpleXMLRPCRequestHandler <EOL> from sqlalchemy . sql import exists <EOL> from debile . utils . log import start_logging <EOL> from debile . master . utils import session <EOL> from debile . master . orm import Person , Builder , Job <EOL> from debile . master . interface import NAMESPACE , DebileMasterInterface <EOL> import SocketServer <EOL> import signal <EOL> import hashlib <EOL> import logging <EOL> import logging . handlers <EOL> import os . path <EOL> import ssl <EOL> def check_shutdown ( ) : <EOL> with session ( ) as s : <EOL> shutdown = not s . query ( exists ( ) . where ( <EOL> ( Job . assigned_at != None ) & ( Job . finished_at == None ) ) <EOL> ) . scalar ( ) <EOL> if shutdown : <EOL> raise SystemExit ( <NUM_LIT:0> ) <EOL> class DebileMasterAuthMixIn ( SimpleXMLRPCRequestHandler ) : <EOL> def authenticate ( self ) : <EOL> cert = self . connection . getpeercert ( True ) <EOL> fingerprint = hashlib . sha1 ( cert ) . hexdigest ( ) . upper ( ) <EOL> NAMESPACE . machine = NAMESPACE . session . query ( Builder ) . filter_by ( <EOL> ssl = fingerprint <EOL> ) . first ( ) <EOL> NAMESPACE . user = NAMESPACE . session . query ( Person ) . filter_by ( <EOL> ssl = fingerprint <EOL> ) . first ( ) <EOL> return NAMESPACE . machine or NAMESPACE . user <EOL> def parse_request ( self , * args ) : <EOL> if SimpleXMLRPCRequestHandler . parse_request ( self , * args ) : <EOL> if self . authenticate ( ) : <EOL> return True <EOL> else : <EOL> self . send_error ( <NUM_LIT> , '<STR_LIT>' ) <EOL> return False <EOL> def handle_one_request ( self ) : <EOL> try : <EOL> with session ( ) as s : <EOL> NAMESPACE . session = s <EOL> SimpleXMLRPCRequestHandler . handle_one_request ( self ) <EOL> finally : <EOL> NAMESPACE . session = None <EOL> NAMESPACE . machine = None <EOL> NAMESPACE . user = None <EOL> if DebileMasterInterface . shutdown_request : <EOL> check_shutdown ( ) <EOL> class DebileMasterSimpleAuthMixIn ( SimpleXMLRPCRequestHandler ) : <EOL> def authenticate ( self ) : <EOL> client_address , _ = self . client_address <EOL> NAMESPACE . machine = NAMESPACE . session . query ( Builder ) . filter_by ( <EOL> ip = client_address <EOL> ) . first ( ) <EOL> NAMESPACE . user = NAMESPACE . session . query ( Person ) . filter_by ( <EOL> ip = client_address <EOL> ) . first ( ) <EOL> return NAMESPACE . machine or NAMESPACE . user <EOL> def parse_request ( self , * args ) : <EOL> if SimpleXMLRPCRequestHandler . parse_request ( self , * args ) : <EOL> if self . authenticate ( ) : <EOL> return True <EOL> else : <EOL> self . send_error ( <NUM_LIT> , '<STR_LIT>' ) <EOL> return False <EOL> def handle_one_request ( self ) : <EOL> try : <EOL> with session ( ) as s : <EOL> NAMESPACE . session = s <EOL> SimpleXMLRPCRequestHandler . handle_one_request ( self ) <EOL> finally : <EOL> NAMESPACE . session = None <EOL> NAMESPACE . machine = None <EOL> NAMESPACE . user = None <EOL> if DebileMasterInterface . shutdown_request : <EOL> check_shutdown ( ) <EOL> class SimpleAsyncXMLRPCServer ( SocketServer . ThreadingMixIn , <EOL> DebileMasterSimpleAuthMixIn ) : <EOL> pass <EOL> class AsyncXMLRPCServer ( SocketServer . ThreadingMixIn , DebileMasterAuthMixIn ) : <EOL> pass <EOL> class SimpleAuthXMLRPCServer ( SimpleXMLRPCServer ) : <EOL> def __init__ ( self , addr , <EOL> requestHandler = SimpleXMLRPCRequestHandler , <EOL> bind_and_activate = True , <EOL> allow_none = False ) : <EOL> SimpleXMLRPCServer . __init__ ( self , addr , <EOL> requestHandler = requestHandler , <EOL> bind_and_activate = bind_and_activate , <EOL> allow_none = allow_none ) <EOL> class SecureXMLRPCServer ( SimpleXMLRPCServer ) : <EOL> def __init__ ( <EOL> self , addr , keyfile , certfile , ca_certs , <EOL> requestHandler = SimpleXMLRPCRequestHandler , logRequests = True , <EOL> allow_none = False , encoding = None , bind_and_activate = True <EOL> ) : <EOL> SimpleXMLRPCServer . __init__ ( self , addr , <EOL> requestHandler = requestHandler , <EOL> logRequests = logRequests , <EOL> allow_none = allow_none , <EOL> encoding = encoding , <EOL> bind_and_activate = False ) <EOL> cert_reqs = ( ssl . CERT_NONE if ca_certs is None <EOL> else ssl . CERT_REQUIRED ) <EOL> self . socket = ssl . wrap_socket ( self . socket , <EOL> keyfile = keyfile , certfile = certfile , <EOL> ca_certs = ca_certs , cert_reqs = cert_reqs , <EOL> ssl_version = ssl . PROTOCOL_TLSv1 ) <EOL> if bind_and_activate : <EOL> self . server_bind ( ) <EOL> self . server_activate ( ) <EOL> def serve ( server_addr , port , auth_method , <EOL> keyfile = None , certfile = None , ssl_keyring = None , pgp_keyring = None ) : <EOL> logger = logging . getLogger ( '<STR_LIT>' ) <EOL> logger . info ( "<STR_LIT>" . format ( ** locals ( ) ) ) <EOL> logger . info ( "<STR_LIT>" . format ( auth_method ) ) <EOL> if auth_method == '<STR_LIT>' : <EOL> logger . info ( "<STR_LIT>" <EOL> "<STR_LIT>" . format ( ** locals ( ) ) ) <EOL> logger . info ( "<STR_LIT>" . format ( ** locals ( ) ) ) <EOL> server = None <EOL> if auth_method == '<STR_LIT>' : <EOL> server = SimpleAuthXMLRPCServer ( ( server_addr , port ) , <EOL> requestHandler = SimpleAsyncXMLRPCServer , <EOL> allow_none = True ) <EOL> else : <EOL> server = SecureXMLRPCServer ( ( server_addr , port ) , keyfile , certfile , <EOL> ca_certs = ssl_keyring , <EOL> requestHandler = AsyncXMLRPCServer , <EOL> allow_none = True ) <EOL> server . register_introspection_functions ( ) <EOL> server . register_instance ( DebileMasterInterface ( ssl_keyring , pgp_keyring ) ) <EOL> server . serve_forever ( ) <EOL> def system_exit_handler ( signum , frame ) : <EOL> raise SystemExit ( <NUM_LIT:1> ) <EOL> def shutdown_request_handler ( signum , frame ) : <EOL> DebileMasterInterface . shutdown_request = True <EOL> check_shutdown ( ) <EOL> def main ( args , config ) : <EOL> start_logging ( args ) <EOL> signal . signal ( signal . SIGQUIT , system_exit_handler ) <EOL> signal . signal ( signal . SIGABRT , system_exit_handler ) <EOL> signal . signal ( signal . SIGTERM , system_exit_handler ) <EOL> signal . signal ( signal . SIGHUP , signal . SIG_IGN ) <EOL> signal . signal ( signal . SIGUSR1 , shutdown_request_handler ) <EOL> logger = logging . getLogger ( '<STR_LIT>' ) <EOL> if not os . path . isfile ( config [ '<STR_LIT>' ] [ '<STR_LIT>' ] ) : <EOL> logger . info ( "<STR_LIT>" . format ( file = config [ '<STR_LIT>' ] [ '<STR_LIT>' ] ) ) <EOL> if args . auth_method == '<STR_LIT>' : <EOL> if not os . path . isfile ( config [ '<STR_LIT>' ] [ '<STR_LIT>' ] ) : <EOL> logger . error ( "<STR_LIT>" . format ( file = config [ '<STR_LIT>' ] [ '<STR_LIT>' ] ) ) <EOL> if not os . path . isfile ( config [ '<STR_LIT>' ] [ '<STR_LIT>' ] ) : <EOL> logger . error ( "<STR_LIT>" . format ( file = config [ '<STR_LIT>' ] [ '<STR_LIT>' ] ) ) <EOL> if not os . path . isfile ( config [ '<STR_LIT>' ] [ '<STR_LIT>' ] ) : <EOL> logger . error ( "<STR_LIT>" . format ( file = config [ '<STR_LIT>' ] [ '<STR_LIT>' ] ) ) <EOL> serve ( config [ '<STR_LIT>' ] [ '<STR_LIT>' ] , config [ '<STR_LIT>' ] [ '<STR_LIT:port>' ] , <EOL> args . auth_method , <EOL> config [ '<STR_LIT>' ] . get ( '<STR_LIT>' ) , <EOL> config [ '<STR_LIT>' ] . get ( '<STR_LIT>' ) , <EOL> config [ '<STR_LIT>' ] . get ( '<STR_LIT>' ) , <EOL> config [ "<STR_LIT>" ] . get ( '<STR_LIT>' ) ) </s>
