Add token init cmd to add new server cfg 11/12511/11
authorThanh Ha <thanh.ha@linuxfoundation.org>
Sat, 8 Sep 2018 19:52:01 +0000 (15:52 -0400)
committerThanh Ha <thanh.ha@linuxfoundation.org>
Tue, 18 Sep 2018 15:09:49 +0000 (11:09 -0400)
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 <thanh.ha@linuxfoundation.org>
lftools/cli/jenkins/token.py
releasenotes/notes/jenkins-token-init-4af337e4d79939f1.yaml [new file with mode: 0644]

index 9dbdbea..73890a4 100644 (file)
@@ -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 (file)
index 0000000..e61fd1c
--- /dev/null
@@ -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