From: Anil Belur Date: Tue, 11 Jun 2019 02:53:10 +0000 (+1000) Subject: Get jenkins config only with the key exists X-Git-Tag: v0.25.0~5^2 X-Git-Url: https://gerrit.linuxfoundation.org/infra/gitweb?a=commitdiff_plain;h=refs%2Fchanges%2F51%2F15851%2F5;p=releng%2Flftools.git Get jenkins config only with the key exists Handle config parser correctly which defaults to "[jenkins]" section when no server is pased. This fixes the issue with checking if the key exists in the configuration read before reading the key-value. The issue is reproduceable by running `lftools jenkins plugins --help` or `tox -e docs`, with the ``jenkins.ini`` file having no "[jenkins]" section. Use SafeConfigParser which is a saner version for parsing config files. Issue: RELENG-2120 Change-Id: I274ac46cf7a1d03cf7c3c6fdaec6094b5e02aa42 Signed-off-by: Anil Belur --- diff --git a/lftools/jenkins/__init__.py b/lftools/jenkins/__init__.py index 79e461bf..c9970563 100644 --- a/lftools/jenkins/__init__.py +++ b/lftools/jenkins/__init__.py @@ -58,11 +58,15 @@ class Jenkins(): if '://' not in server: if self.config_file: log.debug('Using config from {}'.format(self.config_file)) - config = configparser.ConfigParser() + config = configparser.SafeConfigParser() config.read(self.config_file) - user = config.get(server, 'user') - password = config.get(server, 'password') - server = config.get(server, 'url') + if config.has_section(server): + user = config.get(server, 'user') + password = config.get(server, 'password') + server = config.get(server, 'url') + else: + log.error('[{}] section not found in {}' + .format(server, self.config_file)) else: log.debug('jenkins_jobs.ini not found in any of the default paths.') server = 'https://localhost:8080' diff --git a/releasenotes/notes/fix-jenkins-config-parser-d9eb6e7068a7906a.yaml b/releasenotes/notes/fix-jenkins-config-parser-d9eb6e7068a7906a.yaml new file mode 100644 index 00000000..19a7b2a8 --- /dev/null +++ b/releasenotes/notes/fix-jenkins-config-parser-d9eb6e7068a7906a.yaml @@ -0,0 +1,40 @@ +--- +fixes: + - | + Handle config parser correctly which defaults to "[jenkins]" section + when no server is pased. This fixes the issue with checking if the key + exists in the configuration read before reading the key-value. + + The issue is reproduceable by running `lftools jenkins plugins --help` or + `tox -e docs`, with the ``jenkins.ini`` file having no "[jenkins]" section. + + .. code-block: none + + Traceback (most recent call last): + File "/home/jenkins/.local/lftools/env/bin/lftools", line 10, in + sys.exit(main()) + File "/home/jenkins/.local/lftools/lftools/cli/__init__.py", line 104, in main + cli(obj={}) + File "/home/jenkins/.local/lftools/env/lib/python3.7/site-packages/click/core.py", line 764, in __call__ + return self.main(*args, **kwargs) + File "/home/jenkins/.local/lftools/env/lib/python3.7/site-packages/click/core.py", line 717, in main + rv = self.invoke(ctx) + File "/home/jenkins/.local/lftools/env/lib/python3.7/site-packages/click/core.py", line 1137, in invoke + return _process_result(sub_ctx.command.invoke(sub_ctx)) + File "/home/jenkins/.local/lftools/env/lib/python3.7/site-packages/click/core.py", line 1134, in invoke + Command.invoke(self, ctx) + File "/home/jenkins/.local/lftools/env/lib/python3.7/site-packages/click/core.py", line 956, in invoke + return ctx.invoke(self.callback, **ctx.params) + File "/home/jenkins/.local/lftools/env/lib/python3.7/site-packages/click/core.py", line 555, in invoke + return callback(*args, **kwargs) + File "/home/jenkins/.local/lftools/env/lib/python3.7/site-packages/click/decorators.py", line 17, in new_func + return f(get_current_context(), *args, **kwargs) + File "/home/jenkins/.local/lftools/lftools/cli/jenkins/__init__.py", line 44, in jenkins_cli + ctx.obj['jenkins'] = Jenkins(server, user, password, config_file=conf) + File "/home/jenkins/.local/lftools/lftools/jenkins/__init__.py", line 63, in __init__ + user = config.get(server, 'user') + File "/usr/lib64/python3.7/configparser.py", line 780, in get + d = self._unify_values(section, vars) + File "/usr/lib64/python3.7/configparser.py", line 1146, in _unify_values + raise NoSectionError(section) from None + configparser.NoSectionError: No section: 'jenkins'