Move JJB_INI to static variable 09/12509/7
authorThanh Ha <thanh.ha@linuxfoundation.org>
Sat, 8 Sep 2018 18:56:00 +0000 (14:56 -0400)
committerThanh Ha <thanh.ha@linuxfoundation.org>
Tue, 11 Sep 2018 18:59:12 +0000 (14:59 -0400)
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 <thanh.ha@linuxfoundation.org>
lftools/cli/jenkins/__init__.py
lftools/jenkins/__init__.py [new file with mode: 0644]
releasenotes/notes/jjb-ini-839c14f4e500fd56.yaml [new file with mode: 0644]

index 9b255e4..384d504 100644 (file)
@@ -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 (file)
index 0000000..67d95f3
--- /dev/null
@@ -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 (file)
index 0000000..316ab57
--- /dev/null
@@ -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