def add_sim(self, sim_type="mock", proj_name="", user_input="", num_cores=1, ARG=False, DEL=False): """ Adds simulation to the user's list of projects. Calls setup_sim and curr_sim.simulation to configure the new project and actually deploy the simulation :param sim_type: :param proj_name: :param user_input: :param ARG: :param DEL: :return: """ ###### Create Unique VM ID ###### self.curr_sim.generate_vm_id(self.username) ##### If accessed through command-line ##### if ARG: self.curr_sim.name = proj_name self.curr_sim.input = user_input self.curr_sim.cores = num_cores self.curr_sim.tstamp = AzureTools.timestamp() ## Check Project Name is Unique and Valid ### valid = self.curr_sim.check_name(proj_name) unique = self.check_sim(proj_name) ##### Update Simulation File ###### # The Simulation name has invalid syntax... if not valid: stderr.write("\nSimulation name has invalid syntax.") exit(1) elif not unique: stderr.write("Simulation name already exists.") exit(1) else: self.user_info["username"] = self.username self.user_info["email"] = self.email self.user_info["sim"] = self.curr_sim.name self.curr_sim.upload_input(self.user_info) success = self.curr_sim.simulation(self.username, sim_type, ARG, DEL) return success else: # Get user input for simulation name and check if name is unique if self.user_giveSimName() is 1: return 1 # Get user input for input files if self.user_giveSimInput() is 1: return 1 # Get user input for core count self.user_giveSimCores() # Generate timestamp self.curr_sim.tstamp = AzureTools.timestamp() # Run simulation success = self.curr_sim.simulation(self.username, sim_type) return success
def upload_input(self, user_info): """ Calls to zip the provided input files then uploads them to the user's storage container. :param user_info: :return: """ # Convert to Windows format norm_inputs = (os.path.normpath(self.input)) # Create pickle file for user_info f = 'C:/Users/' + comp_user + '/Simulations/' + user_info[ "username"] + '/AzureUserInfo.pickle' pickle.dump(user_info, file(f, 'w')) # Zip input files inputs_zip = AzureTools.zip_files(user_info["username"], norm_inputs) # Try uploading the specified input try: blob_service.put_block_blob_from_path( user_info["username"].lower(), self.vm_id, inputs_zip) except: stderr.write('An error occurred uploading your input.') exit(1)
def upload_input(self, user_info): """ Calls to zip the provided input files then uploads them to the user's storage container. :param user_info: :return: """ # Convert to Windows format norm_inputs = (os.path.normpath(self.input)) # Create pickle file for user_info f = 'C:/Users/' + comp_user + '/Simulations/' + user_info["username"] + '/AzureUserInfo.pickle' pickle.dump(user_info,file(f, 'w')) # Zip input files inputs_zip = AzureTools.zip_files(user_info["username"], norm_inputs) # Try uploading the specified input try: blob_service.put_block_blob_from_path(user_info["username"].lower(), self.vm_id, inputs_zip) except: stderr.write('An error occurred uploading your input.') exit(1)
def user_requestSimResults(self, ARG=False): """ Checks if the specified simulation is done running and if its results file has been uploaded to the client's storage container. If the simulation has finished, the results are downloaded to the clients computer. :param ARG: :return: """ if ARG: print "Checking for " + self.curr_sim.name + " results..." # Check if project is listed in client's simulation file not_listed = self.check_sim(self.curr_sim.name) ######## Get Results if Ready ######## if not_listed: stderr.write("\nProject does not exist.") exit(1) else: # Retrieve names of all user's files in their container try: blobs = blob_service.list_blobs(container_name=self.username.lower()) except: stderr.write('An error occurred while accessing your storage.') exit(1) sim_results = 'r-' + self.curr_sim.vm_id results_in = False # Check if results are in for uploaded_file in blobs: if uploaded_file.name == sim_results: # Download results file file_path = 'c:/Users/' + comp_user + '/Simulations/' + self.username + '/' + self.curr_sim.name + \ '_results.zip' try: blob_service.get_blob_to_path(self.username.lower(), sim_results, file_path) except: stderr.write('An error occurred while downloading your results.') exit(1) AzureSimulation.extract_files(self.curr_sim.name + '_results') results_in = True break if results_in: print "\nYour results are in! Check C:/Users/" + comp_user + "/Simulations/" + self.username + " for " \ "the " + self.curr_sim.name + "_results folder." else: # If the results are not in and the VM is still running... try: sms.get_role(self.username, self.username, self.curr_sim.vm_id) # The simulation is still running print "\nThe simulation is still running. Check back later to retrieve results." # If the results are not in but the VM is already deleted... except: # The results did not upload print "Your results were unable to be uploaded. Try running the simulation again." elif not ARG: ######## Get Simulation Name ######### requested_sim = "1" while requested_sim == '1': requested_sim = raw_input("\nSelect a project to check for results.\n" "Press '1' to list projects or enter project name: ") if requested_sim == '1': self.list_projects() ######## Get Results if Ready ######## while self.check_sim(requested_sim) == True: print "\nProject does not exist." option = None while option not in ['1', '2', '3']: option = raw_input("(1) Check another project\n" "(2) Back to menu\n" "(3) Quit\n" ">> ") if option not in ['1', '2', '3']: stderr.write("\nInput not recognized.\n") sleep(0.5) if option == 1: requested_sim = "1" while requested_sim == '1': requested_sim = raw_input("\nSelect a project to check for results.\n" "Press '1' to list projects or enter project name: ") if requested_sim == '1': self.list_projects() elif option == '2': return elif option == '3': quit(0) # Retrieve all user's files from their container try: blobs = blob_service.list_blobs(container_name=self.username.lower()) except: stderr.write('An error occurred while trying to access your storage.') exit(1) sim_results = 'r-' + self.curr_sim.vm_id results_in = False # Check if results are in for uploaded_file in blobs: if uploaded_file.name == sim_results: # Download results file file_path = 'c:/Users/' + comp_user + '/Simulations/' + self.username + '/' + requested_sim + \ '_results.zip' try: blob_service.get_blob_to_path(self.username.lower(), sim_results, file_path) except: stderr.write('An error occurred when trying to download your results.') exit(1) # Extract results AzureTools.extract_files(requested_sim + '_results', self.username) results_in = True break if results_in: print "\nYour results are in! Check C:/Users/" + comp_user + "/Simulations/" + self.username + " for " \ "the " + requested_sim + "_results folder." return else: # If the results are not in and the VM is still running... try: sms.get_role(self.username, self.username, self.curr_sim.vm_id) # The simulation is still running print "\nThe simulation is still running. Check back later to retrieve results." # If the results are not in but the VM is already deleted... except: # The results did not upload print "Your results were unable to be uploaded. Try running the simulation again." return
def user_requestSimResults(self, ARG=False): """ Checks if the specified simulation is done running and if its results file has been uploaded to the client's storage container. If the simulation has finished, the results are downloaded to the clients computer. :param ARG: :return: """ if ARG: print "Checking for " + self.curr_sim.name + " results..." # Check if project is listed in client's simulation file not_listed = self.check_sim(self.curr_sim.name) ######## Get Results if Ready ######## if not_listed: stderr.write("\nProject does not exist.") exit(1) else: # Retrieve names of all user's files in their container try: blobs = blob_service.list_blobs( container_name=self.username.lower()) except: stderr.write( 'An error occurred while accessing your storage.') exit(1) sim_results = 'r-' + self.curr_sim.vm_id results_in = False # Check if results are in for uploaded_file in blobs: if uploaded_file.name == sim_results: # Download results file file_path = 'c:/Users/' + comp_user + '/Simulations/' + self.username + '/' + self.curr_sim.name + \ '_results.zip' try: blob_service.get_blob_to_path( self.username.lower(), sim_results, file_path) except: stderr.write( 'An error occurred while downloading your results.' ) exit(1) AzureSimulation.extract_files(self.curr_sim.name + '_results') results_in = True break if results_in: print "\nYour results are in! Check C:/Users/" + comp_user + "/Simulations/" + self.username + " for " \ "the " + self.curr_sim.name + "_results folder." else: # If the results are not in and the VM is still running... try: sms.get_role(self.username, self.username, self.curr_sim.vm_id) # The simulation is still running print "\nThe simulation is still running. Check back later to retrieve results." # If the results are not in but the VM is already deleted... except: # The results did not upload print "Your results were unable to be uploaded. Try running the simulation again." elif not ARG: ######## Get Simulation Name ######### requested_sim = "1" while requested_sim == '1': requested_sim = raw_input( "\nSelect a project to check for results.\n" "Press '1' to list projects or enter project name: ") if requested_sim == '1': self.list_projects() ######## Get Results if Ready ######## while self.check_sim(requested_sim) == True: print "\nProject does not exist." option = None while option not in ['1', '2', '3']: option = raw_input("(1) Check another project\n" "(2) Back to menu\n" "(3) Quit\n" ">> ") if option not in ['1', '2', '3']: stderr.write("\nInput not recognized.\n") sleep(0.5) if option == 1: requested_sim = "1" while requested_sim == '1': requested_sim = raw_input( "\nSelect a project to check for results.\n" "Press '1' to list projects or enter project name: " ) if requested_sim == '1': self.list_projects() elif option == '2': return elif option == '3': quit(0) # Retrieve all user's files from their container try: blobs = blob_service.list_blobs( container_name=self.username.lower()) except: stderr.write( 'An error occurred while trying to access your storage.') exit(1) sim_results = 'r-' + self.curr_sim.vm_id results_in = False # Check if results are in for uploaded_file in blobs: if uploaded_file.name == sim_results: # Download results file file_path = 'c:/Users/' + comp_user + '/Simulations/' + self.username + '/' + requested_sim + \ '_results.zip' try: blob_service.get_blob_to_path(self.username.lower(), sim_results, file_path) except: stderr.write( 'An error occurred when trying to download your results.' ) exit(1) # Extract results AzureTools.extract_files(requested_sim + '_results', self.username) results_in = True break if results_in: print "\nYour results are in! Check C:/Users/" + comp_user + "/Simulations/" + self.username + " for " \ "the " + requested_sim + "_results folder." return else: # If the results are not in and the VM is still running... try: sms.get_role(self.username, self.username, self.curr_sim.vm_id) # The simulation is still running print "\nThe simulation is still running. Check back later to retrieve results." # If the results are not in but the VM is already deleted... except: # The results did not upload print "Your results were unable to be uploaded. Try running the simulation again." return