def resetvessel(vesselname, exitparams=(44, '')): if vesselname not in vesseldict: raise BadRequest, "No such vessel" # need to try to stop it until it works... while True: try: returnstring = stopvessel(vesselname, exitparams) except BadRequest: # due to the vessel not running... break # if we successfully stopped it, done... if returnstring.endswith('Success'): break # Okay, it is stopped now. Now I'll clean up the file system... filelist = os.listdir(vesselname + "/") # don't delete any files in the protect part of the namespace for filename in filelist: if not filename.startswith("private_"): os.remove(vesselname + "/" + filename) # and remove the log files and stop file... if os.path.exists(vesseldict[vesselname]['logfilename']): os.remove(vesseldict[vesselname]['logfilename']) if os.path.exists(vesseldict[vesselname]['logfilename'] + ".new"): os.remove(vesseldict[vesselname]['logfilename'] + ".new") if os.path.exists(vesseldict[vesselname]['logfilename'] + ".old"): os.remove(vesseldict[vesselname]['logfilename'] + ".old") if os.path.exists(vesseldict[vesselname]['stopfilename']): os.remove(vesseldict[vesselname]['stopfilename']) if os.path.exists(vesseldict[vesselname]['stopfilename']): os.remove(vesseldict[vesselname]['stopfilename']) # change the status to Fresh statusstorage.write_status('Fresh', vesseldict[vesselname]['statusfilename']) # We need to update the status in the table because the status thread might # not notice this before the next request. nmstatusmonitor.update_status(vesseldict, vesselname, 'Fresh', time.time()) return "\nSuccess"
def resetvessel(vesselname,exitparams=(44, '')): if vesselname not in vesseldict: raise BadRequest, "No such vessel" # need to try to stop it until it works... while True: try: returnstring = stopvessel(vesselname,exitparams) except BadRequest: # due to the vessel not running... break # if we successfully stopped it, done... if returnstring.endswith('Success'): break # Okay, it is stopped now. Now I'll clean up the file system... filelist = os.listdir(vesselname+"/") # don't delete any files in the protect part of the namespace for filename in filelist: if not filename.startswith("private_"): os.remove(vesselname+"/"+filename) # and remove the log files and stop file... if os.path.exists(vesseldict[vesselname]['logfilename']): os.remove(vesseldict[vesselname]['logfilename']) if os.path.exists(vesseldict[vesselname]['logfilename']+".new"): os.remove(vesseldict[vesselname]['logfilename']+".new") if os.path.exists(vesseldict[vesselname]['logfilename']+".old"): os.remove(vesseldict[vesselname]['logfilename']+".old") if os.path.exists(vesseldict[vesselname]['stopfilename']): os.remove(vesseldict[vesselname]['stopfilename']) if os.path.exists(vesseldict[vesselname]['stopfilename']): os.remove(vesseldict[vesselname]['stopfilename']) # change the status to Fresh statusstorage.write_status('Fresh',vesseldict[vesselname]['statusfilename']) # We need to update the status in the table because the status thread might # not notice this before the next request. nmstatusmonitor.update_status(vesseldict, vesselname, 'Fresh', time.time()) return "\nSuccess"
def startvessel_ex(vesselname, prog_platform, argstring): # Convert the programming platform to lowercase to make # it case insensitive. prog_platform = prog_platform.lower() if vesselname not in vesseldict: raise BadRequest, "No such vessel" if vesseldict[vesselname]['status'] == 'Started': raise BadRequest("Vessel has already been started") if prog_platform not in prog_platform_dir.keys(): raise BadRequest("Programming language platform is not supported.") # remove any prior stop file so that we can start if os.path.exists(vesseldict[vesselname]['stopfilename']): os.remove(vesseldict[vesselname]['stopfilename']) for char in argstring: if char not in allowedchars: raise BadRequest("Character '"+char+"' not allowed in arguments") # I'm going to capture the status and timestamp and then check the see if # the timestamp is updated... oldstatus, oldtimestamp = statusstorage.read_status(vesseldict[vesselname]['statusfilename']) # Armon: this is required to fetch the networkrestrictions information from the configuration configuration = persist.restore_object("nodeman.cfg") # Armon: Generate the IP/Iface preferences if they exist ip_iface_preference_flags = [] ip_iface_preference_str = "" # Needed for Win Mobile # Only add the flags if everything necessary exists if 'networkrestrictions' in configuration and 'repy_restricted' in configuration['networkrestrictions'] \ and configuration['networkrestrictions']['repy_restricted'] and 'repy_user_preference' in configuration['networkrestrictions']: # Generate the preference list for (is_ip, value) in configuration['networkrestrictions']['repy_user_preference']: # Append the correct flag if is_ip: ip_iface_preference_flags.append("--ip") ip_iface_preference_str += "--ip " else: ip_iface_preference_flags.append("--iface") ip_iface_preference_str += "--iface " # Append the value ip_iface_preference_flags.append(value) ip_iface_preference_str += "'" + value + "' " # Check for the --nootherips flag if 'repy_nootherips' in configuration['networkrestrictions'] and configuration['networkrestrictions']['repy_nootherips']: # Append the flag ip_iface_preference_flags.append("--nootherips") ip_iface_preference_str += "--nootherips " # Find the location where the sandbox files is located. Location of repyV1, repyV2 etc. prog_platform_location = os.path.join(prog_platform_dir[prog_platform], "repy.py") # I use absolute paths so that repy can still find the files after it # changes directories... # Conrad: switched this to sequence-style Popen invocation so that spaces # in files work. Switched it back to absolute paths. command = [sys.executable, prog_platform_location] + ip_iface_preference_flags + [ "--logfile", os.path.abspath(vesseldict[vesselname]['logfilename']), "--stop", os.path.abspath(vesseldict[vesselname]['stopfilename']), "--status", os.path.abspath(vesseldict[vesselname]['statusfilename']), "--cwd", os.path.abspath(vesselname), "--servicelog", "--execinfo", os.path.abspath(vesseldict[vesselname]['resourcefilename'])] + argstring.split() portable_popen.Popen(command) starttime = nonportable.getruntime() # wait for 10 seconds for it to start (else return an error) while nonportable.getruntime()-starttime < 10: newstatus, newtimestamp = statusstorage.read_status(vesseldict[vesselname]['statusfilename']) # Great! The timestamp was updated... The new status is the result of # our work. Let's tell the user what happened... if newtimestamp != oldtimestamp and newstatus != None: break # sleep while busy waiting... time.sleep(.5) else: return "Did not start in a timely manner\nWarning" # We need to update the status in the table because the status thread might # not notice this before our next request... (else occasional failures on XP) nmstatusmonitor.update_status(vesseldict, vesselname, newstatus, newtimestamp) return newstatus+"\nSuccess"
def startvessel_ex(vesselname, prog_platform, argstring): # Convert the programming platform to lowercase to make # it case insensitive. prog_platform = prog_platform.lower() if vesselname not in vesseldict: raise BadRequest, "No such vessel" if vesseldict[vesselname]['status'] == 'Started': raise BadRequest("Vessel has already been started") if prog_platform not in prog_platform_dir.keys(): raise BadRequest("Programming language platform is not supported.") # remove any prior stop file so that we can start if os.path.exists(vesseldict[vesselname]['stopfilename']): os.remove(vesseldict[vesselname]['stopfilename']) for char in argstring: if char not in allowedchars: raise BadRequest("Character '" + char + "' not allowed in arguments") # I'm going to capture the status and timestamp and then check the see if # the timestamp is updated... oldstatus, oldtimestamp = statusstorage.read_status( vesseldict[vesselname]['statusfilename']) # Armon: this is required to fetch the networkrestrictions information from the configuration configuration = persist.restore_object("nodeman.cfg") # Armon: Generate the IP/Iface preferences if they exist ip_iface_preference_flags = [] ip_iface_preference_str = "" # Needed for Win Mobile # Only add the flags if everything necessary exists if 'networkrestrictions' in configuration and 'repy_restricted' in configuration['networkrestrictions'] \ and configuration['networkrestrictions']['repy_restricted'] and 'repy_user_preference' in configuration['networkrestrictions']: # Generate the preference list for (is_ip, value ) in configuration['networkrestrictions']['repy_user_preference']: # Append the correct flag if is_ip: ip_iface_preference_flags.append("--ip") ip_iface_preference_str += "--ip " else: ip_iface_preference_flags.append("--iface") ip_iface_preference_str += "--iface " # Append the value ip_iface_preference_flags.append(value) ip_iface_preference_str += "'" + value + "' " # Check for the --nootherips flag if 'repy_nootherips' in configuration[ 'networkrestrictions'] and configuration[ 'networkrestrictions']['repy_nootherips']: # Append the flag ip_iface_preference_flags.append("--nootherips") ip_iface_preference_str += "--nootherips " # Find the location where the sandbox files is located. Location of repyV1, repyV2 etc. prog_platform_location = os.path.join(prog_platform_dir[prog_platform], "repy.py") # I use absolute paths so that repy can still find the files after it # changes directories... # Conrad: switched this to sequence-style Popen invocation so that spaces # in files work. Switched it back to absolute paths. command = [ sys.executable, prog_platform_location ] + ip_iface_preference_flags + [ "--logfile", os.path.abspath(vesseldict[vesselname]['logfilename']), "--stop", os.path.abspath(vesseldict[vesselname]['stopfilename']), "--status", os.path.abspath(vesseldict[vesselname]['statusfilename']), "--cwd", os.path.abspath(vesselname), "--servicelog", "--execinfo", os.path.abspath(vesseldict[vesselname]['resourcefilename']) ] + argstring.split() portable_popen.Popen(command) starttime = nonportable.getruntime() # wait for 10 seconds for it to start (else return an error) while nonportable.getruntime() - starttime < 10: newstatus, newtimestamp = statusstorage.read_status( vesseldict[vesselname]['statusfilename']) # Great! The timestamp was updated... The new status is the result of # our work. Let's tell the user what happened... if newtimestamp != oldtimestamp and newstatus != None: break # sleep while busy waiting... time.sleep(.5) else: return "Did not start in a timely manner\nWarning" # We need to update the status in the table because the status thread might # not notice this before our next request... (else occasional failures on XP) nmstatusmonitor.update_status(vesseldict, vesselname, newstatus, newtimestamp) return newstatus + "\nSuccess"