#NEWLINE# def run_instances( self , parameters , secret ) : #NEWLINE# #INDENT# "\n    Start a new virtual machine deployment using the provided parameters. The\n    input parameter set must include an 'infrastructure' parameter which indicates\n    the exact cloud environment to use. Value of this parameter will be used to\n    instantiate a cloud environment specific agent which knows how to interact\n    with the specified cloud platform. The parameters map must also contain a\n    'num_vms' parameter which indicates the number of virtual machines that should\n    be spawned. In addition to that any parameters required to spawn VMs in the\n    specified cloud environment must be included in the parameters map.\n\n    If this InfrastructureManager instance has been created in the blocking mode,\n    this method will not return until the VM deployment is complete. Otherwise\n    this method will simply kick off the VM deployment process and return\n    immediately.\n\n    Args:\n      parameters  A parameter map containing the keys 'infrastructure',\n                  'num_vms' and any other cloud platform specific\n                  parameters. Alternatively one may provide a valid\n                  JSON string instead of a dictionary object.\n      secret      A previously established secret\n\n    Returns:\n      If the secret is valid and all the required parameters are available in\n      the input parameter map, this method will return a dictionary containing\n      a special 'reservation_id' key. If the secret is invalid or a required\n      parameter is missing, this method will return a different map with the\n      key 'success' set to False and 'reason' set to a simple error message.\n\n    Raises:\n      TypeError   If the inputs are not of the expected types\n      ValueError  If the input JSON string (parameters) cannot be parsed properly\n    " #NEWLINE# ( parameters , secret ) = self . __validate_args ( parameters , secret ) #NEWLINE# utils . log ( 'Received a request to run instances.' ) #NEWLINE# if ( self . secret != secret ) : #NEWLINE# #INDENT# utils . log ( 'Incoming secret {0} does not match the current secret {1} - Rejecting request.' . format ( secret , self . secret ) ) #NEWLINE# return self . __generate_response ( False , self . REASON_BAD_SECRET ) #NEWLINE# #UNINDENT# for param in self . RUN_INSTANCES_REQUIRED_PARAMS : #NEWLINE# #INDENT# if ( not utils . has_parameter ( param , parameters ) ) : #NEWLINE# #INDENT# return self . __generate_response ( False , ( 'no ' + param ) ) #NEWLINE# #UNINDENT# #UNINDENT# num_vms = int ( parameters [ self . PARAM_NUM_VMS ] ) #NEWLINE# if ( num_vms <= 0 ) : #NEWLINE# #INDENT# utils . log ( 'Invalid VM count: {0}' . format ( num_vms ) ) #NEWLINE# return self . __generate_response ( False , self . REASON_BAD_VM_COUNT ) #NEWLINE# #UNINDENT# infrastructure = parameters [ self . PARAM_INFRASTRUCTURE ] #NEWLINE# agent = self . agent_factory . create_agent ( parameters ) #NEWLINE# try : #NEWLINE# #INDENT# agent . assert_required_parameters ( parameters , BaseAgent . OPERATION_RUN ) #NEWLINE# #UNINDENT# except AgentConfigurationException as exception : #NEWLINE# #INDENT# return self . __generate_response ( False , str ( exception ) ) #NEWLINE# #UNINDENT# reservation_id = utils . get_random_alphanumeric ( ) #NEWLINE# status_info = { #NEWLINE# #INDENT# 'success' : True , #NEWLINE# 'reason' : 'received run request' , #NEWLINE# 'state' : self . STATE_PENDING , #NEWLINE# 'vm_info' : None , #NEWLINE# #UNINDENT# } #NEWLINE# self . reservations . put ( reservation_id , status_info ) #NEWLINE# utils . log ( 'Generated reservation id {0} for this request.' . format ( reservation_id ) ) #NEWLINE# try : #NEWLINE# #INDENT# if self . blocking : #NEWLINE# #INDENT# self . __spawn_vms ( agent , num_vms , parameters , reservation_id ) #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# thread . start_new_thread ( self . __spawn_vms , ( agent , num_vms , parameters , reservation_id ) ) #NEWLINE# #UNINDENT# #UNINDENT# except AgentConfigurationException as exception : #NEWLINE# #INDENT# status_info = { #NEWLINE# #INDENT# 'success' : False , #NEWLINE# 'reason' : str ( exception ) , #NEWLINE# 'state' : self . STATE_FAILED , #NEWLINE# 'vm_info' : None , #NEWLINE# #UNINDENT# } #NEWLINE# self . reservations . put ( reservation_id , status_info ) #NEWLINE# utils . log ( 'Updated reservation id {0} with failed status because: {1}' . format ( reservation_id , str ( exception ) ) ) #NEWLINE# #UNINDENT# utils . log ( 'Successfully started request {0}.' . format ( reservation_id ) ) #NEWLINE# return self . __generate_response ( True , self . REASON_NONE , { #NEWLINE# #INDENT# 'reservation_id' : reservation_id , #NEWLINE# #UNINDENT# } )