From fb5ffd18315c55eb2c5625de101a4d42b050406b Mon Sep 17 00:00:00 2001 From: Thanh Ha Date: Sat, 8 Sep 2018 14:56:00 -0400 Subject: [PATCH] Move JJB_INI to static variable We don't expect this location to change so set a static variable that we can reuse more easily across modules. Change-Id: Ieb54acc2d12de9bb21620112380f20be14fe9113 Signed-off-by: Thanh Ha --- lftools/cli/jenkins/__init__.py | 14 +++----- lftools/jenkins/__init__.py | 43 ++++++++++++++++++++++++ releasenotes/notes/jjb-ini-839c14f4e500fd56.yaml | 9 +++++ 3 files changed, 57 insertions(+), 9 deletions(-) create mode 100644 lftools/jenkins/__init__.py create mode 100644 releasenotes/notes/jjb-ini-839c14f4e500fd56.yaml diff --git a/lftools/cli/jenkins/__init__.py b/lftools/cli/jenkins/__init__.py index 9b255e47..384d5043 100644 --- a/lftools/cli/jenkins/__init__.py +++ b/lftools/cli/jenkins/__init__.py @@ -13,7 +13,6 @@ __author__ = 'Trevor Bramwell' import logging -import os import click import jenkins as jenkins_python # Don't confuse this with the function ... @@ -24,6 +23,7 @@ from lftools.cli.jenkins.builds import builds from lftools.cli.jenkins.jobs import jobs from lftools.cli.jenkins.nodes import nodes from lftools.cli.jenkins.plugins import plugins_init +from lftools.jenkins import JJB_INI log = logging.getLogger(__name__) @@ -38,20 +38,16 @@ log = logging.getLogger(__name__) @click.pass_context def jenkins_cli(ctx, server, user, password): """Query information about the Jenkins Server.""" - jjb_ini = os.path.join( - os.path.expanduser('~'), - '.config', - 'jenkins_jobs', - 'jenkins_jobs.ini') - if '://' not in server: - if os.path.isfile(jjb_ini): + if JJB_INI: + log.debug('Using config from {}'.format(JJB_INI)) config = configparser.ConfigParser() - config.read(jjb_ini) + config.read(JJB_INI) user = config.get(server, 'user') password = config.get(server, 'password') server = config.get(server, 'url') else: + log.debug('jenkins_jobs.ini not found in any of the default paths.') server = 'https://localhost:8080' # Initial the Jenkins object and pass it to sub-commands diff --git a/lftools/jenkins/__init__.py b/lftools/jenkins/__init__.py new file mode 100644 index 00000000..67d95f3f --- /dev/null +++ b/lftools/jenkins/__init__.py @@ -0,0 +1,43 @@ +# SPDX-License-Identifier: EPL-1.0 +############################################################################## +# Copyright (c) 2018 The Linux Foundation and others. +# +# All rights reserved. This program and the accompanying materials +# are made available under the terms of the Eclipse Public License v1.0 +# which accompanies this distribution, and is available at +# http://www.eclipse.org/legal/epl-v10.html +############################################################################## +"""Jenkins.""" + +__author__ = 'Thanh Ha' + +import logging +import os + +log = logging.getLogger(__name__) + + +def jjb_ini(): + """Return jenkins_jobs.ini file location if it exists, None otherwise.""" + global_conf = '/etc/jenkins_jobs/jenkins_jobs.ini' + user_conf = os.path.join( + os.path.expanduser('~'), + '.config', + 'jenkins_jobs', + 'jenkins_jobs.ini') + local_conf = os.path.join( + os.getcwd(), + 'jenkins_jobs.ini') + + conf = None + if os.path.isfile(local_conf): + conf = local_conf + elif os.path.isfile(user_conf): + conf = user_conf + elif os.path.isfile(global_conf): + conf = global_conf + + return conf + + +JJB_INI = jjb_ini() diff --git a/releasenotes/notes/jjb-ini-839c14f4e500fd56.yaml b/releasenotes/notes/jjb-ini-839c14f4e500fd56.yaml new file mode 100644 index 00000000..316ab571 --- /dev/null +++ b/releasenotes/notes/jjb-ini-839c14f4e500fd56.yaml @@ -0,0 +1,9 @@ +--- +features: + - | + We now support locating the jenkins_jobs.ini in all the same default search + paths as JJB supports. Specifically in this order: + + #. $PWD/jenkins_jobs.ini + #. ~/.config/jenkins_jobs/jenkins_jobs.ini + #. /etc/jenkins_jobs/jenkins_jobs.ini -- 2.16.6