Add --exact to releasedockerhub 74/62374/6
authorBengt Thuree <bthuree@linuxfoundation.org>
Fri, 22 Nov 2019 00:13:06 +0000 (11:13 +1100)
committerBengt Thuree <bthuree@linuxfoundation.org>
Fri, 22 Nov 2019 04:30:02 +0000 (15:30 +1100)
Enables working only on one repo, compared to wildcards or all.

  -e, --exact  Only exact match repo name will be used.
         If used, --repo parameter can not be empty

ISSUE: RELENG-2551

Signed-off-by: Bengt Thuree <bthuree@linuxfoundation.org>
Change-Id: I774d2b206305229af76d1e78570d09849b6c836f

lftools/cli/nexus.py
lftools/nexus/release_docker_hub.py
releasenotes/notes/releasedockerhub_add_param_exact-6da9f2cdc28c0562.yaml [new file with mode: 0644]
tests/test_release_docker_hub.py

index 8ebe9e0..d9a2117 100644 (file)
@@ -156,6 +156,10 @@ def release(ctx, repos, verify, server):
     '-r', '--repo', type=str, default='', required=False,
     help='Only repos containing this string will be selected. '
          'Default set to blank string, which is every repo.')
+@click.option(
+    '-e', '--exact', is_flag=True, required=False, default=False,
+    help='Match the exact repo name. '
+         'If used, --repo parameter can not be empty.')
 @click.option(
     '-s', '--summary', is_flag=True, required=False,
     help='Prints a summary of missing docker tags.')
@@ -163,16 +167,16 @@ def release(ctx, repos, verify, server):
     '-v', '--verbose', is_flag=True, required=False,
     help='Prints all collected repo/tag information.')
 @click.option(
-    '-c', '--copy', is_flag=True, required=False,
+    '-c', '--copy', is_flag=True, required=False, default=False,
     help='Copy missing tags from Nexus3 repos to Docker Hub repos.')
 @click.option(
     '-p', '--progbar', is_flag=True, required=False, default=False,
     help='Display a progress bar for the time consuming jobs.')
 @click.pass_context
-def copy_from_nexus3_to_dockerhub(ctx, org, repo, summary, verbose, copy, progbar):
+def copy_from_nexus3_to_dockerhub(ctx, org, repo, exact, summary, verbose, copy, progbar):
     """Find missing repos in Docker Hub, Copy from Nexus3.
 
     Will by default list all missing repos in Docker Hub, compared to Nexus3.
     If -c (--copy) is provided, it will copy the repos from Nexus3 to Docker Hub.
     """
-    rdh.start_point(org, repo, summary, verbose, copy, progbar)
+    rdh.start_point(org, repo, exact, summary, verbose, copy, progbar)
index e81cbe1..0472045 100644 (file)
@@ -448,11 +448,12 @@ class ProjectClass:
                         raise requests.HTTPError(retry_text)
 
 
-def get_nexus3_catalog(org_name='', find_pattern=''):
+def get_nexus3_catalog(org_name='', find_pattern='', exact_match=False):
     """Main function to collect all Nexus3 repositories.
 
     This function will collect the Nexus catalog for all projects starting with
     'org_name' as well as containing a pattern if specified.
+    If exact_match is specified, it will use the pattern as a unique repo name within the org_name.
 
     If you do it manually, you give the following command.
         curl -s https://nexus3.onap.org:10002/v2/_catalog
@@ -468,10 +469,11 @@ def get_nexus3_catalog(org_name='', find_pattern=''):
         org_name     : Organizational name, for instance 'onap'
         find_pattern : A pattern, that if specified, needs to be part of the
                        repository name.
-                        for instance,
-                         ''     : this pattern finds all repositories.
-                         'eleo' : this pattern finds all repositories with 'eleo'
+                       for instance,
+                        ''     : this pattern finds all repositories.
+                        'eleo' : this pattern finds all repositories with 'eleo'
                                  in its name. --> chameleon
+        exact_match  : If specified, find_pattern is a unique repo name
 
     """
     global NexusCatalog
@@ -481,6 +483,8 @@ def get_nexus3_catalog(org_name='', find_pattern=''):
     containing_str = ''
     if len(find_pattern) > 0:
         containing_str = ', and containing "{}"'.format(find_pattern)
+    if exact_match:
+        containing_str = ', and reponame = "{}"'.format(find_pattern)
     info_str = "Collecting information from Nexus with projects with org = {}".format(org_name)
     log.info("{}{}.".format(info_str, containing_str))
 
@@ -503,11 +507,16 @@ def get_nexus3_catalog(org_name='', find_pattern=''):
         for word in TmpCatalog:
             # Remove all projects that do not start with org_name
             if word.startswith(org_name):
-                # If  a specific search string has been specified, searc = h for it
+                use_this_repo = False
+                # Remove org_name/ from word, so we only get repository left
+                project = (org_name, word[len(org_name)+1:])
+                # If a specific search string has been specified, search for it
                 # Empty string will match all words
-                if word.find(find_pattern) >= 0:
-                    # Remove onap/ from word, so we only get repository left
-                    project = (org_name, word[len(org_name)+1:])
+                if word.find(find_pattern) >= 0 and not exact_match:
+                    use_this_repo = True
+                if exact_match and project[1] == find_pattern:
+                    use_this_repo = True
+                if use_this_repo:
                     NexusCatalog.append(project)
                     log.debug("Added project {} to my list".format(project[1]))
                     if len(project[1]) > project_max_len_chars:
@@ -724,11 +733,15 @@ def print_nbr_tags_to_copy():
     log.info("Summary: {} tags that should be copied from Nexus3 to Docker Hub.".format(_tot_tags))
 
 
-def start_point(org_name, find_pattern='', summary=False,
+def start_point(org_name, find_pattern='', exact_match=False, summary=False,
                 verbose=False, copy=False, progbar=False):
     """Main function."""
+    # Verify find_pattern and specified_repo are not both used.
+    if len(find_pattern) == 0 and exact_match:
+        log.error("You need to provide a Pattern to go with the --exact flag")
+        return
     initialize(org_name)
-    if not get_nexus3_catalog(org_name, find_pattern):
+    if not get_nexus3_catalog(org_name, find_pattern, exact_match):
         log.info("Could not get any catalog from Nexus3 with org = {}".format(org_name))
         return
 
diff --git a/releasenotes/notes/releasedockerhub_add_param_exact-6da9f2cdc28c0562.yaml b/releasenotes/notes/releasedockerhub_add_param_exact-6da9f2cdc28c0562.yaml
new file mode 100644 (file)
index 0000000..56991a7
--- /dev/null
@@ -0,0 +1,5 @@
+---
+features:
+  - |
+    Added --exact to the releasedockerhub command. This enables
+    user to only work on a specific repo (specified by --repo)
index d42aeb5..e5baa55 100644 (file)
@@ -411,6 +411,22 @@ class TestFetchNexus3Catalog:
         rdh.get_nexus3_catalog ('onap', 'aaf')
         assert len(rdh.NexusCatalog) == 18
 
+    def test_get_all_onap_and_specify_1_repo_1(self):
+        rdh.NexusCatalog = []
+        rdh.initialize ('onap')
+        responses.add(responses.GET, self.url, body=self.answer, status=200)
+        rdh.get_nexus3_catalog ('onap', 'clamp', True)
+        assert len(rdh.NexusCatalog) == 1
+        assert rdh.NexusCatalog[0][1] == 'clamp'
+
+    def test_get_all_onap_and_specify_1_repo_2(self):
+        rdh.NexusCatalog = []
+        rdh.initialize ('onap')
+        responses.add(responses.GET, self.url, body=self.answer, status=200)
+        rdh.get_nexus3_catalog ('onap', 'clamp-dashboard-logstash', True)
+        assert len(rdh.NexusCatalog) == 1
+        assert rdh.NexusCatalog[0][1] == 'clamp-dashboard-logstash'
+
 
 class TestFetchAllTagsAndUpdate:
     _test_image_long_id = 'sha256:3450464d68c9443dedc8bfe3272a23e6441c37f707c42d32fee0ebdbcd319d2c'
@@ -565,7 +581,7 @@ class TestFetchAllTagsAndUpdate:
 
     def test_start_no_copy(self, responses, mocker):
         self.initiate_test_fetch(responses, mocker)
-        rdh.start_point ('onap', '', False)
+        rdh.start_point ('onap', '', False, False)
         assert self.counter.pull == 0
         assert self.counter.tag == 0
         assert self.counter.push == 0
@@ -573,7 +589,7 @@ class TestFetchAllTagsAndUpdate:
 
     def test_start_copy(self, responses, mocker):
         self.initiate_test_fetch(responses, mocker)
-        rdh.start_point ('onap', '', False, False, True)
+        rdh.start_point ('onap', '', False, False, False, True)
         assert len(rdh.NexusCatalog) == 3
         assert len(rdh.projects) == 3
         assert len(rdh.projects[0].tags_2_copy.valid) == 1
@@ -589,7 +605,7 @@ class TestFetchAllTagsAndUpdate:
 
     def test_start_copy_repo(self, responses, mocker):
         self.initiate_test_fetch(responses, mocker, 'sanity')
-        rdh.start_point ('onap', 'sanity', False, False, True)
+        rdh.start_point ('onap', 'sanity', False, False, False, True)
         assert len(rdh.NexusCatalog) == 1
         assert len(rdh.projects) == 1
         assert len(rdh.projects[0].tags_2_copy.valid) == 1