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__)
username=user,
password=password)
+ ctx.obj['jenkins_url'] = server
+
@click.command()
@click.pass_context
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)
--- /dev/null
+# 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)
--- /dev/null
+# 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
--- /dev/null
+---
+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.