Add --conf parameter to jenkins sub-cmd 45/13045/2
authorThanh Ha <thanh.ha@linuxfoundation.org>
Thu, 18 Oct 2018 00:09:58 +0000 (20:09 -0400)
committerThanh Ha <thanh.ha@linuxfoundation.org>
Fri, 19 Oct 2018 16:18:54 +0000 (12:18 -0400)
Per request from Ieb54acc2d12de9bb21620112380f20be14fe9113 this
patch adds a --conf parameter to the jenkins subcommand to allow
overriding the JJB_INI default locations.

Issue: RELENG-1206
Change-Id: Ia1af96ccb64451199a1dc3994ae8b3004272afdb
Signed-off-by: Thanh Ha <thanh.ha@linuxfoundation.org>
lftools/cli/jenkins/__init__.py
lftools/cli/jenkins/token.py
lftools/config.py
lftools/jenkins/__init__.py
releasenotes/notes/jenkins-conf-e33db422385a2203.yaml [new file with mode: 0644]

index 25905ca..f13092c 100644 (file)
@@ -28,6 +28,9 @@ log = logging.getLogger(__name__)
 
 
 @click.group()
+@click.option(
+    '-c', '--conf', type=str, default=None,
+    help='Path to jenkins_jobs.ini config.')
 @click.option(
     '-s', '--server', type=str, envvar='JENKINS_URL', default='jenkins',
     help='The URL to a Jenkins server. Alternatively the jenkins_jobs.ini '
@@ -35,10 +38,10 @@ log = logging.getLogger(__name__)
 @click.option('-u', '--user', type=str, envvar='JENKINS_USER', default='admin')
 @click.option('-p', '--password', type=str, envvar='JENKINS_PASSWORD')
 @click.pass_context
-def jenkins_cli(ctx, server, user, password):
+def jenkins_cli(ctx, server, user, password, conf):
     """Query information about the Jenkins Server."""
     # Initial the Jenkins object and pass it to sub-commands
-    ctx.obj['jenkins'] = Jenkins(server, user, password)
+    ctx.obj['jenkins'] = Jenkins(server, user, password, config_file=conf)
 
 
 @click.command()
index 47bb820..1adcdfc 100644 (file)
@@ -12,6 +12,7 @@
 __author__ = 'Thanh Ha'
 
 import logging
+import os
 import sys
 
 import click
@@ -19,7 +20,6 @@ import requests
 from six.moves import configparser
 
 from lftools import config as lftools_cfg
-from lftools.jenkins import JJB_INI
 from lftools.jenkins.token import get_token
 
 log = logging.getLogger(__name__)
@@ -42,12 +42,14 @@ def change(ctx):
 @click.command()
 @click.argument('name')
 @click.argument('url')
-def init(name, url):
+@click.pass_context
+def init(ctx, name, url):
     """Initialize jenkins_jobs.ini config for new server section."""
-    _require_jjb_ini()
+    jenkins = ctx.obj['jenkins']
+    _require_jjb_ini(jenkins.config_file)
 
     config = configparser.ConfigParser()
-    config.read(JJB_INI)
+    config.read(jenkins.config_file)
 
     token = get_token(url, True)
     try:
@@ -60,7 +62,7 @@ def init(name, url):
     config.set(name, 'user', lftools_cfg.get_setting('global', 'username'))
     config.set(name, 'password', token)
 
-    with open(JJB_INI, 'w') as configfile:
+    with open(jenkins.config_file, 'w') as configfile:
         config.write(configfile)
 
 
@@ -87,7 +89,8 @@ def reset(ctx, servers):
     If the server parameter is NOT passed then all servers listed in the
     configuration file will be reset via multi-server mode.
     """
-    _require_jjb_ini()
+    jenkins = ctx.obj['jenkins']
+    _require_jjb_ini(jenkins.config_file)
 
     def _reset_key(config, server):
         url = config.get(server, 'url')
@@ -95,7 +98,7 @@ def reset(ctx, servers):
         try:
             token = get_token(url, True)
             config.set(server, 'password', token)
-            with open(JJB_INI, 'w') as configfile:
+            with open(jenkins.config_file, 'w') as configfile:
                 config.write(configfile)
             return token
         except requests.exceptions.ConnectionError as e:
@@ -104,7 +107,7 @@ def reset(ctx, servers):
     fail = 0
     success = 0
     config = configparser.ConfigParser()
-    config.read(JJB_INI)
+    config.read(jenkins.config_file)
 
     if len(servers) == 0:
         cfg_sections = config.sections()
@@ -138,8 +141,8 @@ token.add_command(print_token)
 token.add_command(reset)
 
 
-def _require_jjb_ini():
-    if not JJB_INI:
+def _require_jjb_ini(config):
+    if not os.path.isfile(config):
         log.error('jenkins_jobs.ini not found in any of the search paths. '
                   'Please provide one before proceeding.')
         sys.exit(1)
index bc0fbb8..e4d94b5 100644 (file)
@@ -37,6 +37,7 @@ def get_setting(section, option=None):
         try:
             return config.get(section, option)
         except configparser.NoOptionError:
+            print(section, option)
             log.error('Config option does not exist.')
             sys.exit(1)
         except configparser.NoSectionError:
index 6ce420d..79e461b 100644 (file)
@@ -49,13 +49,17 @@ JJB_INI = jjb_ini()
 class Jenkins():
     """lftools Jenkins object."""
 
-    def __init__(self, server, user=None, password=None):
+    def __init__(self, server, user=None, password=None, config_file=None):
         """Initialize a Jenkins object."""
+        self.config_file = config_file
+        if not self.config_file:
+            self.config_file = JJB_INI
+
         if '://' not in server:
-            if JJB_INI:
-                log.debug('Using config from {}'.format(JJB_INI))
+            if self.config_file:
+                log.debug('Using config from {}'.format(self.config_file))
                 config = configparser.ConfigParser()
-                config.read(JJB_INI)
+                config.read(self.config_file)
                 user = config.get(server, 'user')
                 password = config.get(server, 'password')
                 server = config.get(server, 'url')
diff --git a/releasenotes/notes/jenkins-conf-e33db422385a2203.yaml b/releasenotes/notes/jenkins-conf-e33db422385a2203.yaml
new file mode 100644 (file)
index 0000000..03c0ba3
--- /dev/null
@@ -0,0 +1,5 @@
+---
+features:
+  - |
+    Add ``--conf`` parameter to jenkins subcommand to allow choosing a jjb
+    config outside of the default paths.