Add config options --jobs-only and --views-only

Add options --jobs-only and --views-only to job_builder
section in config file.

By default JJB updates both jobs and views. Some cases
non-admin users are not allowed to update views on Jenkins
which requires explicity using -j flag.

Allow users to set a config option 'update=jobs|views|all'
in the 'job_builder' section implicitly use --jobs-only or
--views-only and control these flags from the config file.

Note: CLI options takes precedence over the config file.

Change-Id: I2a94e5a2d671ccbfc505de2f19b578ecfef9e9d7
Co-Authored-By: Thanh Ha <zxiiro@linux.com>
Signed-off-by: Anil Belur <askb23@gmail.com>
Signed-off-by: Thanh Ha <zxiiro@linux.com>
Signed-off-by: Anil Belur <askb23@gmail.com>
This commit is contained in:
Anil Belur 2018-09-10 11:01:44 +05:30
parent 8773289f17
commit 9399b1f986
No known key found for this signature in database
GPG Key ID: 0FAA11C1B55BFA62
6 changed files with 36 additions and 21 deletions

View File

@ -66,6 +66,11 @@ job_builder section
they will be accessible in `qux.yml` but not in `bar.yml`. They will also be
accessible in `mydir/bar.yml` and `mydir/qux.yml`. False by default.
**update**
(Optional) If set, allows the user to specify if only "jobs" or "views"
(or "all") are updated. Users can override the setting here by passing
``--jobs-only`` or ``--views-only`` CLI options.
(Valid options: jobs, views, all)
jenkins section
^^^^^^^^^^^^^^^

View File

@ -86,11 +86,17 @@ class JenkinsJobs(object):
self._set_config(self.jjb_config.builder, 'ignore_cache')
self._set_config(self.jjb_config.builder, 'flush_cache')
self._set_config(self.jjb_config.builder, 'update')
self._set_config(self.jjb_config.yamlparser, 'allow_empty_variables')
self._set_config(self.jjb_config.jenkins, 'section')
self._set_config(self.jjb_config.jenkins, 'user')
self._set_config(self.jjb_config.jenkins, 'password')
# Note: CLI options override config file options.
if getattr(self.options, 'update', None) is None:
self.options.update = str(self.jjb_config.builder.get('update',
'all'))
if getattr(self.options, 'plugins_info_path', None) is not None:
with io.open(self.options.plugins_info_path, 'r',
encoding='utf-8') as yaml_file:

View File

@ -72,25 +72,26 @@ class UpdateSubCommand(base.BaseSubCommand):
dest='n_workers',
help="number of workers to use, 0 for autodetection and 1 "
"for just one worker.")
update.add_argument(
'-j', '--jobs-only',
action='store_true', dest='add_jobs',
default=False,
help='update only jobs'
)
update.add_argument(
'-v', '--views-only',
action='store_true', dest='add_views',
default=False,
help='update only views'
)
update.add_argument(
'--existing-only',
action='store_true',
default=False,
dest='existing_only',
help='update existing jobs only'
)
help='update existing jobs only')
update_type = update.add_mutually_exclusive_group()
update_type.add_argument(
'-j', '--jobs-only',
action='store_const',
dest='update',
const='jobs',
help='update only jobs')
update_type.add_argument(
'-v', '--views-only',
action='store_const',
dest='update',
const='views',
help='update only views')
def _generate_xmljobs(self, options, jjb_config=None):
builder = JenkinsManager(jjb_config)
@ -122,11 +123,6 @@ class UpdateSubCommand(base.BaseSubCommand):
return builder, xml_jobs, xml_views
def execute(self, options, jjb_config):
if options.add_jobs and options.add_views:
raise JenkinsJobsException(
'"--views-only" and "--jobs-only" cannot be used together.')
if options.n_workers < 0:
raise JenkinsJobsException(
'Number of workers must be equal or greater than 0')
@ -134,12 +130,12 @@ class UpdateSubCommand(base.BaseSubCommand):
builder, xml_jobs, xml_views = self._generate_xmljobs(
options, jjb_config)
if options.add_jobs:
if options.update == 'jobs':
jobs, num_updated_jobs = builder.update_jobs(
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:
elif options.update == 'views':
views, num_updated_views = builder.update_views(
xml_views, n_workers=options.n_workers,
existing_only=options.existing_only)

View File

@ -313,6 +313,12 @@ class JJBConfig(object):
'retain_anchors')
self.yamlparser['retain_anchors'] = retain_anchors
update = None
if (config and config.has_section('job_builder') and
config.has_option('job_builder', 'update')):
update = config.get('job_builder', 'update')
self.builder['update'] = update
def validate(self):
# Inform the user as to what is likely to happen, as they may specify
# a real jenkins instance in test mode to get the plugin info to check

View File

@ -6,3 +6,4 @@ password=jenkins_password
allow_empty_variables=True
ignore_cache=True
flush_cache=True
update=all

View File

@ -118,6 +118,7 @@ class TestConfigs(CmdTestsBase):
self.assertEqual(jjb_config.jenkins['password'], "jenkins_password")
self.assertEqual(jjb_config.builder['ignore_cache'], True)
self.assertEqual(jjb_config.builder['flush_cache'], True)
self.assertEqual(jjb_config.builder['update'], "all")
self.assertEqual(
jjb_config.yamlparser['allow_empty_variables'], True)