Add jenkins token cmd to retrieve the API token 08/12508/8
authorThanh Ha <thanh.ha@linuxfoundation.org>
Sat, 8 Sep 2018 17:53:14 +0000 (13:53 -0400)
committerThanh Ha <thanh.ha@linuxfoundation.org>
Wed, 12 Sep 2018 19:11:23 +0000 (15:11 -0400)
Change-Id: I43397d25978aaf752db33003044e7cb8d4884705
Signed-off-by: Thanh Ha <thanh.ha@linuxfoundation.org>
lftools/cli/jenkins/__init__.py
lftools/cli/jenkins/token.py [new file with mode: 0644]
lftools/jenkins/token.py [new file with mode: 0644]
releasenotes/notes/jenkins-token-cmd-8e5cdce9175f69a1.yaml [new file with mode: 0644]

index 384d504..de6f555 100644 (file)
@@ -23,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.cli.jenkins.token import token
 from lftools.jenkins import JJB_INI
 
 log = logging.getLogger(__name__)
@@ -56,6 +57,8 @@ def jenkins_cli(ctx, server, user, password):
         username=user,
         password=password)
 
+    ctx.obj['jenkins_url'] = server
+
 
 @click.command()
 @click.pass_context
@@ -190,3 +193,4 @@ jenkins_cli.add_command(groovy)
 jenkins_cli.add_command(jobs)
 jenkins_cli.add_command(quiet_down, name='quiet-down')
 jenkins_cli.add_command(remove_offline_nodes, name='remove-offline-nodes')
+jenkins_cli.add_command(token)
diff --git a/lftools/cli/jenkins/token.py b/lftools/cli/jenkins/token.py
new file mode 100644 (file)
index 0000000..c1c4ba1
--- /dev/null
@@ -0,0 +1,44 @@
+# 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 token commands."""
+
+__author__ = 'Thanh Ha'
+
+import logging
+
+import click
+
+from lftools.jenkins.token import get_token
+
+log = logging.getLogger(__name__)
+
+
+@click.group()
+@click.pass_context
+def token(ctx):
+    """Get API token."""
+
+
+@click.command()
+@click.pass_context
+def change(ctx):
+    """Generate a new API token."""
+    log.info(get_token(ctx.obj['jenkins_url'], change=True))
+
+
+@click.command(name='print')
+@click.pass_context
+def print_token(ctx):
+    """Print current API token."""
+    log.info(get_token(ctx.obj['jenkins_url']))
+
+
+token.add_command(change)
+token.add_command(print_token)
diff --git a/lftools/jenkins/token.py b/lftools/jenkins/token.py
new file mode 100644 (file)
index 0000000..b6128ed
--- /dev/null
@@ -0,0 +1,55 @@
+# 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 token functions."""
+
+__author__ = 'Thanh Ha'
+
+import logging
+
+import jenkins
+
+from lftools import config
+
+log = logging.getLogger(__name__)
+
+
+def get_token(url, change=False):
+    """Get API token.
+
+    This function uses the global username / password for Jenkins from
+    lftools.ini as the user asking for the token may not already know the
+    api token.
+    """
+    username = config.get_setting('global', 'username')
+    password = config.get_setting('global', 'password')
+
+    if change:
+        log.debug('Resetting Jenkins API token on {}'.format(url))
+    else:
+        log.debug('Fetching Jenkins API token from {}'.format(url))
+
+    server = jenkins.Jenkins(
+        url,
+        username=username,
+        password=password)
+
+    get_token = """
+import jenkins.security.*
+User u = User.get("{}")
+ApiTokenProperty t = u.getProperty(ApiTokenProperty.class)
+if ({}) {{
+    t.changeApiToken()
+}}
+def token = t.getApiToken()
+println "$token"
+""".format(username, str(change).lower())
+
+    token = server.run_script(get_token)
+    return token
diff --git a/releasenotes/notes/jenkins-token-cmd-8e5cdce9175f69a1.yaml b/releasenotes/notes/jenkins-token-cmd-8e5cdce9175f69a1.yaml
new file mode 100644 (file)
index 0000000..e1d094a
--- /dev/null
@@ -0,0 +1,13 @@
+---
+features:
+  - |
+    Add new cmd to fetch Jenkins token from user account. An optional
+    ``--change`` parameter can be passed to have Jenkins change the API token.
+
+    Usage: lftools jenkins token [OPTIONS]
+
+      Get API token.
+
+    Options:
+      --change  Generate a new API token.
+      --help    Show this message and exit.