def single_launcher( self, traci_port=9999, bind="0.0.0.0", message_log=os.devnull, run_args_override=None, ): # todo: implement how opp/runner.py in line 78 cmd = [ "/veins_launchd", "-vvv", "--port", str(traci_port), "--bind", bind, "--logfile", message_log, "--single-run", ] if run_args_override is None: run_args_override = {} logger.debug(f"start sumo container(single server)") logger.debug(f"cmd: {' '.join(cmd)}") return self.run(cmd, **run_args_override)
def run_vadere(self): ret = 255 logger.info("Run vadere in container") try: self.build_and_start_vadere_only() ret = 0 # all good if we reached this. except RuntimeError as cErr: logger.error(cErr) ret = 255 except KeyboardInterrupt as K: logger.info("KeyboardInterrupt detected. Shutdown. ") ret = 128 + signal.SIGINT raise finally: # always stop container and delete if no error occurred err_state = ret logger.debug(f"cleanup with ret={ret}") # TODO: does not work # if self.vadere_runner is not None: # self.vadere_runner.container_cleanup(has_error_state=err_state) # self.opp_runner.container_cleanup(has_error_state=err_state) return ret
def exec_single_server( self, config_path, traci_port=9999, message_log=os.devnull, run_args_override=None, ): """ This function is deprecated, please use the single_launcher """ warnings.warn( "SumoRunner.exec_single_server is a deprecated function.") cmd = [ "sumo", "-v", "--remote-port", str(traci_port), "--configuration-file", config_path, "--message-log", message_log, "--no-step-log", "--quit-on-end", ] if run_args_override is None: run_args_override = {} logger.debug(f"start sumo container(single server)") logger.debug(f"cmd: {' '.join(cmd)}") return self.run(cmd, **run_args_override)
def run_simulation_omnet_sumo(self): ret = 255 # fail self.build_opp_runner() try: sumo_args = self.ns["sumo_args"] if self.ns["create_sumo_container"]: self.build_sumo_runner() self.sumo_runner.single_launcher( traci_port=sumo_args.get_value("--port"), bind=sumo_args.get_value("--bind"), ) if self.ns["override-host-config"]: self.ns["opp_args"].add_override( f"--sumo-host={self.sumo_runner.name}:{sumo_args.get_value('--port')}" ) # start OMNeT++ container and attach to it logger.info(f"start simulation {self.ns['run_name']} ...") opp_ret = self.opp_runner.exec_opp_run( arg_list=self.ns["opp_args"], result_dir=self.ns["result_dir"], experiment_label=self.ns["experiment_label"], run_args_override={}, ) ret = opp_ret["StatusCode"] if ret != 0: raise RuntimeError( f"OMNeT++ container exited with StatusCode '{ret}'") if self.sumo_runner is not None: try: self.sumo_runner.container.wait(timeout=600) except ReadTimeout: logger.error( f"Timeout (60s) reached while waiting for sumo container to finished" ) ret = 255 except RuntimeError as cErr: logger.error(cErr) ret = 255 except KeyboardInterrupt as K: logger.info("KeyboardInterrupt detected. Shutdown. ") ret = 128 + signal.SIGINT raise finally: # always stop container and delete if no error occurred err_state = ret != 0 logger.debug(f"cleanup with ret={ret}") if self.sumo_runner is not None: self.sumo_runner.container_cleanup(has_error_state=err_state) self.opp_runner.container_cleanup(has_error_state=err_state) return ret
def run_simulation_vadere_ctl(self): ret = 255 logger.info( "Control vadere without omnetpp. Client: controller, server: vadere, port: 9999" ) output_dir = os.path.join( os.getcwd(), f"results/vadere_controlled_{self.ns['experiment_label']}/vadere.d", ) os.makedirs(output_dir, exist_ok=True) self.build_control_runner() try: if self.ns["create_vadere_container"]: self.build_and_start_vadere_runner(port=9999, output_dir=output_dir) logger.info(f"start simulation {self.ns['run_name']} ...") ctl_ret = self.exec_control_runner(mode="client") ret = ctl_ret["StatusCode"] if ret != 0: raise RuntimeError( f"Control container exited with StatusCode '{ret}'") if self.vadere_runner is not None: try: self.vadere_runner.container.wait( timeout=self.ns["v_wait_timeout"]) except ReadTimeout: logger.error( f"Timeout ({self.ns['v_wait_timeout']}) reached while waiting for vadere container to finished" ) ret = 255 except RuntimeError as cErr: logger.error(cErr) ret = 255 except KeyboardInterrupt as K: logger.info("KeyboardInterrupt detected. Shutdown. ") ret = 128 + signal.SIGINT raise finally: # always stop container and delete if no error occurred err_state = ret != 0 logger.debug(f"cleanup with ret={ret}") if self.vadere_runner is not None: self.vadere_runner.container_cleanup(has_error_state=err_state) if self.control_runner is not None: self.control_runner.container_cleanup( has_error_state=err_state) return ret
def run_simulation_omnet_vadere(self): ret = 255 self.build_opp_runner() try: if self.ns["create_vadere_container"]: self.build_and_start_vadere_runner() if self.ns["override-host-config"]: self.ns["opp_args"].add( f"--vadere-host={self.vadere_runner.name}") # start OMNeT++ container and attach to it. logger.info(f"start simulation {self.ns['run_name']} ...") opp_ret = self.opp_runner.exec_opp_run( arg_list=self.ns["opp_args"], result_dir=self.ns["result_dir"], experiment_label=self.ns["experiment_label"], run_args_override={}, ) ret = opp_ret["StatusCode"] if ret != 0: raise RuntimeError( f"OMNeT++ container exited with StatusCode '{ret}'") if self.vadere_runner is not None: try: self.vadere_runner.container.wait( timeout=self.ns["v_wait_timeout"]) except ReadTimeout: logger.error( f"Timeout ({self.ns['v_wait_timeout']}) reached while waiting for vadere container to finished" ) ret = 255 except RuntimeError as cErr: logger.error(cErr) ret = 255 except KeyboardInterrupt as K: logger.info("KeyboardInterrupt detected. Shutdown. ") ret = 128 + signal.SIGINT raise finally: # always stop container and delete if no error occurred err_state = ret != 0 logger.debug(f"cleanup with ret={ret}") if self.vadere_runner is not None: self.vadere_runner.container_cleanup(has_error_state=err_state) self.opp_runner.container_cleanup(has_error_state=err_state) return ret
def create_container(self, cmd="/init.sh", **run_args) -> Container: """ run container. If no command is given execute the default entry point '/init.sh' """ self.build_run_args(**run_args) # set name if given command = self.wrap_command(cmd) logger.info(f"{'#'*10} create container [image:{self.image}]") logger.debug(f"cmd: \n{pprint.pformat(command, indent=2)}") logger.debug(f"runargs: \n{pprint.pformat(self.run_args, indent=2)}") c: Container = self.client.containers.create( image=self.image, command=command, **self.run_args ) logger.info(f"{'#'*10} container created {c.name} [image:{self.image}]") return c
def container_cleanup(self, has_error_state=False): """ stop and remove based on cleanupPolicy. """ if self._container is None: return # do nothing self.parse_log() logger.debug(f"Stop container {self._container.name} ...") self._container.stop() if self.cleanupPolicy == DockerCleanup.REMOVE or ( self.cleanupPolicy == DockerCleanup.KEEP_FAILED and not has_error_state ): logger.debug(f"remove container {self._container.name} ...") self._container.remove() self._container = None # container removed so do not keep reference
def exec_opp_run( self, arg_list: ArgList, result_dir, experiment_label, run_args_override=None, ): """ Execute opp_run in container. """ _arg = ArgList.from_list(arg_list.data) _arg.add(f"--result-dir={result_dir}") _arg.add(f"--experiment-label={experiment_label}") _arg.add(self.run_cmd, pos=0) logger.debug(f"start omnett++ container(exec_opp_run)") logger.debug(f"cmd: {_arg.to_string()}") return self.run(_arg.to_string(), **run_args_override)
def start_controller( self, loglevel=LogLevel.DEBUG, logfile=os.devnull, run_args_override=None, control_file="control.py", host_name="vadere_rover_run", connection_mode="client", traci_port=9999, use_local=False, scenario=None, ): # if connection_mode == "client": # if scenario is None: # raise ("Scenario file must be provided in client mode.") # use python source code of flowcontroler instead of installed flowcontroler package in the container. # the /init_dev.sh script is part of the container an will install if use_local: exec_cmd = "/init_dev.sh" env = self.run_args.get("environment", {}) env.setdefault("CROWNET_HOME", CrowNetConfig.path_crownet_home()) self.run_args["environment"] = env else: exec_cmd = "python3" cmd = [ exec_cmd, control_file, "--port", str(traci_port), "--host-name", host_name, ] if connection_mode == "client": cmd.extend(["--client-mode", "--scenario-file", scenario]) logger.debug(f"start controller container(start_controller)") logger.debug(f"cmd: {' '.join(cmd)}") return self.run(cmd, self.run_args)
def exec_single_server( self, traci_port=9998, loglevel=LogLevel.DEBUG, logfile=os.devnull, show_gui=False, run_args_override=None, output_dir=None, ): """ start Vadere server waiting for exactly ONE connection on given traci_port. After simulation returns the container will stop. """ cmd = [ "java", "-jar", "/opt/vadere/vadere/VadereManager/target/vadere-server.jar", "--loglevel", loglevel, "--logname", logfile, "--port", str(traci_port), "--bind", "0.0.0.0", "--single-client", ] if show_gui: cmd.append("--gui-mode") if output_dir != None: cmd.extend(["--output-dir", output_dir]) if run_args_override is None: run_args_override = {} logger.debug(f"start vadere container(single server)") logger.debug(f"cmd: {' '.join(cmd)}") return self.run(cmd, **run_args_override)
def exec_vadere_only(self, scenario_file, output_path, run_args_override=None): cmd = [ "java", "-jar", "/opt/vadere/vadere/VadereSimulator/target/vadere-console.jar", "suq", "-f", scenario_file, "-o", output_path, ] if run_args_override is None: run_args_override = {} logger.debug(f"start vadere container(exec_vadere_only") logger.debug(f"cmd: {' '.join(cmd)}") return self.run(cmd, **run_args_override)