<s> import getpass <EOL> import paramiko <EOL> import json <EOL> import requests <EOL> import interactive <EOL> import auth <EOL> import util <EOL> from exceptions import AlaudaExecError <EOL> class Executer ( object ) : <EOL> def __init__ ( self , name , username , namespace , exec_endpoint = '<STR_LIT>' , verbose = False ) : <EOL> self . name = name if username == namespace else '<STR_LIT>' . format ( namespace , name ) <EOL> self . namespace = namespace <EOL> self . username = username <EOL> self . exec_endpoint = exec_endpoint <EOL> self . port = <NUM_LIT> <EOL> self . client = None <EOL> self . chan = None <EOL> self . verbose = verbose <EOL> def connect ( self ) : <EOL> if self . verbose : <EOL> print ( '<STR_LIT>' ) <EOL> self . client = paramiko . SSHClient ( ) <EOL> self . client . load_system_host_keys ( ) <EOL> self . client . set_missing_host_key_policy ( paramiko . AutoAddPolicy ( ) ) <EOL> password = getpass . getpass ( '<STR_LIT>' % ( self . username , self . exec_endpoint ) ) <EOL> self . client . connect ( self . exec_endpoint , <EOL> self . port , <EOL> username = self . username , <EOL> password = password , <EOL> allow_agent = False , <EOL> look_for_keys = False ) <EOL> if self . verbose : <EOL> print ( repr ( self . client . get_transport ( ) ) ) <EOL> def execute ( self , command , * args ) : <EOL> try : <EOL> self . connect ( ) <EOL> transport = self . client . get_transport ( ) <EOL> self . chan = transport . open_session ( ) <EOL> self . chan . get_pty ( ) <EOL> self . chan . exec_command ( '<STR_LIT>' . format ( self . name , command , '<STR_LIT:U+0020>' . join ( args ) ) ) <EOL> interactive . interactive_shell ( self . chan ) <EOL> self . close ( ) <EOL> except Exception as e : <EOL> try : <EOL> self . close ( ) <EOL> except : <EOL> pass <EOL> raise AlaudaExecError ( '<STR_LIT>' . format ( command , e ) ) <EOL> def close ( self ) : <EOL> if self . chan : <EOL> self . chan . close ( ) <EOL> self . client . close ( ) <EOL> @ classmethod <EOL> def fetch ( cls , name , namespace = None ) : <EOL> service_name = name . split ( "<STR_LIT:.>" ) [ <NUM_LIT:0> ] <EOL> api_endpoint , token , username = auth . load_token ( ) <EOL> url = api_endpoint + '<STR_LIT>' . format ( namespace or username ) + service_name <EOL> headers = auth . build_headers ( token ) <EOL> r = requests . get ( url , headers = headers ) <EOL> util . check_response ( r ) <EOL> data = json . loads ( r . text ) <EOL> executer = cls ( name = name , <EOL> username = username , <EOL> exec_endpoint = data [ '<STR_LIT>' ] , <EOL> namespace = data [ '<STR_LIT>' ] ) <EOL> return executer </s>
