From 698a8bbb93d65158a5ffe4bf6a13a0445a56feac Mon Sep 17 00:00:00 2001 From: Thanh Ha Date: Sat, 8 Sep 2018 15:52:01 -0400 Subject: [PATCH] Add token init cmd to add new server cfg New token init command to automatically fetch API token and add new server configuration section into jenkins_jobs.ini. This file should already exist. This cmd uses the globally configured user credentials in lftools.ini to initialize the new Jenkins server configuration. Change-Id: I3d4bb0fd5696fd1bbeb26eb7d609bab5e064ba92 Signed-off-by: Thanh Ha --- lftools/cli/jenkins/token.py | 27 ++++++++++++++++++++++ .../notes/jenkins-token-init-4af337e4d79939f1.yaml | 8 +++++++ 2 files changed, 35 insertions(+) create mode 100644 releasenotes/notes/jenkins-token-init-4af337e4d79939f1.yaml diff --git a/lftools/cli/jenkins/token.py b/lftools/cli/jenkins/token.py index 9dbdbead..73890a46 100644 --- a/lftools/cli/jenkins/token.py +++ b/lftools/cli/jenkins/token.py @@ -18,6 +18,7 @@ import click 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 @@ -37,6 +38,31 @@ def change(ctx): log.info(get_token(ctx.obj['jenkins_url'], change=True)) +@click.command() +@click.argument('name') +@click.argument('url') +def init(name, url): + """Initialize jenkins_jobs.ini config for new server section.""" + _require_jjb_ini() + + config = configparser.ConfigParser() + config.read(JJB_INI) + + token = get_token(url, True) + try: + config.add_section(name) + except configparser.DuplicateSectionError as e: + log.error(e) + sys.exit(1) + + config.set(name, 'url', url) + config.set(name, 'user', lftools_cfg.get_setting('global', 'username')) + config.set(name, 'password', token) + + with open(JJB_INI, 'w') as configfile: + config.write(configfile) + + @click.command(name='print') @click.pass_context def print_token(ctx): @@ -105,6 +131,7 @@ def reset(ctx, servers): token.add_command(change) +token.add_command(init) token.add_command(print_token) token.add_command(reset) diff --git a/releasenotes/notes/jenkins-token-init-4af337e4d79939f1.yaml b/releasenotes/notes/jenkins-token-init-4af337e4d79939f1.yaml new file mode 100644 index 00000000..e61fd1cb --- /dev/null +++ b/releasenotes/notes/jenkins-token-init-4af337e4d79939f1.yaml @@ -0,0 +1,8 @@ +--- +features: + - | + Add jenkins token init command to initialize a new server section in + jenkins_jobs.ini. This command uses credentials found in lftools.ini to + initialize the new Jenkins server configuration. + + Usage: lftools jenkins token init [OPTIONS] NAME URL -- 2.16.6