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
This commit is contained in:
Malcolm Brooks 2017-08-07 23:02:54 +01:00 committed by Sorin Sbarnea
parent a67c4cab87
commit fc1f6fde54
2 changed files with 43 additions and 6 deletions

View File

@ -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

View File

@ -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]