Add global extra roles for "nexus create repo" 52/14752/2
authorEric Ball <eball@linuxfoundation.org>
Fri, 1 Mar 2019 23:43:29 +0000 (15:43 -0800)
committerEric Ball <eball@linuxfoundation.org>
Sat, 2 Mar 2019 01:17:39 +0000 (17:17 -0800)
This will allow users to specify common roles only once, rather than
having to add "extra_privs" to each repo. Documents in the docs repo
have also been updated, and are now linked to from the lftools docs.

Issue: RELENG-291
Change-Id: Ia35d92fa742ca8202f1d4ad7794bf8549e0a8a77
Signed-off-by: Eric Ball <eball@linuxfoundation.org>
docs/commands/nexus.rst
lftools/cli/nexus.py
lftools/nexus/cmd.py

index 0ac1a61..0df0196 100644 (file)
@@ -28,6 +28,9 @@ repo
 
 .. program-output:: lftools nexus create repo --help
 
+For details and examples, please see
+:ref:`Create Nexus2 repos with lftools <create-repos-lftools>`
+
 .. _nexus-reorder-staged-repos:
 
 reorder-staged-repos
index 5690158..a1d928a 100644 (file)
@@ -49,7 +49,7 @@ def create(ctx):
 @create.command()
 @click.option(
     '-c', '--config', type=str, required=True,
-    help='Repo config file for how to the Nexus repository should be created.')
+    help='Repo config file for how the Nexus repository should be created.')
 @click.option(
     '-s', '--settings', type=str, required=True,
     help='Config file containing administrative settings.')
index 802470d..b5301d7 100644 (file)
@@ -121,9 +121,14 @@ def create_repos(config_file, settings_file):
     with open(settings_file, 'r') as f:
         settings = yaml.safe_load(f)
 
-    for setting in ['nexus', 'user', 'password', 'email_domain']:
+    for setting in ['email_domain', 'base_groupId', 'repositories']:
+        if not setting in config:
+            log.error('{} needs to be defined in {}'.format(setting, config_file))
+            sys.exit(1)
+
+    for setting in ['nexus', 'user', 'password']:
         if not setting in settings:
-            log.error('{} needs to be defined'.format(setting))
+            log.error('{} needs to be defined in {}'.format(setting, settings_file))
             sys.exit(1)
 
     _nexus = Nexus(settings['nexus'], settings['user'], settings['password'])
@@ -165,21 +170,26 @@ def create_repos(config_file, settings_file):
         except LookupError as e:
             _nexus.create_user(name, email, role_id, password, extra_privs)
 
-    def build_repo(repo, repoId, config, base_groupId):
+    def build_repo(repo, repoId, config, base_groupId, global_privs, email_domain):
         log.info('-> Building for {}.{} in Nexus'.format(base_groupId, repo))
         groupId = '{}.{}'.format(base_groupId, repo)
         target = util.create_repo_target_regex(groupId)
 
-        if 'extra_privs' in config:
+        if not global_privs and not 'extra_privs' in config:
+            extra_privs = []
+        elif global_privs:
+            extra_privs = global_privs
+            if 'extra_privs' in config:
+                extra_privs += config['extra_privs']
+            log.info('Privileges for this repo:' + ', '.join(extra_privs))
+        elif 'extra_privs' in config:
             extra_privs = config['extra_privs']
             log.info('Privileges for this repo:' + ', '.join(extra_privs))
-        else:
-            extra_privs = []
 
         create_nexus_perms(
             repoId,
             [target],
-            settings['email_domain'],
+            email_domain,
             config['password'],
             extra_privs)
 
@@ -192,11 +202,19 @@ def create_repos(config_file, settings_file):
                     sub_repo,
                     sub_repo_id,
                     config['repositories'][sub_repo],
-                    groupId)
+                    groupId,
+                    extra_privs,
+                    email_domain)
 
     log.warning('Nexus repo creation started. Aborting now could leave tasks undone!')
+    if 'global_privs' in config:
+        global_privs = config['global_privs']
+    else:
+        global_privs = []
+
     for repo in config['repositories']:
-        build_repo(repo, repo, config['repositories'][repo], config['base_groupId'])
+        build_repo(repo, repo, config['repositories'][repo],
+                   config['base_groupId'], global_privs, config['email_domain'])
 
 
 def search(settings_file, url, repo, pattern):