From 66a2742211e33ef7a57dccc2bc5ec004a3d20a63 Mon Sep 17 00:00:00 2001 From: Zack Cerza Date: Mon, 14 Apr 2014 15:38:36 -0500 Subject: [PATCH] Add methods for querying and deleting jobs Signed-off-by: Zack Cerza --- teuthology/report.py | 75 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 75 insertions(+) diff --git a/teuthology/report.py b/teuthology/report.py index e02da9c3e14b6..b450b470f9aa6 100644 --- a/teuthology/report.py +++ b/teuthology/report.py @@ -317,6 +317,46 @@ class ResultsReporter(object): if os.path.exists(self.last_run_file): os.remove(self.last_run_file) + def get_jobs(self, run_name, fields=None): + """ + Query the results server for jobs in a run + + :param run_name: The name of the run + :param fields: Optional. A list of fields to include in the result. + Defaults to returning all fields. + """ + uri = "{base}/runs/{name}/jobs/".format(base=self.base_uri, + name=run_name) + if fields: + if not 'job_id' in fields: + fields.append('job_id') + uri += "?fields=" + ','.join(fields) + response = requests.get(uri) + response.raise_for_status() + return response.json + + def delete_job(self, run_name, job_id): + """ + Delete a job from the results server. + + :param run_name: The name of the run + :param job_id: The job's id + """ + uri = "{base}/runs/{name}/jobs/{job_id}/".format( + base=self.base_uri, name=run_name, job_id=job_id) + response = requests.delete(uri) + response.raise_for_status() + + def delete_jobs(self, run_name, job_ids): + """ + Delete multiple jobs from the results server. + + :param run_name: The name of the run + :param job_ids: A list of job ids + """ + for job_id in job_ids: + self.delete_job(self, run_name, job_id) + def push_job_info(run_name, job_id, job_info, base_uri=None): """ @@ -373,3 +413,38 @@ def try_push_job_info(job_config, extra_info=None): except (requests.exceptions.RequestException, socket.error): log.exception("Could not report results to %s", config.results_server) + + +def try_delete_jobs(run_name, job_ids): + """ + Using the same error checking and retry mechanism as try_push_job_info(), + delete one or more jobs + + :param run_name: The name of the run. + :param job_ids: Either a single job_id, or a list of job_ids + """ + log = init_logging() + + if not config.results_server: + msg = "No results_server set in {yaml}; not attempting to delete job" + log.debug(msg.format(yaml=config.teuthology_yaml)) + return + + if isinstance(job_ids, int): + job_ids = [str(job_ids)] + elif isinstance(job_ids, basestring): + job_ids = [job_ids] + + # We are using archive_base='' here because we KNOW the serializer isn't + # needed for this codepath. + reporter = ResultsReporter(archive_base='') + for job_id in job_ids: + with safe_while(_raise=False) as proceed: + while proceed(): + try: + log.info("Pushing job info to %s", config.results_server) + reporter.delete_job(run_name, job_id) + return + except (requests.exceptions.RequestException, socket.error): + log.exception("Could not report results to %s", + config.results_server) -- 2.39.5