From fc1f6fde548e2f3f008aab1ed0f8c85b9a4cd608 Mon Sep 17 00:00:00 2001 From: Malcolm Brooks Date: Mon, 7 Aug 2017 23:02:54 +0100 Subject: [PATCH] Add 'existing-only' argument for update subcommand Additional argument for the update subcommand to allow user to update only jobs currently installed on the Jenkins server and managed by this tool. Change-Id: I4b0e997df01057f89896b8caa27dc00b5ca077df --- jenkins_jobs/builder.py | 30 +++++++++++++++++++++++++-- jenkins_jobs/cli/subcommand/update.py | 19 +++++++++++++---- 2 files changed, 43 insertions(+), 6 deletions(-) diff --git a/jenkins_jobs/builder.py b/jenkins_jobs/builder.py index abed1d40f..39bc11591 100644 --- a/jenkins_jobs/builder.py +++ b/jenkins_jobs/builder.py @@ -230,8 +230,14 @@ class JenkinsManager(object): logger.debug("'{0}' has not changed".format(job.name)) return changed + def exists(self, job): + exists = self.jenkins.job_exists(job.name) + if not exists: + logger.debug("'{0}' does not currently exist".format(job.name)) + return exists + def update_jobs(self, xml_jobs, output=None, n_workers=None, - config_xml=False): + existing_only=None, config_xml=False): orig = time.time() logger.info("Number of jobs generated: %d", len(xml_jobs)) @@ -283,6 +289,16 @@ class JenkinsManager(object): logging.debug("Filtered for changed jobs in %ss", (time.time() - step)) + if existing_only: + # Filter out the jobs not already in the cache + logging.debug('Filtering %d jobs for existing jobs', + len(jobs)) + step = time.time() + jobs = [job for job in jobs + if self.exists(job)] + logging.debug("Filtered for existing jobs in %ss", + (time.time() - step)) + if not jobs: return [], 0 @@ -383,7 +399,7 @@ class JenkinsManager(object): self.jenkins.create_view(view_name, xml) def update_views(self, xml_views, output=None, n_workers=None, - config_xml=False): + existing_only=None, config_xml=False): orig = time.time() logger.info("Number of views generated: %d", len(xml_views)) @@ -426,6 +442,16 @@ class JenkinsManager(object): logging.debug("Filtered for changed views in %ss", (time.time() - step)) + if existing_only: + # Filter out the jobs not already in the cache + logging.debug('Filtering %d views for existing jobs', + len(views)) + step = time.time() + views = [view for view in views + if self.exists(view)] + logging.debug("Filtered for existing views in %ss", + (time.time() - step)) + if not views: return [], 0 diff --git a/jenkins_jobs/cli/subcommand/update.py b/jenkins_jobs/cli/subcommand/update.py index 7f2ad7c6c..18ce70bfc 100644 --- a/jenkins_jobs/cli/subcommand/update.py +++ b/jenkins_jobs/cli/subcommand/update.py @@ -84,6 +84,13 @@ class UpdateSubCommand(base.BaseSubCommand): default=False, help='update only views' ) + update.add_argument( + '--existing-only', + action='store_true', + default=False, + dest='existing_only', + help='update existing jobs only' + ) def _generate_xmljobs(self, options, jjb_config=None): builder = JenkinsManager(jjb_config) @@ -129,18 +136,22 @@ class UpdateSubCommand(base.BaseSubCommand): if options.add_jobs: jobs, num_updated_jobs = builder.update_jobs( - xml_jobs, n_workers=options.n_workers) + xml_jobs, n_workers=options.n_workers, + existing_only=options.existing_only) logger.info("Number of jobs updated: %d", num_updated_jobs) elif options.add_views: views, num_updated_views = builder.update_views( - xml_views, n_workers=options.n_workers) + xml_views, n_workers=options.n_workers, + existing_only=options.existing_only) logger.info("Number of views updated: %d", num_updated_views) else: jobs, num_updated_jobs = builder.update_jobs( - xml_jobs, n_workers=options.n_workers) + xml_jobs, n_workers=options.n_workers, + existing_only=options.existing_only) logger.info("Number of jobs updated: %d", num_updated_jobs) views, num_updated_views = builder.update_views( - xml_views, n_workers=options.n_workers) + xml_views, n_workers=options.n_workers, + existing_only=options.existing_only) logger.info("Number of views updated: %d", num_updated_views) keep_jobs = [job.name for job in xml_jobs]