<s> import os <EOL> import json <EOL> import time <EOL> import signal <EOL> import socket <EOL> import argparse <EOL> import subprocess <EOL> import threading <EOL> from terminalcloud import terminal <EOL> key_name = '<STR_LIT>' <EOL> class timeout : <EOL> def __init__ ( self , seconds = <NUM_LIT:1> , error_message = '<STR_LIT>' ) : <EOL> self . seconds = seconds <EOL> self . error_message = error_message <EOL> def handle_timeout ( self , signum , frame ) : <EOL> print self . error_message <EOL> def __enter__ ( self ) : <EOL> signal . signal ( signal . SIGALRM , self . handle_timeout ) <EOL> signal . alarm ( self . seconds ) <EOL> def __exit__ ( self , type , value , traceback ) : <EOL> signal . alarm ( <NUM_LIT:0> ) <EOL> def generate_ssh_key ( key_file ) : <EOL> subprocess . call ( [ '<STR_LIT>' , '<STR_LIT>' , key_file , '<STR_LIT>' , '<STR_LIT>' ] ) <EOL> os . chmod ( key_file , <NUM_LIT:0> <NUM_LIT> ) <EOL> def get_public_key ( key_file ) : <EOL> with open ( key_file ) as f : <EOL> content = f . readlines ( ) [ <NUM_LIT:0> ] . rstrip ( '<STR_LIT:\n>' ) <EOL> return str ( content ) <EOL> def start_snap ( name , snapshot_id , size = None , script = None ) : <EOL> output = terminal . start_snapshot ( snapshot_id , size , None , name , None , script ) <EOL> time . sleep ( int ( int ( args . quantity ) * <NUM_LIT> ) + <NUM_LIT:1> ) <EOL> request_id = output [ '<STR_LIT>' ] <EOL> output = terminal . request_progress ( request_id ) <EOL> try : <EOL> if output [ '<STR_LIT:status>' ] != '<STR_LIT:success>' : <EOL> pass <EOL> except : <EOL> time . sleep ( int ( int ( args . quantity ) * <NUM_LIT> ) + <NUM_LIT:1> ) <EOL> output = terminal . request_progress ( request_id ) <EOL> state = '<STR_LIT:None>' <EOL> while output [ '<STR_LIT:status>' ] != '<STR_LIT:success>' : <EOL> try : <EOL> time . sleep ( int ( args . quantity ) * <NUM_LIT> + <NUM_LIT:1> ) <EOL> output = terminal . request_progress ( request_id ) <EOL> if output [ '<STR_LIT:state>' ] != state : <EOL> state = output [ '<STR_LIT:state>' ] <EOL> print ( '<STR_LIT>' % ( name , state ) ) <EOL> except : <EOL> print "<STR_LIT>" % name <EOL> time . sleep ( int ( args . quantity * <NUM_LIT> ) + <NUM_LIT:2> ) <EOL> done = False <EOL> while done is False : <EOL> try : <EOL> output = terminal . request_progress ( request_id ) <EOL> container_key = output [ '<STR_LIT:result>' ] [ '<STR_LIT>' ] <EOL> subdomain = output [ '<STR_LIT:result>' ] [ '<STR_LIT>' ] <EOL> container_ip = output [ '<STR_LIT:result>' ] [ '<STR_LIT>' ] <EOL> done = True <EOL> except : <EOL> print "<STR_LIT>" % name <EOL> time . sleep ( int ( int ( args . quantity ) * <NUM_LIT> ) + <NUM_LIT:1> ) <EOL> done = False <EOL> return container_key , container_ip , subdomain <EOL> def run_on_terminal ( cip , user , pemfile , script ) : <EOL> try : <EOL> p = subprocess . Popen ( <EOL> [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , pemfile , <EOL> user + '<STR_LIT:@>' + cip , script ] , <NUM_LIT:0> , None , None , None , None ) <EOL> p . wait ( ) <EOL> return p . returncode <EOL> except Exception , e : <EOL> return '<STR_LIT>' % e <EOL> def get_script ( filename ) : <EOL> try : <EOL> with open ( filename ) as f : <EOL> data = f . read ( ) <EOL> data . replace ( '<STR_LIT:\n>' , '<STR_LIT:;>' ) <EOL> return data <EOL> except Exception , e : <EOL> print '<STR_LIT>' % e <EOL> return None <EOL> def send_script ( cip , user , pemfile , script , ) : <EOL> print ( cip , user , script , pemfile ) <EOL> destination = '<STR_LIT>' % ( user , cip ) <EOL> try : <EOL> p = subprocess . Popen ( <EOL> [ '<STR_LIT>' , '<STR_LIT>' , pemfile , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , <EOL> script , destination ] ) <EOL> p . wait ( ) <EOL> return p . returncode <EOL> except Exception , e : <EOL> return '<STR_LIT>' % e <EOL> def args_sanitizer ( args ) : <EOL> if args . size not in terminal . instance_types ( ) [ '<STR_LIT>' ] : <EOL> print "<STR_LIT>" <EOL> exit ( <NUM_LIT:1> ) <EOL> if ( int ( args . quantity ) < <NUM_LIT:1> or int ( args . quantity ) > <NUM_LIT> ) : <EOL> print "<STR_LIT>" <EOL> exit ( <NUM_LIT:1> ) <EOL> if args . method not in ( '<STR_LIT>' ) : <EOL> print "<STR_LIT>" <EOL> exit ( <NUM_LIT:1> ) <EOL> if args . ssh_key_file is None : <EOL> key_name = args . ssh_key_file <EOL> def start_terminal_worker ( name ) : <EOL> container_key , container_ip , subdomain = start_snap ( name , snapshot_id , args . size , script ) <EOL> terms . append ( { '<STR_LIT>' : container_key , '<STR_LIT>' : container_ip , '<STR_LIT>' : subdomain , '<STR_LIT:name>' : name } ) <EOL> time . sleep ( <NUM_LIT:2> ) <EOL> terminal . add_authorized_key_to_terminal ( container_key , publicKey ) <EOL> if args . method == '<STR_LIT>' and args . script is not None : <EOL> print '<STR_LIT>' <EOL> send_script ( container_ip , '<STR_LIT:root>' , key_name , args . script ) <EOL> print '<STR_LIT>' <EOL> run_on_terminal ( container_ip , '<STR_LIT:root>' , key_name , '<STR_LIT>' % os . path . basename ( args . script ) ) <EOL> def add_terminal_links_worker ( container_key , links ) : <EOL> output = terminal . add_terminal_links ( container_key , links ) <EOL> print output <EOL> return output <EOL> def single_thread ( ) : <EOL> terms = [ ] <EOL> for i in range ( args . quantity ) : <EOL> name = '<STR_LIT>' % ( args . name , i ) <EOL> print "<STR_LIT>" % name <EOL> container_key , container_ip , subdomain = start_snap ( name , snapshot_id , args . size , script ) <EOL> terms . append ( { '<STR_LIT>' : container_key , '<STR_LIT>' : container_ip , '<STR_LIT>' : subdomain , '<STR_LIT:name>' : name } ) <EOL> time . sleep ( <NUM_LIT:1> ) <EOL> if args . method == '<STR_LIT>' : <EOL> for i in range ( len ( terms ) ) : <EOL> terminal . add_authorized_key_to_terminal ( terms [ i ] [ '<STR_LIT>' ] , publicKey ) <EOL> time . sleep ( <NUM_LIT:1> ) <EOL> if args . script is not None : <EOL> print "<STR_LIT>" <EOL> send_script ( terms [ i ] [ '<STR_LIT>' ] , '<STR_LIT:root>' , key_name , args . script ) <EOL> print "<STR_LIT>" <EOL> run_on_terminal ( terms [ i ] [ '<STR_LIT>' ] , '<STR_LIT:root>' , key_name , '<STR_LIT>' % os . path . basename ( args . script ) ) <EOL> elif args . method == '<STR_LIT>' : <EOL> for i in range ( len ( terms ) ) : <EOL> terminal . add_authorized_key_to_terminal ( terms [ i ] [ '<STR_LIT>' ] , publicKey ) <EOL> time . sleep ( <NUM_LIT:1> ) <EOL> if args . ports is not None : <EOL> host_subdomain = socket . gethostname ( ) <EOL> ports = args . ports . split ( '<STR_LIT:U+002C>' ) <EOL> terms . append ( terminal . get_terminal ( None , host_subdomain ) [ '<STR_LIT>' ] ) <EOL> for t in range ( len ( terms ) ) : <EOL> container_key = terms [ t ] [ '<STR_LIT>' ] <EOL> links = [ ] <EOL> for s in range ( len ( terms ) ) : <EOL> for port in range ( len ( ports ) ) : <EOL> link = { '<STR_LIT:port>' : ( ports [ port ] ) , '<STR_LIT:source>' : terms [ s ] [ '<STR_LIT>' ] } <EOL> links . append ( link ) <EOL> terminal . add_terminal_links ( container_key , links ) <EOL> return terms <EOL> def multi_thread ( ) : <EOL> global terms <EOL> terms = [ ] <EOL> threads = [ ] <EOL> for i in range ( args . quantity ) : <EOL> name = '<STR_LIT>' % ( args . name , i ) <EOL> t = threading . Thread ( target = start_terminal_worker , args = ( name , ) , name = name ) <EOL> threads . append ( t ) <EOL> t . setDaemon ( True ) <EOL> print '<STR_LIT>' % name <EOL> if i % <NUM_LIT:3> == <NUM_LIT:0> : <EOL> time . sleep ( int ( int ( args . quantity ) * <NUM_LIT> ) + <NUM_LIT:1> ) <EOL> t . start ( ) <EOL> for th in range ( len ( threads ) ) : <EOL> while threads [ th ] . is_alive ( ) : <EOL> time . sleep ( <NUM_LIT:1> ) <EOL> if args . ports is not None : <EOL> threads = [ ] <EOL> host_subdomain = socket . gethostname ( ) <EOL> ports = args . ports . split ( '<STR_LIT:U+002C>' ) <EOL> terms . append ( terminal . get_terminal ( None , host_subdomain ) [ '<STR_LIT>' ] ) <EOL> for term in range ( len ( terms ) ) : <EOL> container_key = terms [ term ] [ '<STR_LIT>' ] <EOL> links = [ ] <EOL> for s in range ( len ( terms ) ) : <EOL> for port in range ( len ( ports ) ) : <EOL> link = { '<STR_LIT:port>' : str ( ports [ port ] ) , '<STR_LIT:source>' : terms [ s ] [ '<STR_LIT>' ] } <EOL> links . append ( link ) <EOL> t = threading . Thread ( target = add_terminal_links_worker , args = ( container_key , links ) , name = terms [ term ] [ '<STR_LIT>' ] ) <EOL> threads . append ( t ) <EOL> t . setDaemon ( True ) <EOL> if ( term + <NUM_LIT:1> ) % <NUM_LIT:8> == <NUM_LIT:0> : <EOL> time . sleep ( int ( int ( len ( terms ) ) * <NUM_LIT> ) + <NUM_LIT:1> ) <EOL> print '<STR_LIT>' % t . name <EOL> t . start ( ) <EOL> for th in range ( len ( threads ) ) : <EOL> while threads [ th ] . is_alive ( ) : <EOL> time . sleep ( <NUM_LIT:1> ) <EOL> return terms <EOL> if __name__ == '<STR_LIT:__main__>' : <EOL> parser = argparse . ArgumentParser ( ) <EOL> parser . add_argument ( "<STR_LIT>" , type = int , help = "<STR_LIT>" ) <EOL> parser . add_argument ( '<STR_LIT>' , "<STR_LIT>" , type = str , default = '<STR_LIT>' , help = "<STR_LIT>" ) <EOL> parser . add_argument ( "<STR_LIT>" , "<STR_LIT>" , type = str , default = '<STR_LIT>' , help = "<STR_LIT>" ) <EOL> parser . add_argument ( "<STR_LIT>" , "<STR_LIT>" , type = str , default = None , help = "<STR_LIT>" ) <EOL> parser . add_argument ( "<STR_LIT>" , "<STR_LIT>" , type = str , default = None , help = "<STR_LIT>" ) <EOL> parser . add_argument ( "<STR_LIT:-c>" , "<STR_LIT>" , type = str , default = '<STR_LIT>' , help = "<STR_LIT>" ) <EOL> parser . add_argument ( "<STR_LIT>" , "<STR_LIT>" , type = str , default = None , help = "<STR_LIT>" ) <EOL> parser . add_argument ( '<STR_LIT>' , "<STR_LIT>" , type = str , default = '<STR_LIT>' , help = "<STR_LIT>" ) <EOL> parser . add_argument ( '<STR_LIT>' , "<STR_LIT>" , type = str , default = '<STR_LIT>' , help = "<STR_LIT>" ) <EOL> parser . add_argument ( '<STR_LIT>' , "<STR_LIT>" , type = str , default = None , help = "<STR_LIT>" ) <EOL> parser . add_argument ( '<STR_LIT>' , "<STR_LIT>" , type = str , default = None , help = "<STR_LIT>" ) <EOL> parser . add_argument ( '<STR_LIT>' , "<STR_LIT>" , type = str , default = '<STR_LIT>' , help = "<STR_LIT>" ) <EOL> parser . description = "<STR_LIT>" <EOL> args = parser . parse_args ( ) <EOL> terminal . setup_credentials ( args . utoken , args . atoken , args . creds ) <EOL> args_sanitizer ( args ) <EOL> snapshot_id = args . snapshot_id <EOL> if args . method == '<STR_LIT>' : <EOL> script = None <EOL> else : <EOL> if args . script is not None : <EOL> script = get_script ( args . script ) <EOL> else : <EOL> script = None <EOL> if args . method == '<STR_LIT>' or args . method == '<STR_LIT>' : <EOL> if args . ssh_key_file is None : <EOL> generate_ssh_key ( key_name ) <EOL> else : <EOL> key_name = args . ssh_key_file <EOL> publicKey = get_public_key ( '<STR_LIT>' % key_name ) <EOL> if args . threading == '<STR_LIT>' : <EOL> terminals = multi_thread ( ) <EOL> else : <EOL> terminals = single_thread ( ) <EOL> host = terminals . pop ( ) <EOL> print json . dumps ( terminals ) </s>
