def start_app(appcode, firewall_setup): # don't start app-layer code until the lower layers are initialized firewall_setup.wait() # and beware that something may have failed, so only start app code # if it looks like everything was initialized correctly if setup_ok: try: import_or_die(appcode, []) except Exception as e: import traceback with red(): print('*'*60) print ("Exception while running your code: {}".format(e)) lines = indent(traceback.format_exc(), ' ').split('\n') doprint = False for line in lines: if doprint: print (line) elif appcode in line: print (line) doprint = True print ('*'*60) if netobj is not None: netobj.shutdown()
def get_test_scenario_from_file(sfile): ''' Takes a file name as a parameter, which contains a scenario object either in a .py module form, or serialized in a .srpy form. (str/filename) -> Scenario object ''' sobj = None if fnmatch.fnmatch(sfile, "*.py"): sobj = import_or_die(sfile, ('scenario',)) elif fnmatch.fnmatch(sfile, "*.srpy"): sobj = uncompile_scenario(sfile) else: sobj = import_or_die(sfile, ('scenario',)) return sobj
def __init__(self, node, intf, *args, **kwargs): super().__init__(args) module = args[0] self.__usercode = import_or_die(module) self.__thread = threading.Thread(target=self.__thread_entry) self.__queue = queue.Queue() self.__debugnet = DebugInspector(node, Interface(intf,None,None), self.__queue) self.__thread.start()
def main_test(usercode, scenarios, options): ''' Entrypoint function for either compiling or running test scenarios. ''' if not scenarios or not len(scenarios): log_failure("In test mode, but no scenarios specified.") return if options.compile: for scenario in scenarios: log_info("Compiling scenario {}".format(scenario)) compile_scenario(scenario) else: usercode_entry_point = import_or_die( usercode, ('main', 'srpy_main', 'switchy_main')) if options.dryrun: log_info("Imported your code successfully. Exiting dry run.") return run_tests(scenarios, usercode_entry_point, options)
def compile_scenario(scenario_file, output_filename=None): ''' Compile a Switchy test scenario object to a serialized representation in a file for distribution. Assumes that the input file is a .py module with a 'scenario' variable that refers to some Scenario object. (str/filename) -> str/filename ''' sobj = import_or_die(scenario_file, ('scenario',)) sobj.scenario_sanity_check() outname = scenario_file.rstrip('.py') + '.srpy' pickle_repr = pickle.dumps(sobj, pickle.HIGHEST_PROTOCOL) dig = hashlib.sha512() dig.update(pickle_repr) if output_filename: outname = output_filename xfile = open(outname, 'w') outstr = dig.digest() + pickle_repr xfile.write(base64.b64encode(bz2.compress(outstr)).decode('ascii')) xfile.close() return outname
def main_real(usercode, netobj, options): ''' Entrypoint function for non-test ("real") mode. At this point we assume that we are running as root and have pcap module. ''' usercode_entry_point = import_or_die(usercode, ('main', 'srpy_main', 'switchy_main')) if options.dryrun: log_info("Imported your code successfully. Exiting dry run.") netobj.shutdown() return try: usercode_entry_point(netobj) except Exception as e: import traceback log_failure("Exception while running your code: {}".format(e)) message = '''{0} This is the Switchyard equivalent of the blue screen of death. Here (repeating what's above) is the failure that occurred: '''.format('*' * 60, textwrap.fill(str(e), 60)) with red(): print(message) traceback.print_exc(1) print('*' * 60) if options.nohandle: raise if not options.nopdb: print(''' I'm throwing you into the Python debugger (pdb) at the point of failure. If you don't want pdb, use the --nopdb flag to avoid this fate. ''') import pdb pdb.post_mortem() else: netobj.shutdown()
def main_real(usercode, netobj, options): ''' Entrypoint function for non-test ("real") mode. At this point we assume that we are running as root and have pcap module. ''' usercode_entry_point = import_or_die(usercode, ('main','srpy_main','switchy_main')) if options.dryrun: log_info("Imported your code successfully. Exiting dry run.") netobj.shutdown() return try: usercode_entry_point(netobj) except Exception as e: import traceback log_failure("Exception while running your code: {}".format(e)) message = '''{0} This is the Switchyard equivalent of the blue screen of death. Here (repeating what's above) is the failure that occurred: '''.format('*'*60, textwrap.fill(str(e), 60)) with red(): print(message) traceback.print_exc(1) print('*'*60) if options.nohandle: raise if not options.nopdb: print(''' I'm throwing you into the Python debugger (pdb) at the point of failure. If you don't want pdb, use the --nopdb flag to avoid this fate. ''') import pdb pdb.post_mortem() else: netobj.shutdown()