def repo(request): # Clear the repository dpcmrepo.initialize_repo() repo = dpcmrepo.repo() try: # remove a stale test database dpcmrepo.repo().delete(DB_TEMP.name) except dpcmrepo.NotFound: pass return repo
def delete_simulation(simid): '''Delete a simulation. This fails if the simulation is still running. ''' simdb = repo().SIM try: sim = simdb.get(simid) if sim.get('type', None) != "simoutput": raise NotFound(details="There is no simulation with this id") simdb.delete(simid) except (dpcmrepo.NotFound, NotFound): logger.debug('In api.delete_simulation(%s)', simid, exc_info=1) pass # Ignore # Now, get the job simjob = Manager.get_job_by_simid(simid) if simjob is None: raise NotFound("There is no simulation with this id") if simjob.state != 'PASSIVE': raise BadRequest("Cannot delete unfinished simulation") Manager.delete_job(simid)
def validate_nsd(nsd_id): ''' Return a json object with the results of running NSD validation. ''' try: url = repo().url_of(NSD, nsd_id) results = validate_simulation(url) except Exception as e: logger.error("Unexpected exception from validate_simulation()", exc_info=1) raise ServerError( details="An unexpected exception occurred during NSD validation") return results
def create_simulation(nsdid, xtorname=None): ''' Create a simulation in the project repository and start a job for it. nsdid - the id of the NSD for the new simulation Return the new sim object created. Raises ValueError if nsdid is not in database PR.SIM ''' # Get the executor xtor = Manager.executor(xtorname) # create the simoutput object sim, url, simhome = create_simoutput(xtor, repo(), nsdid) logfile = create_logstatistics(simhome) # create the job Manager.create_job(xtor, url, simhome) return sim
def create_simulation(xtor, args): '''This method will rewrite a uri, as described in the argument parser of main() and will create the simulation in the given executor xtor. Returns the path to the simulation home. ''' if args.src.startswith('nsd:'): # Create simoutput nsdid = args.src initialize_repo(args.repo) prepo = repo() sim, url, simhome = create_simoutput(xtor, prepo, nsdid) return xtor.create_simulation(url, simhome) else: if any(args.src.startswith(p) for p in ["http:", "file:"]): src = args.src else: src = 'file:'+os.path.abspath(args.src) return xtor.create_simulation(src)
def __db(self): return repo().database(cfg[self.db])
def get_nsds(): '''Return an iterator on all projects and NSD for it. ''' return repo().get_nsds()
def get_plans(prjid): '''Return an iterator on all plans for a given project''' return repo().get_plans(prjid)
def get_projects(): '''Return an iterator on all projects''' return repo().get_projects()
def get_simulation(simid): '''Read the simulation object from the PR.''' sim = repo().SIM.get(simid) if sim.get('type', None) != "simoutput": raise NotFound("There is no simulation with this id") return sim
def main(): parser = argparse.ArgumentParser(description=''' This program executes a simulation for the provided simulation object, or for a given NSD. It is possible to select a subset of the steps of a simulation. Also, the standard output of the execution is not redirected into files (unless -r is provided). The name of the simulation home can be provided. The program can be invoked as: % python3 simulation_run.py [...options...] <uri> where <uri> is: (a) file:/abs/path/to/sim.json Then, the file /abs/path/to/sim.json must exist and contain a valid simoutput object. Furthermore, other files inside directory /abs/path/to must exist, in order for the simulation to run smoothly. The resource directory contains such examples (e.g., see directory 'netsim_py/resources/testsim1') (b) http://repo.site/dpcm_simulation/<simobj-id> The provided URL must point to a legal sim object, such as those created by (c) nsd:<nsdid> A new simoutput object is created on the configured project repository and a simulation is started on it. (d) If the uri starts with anything else, it is treated as a path. If needed, it is first converted to absolute and then 'file' is prepended to it. ''') parser.add_argument("src", help="The uri of simulation.", nargs="?") parser.add_argument("--redir", '-r', help=""""Redirect stdout and stdstream to files. If this option is given, the execution will be quiet and standard stream files will be created.""", default=False, action="store_true") parser.add_argument("--loglevel", '-l', help="Set the log level", choices=['CRITICAL','ERROR','WARNING','INFO','DEBUG'], default='INFO' ) parser.add_argument("--repo", '-p', type=str, help="""Give the url of a couchdb server to access as project repository. If not provided, the default is used.""", default = None) parser.add_argument("--name", '-n', type=str, help="""Set the simulation home name. If not set, a default name is provided.""") parser.add_argument("--stages", '-s', help="""Define the stages to run. Stages are: P/G/C/S/F (Prepare/Generate/Compile/Start/Finish). For example -s PG means to only Prepare and Generate.""", default="PGCSF") parser.add_argument("--list", '-t', help="""List the NSDs in the current repository.""", action="store_true") parser.add_argument("--config", help="Path to configuration file", default=None) args = parser.parse_args() assert args.loglevel is not None logging.root.setLevel(args.loglevel) mp.set_start_method("forkserver") logging.info("Logging level: %s", logging.getLevelName(logging.root.getEffectiveLevel())) logging.info("Redirect: %s",args.redir) configure('simulation_run', args.config) if args.list: initialize_repo(args.repo) view_model(repo(), NSD.design) return else: if args.src is None: parser.print_usage() parser.exit(1) if not all(x in "PGCSF" for x in args.stages): logging.getLogger().error("Invalid stage sequence") sys.exit(1) # Create the simulation executor = StandaloneLocalExecutor('test', local_executor_path(), args.name, args.stages) fileloc = create_simulation(executor, args) print("Simulation home: ", fileloc) # Run the simulation simulation_run(executor, fileloc, args.redir, args.stages) print("Output: ", fileloc)
def execute(self): # execute the processor try: dao = self.engine.getDao() success = False executor = Manager.executor(self.job.executor) self.log.info("%s processing.", self.name) if self.job.status == JobStatus.INIT: executor.prepare_simulation(self.job.fileloc) elif self.job.status == JobStatus.PREPARED: executor.generate_simulation(self.job.fileloc) elif self.job.status == JobStatus.GENERATED: executor.compile_simulation(self.job.fileloc) elif self.job.status == JobStatus.COMPILED: executor.start_simulation(self.job.fileloc) elif self.job.status == JobStatus.STARTED: executor.finish_simulation(self.job.fileloc) else: # ABORTED or FINISHED # dao.passivate_job(self.job) try: executor = Manager.executor(self.job.executor) executor.finalize(self.job.fileloc, self.job.jobid, self.job.status) except CalledProcessError as e: self.log.warn("%s failed: %s", self.name, e.returncode) return except Exception as e: self.log.info("%s failed: %s", self.name, self.job.status) self.log.debug("%s raised an exception" % self.name, exc_info=1) else: self.log.info("%s finished successfully", self.name) success = True # Abort the job on failure if not success: self.log.info("Abort for %s", self.name) dao.transition_job_status(self.job, JobStatus.ABORTED) else: # Success, update the job status newstatus = JobStatus.transition(self.job.status) dao.transition_job_status(self.job, newstatus) # Update the project repository object. job = dao.get_job_by_fileloc(self.job.fileloc) # refresh the job # try to update the PR sim record retries = 3 while True: try: repo().update_simulation(job.simid, simulation_status=job.status, last_status=job.last_status, tsinstatus=str(job.tsinstatus)) except dpcmrepo.Conflict: retries -= 1 if retries == 0: raise else: break