Add support for using multiple servers

Allows user to specify which section from ini config
should be used for loading Jenkins server details.

Change-Id: I57633662e3ca2dba48af8db9e08fb8d5f8bc3bfa
This commit is contained in:
Sorin Sbarnea 2017-05-30 15:42:30 +01:00
parent 3799ed2c98
commit 5d3aa0dbb5
3 changed files with 25 additions and 11 deletions

View File

@ -59,7 +59,9 @@ class JenkinsJobs(object):
self.parser = create_parser()
self.options = self.parser.parse_args(args)
self.jjb_config = JJBConfig(self.options.conf, **kwargs)
self.jjb_config = JJBConfig(self.options.conf,
config_section=self.options.section,
**kwargs)
if not self.options.command:
self.parser.error("Must specify a 'command' to be performed")
@ -86,6 +88,7 @@ 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.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')

View File

@ -65,6 +65,11 @@ def create_parser():
default=None,
help="Don\'t fail if any of the variables inside any string are "
"not defined, replace with empty string instead.")
parser.add_argument(
'--server', '-s',
dest='section',
default='jenkins',
help="The Jenkins server ini section to use. Defaults to 'jenkins'")
parser.add_argument(
'--user', '-u',
help="The Jenkins user to use for authentication. This overrides "

View File

@ -42,6 +42,8 @@ exclude=.*
allow_duplicates=False
allow_empty_variables=False
# other named sections could be used in addition to the implicit [jenkins]
# if you have multiple jenkins servers.
[jenkins]
url=http://localhost:8080/
query_plugins_info=True
@ -57,7 +59,9 @@ CONFIG_REQUIRED_MESSAGE = ("A valid configuration file is required. "
class JJBConfig(object):
def __init__(self, config_filename=None, config_file_required=False):
def __init__(self, config_filename=None,
config_file_required=False,
config_section='jenkins'):
"""
The JJBConfig class is intended to encapsulate and resolve priority
@ -128,6 +132,7 @@ class JJBConfig(object):
self.flush_cache = False
self.user = None
self.password = None
self.section = config_section
self.plugins_info = None
self.timeout = builder._DEFAULT_TIMEOUT
self.allow_empty_variables = None
@ -171,12 +176,12 @@ class JJBConfig(object):
logger.debug("Config: {0}".format(config))
# check the ignore_cache setting
if config.has_option('jenkins', 'ignore_cache'):
if config.has_option(self.section, 'ignore_cache'):
logging.warning("ignore_cache option should be moved to the "
"[job_builder] section in the config file, the "
"one specified in the [jenkins] section will be "
"ignored in the future")
self.ignore_cache = config.getboolean('jenkins', 'ignore_cache')
self.ignore_cache = config.getboolean(self.section, 'ignore_cache')
elif config.has_option('job_builder', 'ignore_cache'):
self.ignore_cache = config.getboolean('job_builder',
'ignore_cache')
@ -195,12 +200,12 @@ class JJBConfig(object):
# error
# https://bugs.launchpad.net/openstack-ci/+bug/1259631
try:
self.user = config.get('jenkins', 'user')
self.user = config.get(self.section, 'user')
except (TypeError, configparser.NoOptionError):
pass
try:
self.password = config.get('jenkins', 'password')
self.password = config.get(self.section, 'password')
except (TypeError, configparser.NoOptionError):
pass
@ -212,21 +217,22 @@ class JJBConfig(object):
# "timeout=jenkins_jobs.builder._DEFAULT_TIMEOUT" or not set timeout at
# all.
try:
self.timeout = config.getfloat('jenkins', 'timeout')
self.timeout = config.getfloat(self.section, 'timeout')
except (ValueError):
raise JenkinsJobsException("Jenkins timeout config is invalid")
except (TypeError, configparser.NoOptionError):
pass
if not config.getboolean("jenkins", "query_plugins_info"):
logger.debug("Skipping plugin info retrieval")
self.plugins_info = []
if (config.has_option(self.section, 'query_plugins_info') and
not config.getboolean(self.section, "query_plugins_info")):
logger.debug("Skipping plugin info retrieval")
self.plugins_info = []
self.recursive = config.getboolean('job_builder', 'recursive')
self.excludes = config.get('job_builder', 'exclude').split(os.pathsep)
# The way we want to do things moving forward:
self.jenkins['url'] = config.get('jenkins', 'url')
self.jenkins['url'] = config.get(self.section, 'url')
self.jenkins['user'] = self.user
self.jenkins['password'] = self.password
self.jenkins['timeout'] = self.timeout