<s> import json <EOL> from tornado . web import HTTPError <EOL> from ... error import ProcessError <EOL> from ... process import ProcessConfig <EOL> from . util import CorsHandler , CorsHandlerWithAuth <EOL> class SessionsHandler ( CorsHandlerWithAuth ) : <EOL> """<STR_LIT>""" <EOL> def get ( self , * args ) : <EOL> self . preflight ( ) <EOL> if ( not self . api_key . is_admin ( ) and <EOL> not self . api_key . can_manage_all ( ) ) : <EOL> raise HTTPError ( <NUM_LIT> ) <EOL> m = self . settings . get ( '<STR_LIT>' ) <EOL> self . set_header ( '<STR_LIT:Content-Type>' , '<STR_LIT:application/json>' ) <EOL> self . write ( json . dumps ( { "<STR_LIT>" : m . sessions } ) ) <EOL> class AllJobsHandler ( CorsHandlerWithAuth ) : <EOL> """<STR_LIT>""" <EOL> def get ( self , * args ) : <EOL> self . preflight ( ) <EOL> if ( not self . api_key . is_admin ( ) and <EOL> not self . api_key . can_manage_all ( ) ) : <EOL> raise HTTPError ( <NUM_LIT> ) <EOL> m = self . settings . get ( '<STR_LIT>' ) <EOL> self . set_header ( '<STR_LIT:Content-Type>' , '<STR_LIT:application/json>' ) <EOL> self . write ( json . dumps ( { "<STR_LIT>" : m . jobs ( ) } ) ) <EOL> class JobsHandler ( CorsHandlerWithAuth ) : <EOL> """<STR_LIT>""" <EOL> def get ( self , * args , ** kwargs ) : <EOL> self . preflight ( ) <EOL> m = self . settings . get ( '<STR_LIT>' ) <EOL> sessionid = args [ <NUM_LIT:0> ] <EOL> if not self . api_key . can_manage ( sessionid ) : <EOL> raise HTTPError ( <NUM_LIT> ) <EOL> try : <EOL> jobs = list ( m . jobs ( sessionid ) ) <EOL> except ProcessError as e : <EOL> self . set_status ( e . errno ) <EOL> return self . write ( e . to_dict ( ) ) <EOL> self . set_header ( '<STR_LIT:Content-Type>' , '<STR_LIT:application/json>' ) <EOL> self . write ( json . dumps ( { "<STR_LIT>" : sessionid , <EOL> "<STR_LIT>" : jobs } ) ) <EOL> def post ( self , * args , ** kwargs ) : <EOL> self . preflight ( ) <EOL> sessionid = args [ <NUM_LIT:0> ] <EOL> if not self . api_key . can_manage ( sessionid ) : <EOL> raise HTTPError ( <NUM_LIT> ) <EOL> self . set_header ( '<STR_LIT:Content-Type>' , '<STR_LIT:application/json>' ) <EOL> try : <EOL> name , cmd , settings = self . fetch_body ( ) <EOL> except ValueError : <EOL> self . set_status ( <NUM_LIT> ) <EOL> return self . write ( { "<STR_LIT:error>" : "<STR_LIT>" } ) <EOL> return <EOL> if "<STR_LIT:start>" in settings : <EOL> start = settings . pop ( "<STR_LIT:start>" ) <EOL> else : <EOL> start = True <EOL> config = ProcessConfig ( name , cmd , ** settings ) <EOL> m = self . settings . get ( '<STR_LIT>' ) <EOL> try : <EOL> m . load ( config , sessionid = sessionid , start = start ) <EOL> except ProcessError as e : <EOL> self . set_status ( e . errno ) <EOL> return self . write ( e . to_dict ( ) ) <EOL> self . write ( { "<STR_LIT>" : True } ) <EOL> def fetch_body ( self ) : <EOL> obj = json . loads ( self . request . body . decode ( '<STR_LIT:utf-8>' ) ) <EOL> if not "<STR_LIT:name>" or not "<STR_LIT>" in obj : <EOL> raise ValueError <EOL> name = obj . pop ( "<STR_LIT:name>" ) <EOL> cmd = obj . pop ( "<STR_LIT>" ) <EOL> return name , cmd , obj <EOL> class JobHandler ( CorsHandlerWithAuth ) : <EOL> """<STR_LIT>""" <EOL> def head ( self , * args ) : <EOL> self . preflight ( ) <EOL> self . set_header ( '<STR_LIT:Content-Type>' , '<STR_LIT:application/json>' ) <EOL> m = self . settings . get ( '<STR_LIT>' ) <EOL> pname = "<STR_LIT>" % ( args [ <NUM_LIT:0> ] , args [ <NUM_LIT:1> ] ) <EOL> if not self . api_key . can_read ( pname ) : <EOL> raise HTTPError ( <NUM_LIT> ) <EOL> try : <EOL> m . get ( pname ) <EOL> except ProcessError : <EOL> return self . set_status ( <NUM_LIT> ) <EOL> self . set_status ( <NUM_LIT:200> ) <EOL> def get ( self , * args ) : <EOL> self . preflight ( ) <EOL> self . set_header ( '<STR_LIT:Content-Type>' , '<STR_LIT:application/json>' ) <EOL> m = self . settings . get ( '<STR_LIT>' ) <EOL> pname = "<STR_LIT>" % ( args [ <NUM_LIT:0> ] , args [ <NUM_LIT:1> ] ) <EOL> if not self . api_key . can_read ( pname ) : <EOL> raise HTTPError ( <NUM_LIT> ) <EOL> try : <EOL> info = m . info ( pname ) <EOL> except ProcessError : <EOL> self . set_status ( <NUM_LIT> ) <EOL> return self . write ( { "<STR_LIT:error>" : "<STR_LIT>" } ) <EOL> self . write ( info ) <EOL> def delete ( self , * args ) : <EOL> self . preflight ( ) <EOL> self . set_header ( '<STR_LIT:Content-Type>' , '<STR_LIT:application/json>' ) <EOL> m = self . settings . get ( '<STR_LIT>' ) <EOL> sessionid = args [ <NUM_LIT:0> ] <EOL> name = args [ <NUM_LIT:1> ] <EOL> if not self . api_key . can_manage ( "<STR_LIT>" % ( sessionid , name ) ) : <EOL> raise HTTPError ( <NUM_LIT> ) <EOL> try : <EOL> m . unload ( name , sessionid ) <EOL> except ProcessError : <EOL> self . set_status ( <NUM_LIT> ) <EOL> return self . write ( { "<STR_LIT:error>" : "<STR_LIT>" } ) <EOL> self . write ( { "<STR_LIT>" : True } ) <EOL> def put ( self , * args ) : <EOL> self . preflight ( ) <EOL> self . set_header ( '<STR_LIT:Content-Type>' , '<STR_LIT:application/json>' ) <EOL> m = self . settings . get ( '<STR_LIT>' ) <EOL> sessionid = args [ <NUM_LIT:0> ] <EOL> name = args [ <NUM_LIT:1> ] <EOL> if not self . api_key . can_manage ( "<STR_LIT>" % ( sessionid , name ) ) : <EOL> raise HTTPError ( <NUM_LIT> ) <EOL> try : <EOL> cmd , settings = self . fetch_body ( name ) <EOL> except ValueError as e : <EOL> self . set_status ( <NUM_LIT> ) <EOL> return self . write ( { "<STR_LIT:error>" : "<STR_LIT>" , "<STR_LIT>" : str ( e ) } ) <EOL> if "<STR_LIT:start>" in settings : <EOL> start = settings . pop ( "<STR_LIT:start>" ) <EOL> else : <EOL> start = True <EOL> config = ProcessConfig ( name , cmd , ** settings ) <EOL> try : <EOL> m . update ( config , sessionid = sessionid , start = start ) <EOL> except ProcessError as e : <EOL> self . set_status ( e . errno ) <EOL> return self . write ( e . to_dict ( ) ) <EOL> self . set_status ( <NUM_LIT> ) <EOL> self . write ( { "<STR_LIT>" : True } ) <EOL> def fetch_body ( self , name ) : <EOL> config = json . loads ( self . request . body . decode ( '<STR_LIT:utf-8>' ) ) <EOL> if "<STR_LIT>" not in config : <EOL> raise ValueError ( "<STR_LIT>" ) <EOL> if '<STR_LIT:name>' in config : <EOL> if config . get ( '<STR_LIT:name>' ) != name : <EOL> raise ValueError ( "<STR_LIT>" ) <EOL> del config [ '<STR_LIT:name>' ] <EOL> cmd = config . pop ( "<STR_LIT>" ) <EOL> return cmd , config <EOL> class JobStatsHandler ( CorsHandlerWithAuth ) : <EOL> """<STR_LIT>""" <EOL> def get ( self , * args ) : <EOL> self . preflight ( ) <EOL> self . set_header ( '<STR_LIT:Content-Type>' , '<STR_LIT:application/json>' ) <EOL> m = self . settings . get ( '<STR_LIT>' ) <EOL> pname = "<STR_LIT>" % ( args [ <NUM_LIT:0> ] , args [ <NUM_LIT:1> ] ) <EOL> if not self . api_key . can_read ( pname ) : <EOL> raise HTTPError ( <NUM_LIT> ) <EOL> try : <EOL> stats = m . stats ( pname ) <EOL> except ProcessError as e : <EOL> self . set_status ( e . errno ) <EOL> return self . write ( e . to_dict ( ) ) <EOL> self . write ( stats ) <EOL> class ScaleJobHandler ( CorsHandlerWithAuth ) : <EOL> """<STR_LIT>""" <EOL> def get ( self , * args ) : <EOL> self . preflight ( ) <EOL> self . set_header ( '<STR_LIT:Content-Type>' , '<STR_LIT:application/json>' ) <EOL> m = self . settings . get ( '<STR_LIT>' ) <EOL> pname = "<STR_LIT>" % ( args [ <NUM_LIT:0> ] , args [ <NUM_LIT:1> ] ) <EOL> if not self . api_key . can_read ( pname ) : <EOL> raise HTTPError ( <NUM_LIT> ) <EOL> try : <EOL> t = m . _get_locked_state ( ) <EOL> except ProcessError as e : <EOL> self . set_status ( e . errno ) <EOL> return self . write ( e . to_dict ( ) ) <EOL> self . write ( { "<STR_LIT>" : t . numprocesses } ) <EOL> def post ( self , * args ) : <EOL> self . preflight ( ) <EOL> self . set_header ( '<STR_LIT:Content-Type>' , '<STR_LIT:application/json>' ) <EOL> m = self . settings . get ( '<STR_LIT>' ) <EOL> pname = "<STR_LIT>" % ( args [ <NUM_LIT:0> ] , args [ <NUM_LIT:1> ] ) <EOL> if not self . api_key . can_manage ( pname ) : <EOL> raise HTTPError ( <NUM_LIT> ) <EOL> try : <EOL> n = self . get_scaling_value ( ) <EOL> except ValueError : <EOL> self . set_status ( <NUM_LIT> ) <EOL> return self . write ( { "<STR_LIT:error>" : "<STR_LIT>" } ) <EOL> try : <EOL> ret = m . scale ( pname , n ) <EOL> except ProcessError as e : <EOL> self . set_status ( e . errno ) <EOL> return self . write ( e . to_dict ( ) ) <EOL> self . write ( { "<STR_LIT>" : ret } ) <EOL> def get_scaling_value ( self ) : <EOL> obj = json . loads ( self . request . body . decode ( '<STR_LIT:utf-8>' ) ) <EOL> if "<STR_LIT>" not in obj : <EOL> raise ValueError ( "<STR_LIT>" ) <EOL> return obj [ '<STR_LIT>' ] <EOL> class PidsJobHandler ( CorsHandlerWithAuth ) : <EOL> """<STR_LIT>""" <EOL> def get ( self , * args ) : <EOL> self . preflight ( ) <EOL> self . set_header ( '<STR_LIT:Content-Type>' , '<STR_LIT:application/json>' ) <EOL> m = self . settings . get ( '<STR_LIT>' ) <EOL> pname = "<STR_LIT>" % ( args [ <NUM_LIT:0> ] , args [ <NUM_LIT:1> ] ) <EOL> if not self . api_key . can_read ( pname ) : <EOL> raise HTTPError ( <NUM_LIT> ) <EOL> try : <EOL> pids = m . pids ( pname ) <EOL> except ProcessError as e : <EOL> self . set_status ( e . errno ) <EOL> return self . write ( e . to_dict ( ) ) <EOL> self . write ( { "<STR_LIT>" : pids } ) <EOL> class SignalJobHandler ( CorsHandlerWithAuth ) : <EOL> """<STR_LIT>""" <EOL> def post ( self , * args ) : <EOL> self . preflight ( ) <EOL> self . set_header ( '<STR_LIT:Content-Type>' , '<STR_LIT:application/json>' ) <EOL> m = self . settings . get ( '<STR_LIT>' ) <EOL> pname = "<STR_LIT>" % ( args [ <NUM_LIT:0> ] , args [ <NUM_LIT:1> ] ) <EOL> if not self . api_key . can_manage ( pname ) : <EOL> raise HTTPError ( <NUM_LIT> ) <EOL> try : <EOL> m . kill ( pname , self . get_signal_value ( ) ) <EOL> except ValueError : <EOL> self . set_status ( <NUM_LIT> ) <EOL> return self . write ( { "<STR_LIT:error>" : "<STR_LIT>" } ) <EOL> except ProcessError as e : <EOL> self . set_status ( e . errno ) <EOL> return self . write ( e . to_dict ( ) ) <EOL> self . send_status ( <NUM_LIT> ) <EOL> self . write ( { "<STR_LIT>" : True } ) <EOL> def get_signal_value ( self ) : <EOL> obj = json . loads ( self . request . body . decode ( '<STR_LIT:utf-8>' ) ) <EOL> if "<STR_LIT>" not in obj : <EOL> raise ValueError ( "<STR_LIT>" ) <EOL> return obj [ '<STR_LIT>' ] <EOL> class StateJobHandler ( CorsHandlerWithAuth ) : <EOL> def get ( self , * args ) : <EOL> self . preflight ( ) <EOL> m = self . settings . get ( '<STR_LIT>' ) <EOL> pname = "<STR_LIT>" % ( args [ <NUM_LIT:0> ] , args [ <NUM_LIT:1> ] ) <EOL> if not self . api_key . can_read ( pname ) : <EOL> raise HTTPError ( <NUM_LIT> ) <EOL> try : <EOL> t = m . _get_locked_state ( pname ) <EOL> except ProcessError as e : <EOL> self . set_status ( e . errno ) <EOL> return self . write ( e . to_dict ( ) ) <EOL> self . write ( str ( int ( t . active ) ) ) <EOL> def post ( self , * args ) : <EOL> self . preflight ( ) <EOL> self . set_header ( '<STR_LIT:Content-Type>' , '<STR_LIT:application/json>' ) <EOL> m = self . settings . get ( '<STR_LIT>' ) <EOL> pname = "<STR_LIT>" % ( args [ <NUM_LIT:0> ] , args [ <NUM_LIT:1> ] ) <EOL> if not self . api_key . can_manage ( pname ) : <EOL> raise HTTPError ( <NUM_LIT> ) <EOL> try : <EOL> do = self . get_action ( m ) <EOL> except ValueError : <EOL> self . set_status ( <NUM_LIT> ) <EOL> return self . write ( { "<STR_LIT:error>" : "<STR_LIT>" } ) <EOL> try : <EOL> do ( pname ) <EOL> except ProcessError as e : <EOL> self . set_status ( e . errno ) <EOL> return self . write ( e . to_dict ( ) ) <EOL> self . set_status ( <NUM_LIT> ) <EOL> self . write ( { "<STR_LIT>" : True } ) <EOL> def get_action ( self , m ) : <EOL> state = self . request . body <EOL> if state == b'<STR_LIT:1>' : <EOL> do = m . start_job <EOL> elif state == b'<STR_LIT:0>' : <EOL> do = m . stop_job <EOL> elif state == b'<STR_LIT:2>' : <EOL> do = m . reload <EOL> else : <EOL> raise ValueError ( "<STR_LIT>" ) <EOL> return do <EOL> class CommitJobHandler ( CorsHandlerWithAuth ) : <EOL> """<STR_LIT>""" <EOL> def post ( self , * args ) : <EOL> self . preflight ( ) <EOL> self . set_header ( '<STR_LIT:Content-Type>' , '<STR_LIT:application/json>' ) <EOL> m = self . settings . get ( '<STR_LIT>' ) <EOL> if not self . api_key . can_manage ( args [ <NUM_LIT:0> ] ) : <EOL> raise HTTPError ( <NUM_LIT> ) <EOL> try : <EOL> graceful_timeout , env = self . get_params ( ) <EOL> except ValueError : <EOL> self . set_status ( <NUM_LIT> ) <EOL> return self . write ( { "<STR_LIT:error>" : "<STR_LIT>" } ) <EOL> try : <EOL> pid = m . commit ( "<STR_LIT>" % ( args [ <NUM_LIT:0> ] , args [ <NUM_LIT:1> ] ) , <EOL> graceful_timeout = graceful_timeout , env = env ) <EOL> except ProcessError as e : <EOL> self . set_status ( e . errno ) <EOL> return self . write ( e . to_dict ( ) ) <EOL> self . write ( { "<STR_LIT>" : pid } ) <EOL> def get_params ( self ) : <EOL> obj = json . loads ( self . request . body . decode ( '<STR_LIT:utf-8>' ) ) <EOL> env = obj . get ( '<STR_LIT>' ) <EOL> graceful_timeout = obj . get ( '<STR_LIT>' ) <EOL> if graceful_timeout is not None : <EOL> try : <EOL> graceful_timeout = int ( obj . get ( '<STR_LIT>' ) ) <EOL> except TypeError as e : <EOL> raise ValueError ( str ( e ) ) <EOL> return graceful_timeout , env </s>
