<s> import weblab . experiment . experiment as Experiment <EOL> from voodoo . override import Override <EOL> from voodoo . log import logged <EOL> import voodoo . log as log <EOL> import uuid <EOL> import time <EOL> from voodoo . threaded import threaded <EOL> import weblab . experiment . devices . vm . manager as VirtualMachineManager <EOL> import experiments . vm . user_manager . manager as UserManager <EOL> DEBUG = False <EOL> DEBUG_NOT_PREPARE = False <EOL> CFG_URL = "<STR_LIT>" <EOL> CFG_VM_TYPE = "<STR_LIT>" <EOL> CFG_USER_MANAGER_TYPE = "<STR_LIT>" <EOL> CFG_SHOULD_STORE_IMAGE = "<STR_LIT>" <EOL> CFG_ESTIMATED_LOAD_TIME = "<STR_LIT>" <EOL> PWD_LENGTH = <NUM_LIT:8> <EOL> DEFAULT_URL = "<STR_LIT>" <EOL> DEFAULT_VM_TYPE = "<STR_LIT>" <EOL> DEFAULT_USER_MANAGER_TYPE = "<STR_LIT>" <EOL> DEFAULT_SHOULD_STORE_IMAGE = True <EOL> DEFAULT_ESTIMATED_LOAD_TIME = <NUM_LIT:15> <EOL> TIME_WAITING_START = <NUM_LIT:10> <EOL> class VMExperiment ( Experiment . Experiment ) : <EOL> def __init__ ( self , coord_address , locator , cfg_manager , * args , ** kwargs ) : <EOL> super ( VMExperiment , self ) . __init__ ( * args , ** kwargs ) <EOL> self . _cfg_manager = cfg_manager <EOL> self . read_base_config ( ) <EOL> self . session_id = None <EOL> self . vm = self . find_vm_manager ( self . vm_type ) ( self . _cfg_manager ) <EOL> self . user_manager_class = self . find_user_manager ( self . user_manager_type ) <EOL> self . user_manager = None <EOL> self . is_ready = False <EOL> self . is_error = False <EOL> self . error = None <EOL> self . _start_t = None <EOL> self . _dispose_t = None <EOL> def read_base_config ( self ) : <EOL> """<STR_LIT>""" <EOL> self . url = self . _cfg_manager . get_value ( CFG_URL , DEFAULT_URL ) <EOL> self . vm_type = self . _cfg_manager . get_value ( CFG_VM_TYPE , DEFAULT_VM_TYPE ) <EOL> self . user_manager_type = self . _cfg_manager . get_value ( CFG_USER_MANAGER_TYPE , DEFAULT_USER_MANAGER_TYPE ) <EOL> self . should_store_image = self . _cfg_manager . get_value ( CFG_SHOULD_STORE_IMAGE , DEFAULT_SHOULD_STORE_IMAGE ) <EOL> self . estimated_load_time = self . _cfg_manager . get_value ( CFG_ESTIMATED_LOAD_TIME , DEFAULT_ESTIMATED_LOAD_TIME ) <EOL> @ Override ( Experiment . Experiment ) <EOL> @ logged ( "<STR_LIT:info>" ) <EOL> def do_get_api ( self ) : <EOL> return "<STR_LIT:1>" <EOL> @ logged ( "<STR_LIT:info>" ) <EOL> @ Override ( Experiment . Experiment ) <EOL> def do_start_experiment ( self , * args , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> um = self . user_manager <EOL> if um is not None : <EOL> um . cancel ( ) <EOL> initial = time . time ( ) <EOL> while self . is_ready and ( initial + TIME_WAITING_START ) > time . time ( ) : <EOL> if DEBUG : <EOL> print "<STR_LIT>" , self . is_ready <EOL> time . sleep ( <NUM_LIT:0.1> ) <EOL> if self . is_ready : <EOL> return "<STR_LIT>" <EOL> if self . is_error : <EOL> return "<STR_LIT>" , str ( self . error ) <EOL> if self . _start_t != None and self . _start_t . isAlive ( ) : <EOL> return "<STR_LIT>" <EOL> self . session_id = self . generate_session_id ( ) <EOL> self . _start_t = self . handle_start_exp_t ( ) <EOL> return "<STR_LIT>" <EOL> @ Override ( Experiment . Experiment ) <EOL> @ logged ( "<STR_LIT:info>" ) <EOL> def do_send_command_to_device ( self , command ) : <EOL> """<STR_LIT>""" <EOL> if command == "<STR_LIT>" : <EOL> return self . url + "<STR_LIT>" + self . session_id <EOL> elif command == "<STR_LIT>" : <EOL> if self . is_ready : return "<STR_LIT:1>" <EOL> if self . is_error : return "<STR_LIT>" % str ( self . error ) <EOL> return "<STR_LIT>" % self . estimated_load_time <EOL> elif command == "<STR_LIT>" : <EOL> if not self . is_ready : return "<STR_LIT:0>" <EOL> if self . vm . is_alive_vm ( ) : return "<STR_LIT:1>" <EOL> return "<STR_LIT:0>" <EOL> return "<STR_LIT>" <EOL> @ Override ( Experiment . Experiment ) <EOL> @ logged ( "<STR_LIT:info>" ) <EOL> def do_send_file_to_device ( self , content , file_info ) : <EOL> """<STR_LIT>""" <EOL> if ( DEBUG ) : <EOL> print "<STR_LIT>" <EOL> return "<STR_LIT>" <EOL> @ Override ( Experiment . Experiment ) <EOL> @ logged ( "<STR_LIT:info>" ) <EOL> def do_dispose ( self ) : <EOL> """<STR_LIT>""" <EOL> self . handle_dispose ( ) <EOL> return "<STR_LIT>" <EOL> @ threaded ( ) <EOL> def handle_start_exp_t ( self ) : <EOL> """<STR_LIT>""" <EOL> if DEBUG : <EOL> print "<STR_LIT>" <EOL> self . ensure_vm_not_started ( ) <EOL> if not DEBUG_NOT_PREPARE : <EOL> self . vm . prepare_vm ( ) <EOL> if DEBUG : <EOL> print "<STR_LIT>" <EOL> print type ( self . vm ) <EOL> print self . vm_type <EOL> self . vm . launch_vm ( ) <EOL> if DEBUG : <EOL> print "<STR_LIT>" <EOL> self . setup ( ) <EOL> if DEBUG : <EOL> print "<STR_LIT>" <EOL> if self . is_error == True : <EOL> self . is_ready = False <EOL> else : <EOL> self . is_ready = True <EOL> def ensure_vm_not_started ( self ) : <EOL> """<STR_LIT>""" <EOL> if DEBUG : <EOL> print "<STR_LIT>" <EOL> start_time = time . time ( ) <EOL> while self . vm . is_alive_vm ( ) : <EOL> if DEBUG : <EOL> print "<STR_LIT>" <EOL> self . vm . kill_vm ( ) <EOL> elapsed = time . time ( ) - start_time <EOL> if elapsed > <NUM_LIT:20> : <EOL> raise Exception ( "<STR_LIT>" ) <EOL> def handle_dispose ( self ) : <EOL> """<STR_LIT>""" <EOL> if self . user_manager is not None : <EOL> self . user_manager . cancel ( ) <EOL> self . vm . kill_vm ( ) <EOL> if ( self . should_store_image ) : <EOL> self . vm . store_image ( ) <EOL> self . is_ready = False <EOL> self . error = None <EOL> self . is_error = False <EOL> self . _start_t = None <EOL> self . user_manager = None <EOL> def load_user_manager ( self ) : <EOL> self . user_manager = self . user_manager_class ( self . _cfg_manager ) <EOL> def setup ( self ) : <EOL> """<STR_LIT>""" <EOL> self . load_user_manager ( ) <EOL> try : <EOL> self . user_manager . configure ( self . session_id ) <EOL> if DEBUG : <EOL> print "<STR_LIT>" <EOL> except Exception as ex : <EOL> self . error = ex <EOL> self . is_error = True <EOL> log . log ( <EOL> VMExperiment , <EOL> log . level . Error , <EOL> "<STR_LIT>" % ex <EOL> ) <EOL> log . log_exc ( <EOL> VMExperiment , <EOL> log . level . Warning <EOL> ) <EOL> def generate_session_id ( self ) : <EOL> """<STR_LIT>""" <EOL> """<STR_LIT>""" <EOL> id = uuid . uuid1 ( ) <EOL> idstr = id . get_hex ( ) [ : PWD_LENGTH ] <EOL> return idstr <EOL> def find_user_manager ( self , name ) : <EOL> """<STR_LIT>""" <EOL> subclasses = UserManager . UserManager . __subclasses__ ( ) <EOL> for sc in subclasses : <EOL> if sc . __name__ == name : <EOL> return sc <EOL> raise Exception ( """<STR_LIT>""" ) <EOL> def find_vm_manager ( self , name ) : <EOL> """<STR_LIT>""" <EOL> subclasses = VirtualMachineManager . VirtualMachineManager . __subclasses__ ( ) <EOL> for sc in subclasses : <EOL> if sc . __name__ == name : <EOL> return sc <EOL> raise Exception ( """<STR_LIT>""" ) </s>
