From ef8b9f103871d8ccbb159f4f519588a48d754dfa Mon Sep 17 00:00:00 2001 From: Aric Gardner Date: Tue, 31 Mar 2020 14:56:58 -0400 Subject: [PATCH] Changes for nexus2 automation Allow more permissive regex match project names like integration-simulators-pnf-simulator should allow . or - in their pom.xml Enabled by strict_url_regex=False in config file Now allows passing a url in the format https:// and sourcing nexus user and pass from lftools.ini Signed-off-by: Aric Gardner Change-Id: I85d40f5964a2588093febd29ba9a1d25bc3e1046 --- lftools/cli/nexus.py | 24 +++++-- lftools/nexus/cmd.py | 50 ++++++++++---- lftools/nexus/util.py | 18 ++++- .../notes/nexus-regex-2fbe678031bccd10.yaml | 11 +++ tests/test_nexus.py | 79 +++++++++++++++------- 5 files changed, 140 insertions(+), 42 deletions(-) create mode 100644 lftools/releasenotes/notes/nexus-regex-2fbe678031bccd10.yaml diff --git a/lftools/cli/nexus.py b/lftools/cli/nexus.py index af7e3618..7386228f 100644 --- a/lftools/cli/nexus.py +++ b/lftools/cli/nexus.py @@ -52,13 +52,29 @@ def create(ctx): @create.command() @click.option( - "-c", "--config", type=str, required=True, help="Repo config file for how the Nexus repository should be created." + "-c", + "--configfile", + type=str, + required=True, + help="Repo config file for how the Nexus repository should be created.", +) +@click.option( + "-s", "--settings", type=str, required=False, help="Optional config file containing administrative settings." +) +@click.option( + "-u", + "--url", + type=str, + required=False, + help=( + "Nexus server URL. In the format https:// Can also be set as {} in the environment." + "This will override any URL set in settings.yaml." + ).format(NEXUS_URL_ENV), ) -@click.option("-s", "--settings", type=str, required=True, help="Config file containing administrative settings.") @click.pass_context -def repo(ctx, config, settings): +def repo(ctx, configfile, url, settings=False): """Create a Nexus repository as defined by a repo-config.yaml file.""" - nexuscmd.create_repos(config, settings) + nexuscmd.create_repos(configfile, settings, url) @create.command() diff --git a/lftools/nexus/cmd.py b/lftools/nexus/cmd.py index e8c33b54..fb9d3f54 100644 --- a/lftools/nexus/cmd.py +++ b/lftools/nexus/cmd.py @@ -31,6 +31,7 @@ log = logging.getLogger(__name__) def get_credentials(settings_file, url=None): """Return credentials for Nexus instantiation.""" + if settings_file: try: with open(settings_file, "r") as f: @@ -112,30 +113,44 @@ def reorder_staged_repos(settings_file): _nexus.update_repo_group_details(repo_id, repo_update) -def create_repos(config_file, settings_file): +def create_repos(config_file, settings_file, url): """Create repositories as defined by configuration file. - :arg str config: Configuration file containing repository definitions that + :arg str config_file: Configuration file containing repository definitions that will be used to create the new Nexus repositories. :arg str settings: Settings file containing administrative credentials and information. + :arg str url: url in the format https:// user nad password will be taken from lftools + if url is provided. """ + if not settings_file: + from lftools import config + + settings_url = url.replace("https://", "") + password = config.get_setting(settings_url, "password") + username = config.get_setting(settings_url, "username") + with open(config_file, "r") as f: config = yaml.safe_load(f) - with open(settings_file, "r") as f: - settings = yaml.safe_load(f) + if settings_file: + with open(settings_file, "r") as f: + settings = yaml.safe_load(f) 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 in {}".format(setting, settings_file)) - sys.exit(1) + if settings_file: + for setting in ["nexus", "user", "password"]: + if not setting in settings: + log.error("{} needs to be defined in {}".format(setting, settings_file)) + sys.exit(1) - _nexus = Nexus(settings["nexus"], settings["user"], settings["password"]) + if settings_file: + _nexus = Nexus(settings["nexus"], settings["user"], settings["password"]) + else: + _nexus = Nexus(url, username, password) def create_nexus_perms(name, targets, email, password, extra_privs=[]): # Create target @@ -174,10 +189,10 @@ 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, global_privs, email_domain): + def build_repo(repo, repoId, config, base_groupId, global_privs, email_domain, strict=True): log.info("-> Building for {}.{} in Nexus".format(base_groupId, repo)) groupId = "{}.{}".format(base_groupId, repo) - target = util.create_repo_target_regex(groupId) + target = util.create_repo_target_regex(groupId, strict) if not global_privs and not "extra_privs" in config: extra_privs = [] @@ -204,10 +219,20 @@ def create_repos(config_file, settings_file): global_privs = config["global_privs"] else: global_privs = [] + if "strict_url_regex" in config: + strict = config["strict_url_regex"] + else: + strict = True for repo in config["repositories"]: build_repo( - repo, repo, config["repositories"][repo], config["base_groupId"], global_privs, config["email_domain"] + repo, + repo, + config["repositories"][repo], + config["base_groupId"], + global_privs, + config["email_domain"], + strict, ) @@ -221,6 +246,7 @@ def create_roles(config_file, settings_file): """ with open(config_file, "r") as f: config = yaml.safe_load(f) + with open(settings_file, "r") as f: settings = yaml.safe_load(f) diff --git a/lftools/nexus/util.py b/lftools/nexus/util.py index 98d234db..21d57d09 100644 --- a/lftools/nexus/util.py +++ b/lftools/nexus/util.py @@ -17,6 +17,18 @@ import logging log = logging.getLogger(__name__) -def create_repo_target_regex(group_id): - """Create a repo_target for Nexus use.""" - return "^/{}/.*".format(group_id.replace(".", "[/\.]")) +def create_repo_target_regex(group_id, strict=True): + """ + Create a repo_target for Nexus use. + + Example: group_id = "org.o-ran-sc.org" + Strict = False : --> "/org/o/ran/sc/org + Strict = True : --> "/org/o-ran-sc/org/" + """ + + repotarget = "^/{}/.*".format(group_id.replace(".", "[/\.]")) + if strict: + return repotarget + else: + # Replace - with regex + return repotarget.replace("-", "[/\.]") diff --git a/lftools/releasenotes/notes/nexus-regex-2fbe678031bccd10.yaml b/lftools/releasenotes/notes/nexus-regex-2fbe678031bccd10.yaml new file mode 100644 index 00000000..3cb0bf3e --- /dev/null +++ b/lftools/releasenotes/notes/nexus-regex-2fbe678031bccd10.yaml @@ -0,0 +1,11 @@ +--- +fixes: + - | + project names with many dashes such as + integration-simulators-pnf-simulator + should allow . or - in their pom.xml + fix the regex to replace - with [/\.] + when creating a new nexus2 Repository target. + + This is enabled by setting strict_url_regex=False in config file. + diff --git a/tests/test_nexus.py b/tests/test_nexus.py index 508aa5a2..0d05ec81 100644 --- a/tests/test_nexus.py +++ b/tests/test_nexus.py @@ -53,26 +53,59 @@ def test_create_roles(datafiles, responses, nexus2_obj_create): def test_create_repo_target_regex(): """Test create_repo_target_regex() command.""" - odlparent = util.create_repo_target_regex("org.opendaylight.odlparent") - odlparent_regex = re.compile(odlparent) - assert odlparent_regex.match( - "/org/opendaylight/odlparent/odlparent" "/4.0.0-SNAPSHOT/odlparent-4.0.0-20180424.132124-69.pom" - ) - - honeycomb = util.create_repo_target_regex("org.opendaylight.honeycomb.vbd") - honeycomb_regex = re.compile(honeycomb) - assert honeycomb_regex.match( - "/org/opendaylight/honeycomb/vbd/odl-vbd" "/1.4.0-SNAPSHOT/odl-vbd-1.4.0-20180422.024456-12-features.xml" - ) - - mso = util.create_repo_target_regex("org.openecomp.mso") - mso_regex = re.compile(mso) - assert mso_regex.match("/org/openecomp/mso/" "1.1.0-SNAPSHOT/mso-1.1.0-20170606.171056-26.pom") - - dcaegen2 = util.create_repo_target_regex("org.onap.dcaegen2") - dcaegen2_regex = re.compile(dcaegen2) - assert dcaegen2_regex.match("/org/onap/dcaegen2/" "1.2.0-SNAPSHOT/dcaegen2-1.2.0-20180403.182529-10.pom") - - vpp = util.create_repo_target_regex("io.fd.vpp") - vpp_regex = re.compile(vpp) - assert vpp_regex.match("/io/fd/vpp/jvpp/16.06/jvpp-16.06.jar") + test_url_3_par = [ + [False, "org.o-ran-sc.org", "/org/o/ran/sc/org/"], + [ + False, + "org.opendaylight.odlparent", + "/org/opendaylight/odlparent/odlparent/4.0.0-SNAPSHOT/odlparent-4.0.0-20180424.132124-69.pom", + ], + [ + False, + "org.opendaylight.honeycomb.vbd", + "/org/opendaylight/honeycomb/vbd/odl-vbd/1.4.0-SNAPSHOT/odl-vbd-1.4.0-20180422.024456-12-features.xml", + ], + [False, "org.openecomp.mso", "/org/openecomp/mso/1.1.0-SNAPSHOT/mso-1.1.0-20170606.171056-26.pom"], + [False, "org.onap.dcaegen2", "/org/onap.dcaegen2/1.2.0-SNAPSHOT/dcaegen2-1.2.0-20180403.182529-10.pom"], + [False, "io.fd.vpp", "/io/fd/vpp/jvpp/16.06/jvpp-16.06.jar"], + [True, "org.o-ran-sc.org", "/org/o-ran-sc/org/"], + [True, "org.o-ran-sc.org", "/org/o-ran-sc/org/ric-plt-lib-rmr"], + [ + True, + "org.opendaylight.odlparent", + "/org/opendaylight/odlparent/odlparent/4.0.0-SNAPSHOT/odlparent-4.0.0-20180424.132124-69.pom", + ], + [ + True, + "org.opendaylight.honeycomb.vbd", + "/org/opendaylight/honeycomb/vbd/odl-vbd/1.4.0-SNAPSHOT/odl-vbd-1.4.0-20180422.024456-12-features.xml", + ], + [True, "org.openecomp.mso", "/org/openecomp/mso/1.1.0-SNAPSHOT/mso-1.1.0-20170606.171056-26.pom"], + [True, "org.onap.dcaegen2", "/org/onap/dcaegen2/1.2.0-SNAPSHOT/dcaegen2-1.2.0-20180403.182529-10.pom"], + [True, "io.fd.vpp", "/io/fd/vpp/jvpp/16.06/jvpp-16.06.jar"], + ] + + for url in test_url_3_par: + a = util.create_repo_target_regex(url[1], url[0]) + a_regex = re.compile(a) + assert a_regex.match(url[2]) != None + + test_url_2_par = [ + ["org.o-ran-sc.org", "/org/o-ran-sc/org/"], + [ + "org.opendaylight.odlparent", + "/org/opendaylight/odlparent/odlparent/4.0.0-SNAPSHOT/odlparent-4.0.0-20180424.132124-69.pom", + ], + [ + "org.opendaylight.honeycomb.vbd", + "/org/opendaylight/honeycomb/vbd/odl-vbd/1.4.0-SNAPSHOT/odl-vbd-1.4.0-20180422.024456-12-features.xml", + ], + ["org.openecomp.mso", "/org/openecomp/mso/1.1.0-SNAPSHOT/mso-1.1.0-20170606.171056-26.pom"], + ["org.onap.dcaegen2", "/org/onap/dcaegen2/1.2.0-SNAPSHOT/dcaegen2-1.2.0-20180403.182529-10.pom"], + ["io.fd.vpp", "/io/fd/vpp/jvpp/16.06/jvpp-16.06.jar"], + ] + + for url in test_url_2_par: + a = util.create_repo_target_regex(url[0]) + a_regex = re.compile(a) + assert a_regex.match(url[1]) != None -- 2.16.6