Add support to jenkins cmd for jenkins_jobs.ini 91/12391/2
authorThanh Ha <thanh.ha@linuxfoundation.org>
Sat, 25 Aug 2018 05:23:08 +0000 (01:23 -0400)
committerThanh Ha <thanh.ha@linuxfoundation.org>
Sat, 25 Aug 2018 05:38:56 +0000 (01:38 -0400)
Allow the jenkins command to use existing jenkins_jobs.ini for
configuration. By default assume jenkins_jobs.ini's default
configuration 'jenkins'.

This change makes 'server', 'user', and 'password' parameters no
longer required as defaults replace the need for these options.

'server' now defaults to 'jenkins' if jenkins_jobs.ini exists,
otherwise defaults to 'http://localhost:8080' which is the default
Jenkins deploy port.

'user' now defaults to 'admin' which is the Jenkins default admin
user.

'password' has no default and must be set by the user as this
is randomly generated by Jenkins at boot.

Change-Id: Ic7af1eb53e8e9079b845203f44914310616c4bab
Signed-off-by: Thanh Ha <thanh.ha@linuxfoundation.org>
lftools/cli/jenkins/__init__.py
releasenotes/notes/jenkins-25629106553ebbd5.yaml [new file with mode: 0644]

index 6da1e45..8d54151 100644 (file)
 __author__ = 'Trevor Bramwell'
 
 
+import os
+
 import click
 import jenkins as jenkins_python  # Don't confuse this with the function ...
+from six.moves import configparser
 from six.moves.urllib.error import HTTPError
 
 from lftools.cli.jenkins.builds import builds
@@ -22,13 +25,31 @@ from lftools.cli.jenkins.plugins import plugins_init
 
 
 @click.group()
-@click.option('-s', '--server', type=str, required=True, envvar='JENKINS_URL')
-@click.option('-u', '--user', type=str, required=True, envvar='JENKINS_USER')
-@click.option('-p', '--password', type=str, required=True,
-              envvar='JENKINS_PASSWORD')
+@click.option(
+    '-s', '--server', type=str, envvar='JENKINS_URL', default='jenkins',
+    help='The URL to a Jenkins server. Alternatively the jenkins_jobs.ini '
+    'section to parse for url/user/password configuration if available.')
+@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):
     """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):
+            config = configparser.ConfigParser()
+            config.read(jjb_ini)
+            user = config.get(server, 'user')
+            password = config.get(server, 'password')
+            server = config.get(server, 'url')
+        else:
+            server = 'https://localhost:8080'
+
     # Initial the Jenkins object and pass it to sub-commands
     ctx.obj['server'] = jenkins_python.Jenkins(
         server,
diff --git a/releasenotes/notes/jenkins-25629106553ebbd5.yaml b/releasenotes/notes/jenkins-25629106553ebbd5.yaml
new file mode 100644 (file)
index 0000000..9b8bc0f
--- /dev/null
@@ -0,0 +1,5 @@
+---
+features:
+  - |
+    Add support to the **jenkins** command to parse ``jenkins_jobs.ini`` for
+    configuration if **server** parameter passed is not a URL.