<s> import select <EOL> from paramiko import RSAKey , SSHClient , AutoAddPolicy <EOL> from os . path import exists <EOL> from . securefile import SecureFileStorage <EOL> class Transport ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , server ) : <EOL> self . server = server <EOL> self . callback = lambda out : None <EOL> def set_stdout_callback ( self , callback ) : <EOL> self . callback = callback <EOL> class SSHTransport ( Transport ) : <EOL> """<STR_LIT>""" <EOL> output_timeout = <NUM_LIT:0.5> <EOL> output_buffer = <NUM_LIT> <EOL> def __init__ ( self , server ) : <EOL> super ( SSHTransport , self ) . __init__ ( server ) <EOL> self . secure_files = SecureFileStorage ( self . server . environment_id ) <EOL> self . client = self . create_client ( ) <EOL> self . channel = None <EOL> def run ( self , command ) : <EOL> """<STR_LIT>""" <EOL> self . channel = self . client . get_transport ( ) . open_session ( ) <EOL> self . channel . get_pty ( ) <EOL> self . channel . exec_command ( command ) <EOL> while True : <EOL> rl , _ , _ = select . select ( [ self . channel ] , [ ] , [ ] , self . output_timeout ) <EOL> if len ( rl ) > <NUM_LIT:0> : <EOL> output = self . channel . recv ( self . output_buffer ) <EOL> if output : <EOL> self . callback ( output ) <EOL> else : <EOL> break <EOL> return self . channel . recv_exit_status ( ) <EOL> def create_client ( self ) : <EOL> """<STR_LIT>""" <EOL> private = RSAKey ( filename = self . get_private_key_file ( ) ) <EOL> client = SSHClient ( ) <EOL> client . set_missing_host_key_policy ( AutoAddPolicy ( ) ) <EOL> client . load_host_keys ( self . get_host_keys_file ( ) ) <EOL> if self . server . authentication_method == self . server . OPENSSH_PASSWORD : <EOL> client . connect ( self . server . host , password = self . server . password , <EOL> look_for_keys = False , port = self . server . port , username = self . server . user ) <EOL> elif self . server . authentication_method == self . server . OPENSSH_CERTIFICATE : <EOL> client . connect ( self . server . host , pkey = private , <EOL> look_for_keys = False , port = self . server . port , username = self . server . user ) <EOL> return client <EOL> def close_client ( self ) : <EOL> """<STR_LIT>""" <EOL> self . client . save_host_keys ( self . get_host_keys_file ( ) ) <EOL> if self . channel : <EOL> self . channel . close ( ) <EOL> self . client . close ( ) <EOL> def kill ( self ) : <EOL> """<STR_LIT>""" <EOL> self . close_client ( ) <EOL> def get_host_keys_file ( self ) : <EOL> """<STR_LIT>""" <EOL> filename = self . secure_files . known_hosts . get_file_name ( ) <EOL> if not exists ( filename ) : <EOL> raise RuntimeError ( '<STR_LIT>' ) <EOL> return filename <EOL> def get_private_key_file ( self ) : <EOL> """<STR_LIT>""" <EOL> filename = self . secure_files . private_key . get_file_name ( ) <EOL> if not exists ( filename ) : <EOL> raise RuntimeError ( '<STR_LIT>' ) <EOL> return filename <EOL> class Server ( object ) : <EOL> """<STR_LIT>""" <EOL> OPENSSH_PASSWORD = <NUM_LIT:1> <EOL> OPENSSH_CERTIFICATE = <NUM_LIT:2> <EOL> def __init__ ( self ) : <EOL> self . environment_id = None <EOL> self . host = None <EOL> self . port = None <EOL> self . user = None <EOL> self . authentication_method = '<STR_LIT:key>' <EOL> self . password = None <EOL> @ staticmethod <EOL> def from_model ( model ) : <EOL> instance = Server ( ) <EOL> instance . environment_id = model . environment_id <EOL> instance . host = model . host <EOL> instance . port = model . port <EOL> instance . user = model . user <EOL> instance . password = model . serverauthentication . password <EOL> instance . authentication_method = model . method <EOL> return instance </s>
