<s> import json <EOL> import re <EOL> import socket <EOL> import signal <EOL> import ssl <EOL> import time <EOL> import SOAPpy <EOL> from appscale_logger import AppScaleLogger <EOL> from custom_exceptions import AppControllerException <EOL> from custom_exceptions import TimeoutException <EOL> from custom_exceptions import AppScaleException <EOL> class AppControllerClient ( ) : <EOL> """<STR_LIT>""" <EOL> PORT = <NUM_LIT> <EOL> WAIT_TIME = <NUM_LIT:10> <EOL> MAX_RETRIES = <NUM_LIT:100> <EOL> BAD_SECRET_MESSAGE = '<STR_LIT>' <EOL> DEFAULT_NUM_RETRIES = <NUM_LIT:5> <EOL> DEFAULT_TIMEOUT = <NUM_LIT:10> <EOL> LONGER_TIMEOUT = <NUM_LIT:20> <EOL> def __init__ ( self , host , secret ) : <EOL> """<STR_LIT>""" <EOL> self . host = host <EOL> self . server = SOAPpy . SOAPProxy ( '<STR_LIT>' % ( host , <EOL> self . PORT ) ) <EOL> self . secret = secret <EOL> if hasattr ( ssl , '<STR_LIT>' ) : <EOL> ssl . _create_default_https_context = ssl . _create_unverified_context <EOL> def run_with_timeout ( self , timeout_time , default , num_retries , function , <EOL> * args ) : <EOL> """<STR_LIT>""" <EOL> def timeout_handler ( _ , __ ) : <EOL> """<STR_LIT>""" <EOL> raise TimeoutException ( ) <EOL> signal . signal ( signal . SIGALRM , timeout_handler ) <EOL> signal . alarm ( timeout_time ) <EOL> try : <EOL> retval = function ( * args ) <EOL> except TimeoutException : <EOL> return default <EOL> except socket . error as exception : <EOL> signal . alarm ( <NUM_LIT:0> ) <EOL> if num_retries > <NUM_LIT:0> : <EOL> time . sleep ( <NUM_LIT:1> ) <EOL> return self . run_with_timeout ( timeout_time , default , num_retries - <NUM_LIT:1> , <EOL> function , * args ) <EOL> else : <EOL> raise exception <EOL> except ssl . SSLError : <EOL> signal . alarm ( <NUM_LIT:0> ) <EOL> return self . run_with_timeout ( timeout_time , default , num_retries , function , <EOL> * args ) <EOL> finally : <EOL> signal . alarm ( <NUM_LIT:0> ) <EOL> if retval == self . BAD_SECRET_MESSAGE : <EOL> raise AppControllerException ( "<STR_LIT>" + "<STR_LIT>" ) <EOL> return retval <EOL> def set_parameters ( self , locations , credentials , app = None ) : <EOL> """<STR_LIT>""" <EOL> if app is None : <EOL> app = '<STR_LIT:none>' <EOL> result = self . run_with_timeout ( self . DEFAULT_TIMEOUT , "<STR_LIT>" , self . DEFAULT_NUM_RETRIES , <EOL> self . server . set_parameters , json . dumps ( locations ) , credentials , [ app ] , self . secret ) <EOL> if result . startswith ( '<STR_LIT>' ) : <EOL> raise AppControllerException ( result ) <EOL> def get_all_public_ips ( self ) : <EOL> """<STR_LIT>""" <EOL> all_ips = self . run_with_timeout ( self . DEFAULT_TIMEOUT , "<STR_LIT>" , self . DEFAULT_NUM_RETRIES , <EOL> self . server . get_all_public_ips , self . secret ) <EOL> if all_ips == "<STR_LIT>" : <EOL> return [ ] <EOL> else : <EOL> return json . loads ( all_ips ) <EOL> def get_role_info ( self ) : <EOL> """<STR_LIT>""" <EOL> role_info = self . run_with_timeout ( self . DEFAULT_TIMEOUT , "<STR_LIT>" , self . DEFAULT_NUM_RETRIES , <EOL> self . server . get_role_info , self . secret ) <EOL> if role_info == "<STR_LIT>" : <EOL> return { } <EOL> else : <EOL> return json . loads ( role_info ) <EOL> def get_status ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . run_with_timeout ( self . DEFAULT_TIMEOUT , "<STR_LIT>" , self . DEFAULT_NUM_RETRIES , <EOL> self . server . status , self . secret ) <EOL> def is_initialized ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . run_with_timeout ( self . DEFAULT_TIMEOUT , False , self . DEFAULT_NUM_RETRIES , <EOL> self . server . is_done_initializing , self . secret ) <EOL> def start_roles_on_nodes ( self , roles_to_nodes ) : <EOL> """<STR_LIT>""" <EOL> return self . run_with_timeout ( self . DEFAULT_TIMEOUT , "<STR_LIT>" , self . DEFAULT_NUM_RETRIES , <EOL> self . server . start_roles_on_nodes , roles_to_nodes , self . secret ) <EOL> def stop_app ( self , app_id ) : <EOL> """<STR_LIT>""" <EOL> return self . run_with_timeout ( self . DEFAULT_TIMEOUT , "<STR_LIT>" , self . DEFAULT_NUM_RETRIES , <EOL> self . server . stop_app , app_id , self . secret ) <EOL> def is_app_running ( self , app_id ) : <EOL> """<STR_LIT>""" <EOL> return self . run_with_timeout ( self . DEFAULT_TIMEOUT , "<STR_LIT>" , self . DEFAULT_NUM_RETRIES , <EOL> self . server . is_app_running , app_id , self . secret ) <EOL> def done_uploading ( self , app_id , remote_app_location ) : <EOL> """<STR_LIT>""" <EOL> return self . run_with_timeout ( self . DEFAULT_TIMEOUT , "<STR_LIT>" , self . DEFAULT_NUM_RETRIES , <EOL> self . server . done_uploading , app_id , remote_app_location , self . secret ) <EOL> def update ( self , apps_to_run ) : <EOL> """<STR_LIT>""" <EOL> return self . run_with_timeout ( self . DEFAULT_TIMEOUT , "<STR_LIT>" , self . DEFAULT_NUM_RETRIES , <EOL> self . server . update , apps_to_run , self . secret ) <EOL> def get_app_info_map ( self ) : <EOL> """<STR_LIT>""" <EOL> return json . loads ( self . run_with_timeout ( self . DEFAULT_TIMEOUT , '<STR_LIT:{}>' , self . DEFAULT_NUM_RETRIES , <EOL> self . server . get_app_info_map , self . secret ) ) <EOL> def relocate_app ( self , appid , http_port , https_port ) : <EOL> """<STR_LIT>""" <EOL> return self . run_with_timeout ( self . LONGER_TIMEOUT , "<STR_LIT>" , <EOL> self . DEFAULT_NUM_RETRIES , self . server . relocate_app , appid , http_port , <EOL> https_port , self . secret ) <EOL> def get_property ( self , property_regex ) : <EOL> """<STR_LIT>""" <EOL> return json . loads ( self . run_with_timeout ( self . DEFAULT_TIMEOUT , '<STR_LIT:{}>' , self . DEFAULT_NUM_RETRIES , <EOL> self . server . get_property , property_regex , self . secret ) ) <EOL> def set_property ( self , property_name , property_value ) : <EOL> """<STR_LIT>""" <EOL> return self . run_with_timeout ( self . DEFAULT_TIMEOUT , '<STR_LIT>' , <EOL> self . DEFAULT_NUM_RETRIES , self . server . set_property , property_name , <EOL> property_value , self . secret ) <EOL> def deployment_id_exists ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . run_with_timeout ( self . DEFAULT_TIMEOUT , <EOL> '<STR_LIT>' , <EOL> self . DEFAULT_NUM_RETRIES , self . server . deployment_id_exists , self . secret ) <EOL> def get_deployment_id ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . run_with_timeout ( self . DEFAULT_TIMEOUT , '<STR_LIT>' , <EOL> self . DEFAULT_NUM_RETRIES , self . server . get_deployment_id , self . secret ) <EOL> def set_deployment_id ( self , deployment_id ) : <EOL> """<STR_LIT>""" <EOL> return self . run_with_timeout ( self . DEFAULT_TIMEOUT , '<STR_LIT>' , <EOL> self . DEFAULT_NUM_RETRIES , self . server . set_deployment_id , self . secret , deployment_id ) <EOL> def get_all_stats ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . run_with_timeout ( self . LONGER_TIMEOUT , <EOL> '<STR_LIT>' , self . DEFAULT_NUM_RETRIES , <EOL> self . server . get_all_stats , self . secret ) <EOL> def does_app_exist ( self , appname ) : <EOL> """<STR_LIT>""" <EOL> return self . run_with_timeout ( self . DEFAULT_TIMEOUT , <EOL> '<STR_LIT>' , <EOL> self . DEFAULT_NUM_RETRIES , self . server . does_app_exist , appname , self . secret ) <EOL> def reset_password ( self , username , encrypted_password ) : <EOL> """<STR_LIT>""" <EOL> result = self . run_with_timeout ( self . DEFAULT_TIMEOUT , <EOL> '<STR_LIT>' , self . DEFAULT_NUM_RETRIES , <EOL> self . server . reset_password , username , encrypted_password , self . secret ) <EOL> if result != '<STR_LIT:true>' : <EOL> raise Exception ( result ) <EOL> def does_user_exist ( self , username , silent = False ) : <EOL> """<STR_LIT>""" <EOL> while True : <EOL> try : <EOL> user_exists = self . run_with_timeout ( <EOL> self . DEFAULT_TIMEOUT , '<STR_LIT>' , <EOL> self . DEFAULT_NUM_RETRIES , self . server . does_user_exist , username , <EOL> self . secret ) <EOL> if user_exists == '<STR_LIT:true>' : <EOL> return True <EOL> elif user_exists == '<STR_LIT:false>' : <EOL> return False <EOL> else : <EOL> raise Exception ( user_exists ) <EOL> except Exception as acc_error : <EOL> if not silent : <EOL> AppScaleLogger . log ( "<STR_LIT>" . <EOL> format ( acc_error ) ) <EOL> AppScaleLogger . log ( "<STR_LIT>" ) <EOL> time . sleep ( <NUM_LIT:10> ) <EOL> def create_user ( self , username , password , account_type = '<STR_LIT>' ) : <EOL> """<STR_LIT>""" <EOL> AppScaleLogger . log ( "<STR_LIT>" . format ( username ) ) <EOL> while <NUM_LIT:1> : <EOL> try : <EOL> result = self . run_with_timeout ( self . LONGER_TIMEOUT , <EOL> '<STR_LIT>' , self . DEFAULT_NUM_RETRIES , <EOL> self . server . create_user , username , password , account_type , self . secret ) <EOL> break <EOL> except Exception , exception : <EOL> AppScaleLogger . log ( "<STR_LIT>" . format ( exception ) ) <EOL> AppScaleLogger . log ( "<STR_LIT>" ) <EOL> time . sleep ( <NUM_LIT:10> ) <EOL> if result != '<STR_LIT:true>' : <EOL> raise Exception ( result ) <EOL> def set_admin_role ( self , username , is_cloud_admin , capabilities ) : <EOL> """<STR_LIT>""" <EOL> AppScaleLogger . log ( '<STR_LIT>' % username ) <EOL> return self . run_with_timeout ( self . DEFAULT_TIMEOUT , <EOL> '<STR_LIT>' , self . DEFAULT_NUM_RETRIES , <EOL> self . server . set_admin_role , username , is_cloud_admin , <EOL> capabilities , self . secret ) <EOL> def get_app_admin ( self , app_id ) : <EOL> """<STR_LIT>""" <EOL> app_data_json = self . run_with_timeout ( self . DEFAULT_TIMEOUT , <EOL> '<STR_LIT>' , self . DEFAULT_NUM_RETRIES , <EOL> self . server . get_app_data , app_id , self . secret ) <EOL> if not app_data_json : <EOL> return None <EOL> try : <EOL> app_data = json . loads ( app_data_json ) <EOL> except ValueError as decode_error : <EOL> if '<STR_LIT>' in app_data_json : <EOL> raise AppScaleException ( app_data_json ) <EOL> raise decode_error <EOL> if '<STR_LIT>' not in app_data : <EOL> return None <EOL> return app_data [ '<STR_LIT>' ] <EOL> def reserve_app_id ( self , username , app_id , app_language ) : <EOL> """<STR_LIT>""" <EOL> result = self . run_with_timeout ( self . DEFAULT_TIMEOUT , <EOL> '<STR_LIT>' , self . DEFAULT_NUM_RETRIES , <EOL> self . server . reserve_app_id , username , app_id , app_language , self . secret ) <EOL> if result == "<STR_LIT:true>" : <EOL> AppScaleLogger . log ( "<STR_LIT>" . format ( app_id ) ) <EOL> elif result == "<STR_LIT>" : <EOL> AppScaleLogger . log ( "<STR_LIT>" ) <EOL> elif result == "<STR_LIT>" : <EOL> raise AppScaleException ( "<STR_LIT>" . format ( username ) ) <EOL> else : <EOL> AppScaleLogger . log ( "<STR_LIT>" . format ( result ) ) <EOL> raise AppScaleException ( result ) </s>
